1 package org.opentrafficsim.road.network.factory.xml.old;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import javax.naming.NamingException;
8
9 import org.djunits.value.vdouble.scalar.Direction;
10 import org.opentrafficsim.core.geometry.OTSPoint3D;
11 import org.opentrafficsim.core.network.NetworkException;
12 import org.opentrafficsim.core.network.OTSNode;
13 import org.opentrafficsim.core.network.factory.xml.units.AngleUnits;
14 import org.opentrafficsim.core.network.factory.xml.units.Coordinates;
15 import org.w3c.dom.NamedNodeMap;
16 import org.w3c.dom.Node;
17 import org.w3c.dom.NodeList;
18 import org.xml.sax.SAXException;
19
20
21
22
23
24
25
26
27
28
29 class NodeTag implements Serializable
30 {
31
32 private static final long serialVersionUID = 20150723L;
33
34
35 @SuppressWarnings("checkstyle:visibilitymodifier")
36 String name = null;
37
38
39 @SuppressWarnings("checkstyle:visibilitymodifier")
40 OTSPoint3D coordinate = null;
41
42
43 @SuppressWarnings("checkstyle:visibilitymodifier")
44 Direction direction = null;
45
46
47 @SuppressWarnings("checkstyle:visibilitymodifier")
48 OTSNode node = null;
49
50
51
52
53
54
55
56 @SuppressWarnings("checkstyle:needbraces")
57 static void parseNodes(final NodeList nodeList, final XmlNetworkLaneParserOld parser) throws SAXException, NetworkException
58 {
59 for (Node node : XMLParser.getNodes(nodeList, "NODE"))
60 {
61 NamedNodeMap attributes = node.getAttributes();
62 NodeTag nodeTag = new NodeTag();
63
64 Node name = attributes.getNamedItem("NAME");
65 if (name == null)
66 throw new SAXException("NODE: missing attribute NAME");
67 nodeTag.name = name.getNodeValue().trim();
68 if (parser.nodeTags.keySet().contains(nodeTag.name))
69 throw new SAXException("NODE: NAME " + nodeTag.name + " defined twice");
70
71 if (attributes.getNamedItem("COORDINATE") != null)
72 nodeTag.coordinate = Coordinates.parseCoordinate(attributes.getNamedItem("COORDINATE").getNodeValue());
73 else
74 throw new SAXException("NODE: missing attribute COORDINATE");
75
76 if (attributes.getNamedItem("DIRECTION") != null)
77 nodeTag.direction = AngleUnits.parseDirection(attributes.getNamedItem("DIRECTION").getNodeValue());
78
79 parser.nodeTags.put(nodeTag.name, nodeTag);
80
81 try
82 {
83 makeOTSNode(nodeTag, parser);
84 }
85 catch (NamingException exception)
86 {
87 throw new NetworkException(exception);
88 }
89 }
90 }
91
92
93
94
95
96
97
98
99
100 static List<NodeTag> parseNodeList(final String nodeNames, final XmlNetworkLaneParserOld parser)
101 throws SAXException, NetworkException
102 {
103 List<NodeTag> nodeList = new ArrayList<>();
104 String[] ns = nodeNames.split("\\s");
105 for (String s : ns)
106 {
107 if (!parser.nodeTags.containsKey(s))
108 {
109 throw new SAXException("Node " + s + " from node list [" + nodeNames + "] was not defined");
110 }
111 nodeList.add(parser.nodeTags.get(s));
112 }
113 return nodeList;
114
115 }
116
117
118
119
120
121
122
123
124 static OTSNode makeOTSNode(final NodeTag nodeTag, final XmlNetworkLaneParserOld parser)
125 throws NetworkException, NamingException
126 {
127 String id = nodeTag.name;
128 OTSNode node = new OTSNode(parser.network, id, nodeTag.coordinate);
129 nodeTag.node = node;
130 return node;
131 }
132
133
134 @Override
135 public String toString()
136 {
137 return "NodeTag [name=" + this.name + ", coordinate=" + this.coordinate + ", node=" + this.node + "]";
138 }
139
140 }