1 package org.opentrafficsim.road.network.factory.xml;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.Serializable;
6 import java.net.URL;
7 import java.util.HashMap;
8 import java.util.List;
9 import java.util.Map;
10
11 import javax.naming.NamingException;
12 import javax.xml.parsers.DocumentBuilder;
13 import javax.xml.parsers.DocumentBuilderFactory;
14 import javax.xml.parsers.ParserConfigurationException;
15
16 import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
17 import org.opentrafficsim.core.geometry.OTSGeometryException;
18 import org.opentrafficsim.core.gtu.GTUException;
19 import org.opentrafficsim.core.gtu.GTUType;
20 import org.opentrafficsim.core.network.NetworkException;
21 import org.opentrafficsim.core.network.OTSNetwork;
22 import org.opentrafficsim.road.network.lane.LaneType;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Node;
25 import org.w3c.dom.NodeList;
26 import org.xml.sax.SAXException;
27
28 import nl.tudelft.simulation.dsol.SimRuntimeException;
29
30
31
32
33
34
35
36
37
38
39 public class XmlNetworkLaneParser implements Serializable
40 {
41
42 private static final long serialVersionUID = 20150723L;
43
44
45 @SuppressWarnings("visibilitymodifier")
46 protected GlobalTag globalTag;
47
48
49 @SuppressWarnings("visibilitymodifier")
50 protected Map<String, NodeTag> nodeTags = new HashMap<>();
51
52
53 @SuppressWarnings("visibilitymodifier")
54 protected Map<String, LinkTag> linkTags = new HashMap<>();
55
56
57 @SuppressWarnings("visibilitymodifier")
58 protected Map<String, GTUTag> gtuTags = new HashMap<>();
59
60
61 @SuppressWarnings("visibilitymodifier")
62 protected Map<String, GTUMixTag> gtuMixTags = new HashMap<>();
63
64
65 @SuppressWarnings("visibilitymodifier")
66 protected Map<String, RouteTag> routeTags = new HashMap<>();
67
68
69 @SuppressWarnings("visibilitymodifier")
70 protected Map<String, RouteMixTag> routeMixTags = new HashMap<>();
71
72
73 @SuppressWarnings("visibilitymodifier")
74 protected Map<String, ShortestRouteTag> shortestRouteTags = new HashMap<>();
75
76
77 @SuppressWarnings("visibilitymodifier")
78 protected Map<String, ShortestRouteMixTag> shortestRouteMixTags = new HashMap<>();
79
80
81 @SuppressWarnings("visibilitymodifier")
82 protected Map<String, RoadTypeTag> roadTypeTags = new HashMap<>();
83
84
85 @SuppressWarnings("visibilitymodifier")
86 protected Map<String, RoadLayoutTag> roadLayoutTags = new HashMap<>();
87
88
89 @SuppressWarnings("visibilitymodifier")
90 public Map<String, GTUType> gtuTypes = new HashMap<>();
91
92
93 @SuppressWarnings("visibilitymodifier")
94 protected Map<String, LaneTypeTag> laneTypeTags = new HashMap<>();
95
96
97 @SuppressWarnings("visibilitymodifier")
98 protected Map<String, LaneType> laneTypes = new HashMap<>();
99
100
101 @SuppressWarnings("visibilitymodifier")
102 protected OTSDEVSSimulatorInterface simulator;
103
104
105 @SuppressWarnings("visibilitymodifier")
106 protected OTSNetwork network;
107
108
109
110
111 public XmlNetworkLaneParser(final OTSDEVSSimulatorInterface simulator)
112 {
113 this.simulator = simulator;
114 LaneTypeTag laneTypeTagNoTraffic = new LaneTypeTag();
115 laneTypeTagNoTraffic.name = "NOTRAFFIC";
116 this.laneTypeTags.put(laneTypeTagNoTraffic.name, laneTypeTagNoTraffic);
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131 @SuppressWarnings("checkstyle:needbraces")
132 public final OTSNetwork build(final URL url) throws NetworkException, ParserConfigurationException, SAXException,
133 IOException, NamingException, GTUException, OTSGeometryException, SimRuntimeException
134 {
135 return build(url, new OTSNetwork(url.toString()));
136 }
137
138
139
140
141
142
143
144
145
146
147
148
149
150 @SuppressWarnings("checkstyle:needbraces")
151 public final OTSNetwork build(final InputStream stream) throws NetworkException, ParserConfigurationException, SAXException,
152 IOException, NamingException, GTUException, OTSGeometryException, SimRuntimeException
153 {
154 return build(stream, new OTSNetwork(stream.toString()));
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 @SuppressWarnings("checkstyle:needbraces")
171 public final OTSNetwork build(final URL url, final OTSNetwork network)
172 throws NetworkException, ParserConfigurationException, SAXException, IOException, NamingException, GTUException,
173 OTSGeometryException, SimRuntimeException
174 {
175 return build(url.openStream(), network);
176 }
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191 @SuppressWarnings("checkstyle:needbraces")
192 public final OTSNetwork build(final InputStream stream, final OTSNetwork network)
193 throws NetworkException, ParserConfigurationException, SAXException, IOException, NamingException, GTUException,
194 OTSGeometryException, SimRuntimeException
195 {
196
197
198
199
200
201
202
203
204
205
206 this.network = network;
207
208 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
209 factory.setNamespaceAware(true);
210 factory.setXIncludeAware(true);
211 DocumentBuilder builder = factory.newDocumentBuilder();
212 Document document = builder.parse(stream);
213 NodeList networkNodeList = document.getDocumentElement().getChildNodes();
214
215 if (!document.getDocumentElement().getNodeName().equals("NETWORK"))
216 throw new SAXException("XmlNetworkLaneParser.build: XML document does not start with an NETWORK tag, found "
217 + document.getDocumentElement().getNodeName() + " instead");
218
219
220 List<Node> definitionNodes = XMLParser.getNodes(networkNodeList, "DEFINITIONS");
221
222 if (definitionNodes.size() == 0)
223 throw new SAXException("XmlNetworkLaneParser.build: XML document does not have a DEFINITIONS tag");
224
225
226 this.gtuTypes.put("ALL", GTUType.ALL);
227 this.gtuTypes.put("NONE", GTUType.NONE);
228
229
230 for (Node definitionNode : definitionNodes)
231 GlobalTag.parseGlobal(definitionNode.getChildNodes(), this);
232 for (Node definitionNode : definitionNodes)
233 GTUTypeTag.parseGTUTypes(definitionNode.getChildNodes(), this);
234 for (Node definitionNode : definitionNodes)
235 GTUTag.parseGTUs(definitionNode.getChildNodes(), this);
236 for (Node definitionNode : definitionNodes)
237 GTUMixTag.parseGTUMix(definitionNode.getChildNodes(), this);
238 for (Node definitionNode : definitionNodes)
239 RoadTypeTag.parseRoadTypes(definitionNode.getChildNodes(), this);
240 for (Node definitionNode : definitionNodes)
241 LaneTypeTag.parseLaneTypes(definitionNode.getChildNodes(), this);
242 for (Node definitionNode : definitionNodes)
243 RoadLayoutTag.parseRoadTypes(definitionNode.getChildNodes(), this);
244
245
246 NodeTag.parseNodes(networkNodeList, this);
247 RouteTag.parseRoutes(networkNodeList, this);
248 ShortestRouteTag.parseShortestRoutes(networkNodeList, this);
249 RouteMixTag.parseRouteMix(networkNodeList, this);
250 ShortestRouteMixTag.parseShortestRouteMix(networkNodeList, this);
251 LinkTag.parseLinks(networkNodeList, this);
252
253
254 Links.calculateNodeCoordinates(this);
255 for (LinkTag linkTag : this.linkTags.values())
256 Links.buildLink(linkTag, this, this.simulator);
257 for (LinkTag linkTag : this.linkTags.values())
258 Links.applyRoadTypeToLink(linkTag, this, this.simulator);
259
260
261 for (RouteTag routeTag : this.routeTags.values())
262 routeTag.makeRoute();
263
264
265
266 return makeNetwork();
267 }
268
269
270
271
272
273 private OTSNetwork makeNetwork() throws NetworkException
274 {
275 for (RouteTag routeTag : this.routeTags.values())
276 {
277
278
279 this.network.addRoute(GTUType.ALL, routeTag.route);
280 }
281 return this.network;
282 }
283
284
285 @Override
286 public String toString()
287 {
288 return "XmlNetworkLaneParser [globalTag=" + this.globalTag + ", nodeTags.size=" + this.nodeTags.size()
289 + ", linkTags.size=" + this.linkTags.size() + ", gtuTags.size=" + this.gtuTags.size() + ", gtuMixTags.size="
290 + this.gtuMixTags.size() + ", routeTags.size=" + this.routeTags.size() + ", routeMixTags.size="
291 + this.routeMixTags.size() + ", shortestRouteTagssize.=" + this.shortestRouteTags.size()
292 + ", shortestRouteMixTags.size=" + this.shortestRouteMixTags.size() + ", roadTypeTags.size="
293 + this.roadTypeTags.size() + ", gtuTypes.size=" + this.gtuTypes.size() + ", laneTypes.size="
294 + this.laneTypeTags.size() + "]";
295 }
296
297 }