View Javadoc
1   package org.opentrafficsim.road.network.factory.vissim;
2   
3   import java.io.Serializable;
4   import java.util.ArrayList;
5   import java.util.List;
6   
7   import org.opentrafficsim.core.network.NetworkException;
8   import org.opentrafficsim.core.network.route.Route;
9   import org.w3c.dom.NamedNodeMap;
10  import org.w3c.dom.Node;
11  import org.w3c.dom.NodeList;
12  import org.xml.sax.SAXException;
13  
14  /**
15   * <p>
16   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
18   * <p>
19   * $LastChangedDate: 2019-01-06 01:39:32 +0100 (Sun, 06 Jan 2019) $, @version $Revision: 4833 $, by $Author: averbraeck $,
20   * initial version Jul 23, 2015 <br>
21   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22   */
23  class RouteTag implements Serializable
24  {
25      /** */
26      private static final long serialVersionUID = 20150723L;
27  
28      /** Name. */
29      @SuppressWarnings("checkstyle:visibilitymodifier")
30      String name = null;
31  
32      /** Nodes. */
33      @SuppressWarnings("checkstyle:visibilitymodifier")
34      List<NodeTag> routeNodeTags = new ArrayList<NodeTag>();
35  
36      /** Route that has been generated. */
37      @SuppressWarnings("checkstyle:visibilitymodifier")
38      Route route;
39  
40      /**
41       * Parse the ROUTE tag.
42       * @param nodeList NodeList; nodeList the top-level nodes of the XML-file
43       * @param parser VissimNetworkLaneParser; the parser with the lists of information
44       * @throws SAXException when parsing of the tag fails
45       * @throws NetworkException when parsing of the tag fails
46       */
47      @SuppressWarnings("checkstyle:needbraces")
48      static void parseRoutes(final NodeList nodeList, final VissimNetworkLaneParser parser) throws SAXException, NetworkException
49      {
50          for (Node node : XMLParser.getNodes(nodeList, "ROUTE"))
51          {
52              NamedNodeMap attributes = node.getAttributes();
53              RouteTag routeTag = new RouteTag();
54  
55              if (attributes.getNamedItem("NAME") == null)
56              {
57                  throw new SAXException("ROUTE: missing attribute NAME");
58              }
59              routeTag.name = attributes.getNamedItem("NAME").getNodeValue().trim();
60              if (parser.getRouteTags().keySet().contains(routeTag.name))
61              {
62                  throw new SAXException("ROUTE: NAME " + routeTag.name + " defined twice");
63              }
64  
65              if (attributes.getNamedItem("NODELIST") == null)
66              {
67                  throw new SAXException("ROUTE " + routeTag.name + ": missing attribute NODELIST");
68              }
69              String routeNodes = attributes.getNamedItem("NODELIST").getNodeValue().trim();
70              routeTag.routeNodeTags = NodeTag.parseNodeList(routeNodes, parser);
71  
72              parser.getRouteTags().put(routeTag.name, routeTag);
73          }
74      }
75  
76      /**
77       * Make the route based on the nodes. This method should be called after the Nodes have been created from the NodeTags.
78       * @throws NetworkException when node cannot be added.
79       */
80      void makeRoute() throws NetworkException
81      {
82          this.route = new Route(this.name);
83          for (NodeTag nodeTag : this.routeNodeTags)
84          {
85              this.route.addNode(nodeTag.node);
86          }
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public final String toString()
92      {
93          return "RouteTag [name=" + this.name + ", routeNodeTags=" + this.routeNodeTags + ", route=" + this.route + "]";
94      }
95  }