Coverage details for edu.uci.ics.jung.io.GraphMLFileHandler

LineHitsSource
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      */
4418    public GraphMLFileHandler() {
4518    }
46  
47     protected Graph getGraph() {
4818        return mGraph;
49     }
50  
51     protected StringLabeller getLabeller() {
520        return mLabeller;
53     }
54  
55     private Map getAttributeMap(Attributes attrs) {
56232        Map map = new HashMap();
57232        if (attrs != null) {
58634            for (int i = 0; i < attrs.getLength(); i++) {
59402                map.put(attrs.getQName(i), attrs.getValue(i));
60             }
61         }
62232        return map;
63     }
64  
65     protected Edge createEdge(Map attributeMap) {
66119        if (mGraph == null) {
670            throw new FatalException("Error parsing graph. Graph element must be specified before edge element.");
68         }
69  
70119        String sourceId = (String) attributeMap.remove("source");
71119        Vertex sourceVertex =
72                 mLabeller.getVertex(sourceId);
73  
74119        String targetId = (String) attributeMap.remove("target");
75119        Vertex targetVertex =
76                  mLabeller.getVertex(targetId);
77  
78119        String direction = (String) attributeMap.remove("directed");
79         boolean directed;
80119        if (direction == null)
81         {
82             // use default_directed
83116            directed = default_directed;
84         }
85         else
86         {
87             // use specified direction
883            if (direction.equals("true"))
893                directed = true;
900            else if (direction.equals("false"))
910                directed = false;
92             else
930                throw new FatalException("Error parsing graph: 'directed' tag has invalid value: " + direction);
94         }
95 // Edge e = GraphUtils.addEdge(mGraph, sourceVertex, targetVertex);
96         Edge e;
97119        if (directed)
9858            e = mGraph.addEdge(new DirectedSparseEdge(sourceVertex, targetVertex));
99         else
10061            e = mGraph.addEdge(new UndirectedSparseEdge(sourceVertex, targetVertex));
101  
102119        for (Iterator keyIt = attributeMap.keySet().iterator();
103131             keyIt.hasNext();
104                 ) {
10512            Object key = keyIt.next();
10612            Object value = attributeMap.get(key);
10712            e.setUserDatum(key, value, UserData.SHARED);
108         }
109  
110119        return e;
111     }
112  
113     protected void createGraph(Map attributeMap) {
11418        String edgeDefaultType =
115                 (String) attributeMap.remove("edgedefault");
11618        mGraph = new SparseGraph();
11718        if (edgeDefaultType.equals("directed"))
118         {
1199            default_directed = true;
120 // mGraph = new DirectedSparseGraph();
121         }
1229        else if (edgeDefaultType.equals("undirected"))
123         {
1249            default_directed = false;
125 // mGraph = new UndirectedSparseGraph();
126         }
127         else {
1280            throw new FatalException("Error parsing graph. Edge default type not specified.");
129         }
130  
13118        mLabeller = StringLabeller.getLabeller(mGraph);
132  
13318        for (Iterator keyIt = attributeMap.keySet().iterator(); keyIt.hasNext();) {
1340            Object key = keyIt.next();
1350            Object value = attributeMap.get(key);
1360            mGraph.setUserDatum(key, value, UserData.SHARED);
137         }
138  
13918    }
140  
141     protected ArchetypeVertex createVertex(Map attributeMap) {
14294        if (mGraph == null) {
1430            throw new FatalException("Error parsing graph. Graph element must be specified before node element.");
144         }
145  
14694        ArchetypeVertex vertex = mGraph.addVertex(new SparseVertex());
14794        String idString = (String) attributeMap.remove("id");
148  
149         try {
15094            mLabeller.setLabel((Vertex) vertex,idString);
1510        } catch (StringLabeller.UniqueLabelException ule) {
1520            throw new FatalException("Ids must be unique");
153  
15494        }
155  
15694        for (Iterator keyIt = attributeMap.keySet().iterator();
157128             keyIt.hasNext();
158                 ) {
15934            Object key = keyIt.next();
16034            Object value = attributeMap.get(key);
16134            vertex.setUserDatum(key, value, UserData.SHARED);
162         }
16394        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  
173232        Map attributeMap = getAttributeMap(attrs);
174  
175232        if (qName.toLowerCase().equals("graph")) {
17618            createGraph(attributeMap);
177  
178214        } else if (qName.toLowerCase().equals("node")) {
17994            createVertex(attributeMap);
180  
181120        } else if (qName.toLowerCase().equals("edge")) {
182119            createEdge(attributeMap);
183  
184         }
185232    }
186  
187 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.