You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2012/01/30 10:54:38 UTC

svn commit: r1237585 - in /commons/sandbox/graph/trunk/src: main/java/org/apache/commons/graph/export/ test/java/org/apache/commons/graph/export/

Author: simonetripodi
Date: Mon Jan 30 09:54:37 2012
New Revision: 1237585

URL: http://svn.apache.org/viewvc?rev=1237585&view=rev
Log:
[SANDBOX-370] GraphML format exporter - patch submitted by Matteo Moci

Added:
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/AbstractExporter.java   (with props)
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultNamedExportSelector.java
      - copied, changed from r1236132, commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultNamedEsportSelector.java
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java   (with props)
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphExportException.java   (with props)
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java   (with props)
    commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/export/
    commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/export/ExportTestCase.java   (with props)
Removed:
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultNamedEsportSelector.java
Modified:
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultToStreamBuilder.java
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/ExportSelctor.java

Added: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/AbstractExporter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/AbstractExporter.java?rev=1237585&view=auto
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/AbstractExporter.java (added)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/AbstractExporter.java Mon Jan 30 09:54:37 2012
@@ -0,0 +1,92 @@
+package org.apache.commons.graph.export;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.IOException;
+import java.io.Writer;
+
+import org.apache.commons.graph.Edge;
+import org.apache.commons.graph.Graph;
+import org.apache.commons.graph.Vertex;
+
+abstract class AbstractExporter<V extends Vertex, E extends Edge, G extends Graph<V, E>>
+{
+
+    private static final String G = "G";
+
+    private final G graph;
+
+    private final Writer writer;
+
+    private final String name;
+
+    public AbstractExporter( G graph, Writer writer, String name )
+    {
+        this.graph = graph;
+        this.writer = writer;
+        this.name = name != null ? name : G;
+    }
+
+    protected final G getGraph()
+    {
+        return graph;
+    }
+
+    protected final Writer getWriter()
+    {
+        return writer;
+    }
+
+    protected final String getName()
+    {
+        return name;
+    }
+
+    public final void export()
+        throws GraphExportException
+    {
+        try
+        {
+            internalExport();
+        }
+        catch ( Exception e )
+        {
+            throw new GraphExportException( e, "an error occurred while exporting graph %s (named %s) to writer %s",
+                                            graph,
+                                            name,
+                                            writer );
+        }
+        finally
+        {
+            try
+            {
+                writer.close();
+            }
+            catch ( IOException e )
+            {
+                // swallow it
+            }
+        }
+    }
+
+    protected abstract void internalExport()
+        throws Exception;
+
+}

Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/AbstractExporter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/AbstractExporter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/AbstractExporter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Copied: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultNamedExportSelector.java (from r1236132, commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultNamedEsportSelector.java)
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultNamedExportSelector.java?p2=commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultNamedExportSelector.java&p1=commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultNamedEsportSelector.java&r1=1236132&r2=1237585&rev=1237585&view=diff
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultNamedEsportSelector.java (original)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultNamedExportSelector.java Mon Jan 30 09:54:37 2012
@@ -19,43 +19,28 @@ package org.apache.commons.graph.export;
  * under the License.
  */
 
-import java.io.IOException;
-import java.io.PrintWriter;
 import java.io.Writer;
-import java.util.Date;
 
-import org.apache.commons.graph.DirectedGraph;
 import org.apache.commons.graph.Edge;
 import org.apache.commons.graph.Graph;
-import org.apache.commons.graph.Labeled;
 import org.apache.commons.graph.Vertex;
-import org.apache.commons.graph.VertexPair;
-import org.apache.commons.graph.WeightedEdge;
 
-final class DefaultNamedEsportSelector<V extends Vertex, E extends Edge, G extends Graph<V, E>>
+final class DefaultNamedExportSelector<V extends Vertex, E extends Edge, G extends Graph<V, E>>
     implements NamedExportSelctor<V, E, G>
 {
 
-    private static final String GRAPH = "graph";
-
-    private static final String DIGRAPH = "digraph";
-
-    private static final String CONNECTOR = "--";
-
-    private static final String DICONNECTOR = "->";
-
     private final G graph;
 
     private final Writer writer;
 
     private final String name;
 
-    public DefaultNamedEsportSelector( G graph, Writer writer )
+    public DefaultNamedExportSelector( G graph, Writer writer )
     {
         this( graph, writer, null );
     }
 
-    public DefaultNamedEsportSelector( G graph, Writer writer, String name )
+    public DefaultNamedExportSelector( G graph, Writer writer, String name )
     {
         this.graph = graph;
         this.writer = writer;
@@ -63,94 +48,21 @@ final class DefaultNamedEsportSelector<V
     }
 
     public void usingDotNotation()
+        throws GraphExportException
     {
-        String graphDeclaration;
-        String connector;
+        new DotExporter<V, E, G>( graph, writer, name ).export();
+    }
 
-        if ( graph instanceof DirectedGraph )
-        {
-            graphDeclaration = DIGRAPH;
-            connector = DICONNECTOR;
-        }
-        else
-        {
-            graphDeclaration = GRAPH;
-            connector = CONNECTOR;
-        }
-
-        PrintWriter printWriter = new PrintWriter( writer );
-        try
-        {
-            printWriter.format( "# Graph generated by Apache Commons Graph on %s%n", new Date() );
-            printWriter.format( "%s %s {%n", graphDeclaration, name != null ? name : 'G' );
-
-            for ( V vertex : graph.getVertices() )
-            {
-                printWriter.format( "  %s [label=\"%s\"];%n",
-                                    vertex.hashCode(),
-                                    ( vertex instanceof Labeled) ? ( (Labeled) vertex).getLabel() : vertex.toString() );
-            }
-
-            for ( E edge : graph.getEdges() )
-            {
-                VertexPair<V> vertexPair = graph.getVertices( edge );
-
-                printWriter.format( "  %s %s %s",
-                                    vertexPair.getHead().hashCode(),
-                                    connector,
-                                    vertexPair.getTail().hashCode() );
-
-                boolean attributesFound = false;
-
-                if ( edge instanceof Labeled )
-                {
-                    attributesFound = true;
-                    printWriter.format( " [label=\"%s\"", ( (Labeled) edge ).getLabel() );
-                }
-                if ( edge instanceof WeightedEdge )
-                {
-                    Object weight = ( (WeightedEdge<?>) edge ).getWeight();
-
-                    if ( weight instanceof Number )
-                    {
-                        printWriter.format( " " );
-
-                        if ( !attributesFound )
-                        {
-                            printWriter.format( "[" );
-                            attributesFound = true;
-                        }
-
-                        printWriter.format( "weight=%f", ( (Number) weight ).doubleValue() );
-                    }
-                }
-
-                if ( attributesFound )
-                {
-                    printWriter.format( "]" );
-                }
-
-                printWriter.format( "%n" );
-            }
-
-            printWriter.write( '}' );
-        }
-        finally
-        {
-            try
-            {
-                writer.close();
-            }
-            catch ( IOException e )
-            {
-                // swallow it
-            }
-        }
+    @Override
+    public void usingGraphMLFormat()
+        throws GraphExportException
+    {
+        new GraphMLExporter<V, E, G>( graph, writer, name ).export();
     }
 
     public ExportSelctor<V, E, G> withName( String name )
     {
-        return new DefaultNamedEsportSelector<V, E, G>( graph, writer, name );
+        return new DefaultNamedExportSelector<V, E, G>( graph, writer, name );
     }
 
 }

Modified: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultToStreamBuilder.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultToStreamBuilder.java?rev=1237585&r1=1237584&r2=1237585&view=diff
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultToStreamBuilder.java (original)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DefaultToStreamBuilder.java Mon Jan 30 09:54:37 2012
@@ -63,7 +63,7 @@ public final class DefaultToStreamBuilde
     public NamedExportSelctor<V, E, G> to( Writer writer )
     {
         writer = checkNotNull( writer, "Impossibe to export the graph in a null stream" );
-        return new DefaultNamedEsportSelector<V, E, G>( graph, writer );
+        return new DefaultNamedExportSelector<V, E, G>( graph, writer );
     }
 
 }

Added: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java?rev=1237585&view=auto
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java (added)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java Mon Jan 30 09:54:37 2012
@@ -0,0 +1,126 @@
+package org.apache.commons.graph.export;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.PrintWriter;
+import java.io.Writer;
+import java.util.Date;
+
+import org.apache.commons.graph.DirectedGraph;
+import org.apache.commons.graph.Edge;
+import org.apache.commons.graph.Graph;
+import org.apache.commons.graph.Labeled;
+import org.apache.commons.graph.Vertex;
+import org.apache.commons.graph.VertexPair;
+import org.apache.commons.graph.WeightedEdge;
+
+final class DotExporter<V extends Vertex, E extends Edge, G extends Graph<V, E>>
+    extends AbstractExporter<V, E, G>
+{
+
+    private static final String GRAPH = "graph";
+
+    private static final String DIGRAPH = "digraph";
+
+    private static final String CONNECTOR = "--";
+
+    private static final String DICONNECTOR = "->";
+
+    public DotExporter( G graph, Writer writer, String name )
+    {
+        super( graph, writer, name );
+    }
+
+    @Override
+    protected void internalExport()
+        throws Exception
+    {
+        String graphDeclaration;
+        String connector;
+
+        if ( getGraph() instanceof DirectedGraph )
+        {
+            graphDeclaration = DIGRAPH;
+            connector = DICONNECTOR;
+        }
+        else
+        {
+            graphDeclaration = GRAPH;
+            connector = CONNECTOR;
+        }
+
+        PrintWriter printWriter = new PrintWriter( getWriter() );
+
+        printWriter.format( "# Graph generated by Apache Commons Graph on %s%n", new Date() );
+        printWriter.format( "%s %s {%n", graphDeclaration, getName() );
+
+        for ( V vertex : getGraph().getVertices() )
+        {
+            printWriter.format( "  %s [label=\"%s\"];%n",
+                                vertex.hashCode(),
+                                ( vertex instanceof Labeled) ? ( (Labeled) vertex).getLabel() : vertex.toString() );
+        }
+
+        for ( E edge : getGraph().getEdges() )
+        {
+            VertexPair<V> vertexPair = getGraph().getVertices( edge );
+
+            printWriter.format( "  %s %s %s",
+                                vertexPair.getHead().hashCode(),
+                                connector,
+                                vertexPair.getTail().hashCode() );
+
+            boolean attributesFound = false;
+
+            if ( edge instanceof Labeled )
+            {
+                attributesFound = true;
+                printWriter.format( " [label=\"%s\"", ( (Labeled) edge ).getLabel() );
+            }
+            if ( edge instanceof WeightedEdge )
+            {
+                Object weight = ( (WeightedEdge<?>) edge ).getWeight();
+
+                if ( weight instanceof Number )
+                {
+                    printWriter.format( " " );
+
+                    if ( !attributesFound )
+                    {
+                        printWriter.format( "[" );
+                        attributesFound = true;
+                    }
+
+                    printWriter.format( "weight=%f", ( (Number) weight ).doubleValue() );
+                }
+            }
+
+            if ( attributesFound )
+            {
+                printWriter.format( "]" );
+            }
+
+            printWriter.format( "%n" );
+        }
+
+        printWriter.write( '}' );
+    }
+
+}

Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/DotExporter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/ExportSelctor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/ExportSelctor.java?rev=1237585&r1=1237584&r2=1237585&view=diff
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/ExportSelctor.java (original)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/ExportSelctor.java Mon Jan 30 09:54:37 2012
@@ -29,6 +29,13 @@ public interface ExportSelctor<V extends
     /**
      * Export Graphs in <a href="http://en.wikipedia.org/wiki/DOT_language">DOT language</a>.
      */
-    void usingDotNotation();
+    void usingDotNotation()
+        throws GraphExportException;
+
+    /**
+     * Export Graphs in <a href="http://graphml.graphdrawing.org/">GraphML file format</a>.
+     */
+    void usingGraphMLFormat()
+        throws GraphExportException;
 
 }

Added: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphExportException.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphExportException.java?rev=1237585&view=auto
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphExportException.java (added)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphExportException.java Mon Jan 30 09:54:37 2012
@@ -0,0 +1,35 @@
+package org.apache.commons.graph.export;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import static java.lang.String.format;
+
+final class GraphExportException
+    extends Exception
+{
+
+    private static final long serialVersionUID = 1L;
+
+    public GraphExportException( Throwable cause, String messagePattern, Object...messageArguments )
+    {
+        super( format( messagePattern, messageArguments ), cause );
+    }
+
+}

Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphExportException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphExportException.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphExportException.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java?rev=1237585&view=auto
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java (added)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java Mon Jan 30 09:54:37 2012
@@ -0,0 +1,233 @@
+package org.apache.commons.graph.export;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.commons.graph.Edge;
+import org.apache.commons.graph.Graph;
+import org.apache.commons.graph.Labeled;
+import org.apache.commons.graph.Vertex;
+import org.apache.commons.graph.Weighted;
+
+final class GraphMLExporter<V extends Vertex, E extends Edge, G extends Graph<V, E>>
+    extends AbstractExporter<V, E, G>
+{
+
+    private static final String GRAPHML = "graphml";
+
+    private static final String XMLNS = "xmlns";
+
+    private static final String GRAPHML_XMLNS = "http://graphml.graphdrawing.org/xmlns";
+
+    private static final String EDGEDEFAULT = "edgedefault";
+
+    private static final String DIRECTED = "directed";
+
+    private static final String KEY = "key";
+
+    private static final String FOR = "for";
+
+    private static final String ID = "id";
+
+    private static final String ATTR_NAME = "attr.name";
+
+    private static final String ATTR_TYPE = "attr.type";
+
+    private static final String GRAPH = "graph";
+
+    private static final String NODE = "node";
+
+    private static final String EDGE = "edge";
+
+    private static final String SOURCE = "source";
+
+    private static final String TARGET = "target";
+
+    private static final String DATA = "data";
+
+    private static final String LABEL = "label";
+
+    private static final String STRING = "string";
+
+    private static final String FLOAT = "float";
+
+    private static final String DOUBLE = "double";
+
+    private static final String LONG = "long";
+
+    private static final String BOOLEAN = "boolean";
+
+    private static final String INT = "int";
+
+    private static final String WEIGHT = "weight";
+
+    public GraphMLExporter( G graph, Writer writer, String name )
+    {
+        super( graph, writer, name );
+    }
+
+    @Override
+    protected void internalExport()
+        throws Exception
+    {
+        XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter( getWriter() );
+
+        // start document tokens
+        xmlWriter.writeStartDocument();
+        xmlWriter.writeStartElement( GRAPHML );
+        xmlWriter.writeAttribute( XMLNS, GRAPHML_XMLNS );
+
+        // scan graph and print TYPES description for vertex AND edges' properties
+        Map<String, String> edgeKeyTypes = new HashMap<String, String>();
+
+        for ( E edge : getGraph().getEdges() )
+        {
+            if ( edge instanceof Labeled )
+            {
+                if ( !edgeKeyTypes.containsKey( LABEL ) )
+                {
+                    edgeKeyTypes.put( LABEL, getStringType( ( (Labeled) edge ).getLabel() ) );
+                }
+            }
+
+            if ( edge instanceof Weighted )
+            {
+                if ( !edgeKeyTypes.containsKey( WEIGHT ) )
+                {
+                    edgeKeyTypes.put( WEIGHT, getStringType( ( (Weighted<?>) edge ).getWeight() ) );
+                }
+            }
+        }
+
+        // scan vertices
+        Map<String, String> vertexKeyTypes = new HashMap<String, String>();
+
+        for ( V vertex : getGraph().getVertices() )
+        {
+            if ( vertex instanceof Labeled )
+            {
+                if ( !vertexKeyTypes.containsKey( LABEL ) )
+                {
+                    vertexKeyTypes.put( LABEL, getStringType( ( (Labeled) vertex ).getLabel() ) );
+                }
+            }
+        }
+
+        xmlWriter.writeStartElement( GRAPH );
+        xmlWriter.writeAttribute( ID, getName() );
+        xmlWriter.writeAttribute( EDGEDEFAULT, DIRECTED );
+
+        // write EDGE key types
+        for ( Map.Entry<String, String> entry : edgeKeyTypes.entrySet() )
+        {
+            xmlWriter.writeStartElement( KEY );
+            xmlWriter.writeAttribute( ID, entry.getKey() );
+            xmlWriter.writeAttribute( FOR, EDGE );
+            xmlWriter.writeAttribute( ATTR_NAME, entry.getKey() );
+            xmlWriter.writeAttribute( ATTR_TYPE, entry.getValue() );
+            xmlWriter.writeEndElement();
+        }
+
+        // write VERTICES' key types
+        for ( Map.Entry<String, String> entry : vertexKeyTypes.entrySet() )
+        {
+            xmlWriter.writeStartElement( KEY );
+            xmlWriter.writeAttribute( ID, entry.getKey() );
+            xmlWriter.writeAttribute( FOR, NODE );
+            xmlWriter.writeAttribute( ATTR_NAME, entry.getKey() );
+            xmlWriter.writeAttribute( ATTR_TYPE, entry.getValue() );
+            xmlWriter.writeEndElement();
+        }
+
+        for ( V vertex : getGraph().getVertices() )
+        {
+            xmlWriter.writeStartElement( NODE );
+            xmlWriter.writeAttribute( ID, String.valueOf( vertex.hashCode() ) );
+
+            if ( vertex instanceof Labeled )
+            {
+                String label = ( (Labeled) vertex ).getLabel();
+                xmlWriter.writeStartElement( DATA );
+                xmlWriter.writeAttribute( KEY, LABEL );
+                xmlWriter.writeCharacters( label );
+                xmlWriter.writeEndElement();
+            }
+
+            xmlWriter.writeEndElement();
+        }
+
+        for ( E edge : getGraph().getEdges() )
+        {
+            xmlWriter.writeStartElement( EDGE );
+            xmlWriter.writeAttribute( ID, String.valueOf( edge.hashCode() ) );
+            xmlWriter.writeAttribute( SOURCE, String.valueOf( getGraph().getVertices( edge ).getHead().hashCode() ) );
+            xmlWriter.writeAttribute( TARGET, String.valueOf( getGraph().getVertices( edge ).getTail().hashCode() ) );
+
+            if ( edge instanceof Labeled )
+            {
+                xmlWriter.writeAttribute( LABEL, ( (Labeled) edge ).getLabel() );
+            }
+            if ( edge instanceof Weighted )
+            {
+                xmlWriter.writeAttribute( WEIGHT, ( (Weighted<?>) edge ).getWeight().toString() );
+            }
+            xmlWriter.writeEndElement();
+        }
+
+        xmlWriter.writeEndElement(); // graph
+        xmlWriter.writeEndElement(); // graphml
+        xmlWriter.writeEndDocument();
+
+        xmlWriter.flush();
+        xmlWriter.close();
+    }
+
+    private static <O> String getStringType( O object )
+    {
+        if ( object instanceof Integer )
+        {
+            return INT;
+        }
+        else if ( object instanceof Long )
+        {
+            return LONG;
+        }
+        else if ( object instanceof Float )
+        {
+            return FLOAT;
+        }
+        else if ( object instanceof Double )
+        {
+            return DOUBLE;
+        }
+        else if ( object instanceof Boolean )
+        {
+            return BOOLEAN;
+        }
+        return STRING;
+    }
+
+}

Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/export/ExportTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/export/ExportTestCase.java?rev=1237585&view=auto
==============================================================================
--- commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/export/ExportTestCase.java (added)
+++ commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/export/ExportTestCase.java Mon Jan 30 09:54:37 2012
@@ -0,0 +1,83 @@
+package org.apache.commons.graph.export;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import static org.apache.commons.graph.CommonsGraph.export;
+import static org.apache.commons.graph.CommonsGraph.newUndirectedMutableWeightedGraph;
+
+import org.apache.commons.graph.builder.AbstractGraphConnection;
+import org.apache.commons.graph.model.BaseLabeledVertex;
+import org.apache.commons.graph.model.BaseLabeledWeightedEdge;
+import org.apache.commons.graph.model.UndirectedMutableWeightedGraph;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class ExportTestCase {
+
+    private UndirectedMutableWeightedGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>, Double> actual;
+
+    @Before
+    public void setUp()
+    {
+        actual =
+        newUndirectedMutableWeightedGraph( new AbstractGraphConnection<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>()
+        {
+
+            public void connect()
+            {
+                BaseLabeledVertex start = addVertex( new BaseLabeledVertex( "start" ) );
+                BaseLabeledVertex a = addVertex( new BaseLabeledVertex( "a" ) );
+                BaseLabeledVertex b = addVertex( new BaseLabeledVertex( "b" ) );
+                BaseLabeledVertex goal = addVertex( new BaseLabeledVertex( "goal" ) );
+
+                addEdge( new BaseLabeledWeightedEdge<Double>( "start <-> a", 1.5D ) )
+                        .from(start).to( a );
+                addEdge( new BaseLabeledWeightedEdge<Double>( "a <-> b", 2D ) )
+                        .from(a).to( b );
+                addEdge( new BaseLabeledWeightedEdge<Double>( "a <-> goal", 2D ) )
+                        .from(a).to( goal );
+                addEdge( new BaseLabeledWeightedEdge<Double>( "b <-> goal", 2D ) )
+                        .from( b ).to( goal );
+            }
+        } );
+    }
+
+    @After
+    public void tearDown()
+    {
+        actual = null;
+    }
+
+    @Test
+    public void shouldPrintDotFormat()
+        throws Exception
+    {
+        export( actual ).to( System.out ).usingDotNotation();
+    }
+
+    @Test
+    public void shouldPrintGraphMLFormat()
+        throws Exception
+    {
+        export( actual ).to( System.out ).usingGraphMLFormat();
+    }
+
+}

Propchange: commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/export/ExportTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/export/ExportTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/graph/trunk/src/test/java/org/apache/commons/graph/export/ExportTestCase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain