Package edu.uci.ics.jung.io
Class GraphMLReader<G extends 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:
ContentHandler
,DTDHandler
,EntityResolver
,ErrorHandler
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 and
data
descriptions - vertex and edge IDs
get
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 the Factory
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static enum
protected static enum
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected E
protected G
protected String
protected LinkedList
<GraphMLReader.TagState> protected StringBuilder
protected V
protected EdgeType
protected com.google.common.base.Supplier
<E> protected Map
<String, GraphMLMetadata<E>> protected com.google.common.base.Supplier
<G> protected Map
<String, GraphMLMetadata<G>> protected Collection
<V> protected GraphMLReader.KeyType
protected SAXParser
protected com.google.common.collect.BiMap
<String, GraphMLReader.TagState> protected com.google.common.base.Supplier
<V> protected Map
<String, GraphMLMetadata<V>> -
Constructor Summary
ConstructorsConstructorDescriptionCreates aGraphMLReader
instance that assigns the vertex and edgeid
strings to be the vertex and edge objects, as well as their IDs.GraphMLReader
(com.google.common.base.Supplier<V> vertex_factory, com.google.common.base.Supplier<E> edge_factory) Creates aGraphMLReader
instance with the specified vertex and edge factories. -
Method Summary
Modifier and TypeMethodDescriptionprotected <T> void
addDatum
(Map<String, GraphMLMetadata<T>> metadata, T current_elt, String text) private <T> void
addExtraData
(Map<String, String> atts, Map<String, GraphMLMetadata<T>> metadata_map, T current_elt) protected void
assignEdgeSourceTarget
(E e, Attributes atts, Map<String, String> edge_atts) void
characters
(char[] ch, int start, int length) protected void
protected void
createEdge
(Attributes atts, GraphMLReader.TagState state) protected void
createKey
(Attributes atts) protected void
createVertex
(Attributes atts) void
endElement
(String uri, String name, String qName) getAttributeMap
(Attributes atts) Returns a bidirectional map relating edges and IDs.protected void
handleData
(Attributes atts) protected void
This is separate from initialize() because these data structures are shared among all graphs loaded (i.e., they're defined insidegraphml
rather thangraph
.void
Populates the specified graph with the data parsed from the reader.void
Populates the specified graph with the data parsed from the specified file.loadMultiple
(Reader reader, com.google.common.base.Supplier<G> graph_factory) Returns a list of the graphs parsed from the specified reader, as created by the specified Supplier.loadMultiple
(String filename, com.google.common.base.Supplier<G> graph_factory) Returns a list of the graphs parsed from the specified file, as created by the specified Supplier.protected void
void
startElement
(String uri, String name, String qName, Attributes atts) Methods inherited from class org.xml.sax.helpers.DefaultHandler
endDocument, endPrefixMapping, error, fatalError, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, setDocumentLocator, skippedEntity, startDocument, startPrefixMapping, unparsedEntityDecl, warning
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.xml.sax.ContentHandler
declaration
-
Field Details
-
saxp
-
default_edgetype
-
current_graph
-
current_vertex
-
current_edge
-
current_key
-
current_states
-
tag_state
-
graph_factory
-
vertex_factory
-
edge_factory
-
vertex_ids
-
edge_ids
-
graph_metadata
-
vertex_metadata
-
edge_metadata
-
vertex_desc
-
edge_desc
-
graph_desc
-
key_type
-
hyperedge_vertices
-
graphs
-
current_text
-
-
Constructor Details
-
GraphMLReader
public GraphMLReader(com.google.common.base.Supplier<V> vertex_factory, com.google.common.base.Supplier<E> edge_factory) throws ParserConfigurationException, SAXException Creates aGraphMLReader
instance with the specified vertex and edge factories.- Parameters:
vertex_factory
- the vertex supplier to use to create vertex objectsedge_factory
- the edge supplier to use to create edge objects- Throws:
ParserConfigurationException
- if a SAX parser cannot be constructedSAXException
- if the SAX parser factory cannot be constructed
-
GraphMLReader
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:
ParserConfigurationException
- if a SAX parser cannot be constructedSAXException
- if the SAX parser factory cannot be constructed
-
-
Method Details
-
loadMultiple
public List<G> loadMultiple(Reader reader, com.google.common.base.Supplier<G> graph_factory) throws IOException Returns a list of the graphs parsed from the specified reader, as created by the specified Supplier.- Parameters:
reader
- the source of the graph data in GraphML formatgraph_factory
- used to build graph instances- Returns:
- the graphs parsed from the specified reader
- Throws:
IOException
- if an error is encountered while parsing the graph
-
loadMultiple
public List<G> loadMultiple(String filename, com.google.common.base.Supplier<G> graph_factory) throws IOException Returns a list of the graphs parsed from the specified file, as created by the specified Supplier.- Parameters:
filename
- the source of the graph data in GraphML formatgraph_factory
- used to build graph instances- Returns:
- the graphs parsed from the specified file
- Throws:
IOException
- if an error is encountered while parsing the graph
-
load
Populates the specified graph with the data parsed from the reader.- Parameters:
reader
- the source of the graph data in GraphML formatg
- the graph instance to populate- Throws:
IOException
- if an error is encountered while parsing the graph
-
load
Populates the specified graph with the data parsed from the specified file.- Parameters:
filename
- the source of the graph data in GraphML formatg
- the graph instance to populate- Throws:
IOException
- if an error is encountered while parsing the graph
-
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
- Throws:
IOException
-
startElement
public void startElement(String uri, String name, String qName, Attributes atts) throws SAXNotSupportedException - Specified by:
startElement
in interfaceContentHandler
- Overrides:
startElement
in classDefaultHandler
- Throws:
SAXNotSupportedException
-
addExtraData
private <T> void addExtraData(Map<String, String> atts, Map<String, GraphMLMetadata<T>> metadata_map, T current_elt) - Type Parameters:
T
-- Parameters:
atts
-metadata_map
-current_elt
-
-
characters
- Specified by:
characters
in interfaceContentHandler
- Overrides:
characters
in classDefaultHandler
- Throws:
SAXNotSupportedException
-
addDatum
protected <T> void addDatum(Map<String, GraphMLMetadata<T>> metadata, T current_elt, String text) throws SAXNotSupportedException- Throws:
SAXNotSupportedException
-
endElement
- Specified by:
endElement
in interfaceContentHandler
- Overrides:
endElement
in classDefaultHandler
- Throws:
SAXNotSupportedException
-
getAttributeMap
-
handleData
- Throws:
SAXNotSupportedException
-
createKey
- Throws:
SAXNotSupportedException
-
createVertex
- Throws:
SAXNotSupportedException
-
createEdge
protected void createEdge(Attributes atts, GraphMLReader.TagState state) throws SAXNotSupportedException - Throws:
SAXNotSupportedException
-
assignEdgeSourceTarget
protected void assignEdgeSourceTarget(E e, Attributes atts, Map<String, String> edge_atts) throws SAXNotSupportedException- Throws:
SAXNotSupportedException
-
getVertexIDs
- Returns:
- a bidirectional map relating vertices and IDs.
-
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.- Returns:
- a bidirectional map relating edges and IDs.
-
getGraphMetadata
- Returns:
- a map from graph type name to type metadata
-
getVertexMetadata
- Returns:
- a map from vertex type name to type metadata
-
getEdgeMetadata
- Returns:
- a map from edge type name to type metadata
-
getGraphDescriptions
- Returns:
- a map from graphs to graph descriptions
-
getVertexDescriptions
- Returns:
- a map from vertices to vertex descriptions
-
getEdgeDescriptions
- Returns:
- a map from edges to edge descriptions
-