Skip to content
Permalink
Browse files
started restoring the GraphML exporter, but using the javax.xml.trans…
…form.sax.TransformerHandler rather than the javax.xml.stream.XMLStreamWriter, that is not available in java5 (which is commons-graph jvm target)
  • Loading branch information
simonetripodi committed Jul 29, 2012
1 parent 2a31091 commit 8d9813ab1bd293608837448bb4cbca68e551c67d
Showing 1 changed file with 63 additions and 24 deletions.
@@ -21,13 +21,21 @@

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 @@

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 @@
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 @@ else if ( object instanceof Boolean )

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;
}

0 comments on commit 8d9813a

Please sign in to comment.