Line | Hits | Source |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2003, the JUNG Project and the Regents of the University | |
3 | * of California | |
4 | * All rights reserved. | |
5 | * | |
6 | * This software is open-source under the BSD license; see either | |
7 | * "license.txt" or | |
8 | * http://jung.sourceforge.net/license.txt for a description. | |
9 | */ | |
10 | package edu.uci.ics.jung.io; | |
11 | ||
12 | import java.util.HashMap; | |
13 | import java.util.Iterator; | |
14 | import java.util.Map; | |
15 | ||
16 | import org.xml.sax.Attributes; | |
17 | import org.xml.sax.SAXException; | |
18 | import org.xml.sax.helpers.DefaultHandler; | |
19 | ||
20 | import edu.uci.ics.jung.exceptions.FatalException; | |
21 | import edu.uci.ics.jung.graph.ArchetypeVertex; | |
22 | import edu.uci.ics.jung.graph.Edge; | |
23 | import edu.uci.ics.jung.graph.Graph; | |
24 | import edu.uci.ics.jung.graph.Vertex; | |
25 | import edu.uci.ics.jung.graph.decorators.StringLabeller; | |
26 | import edu.uci.ics.jung.graph.impl.DirectedSparseEdge; | |
27 | import edu.uci.ics.jung.graph.impl.SparseGraph; | |
28 | import edu.uci.ics.jung.graph.impl.SparseVertex; | |
29 | import edu.uci.ics.jung.graph.impl.UndirectedSparseEdge; | |
30 | import edu.uci.ics.jung.utils.UserData; | |
31 | ||
32 | /** | |
33 | * The default GraphML file handler to use to parse the xml file | |
34 | * @author Scott White | |
35 | */ | |
36 | public class GraphMLFileHandler extends DefaultHandler { | |
37 | private Graph mGraph; | |
38 | private StringLabeller mLabeller; | |
39 | private boolean default_directed; | |
40 | ||
41 | /** | |
42 | * The default constructor | |
43 | */ | |
44 | 18 | public GraphMLFileHandler() { |
45 | 18 | } |
46 | ||
47 | protected Graph getGraph() { | |
48 | 18 | return mGraph; |
49 | } | |
50 | ||
51 | protected StringLabeller getLabeller() { | |
52 | 0 | return mLabeller; |
53 | } | |
54 | ||
55 | private Map getAttributeMap(Attributes attrs) { | |
56 | 232 | Map map = new HashMap(); |
57 | 232 | if (attrs != null) { |
58 | 634 | for (int i = 0; i < attrs.getLength(); i++) { |
59 | 402 | map.put(attrs.getQName(i), attrs.getValue(i)); |
60 | } | |
61 | } | |
62 | 232 | return map; |
63 | } | |
64 | ||
65 | protected Edge createEdge(Map attributeMap) { | |
66 | 119 | if (mGraph == null) { |
67 | 0 | throw new FatalException("Error parsing graph. Graph element must be specified before edge element."); |
68 | } | |
69 | ||
70 | 119 | String sourceId = (String) attributeMap.remove("source"); |
71 | 119 | Vertex sourceVertex = |
72 | mLabeller.getVertex(sourceId); | |
73 | ||
74 | 119 | String targetId = (String) attributeMap.remove("target"); |
75 | 119 | Vertex targetVertex = |
76 | mLabeller.getVertex(targetId); | |
77 | ||
78 | 119 | String direction = (String) attributeMap.remove("directed"); |
79 | boolean directed; | |
80 | 119 | if (direction == null) |
81 | { | |
82 | // use default_directed | |
83 | 116 | directed = default_directed; |
84 | } | |
85 | else | |
86 | { | |
87 | // use specified direction | |
88 | 3 | if (direction.equals("true")) |
89 | 3 | directed = true; |
90 | 0 | else if (direction.equals("false")) |
91 | 0 | directed = false; |
92 | else | |
93 | 0 | throw new FatalException("Error parsing graph: 'directed' tag has invalid value: " + direction); |
94 | } | |
95 | // Edge e = GraphUtils.addEdge(mGraph, sourceVertex, targetVertex); | |
96 | Edge e; | |
97 | 119 | if (directed) |
98 | 58 | e = mGraph.addEdge(new DirectedSparseEdge(sourceVertex, targetVertex)); |
99 | else | |
100 | 61 | e = mGraph.addEdge(new UndirectedSparseEdge(sourceVertex, targetVertex)); |
101 | ||
102 | 119 | for (Iterator keyIt = attributeMap.keySet().iterator(); |
103 | 131 | keyIt.hasNext(); |
104 | ) { | |
105 | 12 | Object key = keyIt.next(); |
106 | 12 | Object value = attributeMap.get(key); |
107 | 12 | e.setUserDatum(key, value, UserData.SHARED); |
108 | } | |
109 | ||
110 | 119 | return e; |
111 | } | |
112 | ||
113 | protected void createGraph(Map attributeMap) { | |
114 | 18 | String edgeDefaultType = |
115 | (String) attributeMap.remove("edgedefault"); | |
116 | 18 | mGraph = new SparseGraph(); |
117 | 18 | if (edgeDefaultType.equals("directed")) |
118 | { | |
119 | 9 | default_directed = true; |
120 | // mGraph = new DirectedSparseGraph(); | |
121 | } | |
122 | 9 | else if (edgeDefaultType.equals("undirected")) |
123 | { | |
124 | 9 | default_directed = false; |
125 | // mGraph = new UndirectedSparseGraph(); | |
126 | } | |
127 | else { | |
128 | 0 | throw new FatalException("Error parsing graph. Edge default type not specified."); |
129 | } | |
130 | ||
131 | 18 | mLabeller = StringLabeller.getLabeller(mGraph); |
132 | ||
133 | 18 | for (Iterator keyIt = attributeMap.keySet().iterator(); keyIt.hasNext();) { |
134 | 0 | Object key = keyIt.next(); |
135 | 0 | Object value = attributeMap.get(key); |
136 | 0 | mGraph.setUserDatum(key, value, UserData.SHARED); |
137 | } | |
138 | ||
139 | 18 | } |
140 | ||
141 | protected ArchetypeVertex createVertex(Map attributeMap) { | |
142 | 94 | if (mGraph == null) { |
143 | 0 | throw new FatalException("Error parsing graph. Graph element must be specified before node element."); |
144 | } | |
145 | ||
146 | 94 | ArchetypeVertex vertex = mGraph.addVertex(new SparseVertex()); |
147 | 94 | String idString = (String) attributeMap.remove("id"); |
148 | ||
149 | try { | |
150 | 94 | mLabeller.setLabel((Vertex) vertex,idString); |
151 | 0 | } catch (StringLabeller.UniqueLabelException ule) { |
152 | 0 | throw new FatalException("Ids must be unique"); |
153 | ||
154 | 94 | } |
155 | ||
156 | 94 | for (Iterator keyIt = attributeMap.keySet().iterator(); |
157 | 128 | keyIt.hasNext(); |
158 | ) { | |
159 | 34 | Object key = keyIt.next(); |
160 | 34 | Object value = attributeMap.get(key); |
161 | 34 | vertex.setUserDatum(key, value, UserData.SHARED); |
162 | } | |
163 | 94 | return vertex; |
164 | } | |
165 | ||
166 | public void startElement( | |
167 | String namespaceURI, | |
168 | String lName, | |
169 | // local name | |
170 | String qName, // qualified name | |
171 | Attributes attrs) throws SAXException { | |
172 | ||
173 | 232 | Map attributeMap = getAttributeMap(attrs); |
174 | ||
175 | 232 | if (qName.toLowerCase().equals("graph")) { |
176 | 18 | createGraph(attributeMap); |
177 | ||
178 | 214 | } else if (qName.toLowerCase().equals("node")) { |
179 | 94 | createVertex(attributeMap); |
180 | ||
181 | 120 | } else if (qName.toLowerCase().equals("edge")) { |
182 | 119 | createEdge(attributeMap); |
183 | ||
184 | } | |
185 | 232 | } |
186 | ||
187 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |