Package edu.uci.ics.jung.io
Class GraphMLReader<G extends edu.uci.ics.jung.graph.Hypergraph<V,E>,V,E>
- java.lang.Object
-
- org.xml.sax.helpers.DefaultHandler
-
- edu.uci.ics.jung.io.GraphMLReader<G,V,E>
-
- All Implemented Interfaces:
org.xml.sax.ContentHandler
,org.xml.sax.DTDHandler
,org.xml.sax.EntityResolver
,org.xml.sax.ErrorHandler
public class GraphMLReader<G extends edu.uci.ics.jung.graph.Hypergraph<V,E>,V,E> extends org.xml.sax.helpers.DefaultHandler
Reads in data from a GraphML-formatted file and generates graphs based on that data. Currently supports the following parts of the GraphML specification:-
graphs and hypergraphs
directed and undirected edges
graph, vertex, edge
data
graph, vertex, edge descriptions anddata
descriptions vertex and edge IDsget
methods. Does not currently support nested graphs or ports.Note that the user is responsible for supplying a graph
Factory
that can support the edge types in the supplied GraphML file. If the graph generated by theFactory
is not compatible (for example: if the graph does not accept directed edges, and the GraphML file contains a directed edge) then the results are graph-implementation-dependent.- See Also:
- "http://graphml.graphdrawing.org/specification.html"
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
GraphMLReader.KeyType
protected static class
GraphMLReader.TagState
-
Field Summary
Fields Modifier and Type Field Description protected E
current_edge
protected G
current_graph
protected java.lang.String
current_key
protected java.util.LinkedList<GraphMLReader.TagState>
current_states
protected java.lang.StringBuilder
current_text
protected V
current_vertex
protected edu.uci.ics.jung.graph.util.EdgeType
default_edgetype
protected java.util.Map<E,java.lang.String>
edge_desc
protected org.apache.commons.collections4.Factory<E>
edge_factory
protected org.apache.commons.collections4.BidiMap<E,java.lang.String>
edge_ids
protected java.util.Map<java.lang.String,GraphMLMetadata<E>>
edge_metadata
protected java.util.Map<G,java.lang.String>
graph_desc
protected org.apache.commons.collections4.Factory<G>
graph_factory
protected java.util.Map<java.lang.String,GraphMLMetadata<G>>
graph_metadata
protected java.util.List<G>
graphs
protected java.util.Collection<V>
hyperedge_vertices
protected GraphMLReader.KeyType
key_type
protected javax.xml.parsers.SAXParser
saxp
protected org.apache.commons.collections4.BidiMap<java.lang.String,GraphMLReader.TagState>
tag_state
protected java.util.Map<V,java.lang.String>
vertex_desc
protected org.apache.commons.collections4.Factory<V>
vertex_factory
protected org.apache.commons.collections4.BidiMap<V,java.lang.String>
vertex_ids
protected java.util.Map<java.lang.String,GraphMLMetadata<V>>
vertex_metadata
-
Constructor Summary
Constructors Constructor Description GraphMLReader()
Creates aGraphMLReader
instance that assigns the vertex and edgeid
strings to be the vertex and edge objects, as well as their IDs.GraphMLReader(org.apache.commons.collections4.Factory<V> vertex_factory, org.apache.commons.collections4.Factory<E> edge_factory)
Creates aGraphMLReader
instance with the specified vertex and edge factories.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <T> void
addDatum(java.util.Map<java.lang.String,GraphMLMetadata<T>> metadata, T current_elt, java.lang.String text)
protected <T> void
addExtraData(java.util.Map<java.lang.String,java.lang.String> atts, java.util.Map<java.lang.String,GraphMLMetadata<T>> metadata_map, T current_elt)
protected void
assignEdgeSourceTarget(E e, org.xml.sax.Attributes atts, java.util.Map<java.lang.String,java.lang.String> edge_atts)
void
characters(char[] ch, int start, int length)
protected void
clearData()
protected void
createEdge(org.xml.sax.Attributes atts, GraphMLReader.TagState state)
protected void
createKey(org.xml.sax.Attributes atts)
protected void
createVertex(org.xml.sax.Attributes atts)
void
endElement(java.lang.String uri, java.lang.String name, java.lang.String qName)
protected java.util.Map<java.lang.String,java.lang.String>
getAttributeMap(org.xml.sax.Attributes atts)
java.util.Map<E,java.lang.String>
getEdgeDescriptions()
Returns a map from edges to edge descriptions.org.apache.commons.collections4.BidiMap<E,java.lang.String>
getEdgeIDs()
Returns a bidirectional map relating edges and IDs.java.util.Map<java.lang.String,GraphMLMetadata<E>>
getEdgeMetadata()
Returns a map from edge type name to type metadata.java.util.Map<G,java.lang.String>
getGraphDescriptions()
Returns a map from graphs to graph descriptions.java.util.Map<java.lang.String,GraphMLMetadata<G>>
getGraphMetadata()
Returns a map from graph type name to type metadata.java.util.Map<V,java.lang.String>
getVertexDescriptions()
Returns a map from vertices to vertex descriptions.org.apache.commons.collections4.BidiMap<V,java.lang.String>
getVertexIDs()
Returns a bidirectional map relating vertices and IDs.java.util.Map<java.lang.String,GraphMLMetadata<V>>
getVertexMetadata()
Returns a map from vertex type name to type metadata.protected void
handleData(org.xml.sax.Attributes atts)
protected void
initializeData()
This is separate from initialize() because these data structures are shared among all graphs loaded (i.e., they're defined insidegraphml
rather thangraph
.void
load(java.io.Reader reader, G g)
Populates the specified graph with the data parsed from the reader.void
load(java.lang.String filename, G g)
Populates the specified graph with the data parsed from the specified file.java.util.List<G>
loadMultiple(java.io.Reader reader, org.apache.commons.collections4.Factory<G> graph_factory)
Returns a list of the graphs parsed from the specified reader, as created by the specified factory.java.util.List<G>
loadMultiple(java.lang.String filename, org.apache.commons.collections4.Factory<G> graph_factory)
Returns a list of the graphs parsed from the specified file, as created by the specified factory.protected void
parse(java.io.Reader reader)
void
startElement(java.lang.String uri, java.lang.String name, java.lang.String qName, org.xml.sax.Attributes atts)
-
-
-
Field Detail
-
saxp
protected javax.xml.parsers.SAXParser saxp
-
default_edgetype
protected edu.uci.ics.jung.graph.util.EdgeType default_edgetype
-
current_vertex
protected V current_vertex
-
current_edge
protected E current_edge
-
current_key
protected java.lang.String current_key
-
current_states
protected java.util.LinkedList<GraphMLReader.TagState> current_states
-
tag_state
protected org.apache.commons.collections4.BidiMap<java.lang.String,GraphMLReader.TagState> tag_state
-
graph_factory
protected org.apache.commons.collections4.Factory<G extends edu.uci.ics.jung.graph.Hypergraph<V,E>> graph_factory
-
vertex_factory
protected org.apache.commons.collections4.Factory<V> vertex_factory
-
edge_factory
protected org.apache.commons.collections4.Factory<E> edge_factory
-
vertex_ids
protected org.apache.commons.collections4.BidiMap<V,java.lang.String> vertex_ids
-
edge_ids
protected org.apache.commons.collections4.BidiMap<E,java.lang.String> edge_ids
-
graph_metadata
protected java.util.Map<java.lang.String,GraphMLMetadata<G extends edu.uci.ics.jung.graph.Hypergraph<V,E>>> graph_metadata
-
vertex_metadata
protected java.util.Map<java.lang.String,GraphMLMetadata<V>> vertex_metadata
-
edge_metadata
protected java.util.Map<java.lang.String,GraphMLMetadata<E>> edge_metadata
-
vertex_desc
protected java.util.Map<V,java.lang.String> vertex_desc
-
edge_desc
protected java.util.Map<E,java.lang.String> edge_desc
-
graph_desc
protected java.util.Map<G extends edu.uci.ics.jung.graph.Hypergraph<V,E>,java.lang.String> graph_desc
-
key_type
protected GraphMLReader.KeyType key_type
-
hyperedge_vertices
protected java.util.Collection<V> hyperedge_vertices
-
current_text
protected java.lang.StringBuilder current_text
-
-
Constructor Detail
-
GraphMLReader
public GraphMLReader(org.apache.commons.collections4.Factory<V> vertex_factory, org.apache.commons.collections4.Factory<E> edge_factory) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException
Creates aGraphMLReader
instance with the specified vertex and edge factories.- Parameters:
vertex_factory
- the vertex factory to use to create vertex objectsedge_factory
- the edge factory to use to create edge objects- Throws:
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXException
-
GraphMLReader
public GraphMLReader() throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException
Creates aGraphMLReader
instance that assigns the vertex and edgeid
strings to be the vertex and edge objects, as well as their IDs. Note that this requires that (a) each edge have a valid ID, which is not normally a requirement for edges in GraphML, and (b) that the vertex and edge types be assignment-compatible withString
.- Throws:
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXException
-
-
Method Detail
-
loadMultiple
public java.util.List<G> loadMultiple(java.io.Reader reader, org.apache.commons.collections4.Factory<G> graph_factory) throws java.io.IOException
Returns a list of the graphs parsed from the specified reader, as created by the specified factory.- Throws:
java.io.IOException
-
loadMultiple
public java.util.List<G> loadMultiple(java.lang.String filename, org.apache.commons.collections4.Factory<G> graph_factory) throws java.io.IOException
Returns a list of the graphs parsed from the specified file, as created by the specified factory.- Throws:
java.io.IOException
-
load
public void load(java.io.Reader reader, G g) throws java.io.IOException
Populates the specified graph with the data parsed from the reader.- Throws:
java.io.IOException
-
load
public void load(java.lang.String filename, G g) throws java.io.IOException
Populates the specified graph with the data parsed from the specified file.- Throws:
java.io.IOException
-
clearData
protected void clearData()
-
initializeData
protected void initializeData()
This is separate from initialize() because these data structures are shared among all graphs loaded (i.e., they're defined insidegraphml
rather thangraph
.
-
parse
protected void parse(java.io.Reader reader) throws java.io.IOException
- Throws:
java.io.IOException
-
startElement
public void startElement(java.lang.String uri, java.lang.String name, java.lang.String qName, org.xml.sax.Attributes atts) throws org.xml.sax.SAXNotSupportedException
- Specified by:
startElement
in interfaceorg.xml.sax.ContentHandler
- Overrides:
startElement
in classorg.xml.sax.helpers.DefaultHandler
- Throws:
org.xml.sax.SAXNotSupportedException
-
addExtraData
protected <T> void addExtraData(java.util.Map<java.lang.String,java.lang.String> atts, java.util.Map<java.lang.String,GraphMLMetadata<T>> metadata_map, T current_elt)
- Type Parameters:
T
-- Parameters:
atts
-metadata_map
-current_elt
-
-
characters
public void characters(char[] ch, int start, int length) throws org.xml.sax.SAXNotSupportedException
- Specified by:
characters
in interfaceorg.xml.sax.ContentHandler
- Overrides:
characters
in classorg.xml.sax.helpers.DefaultHandler
- Throws:
org.xml.sax.SAXNotSupportedException
-
addDatum
protected <T> void addDatum(java.util.Map<java.lang.String,GraphMLMetadata<T>> metadata, T current_elt, java.lang.String text) throws org.xml.sax.SAXNotSupportedException
- Throws:
org.xml.sax.SAXNotSupportedException
-
endElement
public void endElement(java.lang.String uri, java.lang.String name, java.lang.String qName) throws org.xml.sax.SAXNotSupportedException
- Specified by:
endElement
in interfaceorg.xml.sax.ContentHandler
- Overrides:
endElement
in classorg.xml.sax.helpers.DefaultHandler
- Throws:
org.xml.sax.SAXNotSupportedException
-
getAttributeMap
protected java.util.Map<java.lang.String,java.lang.String> getAttributeMap(org.xml.sax.Attributes atts)
-
handleData
protected void handleData(org.xml.sax.Attributes atts) throws org.xml.sax.SAXNotSupportedException
- Throws:
org.xml.sax.SAXNotSupportedException
-
createKey
protected void createKey(org.xml.sax.Attributes atts) throws org.xml.sax.SAXNotSupportedException
- Throws:
org.xml.sax.SAXNotSupportedException
-
createVertex
protected void createVertex(org.xml.sax.Attributes atts) throws org.xml.sax.SAXNotSupportedException
- Throws:
org.xml.sax.SAXNotSupportedException
-
createEdge
protected void createEdge(org.xml.sax.Attributes atts, GraphMLReader.TagState state) throws org.xml.sax.SAXNotSupportedException
- Throws:
org.xml.sax.SAXNotSupportedException
-
assignEdgeSourceTarget
protected void assignEdgeSourceTarget(E e, org.xml.sax.Attributes atts, java.util.Map<java.lang.String,java.lang.String> edge_atts) throws org.xml.sax.SAXNotSupportedException
- Throws:
org.xml.sax.SAXNotSupportedException
-
getVertexIDs
public org.apache.commons.collections4.BidiMap<V,java.lang.String> getVertexIDs()
Returns a bidirectional map relating vertices and IDs.
-
getEdgeIDs
public org.apache.commons.collections4.BidiMap<E,java.lang.String> getEdgeIDs()
Returns a bidirectional map relating edges and IDs. This is not guaranteed to always be populated (edge IDs are not required in GraphML files.
-
getGraphMetadata
public java.util.Map<java.lang.String,GraphMLMetadata<G>> getGraphMetadata()
Returns a map from graph type name to type metadata.
-
getVertexMetadata
public java.util.Map<java.lang.String,GraphMLMetadata<V>> getVertexMetadata()
Returns a map from vertex type name to type metadata.
-
getEdgeMetadata
public java.util.Map<java.lang.String,GraphMLMetadata<E>> getEdgeMetadata()
Returns a map from edge type name to type metadata.
-
getGraphDescriptions
public java.util.Map<G,java.lang.String> getGraphDescriptions()
Returns a map from graphs to graph descriptions.
-
getVertexDescriptions
public java.util.Map<V,java.lang.String> getVertexDescriptions()
Returns a map from vertices to vertex descriptions.
-
getEdgeDescriptions
public java.util.Map<E,java.lang.String> getEdgeDescriptions()
Returns a map from edges to edge descriptions.
-
-