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