1
2
3
4
5
6
7
8
9
10
11 package edu.uci.ics.jung.io.graphml.parser;
12
13 import java.util.Iterator;
14
15 import javax.xml.stream.XMLEventReader;
16 import javax.xml.stream.events.Attribute;
17 import javax.xml.stream.events.EndElement;
18 import javax.xml.stream.events.StartElement;
19 import javax.xml.stream.events.XMLEvent;
20
21 import edu.uci.ics.jung.graph.Hypergraph;
22 import edu.uci.ics.jung.io.GraphIOException;
23 import edu.uci.ics.jung.io.graphml.GraphMLConstants;
24 import edu.uci.ics.jung.io.graphml.Key;
25 import edu.uci.ics.jung.io.graphml.ExceptionConverter;
26
27
28
29
30
31
32 public class KeyElementParser<G extends Hypergraph<V,E>,V,E> extends AbstractElementParser<G,V,E> {
33
34 public KeyElementParser(ParserContext<G,V,E> parserContext) {
35 super(parserContext);
36 }
37
38 public Key parse(XMLEventReader xmlEventReader, StartElement start)
39 throws GraphIOException {
40
41 try {
42
43 Key key = new Key();
44
45
46 @SuppressWarnings("unchecked")
47 Iterator<Attribute> iterator = start.getAttributes();
48 while (iterator.hasNext()) {
49 Attribute attribute = iterator.next();
50 String name = attribute.getName().getLocalPart();
51 String value = attribute.getValue();
52 if (key.getId() == null && GraphMLConstants.ID_NAME.equals(name)) {
53 key.setId(value);
54 } else if (key.getAttributeName() == null
55 && GraphMLConstants.ATTRNAME_NAME.equals(name)) {
56 key.setAttributeName(value);
57 } else if (key.getAttributeType() == null
58 && GraphMLConstants.ATTRTYPE_NAME.equals(name)) {
59 key.setAttributeType(value);
60 } else if (GraphMLConstants.FOR_NAME.equals(name)) {
61 key.setForType(convertFor(value));
62 }
63 }
64
65
66 if (key.getId() == null) {
67 throw new GraphIOException(
68 "Element 'key' is missing attribute 'id'");
69 }
70
71 while (xmlEventReader.hasNext()) {
72
73 XMLEvent event = xmlEventReader.nextEvent();
74 if (event.isStartElement()) {
75 StartElement element = (StartElement) event;
76
77 String name = element.getName().getLocalPart();
78 if(GraphMLConstants.DESC_NAME.equals(name)) {
79 String desc = (String)getParser(name).parse(xmlEventReader, element);
80 key.setDescription(desc);
81 } else if(GraphMLConstants.DEFAULT_NAME.equals(name)) {
82 String defaultValue = (String)getParser(name).parse(xmlEventReader, element);
83 key.setDefaultValue(defaultValue);
84 } else {
85
86
87 getUnknownParser().parse(xmlEventReader, element);
88 }
89
90 }
91 if (event.isEndElement()) {
92 EndElement end = (EndElement) event;
93 verifyMatch(start, end);
94 break;
95 }
96 }
97
98 return key;
99
100 } catch (Exception e) {
101 ExceptionConverter.convert(e);
102 }
103
104 return null;
105 }
106
107 static public Key.ForType convertFor(String value) {
108
109 if (value != null) {
110
111 if (GraphMLConstants.GRAPH_NAME.equals(value)) {
112 return Key.ForType.GRAPH;
113 }
114 if (GraphMLConstants.EDGE_NAME.equals(value)) {
115 return Key.ForType.EDGE;
116 }
117 if (GraphMLConstants.ENDPOINT_NAME.equals(value)) {
118 return Key.ForType.ENDPOINT;
119 }
120 if (GraphMLConstants.HYPEREDGE_NAME.equals(value)) {
121 return Key.ForType.HYPEREDGE;
122 }
123 if (GraphMLConstants.NODE_NAME.equals(value)) {
124 return Key.ForType.NODE;
125 }
126 if (GraphMLConstants.PORT_NAME.equals(value)) {
127 return Key.ForType.PORT;
128 }
129 }
130
131 return Key.ForType.ALL;
132 }
133 }