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.w3c.dom.NamedNodeMap;
9   import org.w3c.dom.Node;
10  import org.w3c.dom.NodeList;
11  import org.xml.sax.SAXException;
12  
13  /**
14   * <p>
15   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
17   * <p>
18   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
19   * initial version Jul 23, 2015 <br>
20   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21   */
22  class RouteMixTag implements Serializable
23  {
24      /** */
25      private static final long serialVersionUID = 20150723L;
26  
27      /** Name. */
28      @SuppressWarnings("checkstyle:visibilitymodifier")
29      String name = null;
30  
31      /** Routes. */
32      @SuppressWarnings("checkstyle:visibilitymodifier")
33      List<RouteTag> routes = new ArrayList<RouteTag>();
34  
35      /** Weights. */
36      @SuppressWarnings("checkstyle:visibilitymodifier")
37      List<Double> weights = new ArrayList<Double>();
38  
39      /**
40       * Parse the ROUTE tag.
41       * @param nodeList NodeList; nodeList the top-level nodes of the XML-file
42       * @param parser XmlNetworkLaneParserOld; the parser with the lists of information
43       * @throws SAXException when parsing of the tag fails
44       * @throws NetworkException when parsing of the tag fails
45       */
46      @SuppressWarnings("checkstyle:needbraces")
47      static void parseRouteMix(final NodeList nodeList, final XmlNetworkLaneParserOld parser)
48              throws SAXException, NetworkException
49      {
50          for (Node node : XMLParser.getNodes(nodeList, "ROUTEMIX"))
51          {
52              NamedNodeMap attributes = node.getAttributes();
53              RouteMixTag routeMixTag = new RouteMixTag();
54  
55              if (attributes.getNamedItem("NAME") == null)
56                  throw new SAXException("ROUTEMIX: missing attribute NAME");
57              routeMixTag.name = attributes.getNamedItem("NAME").getNodeValue().trim();
58              if (parser.routeTags.keySet().contains(routeMixTag.name))
59                  throw new SAXException("ROUTEMIX: NAME " + routeMixTag.name + " defined twice");
60  
61              List<Node> routeList = XMLParser.getNodes(node.getChildNodes(), "ROUTE");
62              if (routeList.size() == 0)
63                  throw new SAXException("ROUTEMIX: missing tag ROUTE");
64              for (Node routeNode : routeList)
65              {
66                  parseRouteMixRouteTag(routeNode, parser, routeMixTag);
67              }
68  
69              parser.routeMixTags.put(routeMixTag.name, routeMixTag);
70          }
71      }
72  
73      /**
74       * Parse the ROUTEMIX's ROUTE tag.
75       * @param routeNode Node; the ROUTE node to parse
76       * @param parser XmlNetworkLaneParserOld; the parser with the lists of information
77       * @param routeMixTag RouteMixTag; the parent tag
78       * @throws SAXException when parsing of the tag fails
79       * @throws NetworkException when parsing of the tag fails
80       */
81      @SuppressWarnings("checkstyle:needbraces")
82      private static void parseRouteMixRouteTag(final Node routeNode, final XmlNetworkLaneParserOld parser,
83              final RouteMixTag routeMixTag) throws NetworkException, SAXException
84      {
85          NamedNodeMap attributes = routeNode.getAttributes();
86  
87          Node routeName = attributes.getNamedItem("NAME");
88          if (routeName == null)
89              throw new NetworkException("ROUTEMIX: No ROUTE NAME defined");
90          if (!parser.routeTags.containsKey(routeName.getNodeValue().trim()))
91              throw new NetworkException(
92                      "ROUTEMIX: " + routeMixTag.name + " ROUTE " + routeName.getNodeValue().trim() + " not defined");
93          routeMixTag.routes.add(parser.routeTags.get(routeName.getNodeValue().trim()));
94  
95          Node weight = attributes.getNamedItem("WEIGHT");
96          if (weight == null)
97              throw new NetworkException(
98                      "ROUTEMIX: " + routeMixTag.name + " ROUTE " + routeName.getNodeValue().trim() + ": weight not defined");
99          routeMixTag.weights.add(Double.parseDouble(weight.getNodeValue()));
100     }
101 
102     /** {@inheritDoc} */
103     @Override
104     public final String toString()
105     {
106         return "RouteMixTag [name=" + this.name + ", routes=" + this.routes + ", weights=" + this.weights + "]";
107     }
108 
109 }