View Javadoc
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 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: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, 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 XmlNetworkLaneParserOld; 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 XmlNetworkLaneParserOld 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                  throw new SAXException("ROUTE: missing attribute NAME");
57              routeTag.name = attributes.getNamedItem("NAME").getNodeValue().trim();
58              if (parser.routeTags.keySet().contains(routeTag.name))
59                  throw new SAXException("ROUTE: NAME " + routeTag.name + " defined twice");
60  
61              if (attributes.getNamedItem("NODELIST") == null)
62                  throw new SAXException("ROUTE " + routeTag.name + ": missing attribute NODELIST");
63              String routeNodes = attributes.getNamedItem("NODELIST").getNodeValue().trim();
64              routeTag.routeNodeTags = NodeTag.parseNodeList(routeNodes, parser);
65  
66              parser.routeTags.put(routeTag.name, routeTag);
67          }
68      }
69  
70      /**
71       * Make the route based on the nodes. This method should be called after the Nodes have been created from the NodeTags.
72       * @throws NetworkException when node cannot be added.
73       */
74      void makeRoute() throws NetworkException
75      {
76          this.route = new Route(this.name);
77          for (NodeTag nodeTag : this.routeNodeTags)
78          {
79              this.route.addNode(nodeTag.node);
80          }
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      public final String toString()
86      {
87          return "RouteTag [name=" + this.name + ", routeNodeTags=" + this.routeNodeTags + ", route=" + this.route + "]";
88      }
89  }