📄 graphtoxgmmlsaxhandler.java
字号:
package salvo.jesus.graph.xml;import salvo.jesus.graph.*;import salvo.jesus.graph.visual.*;import java.io.*;import org.xml.sax.*;import org.xml.sax.helpers.*;import org.apache.xml.serialize.*;import org.apache.xerces.parsers.*;import org.apache.xerces.utils.*;import org.apache.xerces.framework.*;import java.util.*;/** * An implmentation of GraphToXMLHandler that serializes a Graph and/or * VisualGraph to XGMML via SAX. * <p> * This class simply delegates handling of events to either the inner class * <tt>GraphToXGMMLSAXHandler.GraphHandler</tt> or * <tt>GraphToXGMMLSAXHandler.VisualGraphHandler</tt>, depending on which * method between <tt>startSerialize()</tt> is called. Users should simply create * an instance of this class and should not be concerned of the inner classes, * and call the appropriate overloaded <tt>startSerialize()</tt> method. * <p> * * See <a href="http://www.cs.rpi.edu/~puninj/XGMML/draft-xgmml-20001006.html"> * XGMML 1.0 Draft Specification</a> * * @author Jesus M. Salvo Jr. */public class GraphToXGMMLSAXHandler implements GraphToXMLHandler { private GraphToXMLEventGenerator eventGenerator; protected XMLSerializer serializer; /** * Delegate that performs the actual serialization */ private GraphToXGMMLSAXHandler delegate; public GraphToXGMMLSAXHandler( GraphToXMLEventGenerator eventGenerator, XMLSerializer serializer ) { this.eventGenerator = eventGenerator; this.serializer = serializer; } /** * Initializes the delegate to be the inner class <tt>GraphHandler</tt>, * and calls the <tt>startSerialize()</tt> method of the delegate. */ public void startSerialize( Graph graph ) throws Exception { this.delegate = new GraphHandler( this.eventGenerator, this.serializer, new CommonHandler( this.eventGenerator, this.serializer ) ); this.delegate.startSerialize( graph ); } /** * Initializes the delegate to be the inner class <tt>VisualGraphHandler</tt>, * and calls the <tt>startSerialize()</tt> method of the delegate. */ public void startSerialize( VisualGraph vGraph ) throws Exception { this.delegate = new VisualGraphHandler( this.eventGenerator, this.serializer, new CommonHandler( this.eventGenerator, this.serializer ) ); this.delegate.startSerialize( vGraph ); } /** * Calls the delegate's <tt>serializerEdge()</tt> method */ public void serializeEdge( Edge edge ) throws Exception { this.delegate.serializeEdge( edge ); } /** * Calls the delegate's <tt>endSerializerEdge()</tt> method */ public void endSerializeEdge( Edge edge ) throws Exception { this.delegate.endSerializeEdge( edge ); } /** * Calls the delegate's <tt>serializerVertex()</tt> method */ public void serializeVertex( Vertex vertex ) throws Exception { this.delegate.serializeVertex( vertex ); } /** * Calls the delegate's <tt>endSerializerVertex()</tt> method */ public void endSerializeVertex( Vertex vertex ) throws Exception { this.delegate.endSerializeVertex( vertex ); } /** * Calls the delegate's <tt>serializerVertex()</tt> method */ public void endSerialize() throws Exception { this.delegate.endSerialize(); } public GraphToXMLEventGenerator getEventGenerator() { return this.eventGenerator; } /** * Methods and instance variables common to either of the two inner class delegates * <tt>GraphHandler</tt> and <tt>VisualGraphHandler</tt>. * * @author Jesus M. Salvo Jr. */ class CommonHandler extends GraphToXGMMLSAXHandler { /** * Index of the DTD's name in the pool of Strings */ protected int DOCTYPE_NAME_STRINGPOOL_INDEX; /** * Index of the Public ID in the pool of Strings */ protected int PUBLIC_ID_STRINGPOOL_INDEX; /** * Index of the System ID in the pool of Strings */ protected int SYSTEM_ID_STRINGPOOL_INDEX; /** * Index of the "OpenJGraph" literal in the pool of Strings */ protected int OPENJGRAPH_LITERAL_STRINGPOOL_INDEX; /** * Index of the "graph" literal in the pool of Strings */ protected int GRAPH_ELEMENT_LITERAL_STRINGPOOL_INDEX; /** * Index of the "Vendor" literal in the pool of Strings */ protected int VENDOR_ATTRIBUTE_LITERAL_STRINGPOOL_INDEX; /** * Index of the "directed" literal in the pool of Strings */ protected int DIRECTED_ATTRIBUTE_LITERAL_STRINGPOOL_INDEX; /** * Index of the "Graphic" literal in the pool of Strings */ protected int GRAPHIC_ATTRIBUTE_LITERAL_STRINGPOOL_INDEX; /** * Index of the "att" literal in the pool of Strings */ protected int ATT_ELEMENT_LITERAL_STRINGPOOL_INDEX; /** * Index of the "weighted" literal in the pool of Strings */ protected int ATT_ELEMENT_NAME_ATTRIBUTE_VALUE_WEIGHTED_STRINGPOOL_INDEX; /** * Index of the "dag" literal in the pool of Strings */ protected int ATT_ELEMENT_NAME_ATTRIBUTE_VALUE_DAG_STRINGPOOL_INDEX; /** * Index of the "type" literal in the pool of Strings */ protected int TYPE_ATTRIBUTE_LITERAL_STRINGPOOL_INDEX; /** * Index of the "name" literal in the pool of Strings */ protected int NAME_ATTRIBUTE_LITERAL_STRINGPOOL_INDEX; /** * Index of the "value" literal in the pool of Strings */ protected int VALUE_ATTRIBUTE_LITERAL_STRINGPOOL_INDEX; /** * Index of the "node" literal in the pool of Strings */ protected int NODE_ELEMENT_LITERAL_STRINGPOOL_INDEX; /** * Index of the "edge" literal in the pool of Strings */ protected int EDGE_ELEMENT_LITERAL_STRINGPOOL_INDEX; /** * Index of the "source" literal in the pool of Strings */ protected int SOURCE_ATTRIBUTE_LITERAL_STRINGPOOL_INDEX; /** * Index of the "target" literal in the pool of Strings */ protected int TARGET_ATTRIBUTE_LITERAL_STRINGPOOL_INDEX; /** * Index of the "weight" literal in the pool of Strings */ protected int WEIGHT_ATTRIBUTE_LITERAL_STRINGPOOL_INDEX; /** * Index of the "id" literal in the pool of Strings */ protected int ID_ATTRIBUTE_LITERAL_STRINGPOOL_INDEX; /** * Index of the "label" literal in the pool of Strings */ protected int LABEL_ATTRIBUTE_LITERAL_STRINGPOOL_INDEX; /** * Index of the "string" literal in the pool of Strings */ protected int STRING_TYPE_STRINGPOOL_INDEX; /** * Index of the "integer" literal in the pool of Strings */ protected int INTEGER_TYPE_STRINGPOOL_INDEX; /** * Index of the "double" literal in the pool of Strings */ protected int DOUBLE_TYPE_STRINGPOOL_INDEX; /** * Index of the "boolean" literal in the pool of Strings */ protected int BOOLEAN_TYPE_STRINGPOOL_INDEX; /** * Index of the "1" literal in the pool of Strings */ protected int ZERO_LITERAL_STRINGPOOL_INDEX; /** * Index of the "0" literal in the pool of Strings */ protected int ONE_LITERAL_STRINGPOOL_INDEX; private XMLReaderAdapter readerAdapter; private Graph graph; private SAXParser parser; // Wrap the parser with a XMLReaderAdapter so that we can call // startElement() and endElement() methods. private OutputFormat format; private TreeMap vertexIDMap; private long idAttribute; /** * A pool of Strings that we will use often throughout the serialization. * These Strings include the element's name, attributes names, and possibly * values as well. */ private StringPool sPool; public CommonHandler( GraphToXMLEventGenerator eventGenerator, XMLSerializer serializer ) { super( eventGenerator, serializer ); this.sPool = this.initStringPool(); } /** * Initialized the SAXParser, XMLReaderAdapter and OutputFormat. * Serializes the DTD declaration. */ public void startSerialize( Graph graph ) throws Exception { this.graph = graph; // Create a SAX2 parser: org.apache.xerces.parsers.SAXParser this.parser = new SAXParser(); // Wrap the parser with a XMLReaderAdapter so that we can call // startElement() and endElement() methods. this.readerAdapter = new XMLReaderAdapter( parser ); this.format = new OutputFormat(); // Reference to the mappings between a vertex and its ID attribute this.vertexIDMap = new TreeMap( new VertexComparator() ); // The id attribute that we assign to all elements this.idAttribute = 1; // Initialise the OutputFormat this.format.setOmitXMLDeclaration( false ); this.format.setEncoding( OutputFormat.Defaults.Encoding ); this.format.setIndenting( true ); this.format.setDoctype( XGMML.PUBLIC_ID, XGMML.SYSTEM_ID ); // Tell the serializer the OutputFormat to use this.serializer.setOutputFormat( format ); // Tell the parser the ContentHander to use this.parser.setContentHandler( this.serializer ); this.parser.startDocument(); // Serialize the DTD declaration this.parser.startDTD( new QName( -1, this.DOCTYPE_NAME_STRINGPOOL_INDEX, this.DOCTYPE_NAME_STRINGPOOL_INDEX ), this.PUBLIC_ID_STRINGPOOL_INDEX, this.SYSTEM_ID_STRINGPOOL_INDEX ); this.parser.endDTD(); // Tell the XMLReaderAdapter the DocumentHandler to use // Even though we have told the parser the ContentHandler to use, // remember that the XMLReadeAdapter is a wrapper around the SAXParser. // Therefore we need to indepedently tell the this adapter to DocumentHandler this.readerAdapter.setDocumentHandler( this.serializer ); } /** * Empty method implementation */ public void startSerialize( VisualGraph vGraph ) throws Exception {} /** * Empty method implementation */ public void serializeVertex( Vertex vertex ) throws Exception {} /** * Serializes the end of the Vertex's element and maps * the Vertex to the unique ID attribute. */ public void endSerializeVertex( Vertex vertex ) throws Exception { this.readerAdapter.endElement( "", XGMML.NODE_ELEMENT_LITERAL, XGMML.NODE_ELEMENT_LITERAL ); // Store the mapping between the vertex and the ID attribute // This is later used when writing out the edges this.vertexIDMap.put( vertex, new Long( idAttribute )); this.incremenetIDAttribute(); } /** * Empty method implementation */ public void serializeEdge( Edge edge ) throws Exception {} /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -