⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 graphtoxgmmlsaxhandler.java

📁 OpenJGraph是一个开源的Java库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
         * Serializes the end of the Edge's element.         */        public void endSerializeEdge( Edge edge ) throws Exception {            this.readerAdapter.endElement( "",                XGMML.EDGE_ELEMENT_LITERAL, XGMML.EDGE_ELEMENT_LITERAL );        }        /**         * Serializes the end of the Graph's element.         */        public void endSerialize() throws Exception {            this.readerAdapter.endElement( "",                XGMML.GRAPH_ELEMENT_LITERAL, XGMML.GRAPH_ELEMENT_LITERAL );            this.parser.endDocument();        }        protected XMLReaderAdapter getReaderAdapter() {            return this.readerAdapter;        }        protected TreeMap getVertexIDMap() {            return this.vertexIDMap;        }        protected long getCurrentIDAttribute() {            return this.idAttribute;        }        protected void incremenetIDAttribute() {            this.idAttribute++;        }        /**         * Initialise the pool of Strings that we shall be using         */        private StringPool initStringPool() {            StringPool  sPool = new StringPool();            this.DOCTYPE_NAME_STRINGPOOL_INDEX = sPool.addString( XGMML.DOCTYPE_NAME );            this.PUBLIC_ID_STRINGPOOL_INDEX = sPool.addString( XGMML.PUBLIC_ID );            this.SYSTEM_ID_STRINGPOOL_INDEX = sPool.addString( XGMML.SYSTEM_ID );            this.GRAPH_ELEMENT_LITERAL_STRINGPOOL_INDEX = sPool.addString( XGMML.GRAPH_ELEMENT_LITERAL );            this.VENDOR_ATTRIBUTE_LITERAL_STRINGPOOL_INDEX = sPool.addString( XGMML.VENDOR_ATTRIBUTE_LITERAL );            this.OPENJGRAPH_LITERAL_STRINGPOOL_INDEX = sPool.addString( "OpenJGraph" );            this.GRAPHIC_ATTRIBUTE_LITERAL_STRINGPOOL_INDEX = sPool.addString( XGMML.GRAPHIC_ATTRIBUTE_LITERAL );            this.DIRECTED_ATTRIBUTE_LITERAL_STRINGPOOL_INDEX = sPool.addString( XGMML.DIRECTED_ATTRIBUTE_LITERAL );            this.STRING_TYPE_STRINGPOOL_INDEX = sPool.addString( "string" );            this.INTEGER_TYPE_STRINGPOOL_INDEX = sPool.addString( "integer" );            this.DOUBLE_TYPE_STRINGPOOL_INDEX = sPool.addString( "double" );            this.BOOLEAN_TYPE_STRINGPOOL_INDEX = sPool.addString( "boolean" );            this.ZERO_LITERAL_STRINGPOOL_INDEX = sPool.addString( "0" );            this.ONE_LITERAL_STRINGPOOL_INDEX = sPool.addString( "1" );            return sPool;        }        /**         * Serialise other meta information about the graph into an <tt>att</tt> element         * under the <tt>graph</tt> element.         */        protected void serializeOtherGraphMetaInformation()                throws Exception        {            AttributesImpl attrs = new AttributesImpl();            if( graph instanceof DirectedAcyclicGraph ) {                // Add the name attribute whose value is "dag"                attrs.addAttribute( "",                    XGMML.NAME_ATTRIBUTE_LITERAL, XGMML.NAME_ATTRIBUTE_LITERAL,                    this.sPool.toString( this.STRING_TYPE_STRINGPOOL_INDEX ),                    XGMML.ATT_ELEMENT_NAME_ATTRIBUTE_VALUE_DAG );                attrs.addAttribute( "",                    XGMML.TYPE_ATTRIBUTE_LITERAL, XGMML.TYPE_ATTRIBUTE_LITERAL,                    this.sPool.toString( this.STRING_TYPE_STRINGPOOL_INDEX ),                    "integer" );                attrs.addAttribute( "",                    XGMML.VALUE_ATTRIBUTE_LITERAL, XGMML.VALUE_ATTRIBUTE_LITERAL,                    this.sPool.toString( this.STRING_TYPE_STRINGPOOL_INDEX ),                    this.sPool.toString( this.ONE_LITERAL_STRINGPOOL_INDEX ));                readerAdapter.startElement( "",                    XGMML.ATT_ELEMENT_LITERAL, XGMML.ATT_ELEMENT_LITERAL, attrs );                readerAdapter.endElement( "",                    XGMML.ATT_ELEMENT_LITERAL, XGMML.ATT_ELEMENT_LITERAL );            }            else if( graph instanceof WeightedGraph ) {                // Add the name attribute whose value is "weighted"                attrs.addAttribute( "",                    XGMML.NAME_ATTRIBUTE_LITERAL, XGMML.NAME_ATTRIBUTE_LITERAL,                    this.sPool.toString( this.STRING_TYPE_STRINGPOOL_INDEX ),                    XGMML.ATT_ELEMENT_NAME_ATTRIBUTE_VALUE_WEIGHTED );                attrs.addAttribute( "",                    XGMML.TYPE_ATTRIBUTE_LITERAL, XGMML.TYPE_ATTRIBUTE_LITERAL,                    this.sPool.toString( this.STRING_TYPE_STRINGPOOL_INDEX ),                    "integer" );                attrs.addAttribute( "",                    XGMML.VALUE_ATTRIBUTE_LITERAL, XGMML.VALUE_ATTRIBUTE_LITERAL,                    this.sPool.toString( this.STRING_TYPE_STRINGPOOL_INDEX ),                    this.sPool.toString( this.ONE_LITERAL_STRINGPOOL_INDEX ));                readerAdapter.startElement( "",                    XGMML.ATT_ELEMENT_LITERAL, XGMML.ATT_ELEMENT_LITERAL, attrs );                readerAdapter.endElement( "",                    XGMML.ATT_ELEMENT_LITERAL, XGMML.ATT_ELEMENT_LITERAL );            }            // Add the graphFactory            attrs = new AttributesImpl();            attrs.addAttribute( "",                XGMML.NAME_ATTRIBUTE_LITERAL, XGMML.NAME_ATTRIBUTE_LITERAL,                this.sPool.toString( this.STRING_TYPE_STRINGPOOL_INDEX ),                XGMML.ATT_ELEMENT_NAME_ATTRIBUTE_VALUE_GRAPHFACTORY );            attrs.addAttribute( "",                XGMML.TYPE_ATTRIBUTE_LITERAL, XGMML.TYPE_ATTRIBUTE_LITERAL,                this.sPool.toString( this.STRING_TYPE_STRINGPOOL_INDEX ),                "string" );            attrs.addAttribute( "",                XGMML.VALUE_ATTRIBUTE_LITERAL, XGMML.VALUE_ATTRIBUTE_LITERAL,                this.sPool.toString( this.STRING_TYPE_STRINGPOOL_INDEX ),                graph.getGraphFactory().getClass().getName() );            readerAdapter.startElement( "",                XGMML.ATT_ELEMENT_LITERAL, XGMML.ATT_ELEMENT_LITERAL, attrs );            readerAdapter.endElement( "",                XGMML.ATT_ELEMENT_LITERAL, XGMML.ATT_ELEMENT_LITERAL );            // Add the traversal            attrs = new AttributesImpl();            attrs.addAttribute( "",                XGMML.NAME_ATTRIBUTE_LITERAL, XGMML.NAME_ATTRIBUTE_LITERAL,                this.sPool.toString( this.STRING_TYPE_STRINGPOOL_INDEX ),                XGMML.ATT_ELEMENT_NAME_ATTRIBUTE_VALUE_TRAVERSAL );            attrs.addAttribute( "",                XGMML.TYPE_ATTRIBUTE_LITERAL, XGMML.TYPE_ATTRIBUTE_LITERAL,                this.sPool.toString( this.STRING_TYPE_STRINGPOOL_INDEX ),                "string" );            attrs.addAttribute( "",                XGMML.VALUE_ATTRIBUTE_LITERAL, XGMML.VALUE_ATTRIBUTE_LITERAL,                this.sPool.toString( this.STRING_TYPE_STRINGPOOL_INDEX ),                graph.getTraversal().getClass().getName() );            readerAdapter.startElement( "",                XGMML.ATT_ELEMENT_LITERAL, XGMML.ATT_ELEMENT_LITERAL, attrs );            readerAdapter.endElement( "",                XGMML.ATT_ELEMENT_LITERAL, XGMML.ATT_ELEMENT_LITERAL );        }    }    /**     * Inner class delegate of <tt>GraphToXGMMLSAXHandler</tt> for     * serializing <tt>Graph</tt> objects and not <tt>VisualGraph</tt> objects.     *     * @author  Jesus M. Salvo Jr.     */    class GraphHandler extends GraphToXGMMLSAXHandler {        private CommonHandler   commonHandler;        public GraphHandler( GraphToXMLEventGenerator eventGenerator,                XMLSerializer serializer, CommonHandler commonHandler ) {            super( eventGenerator, serializer );            this.commonHandler = commonHandler;        }        /**         * Serializes the <tt>&lt;graph&gt;</tt> element, including setting the         * <tt>Graphic</tt> attribute to <tt>0</tt>.         */        public void startSerialize( Graph graph ) throws Exception {            this.commonHandler.startSerialize( graph );            // Serialize the beginning tag of the graph element            AttributesImpl attrs = new AttributesImpl();            // Add the Graphic attribute            attrs.addAttribute( "",                XGMML.GRAPHIC_ATTRIBUTE_LITERAL, XGMML.GRAPHIC_ATTRIBUTE_LITERAL,                this.commonHandler.sPool.toString( this.commonHandler.BOOLEAN_TYPE_STRINGPOOL_INDEX ),                this.commonHandler.sPool.toString( this.commonHandler.ZERO_LITERAL_STRINGPOOL_INDEX ) );            // Add the Vendor attribute            attrs.addAttribute( "",                XGMML.VENDOR_ATTRIBUTE_LITERAL, XGMML.VENDOR_ATTRIBUTE_LITERAL,                this.commonHandler.sPool.toString( this.commonHandler.STRING_TYPE_STRINGPOOL_INDEX ),                this.commonHandler.sPool.toString( this.commonHandler.OPENJGRAPH_LITERAL_STRINGPOOL_INDEX ));            // Add the directed attribute            attrs.addAttribute( "",                XGMML.DIRECTED_ATTRIBUTE_LITERAL, XGMML.DIRECTED_ATTRIBUTE_LITERAL,                this.commonHandler.sPool.toString( this.commonHandler.BOOLEAN_TYPE_STRINGPOOL_INDEX ),                graph instanceof DirectedGraph ?                    this.commonHandler.sPool.toString( this.commonHandler.ONE_LITERAL_STRINGPOOL_INDEX ) :                    this.commonHandler.sPool.toString( this.commonHandler.ZERO_LITERAL_STRINGPOOL_INDEX ));            this.commonHandler.getReaderAdapter().startElement( "",                XGMML.GRAPH_ELEMENT_LITERAL, XGMML.GRAPH_ELEMENT_LITERAL, attrs );            // Serialize the att element for other meta information about the graph            this.commonHandler.serializeOtherGraphMetaInformation();        }        /**         * Empty method implementation         */        public void startSerialize( VisualGraph vGraph ) throws Exception {}        /**         * Serializes the <tt>&lt;node&gt;</tt> element         */        public void serializeVertex( Vertex vertex ) throws Exception {            XMLReaderAdapter    readerAdapter = this.commonHandler.getReaderAdapter();            TreeMap             vertexIDMap = this.commonHandler.getVertexIDMap();            long                idAttribute = this.commonHandler.getCurrentIDAttribute();            AttributesImpl      attrs = new AttributesImpl();            // Add the id attribute            attrs.addAttribute( "",                XGMML.ID_ATTRIBUTE_LITERAL, XGMML.ID_ATTRIBUTE_LITERAL,                this.commonHandler.sPool.toString( this.commonHandler.STRING_TYPE_STRINGPOOL_INDEX ),                Long.toString( idAttribute ) );            // Add the label attribute            attrs.addAttribute( "",                XGMML.LABEL_ATTRIBUTE_LITERAL, XGMML.LABEL_ATTRIBUTE_LITERAL,                this.commonHandler.sPool.toString( this.commonHandler.STRING_TYPE_STRINGPOOL_INDEX ),                vertex.toString() );            readerAdapter.startElement( "", XGMML.NODE_ELEMENT_LITERAL, XGMML.NODE_ELEMENT_LITERAL, attrs);            attrs.clear();            // Add the name attribute            attrs.addAttribute( "",                XGMML.NAME_ATTRIBUTE_LITERAL, XGMML.NAME_ATTRIBUTE_LITERAL,                this.commonHandler.sPool.toString( this.commonHandler.STRING_TYPE_STRINGPOOL_INDEX ),                "className" );            // Add the type attribute            attrs.addAttribute( "",                XGMML.TYPE_ATTRIBUTE_LITERAL, XGMML.TYPE_ATTRIBUTE_LITERAL,                this.commonHandler.sPool.toString( this.commonHandler.STRING_TYPE_STRINGPOOL_INDEX ),                "string" );            // Add the value attribute            attrs.addAttribute( "",                XGMML.VALUE_ATTRIBUTE_LITERAL, XGMML.VALUE_ATTRIBUTE_LITERAL,                this.commonHandler.sPool.toString( this.commonHandler.STRING_TYPE_STRINGPOOL_INDEX ),                vertex.getClass().getName() );            readerAdapter.startElement( "", XGMML.ATT_ELEMENT_LITERAL, XGMML.ATT_ELEMENT_LITERAL, attrs);            readerAdapter.endElement( "", XGMML.ATT_ELEMENT_LITERAL, XGMML.ATT_ELEMENT_LITERAL );        }        /**         * Calls <tt>CommonHandler.endSerializeVertex()</tt>         */        public void endSerializeVertex( Vertex vertex ) throws Exception {            this.commonHandler.endSerializeVertex( vertex );        }        /**         * Serializes the <tt>&lt;edge&gt;</tt> element         */        public void serializeEdge( Edge edge ) throws Exception {            XMLReaderAdapter    readerAdapter = this.commonHandler.getReaderAdapter();            TreeMap         vertexIDMap = this.commonHandler.getVertexIDMap();            AttributesImpl  attrs = new AttributesImpl();            // Add the label attribute            attrs.addAttribute( "",                XGMML.LABEL_ATTRIBUTE_LITERAL, XGMML.LABEL_ATTRIBUTE_LITERAL,                this.commonHandler.sPool.toString( this.commonHandler.STRING_TYPE_STRINGPOOL_INDEX ),                edge.toString() );            if( edge instanceof DirectedEdgeImpl ) {                DirectedEdge    dEdge = (DirectedEdge) edge;                // Get the source and target attribute values via                // getSource() and getSink() methods                attrs.addAttribute( "",                    XGMML.SOURCE_ATTRIBUTE_LITERAL, XGMML.SOURCE_ATTRIBUTE_LITERAL,                    this.commonHandler.sPool.toString( this.commonHandler.INTEGER_TYPE_STRINGPOOL_INDEX ),                    (( Long ) vertexIDMap.get( dEdge.getSource() )).toString() );                attrs.addAttribute( "",                    XGMML.TARGET_ATTRIBUTE_LITERAL, XGMML.TARGET_ATTRIBUTE_LITERAL,                    this.commonHandler.sPool.toString( this.commonHandler.INTEGER_TYPE_STRINGPOOL_INDEX ),                    (( Long ) vertexIDMap.get( dEdge.getSink() )).toString() );            }            else {                // Get the source and target attribute values via                // getVertexA() and getVertexB() methods                attrs.addAttribute( "",                    XGMML.SOURCE_ATTRIBUTE_LITERAL, XGMML.SOURCE_ATTRIBUTE_LITERAL,                    this.commonHandler.sPool.toString( this.commonHandler.INTEGER_TYPE_STRINGPOOL_INDEX ),                    (( Long ) vertexIDMap.get( edge.getVertexA() )).toString() );                attrs.addAttribute( "",                    XGMML.TARGET_ATTRIBUTE_LITERAL, XGMML.TARGET_ATTRIBUTE_LITERAL,                    this.commonHandler.sPool.toString( this.commonHandler.INTEGER_TYPE_STRINGPOOL_INDEX ),                    (( Long ) vertexIDMap.get( edge.getVertexB() )).toString() );            }            if( edge instanceof WeightedEdgeImpl ) {                // Add the weight attribute                attrs.addAttribute( "",                    XGMML.WEIGHT_ATTRIBUTE_LITERAL, XGMML.WEIGHT_ATTRIBUTE_LITERAL,                    this.commonHandler.sPool.toString( this.commonHandler.DOUBLE_TYPE_STRINGPOOL_INDEX ),                    Double.toString( ((WeightedEdge) edge).getWeight()) );            }            readerAdapter.startElement( "",                XGMML.EDGE_ELEMENT_LITERAL, XGMML.EDGE_ELEMENT_LITERAL, attrs );            attrs.clear();            // Add the name attribute            attrs.addAttribute( "",                XGMML.NAME_ATTRIBUTE_LITERAL, XGMML.NAME_ATTRIBUTE_LITERAL,                this.commonHandler.sPool.toString( this.commonHandler.STRING_TYPE_STRINGPOOL_INDEX ),                "className" );            // Add the type attribute            attrs.addAttribute( "",                XGMML.TYPE_ATTRIBUTE_LITERAL, XGMML.TYPE_ATTRIBUTE_LITERAL,                this.commonHandler.sPool.toString( this.commonHandler.STRING_TYPE_STRINGPOOL_INDEX ),                "string" );            // Add the value attribute            attrs.addAttribute( "",                XGMML.VALUE_ATTRIBUTE_LITERAL, XGMML.VALUE_ATTRIBUTE_LITERAL,                this.commonHandler.sPool.toString( this.commonHandler.STRING_TYPE_STRINGPOOL_INDEX ),                edge.getClass().getName() );            readerAdapter.startElement( "", XGMML.ATT_ELEMENT_LITERAL, XGMML.ATT_ELEMENT_LITERAL, attrs);            readerAdapter.endElement( "", XGMML.ATT_ELEMENT_LITERAL, XGMML.ATT_ELEMENT_LITERAL );        }        /**         * Calls <tt>CommonHandler.endSerializeEdge()</tt>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -