[dna-commits] DNA SVN: r829 - in trunk: dna-graph/src/main/java/org/jboss/dna/graph/io and 7 other directories.

dna-commits at lists.jboss.org dna-commits at lists.jboss.org
Wed Apr 15 18:42:18 EDT 2009


Author: rhauch
Date: 2009-04-15 18:42:18 -0400 (Wed, 15 Apr 2009)
New Revision: 829

Added:
   trunk/dna-graph/src/main/java/org/jboss/dna/graph/io/
   trunk/dna-graph/src/main/java/org/jboss/dna/graph/io/Destination.java
   trunk/dna-graph/src/main/java/org/jboss/dna/graph/io/GraphBatchDestination.java
   trunk/dna-graph/src/main/java/org/jboss/dna/graph/io/GraphImporter.java
   trunk/dna-graph/src/test/java/org/jboss/dna/graph/io/
   trunk/dna-graph/src/test/java/org/jboss/dna/graph/io/GraphImporterTest.java
Removed:
   trunk/dna-graph/src/main/java/org/jboss/dna/graph/GraphImporter.java
   trunk/dna-graph/src/test/java/org/jboss/dna/graph/GraphImporterTest.java
Modified:
   trunk/dna-graph/src/main/java/org/jboss/dna/graph/Graph.java
   trunk/dna-graph/src/main/java/org/jboss/dna/graph/xml/XmlHandler.java
   trunk/dna-graph/src/test/java/org/jboss/dna/graph/xml/XmlHandlerTest.java
   trunk/dna-jcr-tck/src/test/java/org/jboss/dna/jcr/InMemoryRepositoryStub.java
   trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrWorkspace.java
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrTckTest.java
Log:
DNA-358 Break the XmlHandler.Destination into an area where it can be reused by other importers

Pulled the Destination interface and the current implementations into a new 'org.jboss.dna.graph.io' package.  Move the GraphImporter class and it's test case into the same package.

Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/Graph.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/Graph.java	2009-04-15 14:28:30 UTC (rev 828)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/Graph.java	2009-04-15 22:42:18 UTC (rev 829)
@@ -50,6 +50,7 @@
 import org.jboss.dna.graph.connector.RepositoryConnectionFactory;
 import org.jboss.dna.graph.connector.RepositorySource;
 import org.jboss.dna.graph.connector.RepositorySourceException;
+import org.jboss.dna.graph.io.GraphImporter;
 import org.jboss.dna.graph.property.Binary;
 import org.jboss.dna.graph.property.DateTime;
 import org.jboss.dna.graph.property.Name;

Deleted: trunk/dna-graph/src/main/java/org/jboss/dna/graph/GraphImporter.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/GraphImporter.java	2009-04-15 14:28:30 UTC (rev 828)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/GraphImporter.java	2009-04-15 22:42:18 UTC (rev 829)
@@ -1,278 +0,0 @@
-/*
- * JBoss DNA (http://www.jboss.org/dna)
- * See the COPYRIGHT.txt file distributed with this work for information
- * regarding copyright ownership.  Some portions may be licensed
- * to Red Hat, Inc. under one or more contributor license agreements.
- * See the AUTHORS.txt file in the distribution for a full listing of 
- * individual contributors. 
- *
- * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
- * is licensed to you under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * JBoss DNA is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.dna.graph;
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URI;
-import java.util.List;
-import net.jcip.annotations.NotThreadSafe;
-import org.jboss.dna.common.text.TextDecoder;
-import org.jboss.dna.common.util.CheckArg;
-import org.jboss.dna.graph.connector.RepositorySource;
-import org.jboss.dna.graph.connector.RepositorySourceException;
-import org.jboss.dna.graph.property.Name;
-import org.jboss.dna.graph.property.NamespaceRegistry;
-import org.jboss.dna.graph.property.Path;
-import org.jboss.dna.graph.property.Property;
-import org.jboss.dna.graph.xml.XmlHandler;
-import org.jboss.dna.graph.xml.XmlHandler.Destination;
-import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
-import org.xml.sax.XMLReader;
-import org.xml.sax.helpers.XMLReaderFactory;
-
-/**
- * @author Randall Hauch
- * @author John Verhaeg
- */
-public class GraphImporter {
-
-    private final Graph graph;
-
-    public GraphImporter( Graph graph ) {
-        CheckArg.isNotNull(graph, "graph");
-        this.graph = graph;
-    }
-
-    /**
-     * Get the context in which the importer will be executed.
-     * 
-     * @return the execution context; never null
-     */
-    public ExecutionContext getContext() {
-        return this.graph.getContext();
-    }
-
-    /**
-     * The graph that this importer uses.
-     * 
-     * @return the graph; never null
-     */
-    public Graph getGraph() {
-        return graph;
-    }
-
-    /**
-     * Read the content from the supplied URI and import into the repository at the supplied location.
-     * 
-     * @param uri the URI where the importer can read the content that is to be imported
-     * @param location the location in the {@link RepositorySource repository source} where the content is to be written; may not
-     *        be null
-     * @return the batch of requests for creating the graph content that represents the imported content
-     * @throws IllegalArgumentException if the <code>uri</code> or destination path are null
-     * @throws IOException if there is a problem reading the content
-     * @throws SAXException if there is a problem with the SAX Parser
-     * @throws RepositorySourceException if there is a problem while writing the content to the {@link RepositorySource repository
-     *         source}
-     */
-    public Graph.Batch importXml( URI uri,
-                                  Location location ) throws IOException, SAXException, RepositorySourceException {
-        return importXml(uri, location, false);
-    }
-
-    /**
-     * Read the content from the supplied URI and import into the repository at the supplied location.
-     * 
-     * @param uri the URI where the importer can read the content that is to be imported
-     * @param location the location in the {@link RepositorySource repository source} where the content is to be written; may not
-     *        be null
-     * @param skip true if the root element should be skipped, or false if a node should be created for the root XML element
-     * @return the batch of requests for creating the graph content that represents the imported content
-     * @throws IllegalArgumentException if the <code>uri</code> or destination path are null
-     * @throws IOException if there is a problem reading the content
-     * @throws SAXException if there is a problem with the SAX Parser
-     * @throws RepositorySourceException if there is a problem while writing the content to the {@link RepositorySource repository
-     *         source}
-     */
-    public Graph.Batch importXml( URI uri,
-                                  Location location,
-                                  boolean skip ) throws IOException, SAXException, RepositorySourceException {
-        CheckArg.isNotNull(uri, "uri");
-        CheckArg.isNotNull(location, "location");
-        CheckArg.isNotNull(location.getPath(), "location.getPath()");
-        InputStream stream = null;
-        try {
-            stream = uri.toURL().openStream();
-            return importXml(stream, location, skip);
-        } finally {
-            if (stream != null) stream.close();
-        }
-    }
-
-    /**
-     * Read the content from the supplied URI and import into the repository at the supplied location. This method does <i>not</i>
-     * close the stream.
-     * 
-     * @param stream the stream containing the content to be imported
-     * @param location the location in the {@link RepositorySource repository source} where the content is to be written; may not
-     *        be null
-     * @return the batch of requests for creating the graph content that represents the imported content
-     * @throws IllegalArgumentException if the <code>stream</code> or destination path are null
-     * @throws IOException if there is a problem reading the content
-     * @throws SAXException if there is a problem with the SAX Parser
-     * @throws RepositorySourceException if there is a problem while writing the content to the {@link RepositorySource repository
-     *         source}
-     */
-    public Graph.Batch importXml( InputStream stream,
-                                  Location location ) throws IOException, SAXException, RepositorySourceException {
-        return importXml(stream, location, false);
-    }
-
-    /**
-     * Read the content from the supplied URI and import into the repository at the supplied location. This method does <i>not</i>
-     * close the stream.
-     * 
-     * @param stream the stream containing the content to be imported
-     * @param location the location in the {@link RepositorySource repository source} where the content is to be written; may not
-     *        be null
-     * @param skip true if the root element should be skipped, or false if a node should be created for the root XML element
-     * @return the batch of requests for creating the graph content that represents the imported content
-     * @throws IllegalArgumentException if the <code>stream</code> or destination path are null
-     * @throws IOException if there is a problem reading the content
-     * @throws SAXException if there is a problem with the SAX Parser
-     * @throws RepositorySourceException if there is a problem while writing the content to the {@link RepositorySource repository
-     *         source}
-     */
-    public Graph.Batch importXml( InputStream stream,
-                                  Location location,
-                                  boolean skip ) throws IOException, SAXException, RepositorySourceException {
-        CheckArg.isNotNull(stream, "uri");
-        CheckArg.isNotNull(location, "location");
-        CheckArg.isNotNull(location.getPath(), "location.getPath()");
-
-        // Create the destination for the XmlHandler ...
-        Graph.Batch batch = graph.batch();
-        XmlHandler.Destination destination = new CreateOnGraphInBatch(batch);
-
-        // Determine where the content is to be placed ...
-        Path parentPath = location.getPath();
-        Name nameAttribute = JcrLexicon.NAME;
-        Name typeAttribute = JcrLexicon.PRIMARY_TYPE;
-        Name typeAttributeValue = null;
-        NamespaceRegistry reg = graph.getContext().getNamespaceRegistry();
-        if (reg.isRegisteredNamespaceUri(JcrNtLexicon.Namespace.URI)) {
-            typeAttributeValue = JcrNtLexicon.UNSTRUCTURED;
-        }
-
-        TextDecoder decoder = null;
-        XmlHandler.AttributeScoping scoping = XmlHandler.AttributeScoping.USE_DEFAULT_NAMESPACE;
-        XmlHandler handler = new XmlHandler(destination, skip, parentPath, decoder, nameAttribute, typeAttribute,
-                                            typeAttributeValue, scoping);
-        XMLReader reader = XMLReaderFactory.createXMLReader();
-        reader.setContentHandler(handler);
-        reader.setErrorHandler(handler);
-        reader.parse(new InputSource(stream));
-        if (stream != null) stream.close();
-        return batch;
-    }
-
-    /**
-     * Return an {@link XmlHandler} that can be used to import content directly into the supplied location. The operations
-     * resulting from the {@link XmlHandler} operations are batched until the {@link XmlHandler#endDocument()} is called, at which
-     * point all enqueued operations are submitted to the graph.
-     * 
-     * @param location the location in the {@link RepositorySource repository source} where the content is to be written; may not
-     *        be null
-     * @param skip true if the root element should be skipped, or false if a node should be created for the root XML element
-     * @return the {@link XmlHandler} that can be used to import content
-     * @throws IllegalArgumentException if the <code>stream</code> or destination path are null
-     */
-    public XmlHandler getHandlerForImportingXml( Location location,
-                                                 boolean skip ) {
-        CheckArg.isNotNull(location, "location");
-        CheckArg.isNotNull(location.getPath(), "location.getPath()");
-
-        // Create the destination for the XmlHandler ...
-        Graph.Batch batch = graph.batch();
-        XmlHandler.Destination destination = new SubmitToGraphInBatch(batch);
-
-        // Determine where the content is to be placed ...
-        Path parentPath = location.getPath();
-        Name nameAttribute = JcrLexicon.NAME;
-        Name typeAttribute = JcrLexicon.PRIMARY_TYPE;
-        Name typeAttributeValue = null;
-        NamespaceRegistry reg = graph.getContext().getNamespaceRegistry();
-        if (reg.isRegisteredNamespaceUri(JcrNtLexicon.Namespace.URI)) {
-            typeAttributeValue = JcrNtLexicon.UNSTRUCTURED;
-        }
-
-        TextDecoder decoder = null;
-        XmlHandler.AttributeScoping scoping = XmlHandler.AttributeScoping.USE_DEFAULT_NAMESPACE;
-        XmlHandler handler = new XmlHandler(destination, skip, parentPath, decoder, nameAttribute, typeAttribute,
-                                            typeAttributeValue, scoping);
-        return handler;
-    }
-
-    @NotThreadSafe
-    protected static class CreateOnGraphInBatch implements Destination {
-        protected final Graph.Batch batch;
-
-        protected CreateOnGraphInBatch( Graph.Batch batch ) {
-            assert batch != null;
-            this.batch = batch;
-        }
-
-        public ExecutionContext getExecutionContext() {
-            return batch.getGraph().getContext();
-        }
-
-        public void create( Path path,
-                            List<Property> properties ) {
-            assert properties != null;
-            if (properties.isEmpty()) {
-                batch.create(path).and();
-            } else {
-                batch.create(path, properties).and();
-            }
-        }
-
-        public void create( Path path,
-                            Property firstProperty,
-                            Property... additionalProperties ) {
-            if (firstProperty == null) {
-                batch.create(path).and();
-            } else {
-                batch.create(path, firstProperty, additionalProperties).and();
-            }
-        }
-
-        public void submit() {
-        }
-    }
-
-    @NotThreadSafe
-    protected final static class SubmitToGraphInBatch extends CreateOnGraphInBatch {
-        protected SubmitToGraphInBatch( Graph.Batch batch ) {
-            super(batch);
-        }
-
-        @Override
-        public void submit() {
-            super.submit();
-            batch.execute();
-        }
-    }
-
-}

Added: trunk/dna-graph/src/main/java/org/jboss/dna/graph/io/Destination.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/io/Destination.java	                        (rev 0)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/io/Destination.java	2009-04-15 22:42:18 UTC (rev 829)
@@ -0,0 +1,73 @@
+/*
+ * JBoss DNA (http://www.jboss.org/dna)
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership.  Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ * See the AUTHORS.txt file in the distribution for a full listing of 
+ * individual contributors.
+ *
+ * Unless otherwise indicated, all code in JBoss DNA is licensed
+ * to you under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ * 
+ * JBoss DNA is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.dna.graph.io;
+
+import java.util.List;
+import net.jcip.annotations.NotThreadSafe;
+import org.jboss.dna.graph.ExecutionContext;
+import org.jboss.dna.graph.property.Path;
+import org.jboss.dna.graph.property.Property;
+
+/**
+ * Interface used internally as the destination for the requests. This is used to abstract whether the requests should be
+ * submitted immediately or in a single batch.
+ * 
+ * @author Randall Hauch
+ */
+ at NotThreadSafe
+public interface Destination {
+
+    /**
+     * Obtain the execution context of the destination.
+     * 
+     * @return the destination's execution context
+     */
+    public ExecutionContext getExecutionContext();
+
+    /**
+     * Create a node at the supplied path and with the supplied attributes. The path will be absolute.
+     * 
+     * @param path the absolute path of the node
+     * @param properties the properties for the node; never null, but may be empty if there are no properties
+     */
+    public void create( Path path,
+                        List<Property> properties );
+
+    /**
+     * Create a node at the supplied path and with the supplied attributes. The path will be absolute.
+     * 
+     * @param path the absolute path of the node
+     * @param firstProperty the first property
+     * @param additionalProperties the remaining properties for the node
+     */
+    public void create( Path path,
+                        Property firstProperty,
+                        Property... additionalProperties );
+
+    /**
+     * Signal to this destination that any enqueued create requests should be submitted. Usually this happens at the end of the
+     * document parsing, but an implementer must allow for it to be called multiple times and anytime during parsing.
+     */
+    public void submit();
+}


Property changes on: trunk/dna-graph/src/main/java/org/jboss/dna/graph/io/Destination.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Added: trunk/dna-graph/src/main/java/org/jboss/dna/graph/io/GraphBatchDestination.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/io/GraphBatchDestination.java	                        (rev 0)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/io/GraphBatchDestination.java	2009-04-15 22:42:18 UTC (rev 829)
@@ -0,0 +1,128 @@
+/*
+ * JBoss DNA (http://www.jboss.org/dna)
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership.  Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ * See the AUTHORS.txt file in the distribution for a full listing of 
+ * individual contributors.
+ *
+ * Unless otherwise indicated, all code in JBoss DNA is licensed
+ * to you under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ * 
+ * JBoss DNA is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.dna.graph.io;
+
+import java.util.List;
+import net.jcip.annotations.NotThreadSafe;
+import org.jboss.dna.graph.ExecutionContext;
+import org.jboss.dna.graph.Graph;
+import org.jboss.dna.graph.Graph.Batch;
+import org.jboss.dna.graph.property.Path;
+import org.jboss.dna.graph.property.Property;
+
+/**
+ * A {@link Destination} that makes the changes to a graph via a {@link Batch}.
+ */
+ at NotThreadSafe
+public class GraphBatchDestination implements Destination {
+    protected final Graph.Batch batch;
+    protected final boolean ignoreSubmit;
+
+    /**
+     * Create a new instance that will use the specified batch. When {@link #submit()} is called, the batch will be
+     * {@link Batch#execute() executed}.
+     * 
+     * @param batch the batch
+     * @throws IllegalArgumentException if the batch is null
+     */
+    public GraphBatchDestination( Graph.Batch batch ) {
+        this(batch, false);
+    }
+
+    /**
+     * Create a new instance that will use the specified batch. If {@code ignoreSubmit} is true, then {@link #submit()} does
+     * nothing (and the batch must be executed}; otherwise, {@link #submit()} immediately calls the {@link Batch#execute()
+     * executed}.
+     * 
+     * @param batch the batch
+     * @param ignoreSubmit true if the {@link #submit()} method should be ignored, or false otherwise
+     * @throws IllegalArgumentException if the batch is null
+     */
+    public GraphBatchDestination( Graph.Batch batch,
+                                  boolean ignoreSubmit ) {
+        assert batch != null;
+        this.batch = batch;
+        this.ignoreSubmit = ignoreSubmit;
+    }
+
+    /**
+     * Return whether this instance is ignoring calls to {@link #submit()}.
+     * 
+     * @return ignoreSubmit
+     */
+    public boolean isSubmitIgnored() {
+        return ignoreSubmit;
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     * @see org.jboss.dna.graph.io.Destination#getExecutionContext()
+     */
+    public ExecutionContext getExecutionContext() {
+        return batch.getGraph().getContext();
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     * @see org.jboss.dna.graph.io.Destination#create(org.jboss.dna.graph.property.Path, java.util.List)
+     */
+    public void create( Path path,
+                        List<Property> properties ) {
+        assert properties != null;
+        if (properties.isEmpty()) {
+            batch.create(path).and();
+        } else {
+            batch.create(path, properties).and();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     * @see org.jboss.dna.graph.io.Destination#create(org.jboss.dna.graph.property.Path, org.jboss.dna.graph.property.Property,
+     *      org.jboss.dna.graph.property.Property[])
+     */
+    public void create( Path path,
+                        Property firstProperty,
+                        Property... additionalProperties ) {
+        if (firstProperty == null) {
+            batch.create(path).and();
+        } else {
+            batch.create(path, firstProperty, additionalProperties).and();
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     * @see org.jboss.dna.graph.io.Destination#submit()
+     */
+    public void submit() {
+        // Execute only if we're not ignoring submits ...
+        if (!this.ignoreSubmit && !batch.hasExecuted()) batch.execute();
+
+    }
+}


Property changes on: trunk/dna-graph/src/main/java/org/jboss/dna/graph/io/GraphBatchDestination.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Copied: trunk/dna-graph/src/main/java/org/jboss/dna/graph/io/GraphImporter.java (from rev 809, trunk/dna-graph/src/main/java/org/jboss/dna/graph/GraphImporter.java)
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/io/GraphImporter.java	                        (rev 0)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/io/GraphImporter.java	2009-04-15 22:42:18 UTC (rev 829)
@@ -0,0 +1,229 @@
+/*
+ * JBoss DNA (http://www.jboss.org/dna)
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership.  Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ * See the AUTHORS.txt file in the distribution for a full listing of 
+ * individual contributors. 
+ *
+ * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
+ * is licensed to you under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * JBoss DNA is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.dna.graph.io;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import org.jboss.dna.common.text.TextDecoder;
+import org.jboss.dna.common.util.CheckArg;
+import org.jboss.dna.graph.ExecutionContext;
+import org.jboss.dna.graph.Graph;
+import org.jboss.dna.graph.JcrLexicon;
+import org.jboss.dna.graph.JcrNtLexicon;
+import org.jboss.dna.graph.Location;
+import org.jboss.dna.graph.connector.RepositorySource;
+import org.jboss.dna.graph.connector.RepositorySourceException;
+import org.jboss.dna.graph.property.Name;
+import org.jboss.dna.graph.property.NamespaceRegistry;
+import org.jboss.dna.graph.property.Path;
+import org.jboss.dna.graph.xml.XmlHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+import org.xml.sax.helpers.XMLReaderFactory;
+
+/**
+ * @author Randall Hauch
+ * @author John Verhaeg
+ */
+public class GraphImporter {
+
+    private final Graph graph;
+
+    public GraphImporter( Graph graph ) {
+        CheckArg.isNotNull(graph, "graph");
+        this.graph = graph;
+    }
+
+    /**
+     * Get the context in which the importer will be executed.
+     * 
+     * @return the execution context; never null
+     */
+    public ExecutionContext getContext() {
+        return this.graph.getContext();
+    }
+
+    /**
+     * The graph that this importer uses.
+     * 
+     * @return the graph; never null
+     */
+    public Graph getGraph() {
+        return graph;
+    }
+
+    /**
+     * Read the content from the supplied URI and import into the repository at the supplied location.
+     * 
+     * @param uri the URI where the importer can read the content that is to be imported
+     * @param location the location in the {@link RepositorySource repository source} where the content is to be written; may not
+     *        be null
+     * @return the batch of requests for creating the graph content that represents the imported content
+     * @throws IllegalArgumentException if the <code>uri</code> or destination path are null
+     * @throws IOException if there is a problem reading the content
+     * @throws SAXException if there is a problem with the SAX Parser
+     * @throws RepositorySourceException if there is a problem while writing the content to the {@link RepositorySource repository
+     *         source}
+     */
+    public Graph.Batch importXml( URI uri,
+                                  Location location ) throws IOException, SAXException, RepositorySourceException {
+        return importXml(uri, location, false);
+    }
+
+    /**
+     * Read the content from the supplied URI and import into the repository at the supplied location.
+     * 
+     * @param uri the URI where the importer can read the content that is to be imported
+     * @param location the location in the {@link RepositorySource repository source} where the content is to be written; may not
+     *        be null
+     * @param skip true if the root element should be skipped, or false if a node should be created for the root XML element
+     * @return the batch of requests for creating the graph content that represents the imported content
+     * @throws IllegalArgumentException if the <code>uri</code> or destination path are null
+     * @throws IOException if there is a problem reading the content
+     * @throws SAXException if there is a problem with the SAX Parser
+     * @throws RepositorySourceException if there is a problem while writing the content to the {@link RepositorySource repository
+     *         source}
+     */
+    public Graph.Batch importXml( URI uri,
+                                  Location location,
+                                  boolean skip ) throws IOException, SAXException, RepositorySourceException {
+        CheckArg.isNotNull(uri, "uri");
+        CheckArg.isNotNull(location, "location");
+        CheckArg.isNotNull(location.getPath(), "location.getPath()");
+        InputStream stream = null;
+        try {
+            stream = uri.toURL().openStream();
+            return importXml(stream, location, skip);
+        } finally {
+            if (stream != null) stream.close();
+        }
+    }
+
+    /**
+     * Read the content from the supplied URI and import into the repository at the supplied location. This method does <i>not</i>
+     * close the stream.
+     * 
+     * @param stream the stream containing the content to be imported
+     * @param location the location in the {@link RepositorySource repository source} where the content is to be written; may not
+     *        be null
+     * @return the batch of requests for creating the graph content that represents the imported content
+     * @throws IllegalArgumentException if the <code>stream</code> or destination path are null
+     * @throws IOException if there is a problem reading the content
+     * @throws SAXException if there is a problem with the SAX Parser
+     * @throws RepositorySourceException if there is a problem while writing the content to the {@link RepositorySource repository
+     *         source}
+     */
+    public Graph.Batch importXml( InputStream stream,
+                                  Location location ) throws IOException, SAXException, RepositorySourceException {
+        return importXml(stream, location, false);
+    }
+
+    /**
+     * Read the content from the supplied URI and import into the repository at the supplied location. This method does <i>not</i>
+     * close the stream.
+     * 
+     * @param stream the stream containing the content to be imported
+     * @param location the location in the {@link RepositorySource repository source} where the content is to be written; may not
+     *        be null
+     * @param skip true if the root element should be skipped, or false if a node should be created for the root XML element
+     * @return the batch of requests for creating the graph content that represents the imported content
+     * @throws IllegalArgumentException if the <code>stream</code> or destination path are null
+     * @throws IOException if there is a problem reading the content
+     * @throws SAXException if there is a problem with the SAX Parser
+     * @throws RepositorySourceException if there is a problem while writing the content to the {@link RepositorySource repository
+     *         source}
+     */
+    public Graph.Batch importXml( InputStream stream,
+                                  Location location,
+                                  boolean skip ) throws IOException, SAXException, RepositorySourceException {
+        CheckArg.isNotNull(stream, "uri");
+        CheckArg.isNotNull(location, "location");
+        CheckArg.isNotNull(location.getPath(), "location.getPath()");
+
+        // Create the destination for the XmlHandler ...
+        Graph.Batch batch = graph.batch();
+        Destination destination = new GraphBatchDestination(batch, true);
+
+        // Determine where the content is to be placed ...
+        Path parentPath = location.getPath();
+        Name nameAttribute = JcrLexicon.NAME;
+        Name typeAttribute = JcrLexicon.PRIMARY_TYPE;
+        Name typeAttributeValue = null;
+        NamespaceRegistry reg = graph.getContext().getNamespaceRegistry();
+        if (reg.isRegisteredNamespaceUri(JcrNtLexicon.Namespace.URI)) {
+            typeAttributeValue = JcrNtLexicon.UNSTRUCTURED;
+        }
+
+        TextDecoder decoder = null;
+        XmlHandler.AttributeScoping scoping = XmlHandler.AttributeScoping.USE_DEFAULT_NAMESPACE;
+        XmlHandler handler = new XmlHandler(destination, skip, parentPath, decoder, nameAttribute, typeAttribute,
+                                            typeAttributeValue, scoping);
+        XMLReader reader = XMLReaderFactory.createXMLReader();
+        reader.setContentHandler(handler);
+        reader.setErrorHandler(handler);
+        reader.parse(new InputSource(stream));
+        if (stream != null) stream.close();
+        return batch;
+    }
+
+    /**
+     * Return an {@link XmlHandler} that can be used to import content directly into the supplied location. The operations
+     * resulting from the {@link XmlHandler} operations are batched until the {@link XmlHandler#endDocument()} is called, at which
+     * point all enqueued operations are submitted to the graph.
+     * 
+     * @param location the location in the {@link RepositorySource repository source} where the content is to be written; may not
+     *        be null
+     * @param skip true if the root element should be skipped, or false if a node should be created for the root XML element
+     * @return the {@link XmlHandler} that can be used to import content
+     * @throws IllegalArgumentException if the <code>stream</code> or destination path are null
+     */
+    public XmlHandler getHandlerForImportingXml( Location location,
+                                                 boolean skip ) {
+        CheckArg.isNotNull(location, "location");
+        CheckArg.isNotNull(location.getPath(), "location.getPath()");
+
+        // Create the destination for the XmlHandler ...
+        Graph.Batch batch = graph.batch();
+        Destination destination = new GraphBatchDestination(batch, false);
+
+        // Determine where the content is to be placed ...
+        Path parentPath = location.getPath();
+        Name nameAttribute = JcrLexicon.NAME;
+        Name typeAttribute = JcrLexicon.PRIMARY_TYPE;
+        Name typeAttributeValue = null;
+        NamespaceRegistry reg = graph.getContext().getNamespaceRegistry();
+        if (reg.isRegisteredNamespaceUri(JcrNtLexicon.Namespace.URI)) {
+            typeAttributeValue = JcrNtLexicon.UNSTRUCTURED;
+        }
+
+        TextDecoder decoder = null;
+        XmlHandler.AttributeScoping scoping = XmlHandler.AttributeScoping.USE_DEFAULT_NAMESPACE;
+        XmlHandler handler = new XmlHandler(destination, skip, parentPath, decoder, nameAttribute, typeAttribute,
+                                            typeAttributeValue, scoping);
+        return handler;
+    }
+
+}


Property changes on: trunk/dna-graph/src/main/java/org/jboss/dna/graph/io/GraphImporter.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/xml/XmlHandler.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/xml/XmlHandler.java	2009-04-15 14:28:30 UTC (rev 828)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/xml/XmlHandler.java	2009-04-15 22:42:18 UTC (rev 829)
@@ -34,6 +34,7 @@
 import org.jboss.dna.common.text.XmlNameEncoder;
 import org.jboss.dna.common.util.CheckArg;
 import org.jboss.dna.graph.ExecutionContext;
+import org.jboss.dna.graph.io.Destination;
 import org.jboss.dna.graph.property.Name;
 import org.jboss.dna.graph.property.NameFactory;
 import org.jboss.dna.graph.property.NamespaceRegistry;
@@ -405,46 +406,4 @@
         return result;
     }
 
-    /**
-     * Interface used internally as the destination for the requests. This is used to abstract whether the requests should be
-     * submitted immediately or in a single batch.
-     * 
-     * @author Randall Hauch
-     */
-    @NotThreadSafe
-    public static interface Destination {
-
-        /**
-         * Obtain the execution context of the destination.
-         * 
-         * @return the destination's execution context
-         */
-        public ExecutionContext getExecutionContext();
-
-        /**
-         * Create a node at the supplied path and with the supplied attributes. The path will be absolute.
-         * 
-         * @param path the absolute path of the node
-         * @param properties the properties for the node; never null, but may be empty if there are no properties
-         */
-        public void create( Path path,
-                            List<Property> properties );
-
-        /**
-         * Create a node at the supplied path and with the supplied attributes. The path will be absolute.
-         * 
-         * @param path the absolute path of the node
-         * @param firstProperty the first property
-         * @param additionalProperties the remaining properties for the node
-         */
-        public void create( Path path,
-                            Property firstProperty,
-                            Property... additionalProperties );
-
-        /**
-         * Signal to this destination that any enqueued create requests should be submitted. Usually this happens at the end of
-         * the document parsing, but an implementer must allow for it to be called multiple times and anytime during parsing.
-         */
-        public void submit();
-    }
 }

Deleted: trunk/dna-graph/src/test/java/org/jboss/dna/graph/GraphImporterTest.java
===================================================================
--- trunk/dna-graph/src/test/java/org/jboss/dna/graph/GraphImporterTest.java	2009-04-15 14:28:30 UTC (rev 828)
+++ trunk/dna-graph/src/test/java/org/jboss/dna/graph/GraphImporterTest.java	2009-04-15 22:42:18 UTC (rev 829)
@@ -1,186 +0,0 @@
-/*
- * JBoss DNA (http://www.jboss.org/dna)
- * See the COPYRIGHT.txt file distributed with this work for information
- * regarding copyright ownership.  Some portions may be licensed
- * to Red Hat, Inc. under one or more contributor license agreements.
- * See the AUTHORS.txt file in the distribution for a full listing of 
- * individual contributors. 
- *
- * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
- * is licensed to you under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * JBoss DNA is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.dna.graph;
-
-import static org.hamcrest.core.Is.is;
-import static org.hamcrest.core.IsInstanceOf.instanceOf;
-import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.stub;
-import java.io.File;
-import java.net.URI;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.concurrent.TimeUnit;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-import javax.transaction.xa.XAResource;
-import org.jboss.dna.graph.cache.CachePolicy;
-import org.jboss.dna.graph.connector.RepositoryConnection;
-import org.jboss.dna.graph.connector.RepositoryConnectionFactory;
-import org.jboss.dna.graph.connector.RepositorySourceException;
-import org.jboss.dna.graph.connector.RepositorySourceListener;
-import org.jboss.dna.graph.property.Name;
-import org.jboss.dna.graph.property.Path;
-import org.jboss.dna.graph.property.Property;
-import org.jboss.dna.graph.request.CompositeRequest;
-import org.jboss.dna.graph.request.CreateNodeRequest;
-import org.jboss.dna.graph.request.Request;
-import org.jboss.dna.graph.request.VerifyWorkspaceRequest;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.MockitoAnnotations;
-import org.mockito.MockitoAnnotations.Mock;
-
-/**
- * @author Randall Hauch
- */
-public class GraphImporterTest {
-
-    private Graph graph;
-    private GraphImporter importer;
-    private String sourceName;
-    private ExecutionContext context;
-    private URI xmlContent;
-    private MockRepositoryConnection connection;
-    private Request lastExecutedRequest;
-    private Path destinationPath;
-    @Mock
-    private RepositoryConnectionFactory sources;
-
-    @Before
-    public void beforeEach() throws Exception {
-        MockitoAnnotations.initMocks(this);
-        xmlContent = new File("src/test/resources/repositoryImporterTestData1.xml").toURI();
-        context = new ExecutionContext();
-        context.getNamespaceRegistry().register(DnaLexicon.Namespace.PREFIX, DnaLexicon.Namespace.URI);
-        context.getNamespaceRegistry().register("jcr", "http://www.jcp.org/jcr/1.0");
-        context.getNamespaceRegistry().register("nt", "http://www.jcp.org/jcr/nt/1.0");
-        sourceName = "sourceA";
-        destinationPath = context.getValueFactories().getPathFactory().create("/a/b");
-        graph = Graph.create(sourceName, sources, context);
-        importer = new GraphImporter(graph);
-        connection = new MockRepositoryConnection();
-        stub(sources.createConnection(sourceName)).toReturn(connection);
-    }
-
-    @Test
-    public void shouldImportXmlContentAndGenerateTheCorrectCommands() throws Exception {
-        System.out.println(xmlContent);
-        Graph.Batch batch = importer.importXml(xmlContent, Location.create(destinationPath));
-        batch.execute();
-        // 'lastExecutedCommand'
-        assertThat(lastExecutedRequest, is(instanceOf(CompositeRequest.class)));
-        Iterator<Request> iter = ((CompositeRequest)lastExecutedRequest).iterator();
-        // assertCreateNode(iter, "/a/b/", "jcr:primaryType={http://www.jboss.org/dna/xml/1.0}document");
-        assertCreateNode(iter, "/a/b/dna:system[1]", "jcr:primaryType={http://www.jcp.org/jcr/nt/1.0}unstructured");
-        assertCreateNode(iter, "/a/b/dna:system[1]/dna:sources[1]", "jcr:primaryType={http://www.jcp.org/jcr/nt/1.0}unstructured");
-        assertCreateNode(iter,
-                         "/a/b/dna:system[1]/dna:sources[1]/sourceA[1]",
-                         "repositoryName=repositoryA",
-                         "retryLimit=3",
-                         "jcr:primaryType={http://www.jboss.org/dna/1.0}xyz",
-                         "dna:classname=org.jboss.dna.connector.inmemory.InMemoryRepositorySource");
-        assertCreateNode(iter,
-                         "/a/b/dna:system[1]/dna:sources[1]/sourceB[1]",
-                         "repositoryName=repositoryB",
-                         "jcr:primaryType={http://www.jcp.org/jcr/nt/1.0}unstructured",
-                         "dna:classname=org.jboss.dna.connector.inmemory.InMemoryRepositorySource");
-        assertThat(iter.hasNext(), is(false));
-    }
-
-    public void assertCreateNode( Iterator<Request> iterator,
-                                  String path,
-                                  String... properties ) {
-        Request nextCommand = iterator.next();
-        assertThat(nextCommand, is(instanceOf(CreateNodeRequest.class)));
-        CreateNodeRequest createNode = (CreateNodeRequest)nextCommand;
-        Path expectedPath = context.getValueFactories().getPathFactory().create(path);
-        Path parentPath = createNode.under().getPath();
-        assertThat(parentPath, is(expectedPath.getParent()));
-        assertThat(createNode.named(), is(expectedPath.getLastSegment().getName()));
-        Map<Name, Property> propertiesByName = new HashMap<Name, Property>();
-        for (Property prop : createNode.properties()) {
-            propertiesByName.put(prop.getName(), prop);
-        }
-        for (String propertyStr : properties) {
-            if (propertyStr == "any properties") {
-                propertiesByName.clear();
-                break;
-            }
-            Matcher matcher = Pattern.compile("([^=]+)=(.*)").matcher(propertyStr);
-            if (!matcher.matches()) continue;
-            System.out.println("Property: " + propertyStr + " ==> " + matcher);
-            Name propertyName = context.getValueFactories().getNameFactory().create(matcher.group(1));
-            System.out.println("Property name: " + matcher.group(1));
-            String value = matcher.group(2); // doesn't handle multiple values!!
-            if (value.trim().length() == 0) value = null;
-            Property actual = propertiesByName.remove(propertyName);
-            Property expectedProperty = context.getPropertyFactory().create(propertyName, value);
-            assertThat("missing property " + propertyName, actual, is(expectedProperty));
-        }
-        if (!propertiesByName.isEmpty()) {
-            System.out.println("Properties for " + path + "\n" + propertiesByName);
-        }
-        assertThat(propertiesByName.isEmpty(), is(true));
-    }
-
-    protected class MockRepositoryConnection implements RepositoryConnection {
-        public void close() {
-        }
-
-        @SuppressWarnings( "synthetic-access" )
-        public void execute( ExecutionContext context,
-                             Request request ) throws RepositorySourceException {
-            lastExecutedRequest = request;
-            if (request instanceof VerifyWorkspaceRequest) {
-                VerifyWorkspaceRequest workspaceRequest = (VerifyWorkspaceRequest)request;
-                workspaceRequest.setActualRootLocation(Location.create(context.getValueFactories().getPathFactory().createRootPath()));
-                workspaceRequest.setActualWorkspaceName("default");
-            }
-        }
-
-        public CachePolicy getDefaultCachePolicy() {
-            return null;
-        }
-
-        @SuppressWarnings( "synthetic-access" )
-        public String getSourceName() {
-            return sourceName;
-        }
-
-        public XAResource getXAResource() {
-            return null;
-        }
-
-        public boolean ping( long time,
-                             TimeUnit unit ) {
-            return true;
-        }
-
-        public void setListener( RepositorySourceListener listener ) {
-        }
-    }
-
-}

Copied: trunk/dna-graph/src/test/java/org/jboss/dna/graph/io/GraphImporterTest.java (from rev 776, trunk/dna-graph/src/test/java/org/jboss/dna/graph/GraphImporterTest.java)
===================================================================
--- trunk/dna-graph/src/test/java/org/jboss/dna/graph/io/GraphImporterTest.java	                        (rev 0)
+++ trunk/dna-graph/src/test/java/org/jboss/dna/graph/io/GraphImporterTest.java	2009-04-15 22:42:18 UTC (rev 829)
@@ -0,0 +1,192 @@
+/*
+ * JBoss DNA (http://www.jboss.org/dna)
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership.  Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ * See the AUTHORS.txt file in the distribution for a full listing of 
+ * individual contributors. 
+ *
+ * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
+ * is licensed to you under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * JBoss DNA is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.dna.graph.io;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsInstanceOf.instanceOf;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.stub;
+import java.io.File;
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import javax.transaction.xa.XAResource;
+import org.jboss.dna.graph.DnaLexicon;
+import org.jboss.dna.graph.ExecutionContext;
+import org.jboss.dna.graph.Graph;
+import org.jboss.dna.graph.Location;
+import org.jboss.dna.graph.cache.CachePolicy;
+import org.jboss.dna.graph.connector.RepositoryConnection;
+import org.jboss.dna.graph.connector.RepositoryConnectionFactory;
+import org.jboss.dna.graph.connector.RepositorySourceException;
+import org.jboss.dna.graph.connector.RepositorySourceListener;
+import org.jboss.dna.graph.property.Name;
+import org.jboss.dna.graph.property.Path;
+import org.jboss.dna.graph.property.Property;
+import org.jboss.dna.graph.request.CompositeRequest;
+import org.jboss.dna.graph.request.CreateNodeRequest;
+import org.jboss.dna.graph.request.Request;
+import org.jboss.dna.graph.request.VerifyWorkspaceRequest;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.MockitoAnnotations;
+import org.mockito.MockitoAnnotations.Mock;
+
+/**
+ * @author Randall Hauch
+ */
+public class GraphImporterTest {
+
+    private Graph graph;
+    private GraphImporter importer;
+    private String sourceName;
+    private ExecutionContext context;
+    private URI xmlContent;
+    private MockRepositoryConnection connection;
+    private Request lastExecutedRequest;
+    private Path destinationPath;
+    @Mock
+    private RepositoryConnectionFactory sources;
+
+    @Before
+    public void beforeEach() throws Exception {
+        MockitoAnnotations.initMocks(this);
+        xmlContent = new File("src/test/resources/repositoryImporterTestData1.xml").toURI();
+        context = new ExecutionContext();
+        context.getNamespaceRegistry().register(DnaLexicon.Namespace.PREFIX, DnaLexicon.Namespace.URI);
+        context.getNamespaceRegistry().register("jcr", "http://www.jcp.org/jcr/1.0");
+        context.getNamespaceRegistry().register("nt", "http://www.jcp.org/jcr/nt/1.0");
+        sourceName = "sourceA";
+        destinationPath = context.getValueFactories().getPathFactory().create("/a/b");
+        graph = Graph.create(sourceName, sources, context);
+        importer = new GraphImporter(graph);
+        connection = new MockRepositoryConnection();
+        stub(sources.createConnection(sourceName)).toReturn(connection);
+    }
+
+    @Test
+    public void shouldImportXmlContentAndGenerateTheCorrectCommands() throws Exception {
+        System.out.println(xmlContent);
+        Graph.Batch batch = importer.importXml(xmlContent, Location.create(destinationPath));
+        batch.execute();
+        // 'lastExecutedCommand'
+        assertThat(lastExecutedRequest, is(instanceOf(CompositeRequest.class)));
+        Iterator<Request> iter = ((CompositeRequest)lastExecutedRequest).iterator();
+        // assertCreateNode(iter, "/a/b/", "jcr:primaryType={http://www.jboss.org/dna/xml/1.0}document");
+        assertCreateNode(iter, "/a/b/dna:system[1]", "jcr:primaryType={http://www.jcp.org/jcr/nt/1.0}unstructured");
+        assertCreateNode(iter, "/a/b/dna:system[1]/dna:sources[1]", "jcr:primaryType={http://www.jcp.org/jcr/nt/1.0}unstructured");
+        assertCreateNode(iter,
+                         "/a/b/dna:system[1]/dna:sources[1]/sourceA[1]",
+                         "repositoryName=repositoryA",
+                         "retryLimit=3",
+                         "jcr:primaryType={http://www.jboss.org/dna/1.0}xyz",
+                         "dna:classname=org.jboss.dna.connector.inmemory.InMemoryRepositorySource");
+        assertCreateNode(iter,
+                         "/a/b/dna:system[1]/dna:sources[1]/sourceB[1]",
+                         "repositoryName=repositoryB",
+                         "jcr:primaryType={http://www.jcp.org/jcr/nt/1.0}unstructured",
+                         "dna:classname=org.jboss.dna.connector.inmemory.InMemoryRepositorySource");
+        assertThat(iter.hasNext(), is(false));
+    }
+
+    public void assertCreateNode( Iterator<Request> iterator,
+                                  String path,
+                                  String... properties ) {
+        Request nextCommand = iterator.next();
+        assertThat(nextCommand, is(instanceOf(CreateNodeRequest.class)));
+        CreateNodeRequest createNode = (CreateNodeRequest)nextCommand;
+        Path expectedPath = context.getValueFactories().getPathFactory().create(path);
+        Path parentPath = createNode.under().getPath();
+        assertThat(parentPath, is(expectedPath.getParent()));
+        assertThat(createNode.named(), is(expectedPath.getLastSegment().getName()));
+        Map<Name, Property> propertiesByName = new HashMap<Name, Property>();
+        for (Property prop : createNode.properties()) {
+            propertiesByName.put(prop.getName(), prop);
+        }
+        for (String propertyStr : properties) {
+            if (propertyStr == "any properties") {
+                propertiesByName.clear();
+                break;
+            }
+            Matcher matcher = Pattern.compile("([^=]+)=(.*)").matcher(propertyStr);
+            if (!matcher.matches()) continue;
+            System.out.println("Property: " + propertyStr + " ==> " + matcher);
+            Name propertyName = context.getValueFactories().getNameFactory().create(matcher.group(1));
+            System.out.println("Property name: " + matcher.group(1));
+            String value = matcher.group(2); // doesn't handle multiple values!!
+            if (value.trim().length() == 0) value = null;
+            Property actual = propertiesByName.remove(propertyName);
+            Property expectedProperty = context.getPropertyFactory().create(propertyName, value);
+            assertThat("missing property " + propertyName, actual, is(expectedProperty));
+        }
+        if (!propertiesByName.isEmpty()) {
+            System.out.println("Properties for " + path + "\n" + propertiesByName);
+        }
+        assertThat(propertiesByName.isEmpty(), is(true));
+    }
+
+    protected class MockRepositoryConnection implements RepositoryConnection {
+        public void close() {
+        }
+
+        @SuppressWarnings( "synthetic-access" )
+        public void execute( ExecutionContext context,
+                             Request request ) throws RepositorySourceException {
+            lastExecutedRequest = request;
+            if (request instanceof VerifyWorkspaceRequest) {
+                VerifyWorkspaceRequest workspaceRequest = (VerifyWorkspaceRequest)request;
+                workspaceRequest.setActualRootLocation(Location.create(context.getValueFactories()
+                                                                              .getPathFactory()
+                                                                              .createRootPath()));
+                workspaceRequest.setActualWorkspaceName("default");
+            }
+        }
+
+        public CachePolicy getDefaultCachePolicy() {
+            return null;
+        }
+
+        @SuppressWarnings( "synthetic-access" )
+        public String getSourceName() {
+            return sourceName;
+        }
+
+        public XAResource getXAResource() {
+            return null;
+        }
+
+        public boolean ping( long time,
+                             TimeUnit unit ) {
+            return true;
+        }
+
+        public void setListener( RepositorySourceListener listener ) {
+        }
+    }
+
+}


Property changes on: trunk/dna-graph/src/test/java/org/jboss/dna/graph/io/GraphImporterTest.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: trunk/dna-graph/src/test/java/org/jboss/dna/graph/xml/XmlHandlerTest.java
===================================================================
--- trunk/dna-graph/src/test/java/org/jboss/dna/graph/xml/XmlHandlerTest.java	2009-04-15 14:28:30 UTC (rev 828)
+++ trunk/dna-graph/src/test/java/org/jboss/dna/graph/xml/XmlHandlerTest.java	2009-04-15 22:42:18 UTC (rev 829)
@@ -40,6 +40,7 @@
 import org.jboss.dna.graph.ExecutionContext;
 import org.jboss.dna.graph.JcrLexicon;
 import org.jboss.dna.graph.Location;
+import org.jboss.dna.graph.io.Destination;
 import org.jboss.dna.graph.property.Name;
 import org.jboss.dna.graph.property.NamespaceRegistry;
 import org.jboss.dna.graph.property.Path;
@@ -62,7 +63,7 @@
 
     private XmlHandler handler;
     private ExecutionContext context;
-    private XmlHandler.Destination destination;
+    private Destination destination;
     private boolean skipRootElement = false;
     private Path parentPath;
     private TextDecoder decoder;
@@ -432,7 +433,7 @@
         }
     }
 
-    protected class RecordingDestination implements XmlHandler.Destination {
+    protected class RecordingDestination implements Destination {
         private final LinkedList<CreateNodeRequest> requests = new LinkedList<CreateNodeRequest>();
         private final String workspace = "Recording Workspace";
 

Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrWorkspace.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrWorkspace.java	2009-04-15 14:28:30 UTC (rev 828)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrWorkspace.java	2009-04-15 22:42:18 UTC (rev 829)
@@ -46,11 +46,11 @@
 import org.jboss.dna.common.util.CheckArg;
 import org.jboss.dna.graph.ExecutionContext;
 import org.jboss.dna.graph.Graph;
-import org.jboss.dna.graph.GraphImporter;
 import org.jboss.dna.graph.Location;
 import org.jboss.dna.graph.connector.RepositoryConnectionFactory;
 import org.jboss.dna.graph.connector.RepositorySource;
 import org.jboss.dna.graph.connector.RepositorySourceException;
+import org.jboss.dna.graph.io.GraphImporter;
 import org.jboss.dna.graph.property.Name;
 import org.jboss.dna.graph.property.Path;
 import org.jboss.dna.graph.property.PathFactory;

Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrTckTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrTckTest.java	2009-04-15 14:28:30 UTC (rev 828)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrTckTest.java	2009-04-15 22:42:18 UTC (rev 829)
@@ -72,12 +72,12 @@
 import org.jboss.dna.graph.DnaLexicon;
 import org.jboss.dna.graph.ExecutionContext;
 import org.jboss.dna.graph.Graph;
-import org.jboss.dna.graph.GraphImporter;
 import org.jboss.dna.graph.JcrNtLexicon;
 import org.jboss.dna.graph.Location;
 import org.jboss.dna.graph.connector.RepositoryConnection;
 import org.jboss.dna.graph.connector.RepositoryConnectionFactory;
 import org.jboss.dna.graph.connector.inmemory.InMemoryRepositorySource;
+import org.jboss.dna.graph.io.GraphImporter;
 import org.jboss.dna.graph.property.Path;
 import org.jboss.dna.jcr.JcrRepository.Options;
 

Modified: trunk/dna-jcr-tck/src/test/java/org/jboss/dna/jcr/InMemoryRepositoryStub.java
===================================================================
--- trunk/dna-jcr-tck/src/test/java/org/jboss/dna/jcr/InMemoryRepositoryStub.java	2009-04-15 14:28:30 UTC (rev 828)
+++ trunk/dna-jcr-tck/src/test/java/org/jboss/dna/jcr/InMemoryRepositoryStub.java	2009-04-15 22:42:18 UTC (rev 829)
@@ -36,12 +36,12 @@
 import org.jboss.dna.graph.DnaLexicon;
 import org.jboss.dna.graph.ExecutionContext;
 import org.jboss.dna.graph.Graph;
-import org.jboss.dna.graph.GraphImporter;
 import org.jboss.dna.graph.JcrNtLexicon;
 import org.jboss.dna.graph.Location;
 import org.jboss.dna.graph.connector.RepositoryConnection;
 import org.jboss.dna.graph.connector.RepositoryConnectionFactory;
 import org.jboss.dna.graph.connector.inmemory.InMemoryRepositorySource;
+import org.jboss.dna.graph.io.GraphImporter;
 import org.jboss.dna.graph.property.Path;
 import org.jboss.dna.jcr.JcrRepository.Options;
 




More information about the dna-commits mailing list