commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1366791 - /commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java
Date Sun, 29 Jul 2012 08:09:23 GMT
Author: simonetripodi
Date: Sun Jul 29 08:09:23 2012
New Revision: 1366791

URL: http://svn.apache.org/viewvc?rev=1366791&view=rev
Log:
started restoring the GraphML exporter, but using the javax.xml.transform.sax.TransformerHandler
rather than the javax.xml.stream.XMLStreamWriter, that is not available in java5 (which is
commons-graph jvm target)

Modified:
    commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java

Modified: 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=1366791&r1=1366790&r2=1366791&view=diff
==============================================================================
--- commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java
(original)
+++ commons/sandbox/graph/trunk/src/main/java/org/apache/commons/graph/export/GraphMLExporter.java
Sun Jul 29 08:09:23 2012
@@ -21,13 +21,21 @@ package org.apache.commons.graph.export;
 
 import java.util.Map;
 
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.sax.SAXTransformerFactory;
+import javax.xml.transform.sax.TransformerHandler;
+import javax.xml.transform.stream.StreamResult;
+
 import org.apache.commons.graph.Graph;
 import org.apache.commons.graph.Mapper;
+import org.xml.sax.helpers.AttributesImpl;
 
 final class GraphMLExporter<V, E>
     extends AbstractExporter<V, E, GraphMLExporter<V, E>>
 {
 
+    private static final SAXTransformerFactory SAX_TRANSFORMER_FACTORY = (SAXTransformerFactory)
TransformerFactory.newInstance();
+
     private static final String GRAPHML = "graphml";
 
     private static final String XMLNS = "xmlns";
@@ -76,6 +84,10 @@ final class GraphMLExporter<V, E>
 
     private static final String WEIGHT = "weight";
 
+    private static final String CDATA_TYPE = "CDATA";
+
+    private TransformerHandler transformerHandler;
+
     GraphMLExporter( Graph<V, E> graph, String name )
     {
         super( graph, name );
@@ -85,93 +97,120 @@ final class GraphMLExporter<V, E>
     protected void startSerialization()
         throws Exception
     {
-        // TODO Auto-generated method stub
-
+        transformerHandler = SAX_TRANSFORMER_FACTORY.newTransformerHandler();
+        transformerHandler.setResult( new StreamResult( getWriter() ) );
+        transformerHandler.startDocument();
     }
 
     @Override
     protected void endSerialization()
         throws Exception
     {
-        // TODO Auto-generated method stub
-
+        transformerHandler.endDocument();
+        getWriter().flush();
+        getWriter().close();
     }
 
     @Override
     protected void startGraph( String name )
         throws Exception
     {
-        // TODO Auto-generated method stub
+        transformerHandler.startElement( GRAPHML_XMLNS, GRAPHML, GRAPHML, new AttributesImpl()
);
 
+        AttributesImpl atts = new AttributesImpl();
+        atts.addAttribute( GRAPHML_XMLNS, ID, ID, CDATA_TYPE, name );
+        atts.addAttribute( GRAPHML_XMLNS, EDGEDEFAULT, EDGEDEFAULT, CDATA_TYPE, DIRECTED
);
+        transformerHandler.startElement( GRAPHML_XMLNS, GRAPH, GRAPH, atts );
     }
 
     @Override
     protected void endGraph()
         throws Exception
     {
-        // TODO Auto-generated method stub
-
+        transformerHandler.endElement( GRAPHML_XMLNS, GRAPH, GRAPH ); // graph
+        transformerHandler.endElement( GRAPHML_XMLNS, GRAPHML, GRAPHML ); // graphml
     }
 
     @Override
     protected void comment( String text )
         throws Exception
     {
-        // TODO Auto-generated method stub
-
+        transformerHandler.comment( text.toCharArray(), 0, text.length() );
     }
 
     @Override
     protected void enlistVerticesProperty( String name, Class<?> type )
         throws Exception
     {
-        // TODO Auto-generated method stub
-
+        enlistProperty( name, type, NODE );
     }
 
     @Override
     protected void enlistEdgesProperty( String name, Class<?> type )
         throws Exception
     {
-        // TODO Auto-generated method stub
+        enlistProperty( name, type, EDGE );
+    }
 
+    private void enlistProperty( String name, Class<?> type, String element )
+        throws Exception
+    {
+        AttributesImpl atts = new AttributesImpl();
+        atts.addAttribute( GRAPHML_XMLNS, ID, ID, CDATA_TYPE, name );
+        atts.addAttribute( GRAPHML_XMLNS, FOR, FOR, CDATA_TYPE, element );
+        atts.addAttribute( GRAPHML_XMLNS, ATTR_NAME, ATTR_NAME, CDATA_TYPE, name );
+        atts.addAttribute( GRAPHML_XMLNS, ATTR_TYPE, ATTR_TYPE, CDATA_TYPE, getStringType(
type ) );
+        transformerHandler.startElement( GRAPHML_XMLNS, KEY, KEY, atts );
+        transformerHandler.endElement( GRAPHML_XMLNS, KEY, KEY );
     }
 
     @Override
     protected void vertex( V vertex, Map<String, Object> properties )
         throws Exception
     {
-        // TODO Auto-generated method stub
+        AttributesImpl atts = new AttributesImpl();
+        atts.addAttribute( GRAPHML_XMLNS, ID, ID, CDATA_TYPE, String.valueOf( vertex.hashCode()
) );
+        transformerHandler.startElement( GRAPHML_XMLNS, NODE, NODE, atts );
+
+        // TODO print properties
 
+        transformerHandler.endElement( GRAPHML_XMLNS, NODE, NODE );
     }
 
     @Override
     protected void edge( E edge, V head, V tail, Map<String, Object> properties )
         throws Exception
     {
-        // TODO Auto-generated method stub
+        AttributesImpl atts = new AttributesImpl();
+        atts.addAttribute( GRAPHML_XMLNS, ID, ID, CDATA_TYPE, String.valueOf( edge.hashCode()
) );
+        atts.addAttribute( GRAPHML_XMLNS, SOURCE, SOURCE, CDATA_TYPE, String.valueOf( getGraph().getVertices(
edge ).getHead().hashCode() ) );
+        atts.addAttribute( GRAPHML_XMLNS, TARGET, TARGET, CDATA_TYPE, String.valueOf( getGraph().getVertices(
edge ).getTail().hashCode() ) );
+        transformerHandler.startElement( GRAPHML_XMLNS, EDGE, EDGE, atts );
+
+        // TODO print properties
 
+        transformerHandler.endElement( GRAPHML_XMLNS, NODE, NODE );
     }
 
-    private static <O> String getStringType( O object )
+    private static <T> String getStringType( Class<T> type )
     {
-        if ( object instanceof Integer )
+        if ( Integer.class == type )
         {
             return INT;
         }
-        else if ( object instanceof Long )
+        else if ( Long.class == type )
         {
             return LONG;
         }
-        else if ( object instanceof Float )
+        else if ( Float.class == type )
         {
             return FLOAT;
         }
-        else if ( object instanceof Double )
+        else if ( Double.class == type )
         {
             return DOUBLE;
         }
-        else if ( object instanceof Boolean )
+        else if ( Boolean.class == type )
         {
             return BOOLEAN;
         }
@@ -180,25 +219,25 @@ final class GraphMLExporter<V, E>
 
     public <N extends Number> GraphMLExporter<V, E> withEdgeWeights( Mapper<E,
N> edgeWeights )
     {
-        // TODO Auto-generated method stub
+        super.addEdgeProperty( WEIGHT, edgeWeights );
         return this;
     }
 
     public <N extends Number> GraphMLExporter<V, E> withVertexWeights( Mapper<V,
N> vertexWeights )
     {
-        // TODO Auto-generated method stub
+        super.addVertexProperty( WEIGHT, vertexWeights );
         return this;
     }
 
     public GraphMLExporter<V, E> withEdgeLabels( Mapper<E, String> edgeLabels
)
     {
-        // TODO Auto-generated method stub
+        super.addEdgeProperty( LABEL, edgeLabels );
         return this;
     }
 
     public GraphMLExporter<V, E> withVertexLabels( Mapper<V, String> vertexLabels
)
     {
-        // TODO Auto-generated method stub
+        super.addVertexProperty( LABEL, vertexLabels );
         return this;
     }
 



Mime
View raw message