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.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: 2019-01-06 01:39:32 +0100 (Sun, 06 Jan 2019) $, @version $Revision: 4833 $, 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 ShortestRouteMixTag 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<ShortestRouteTag> routes = new ArrayList<ShortestRouteTag>();
34  
35      /** Weights. */
36      @SuppressWarnings("checkstyle:visibilitymodifier")
37      List<Double> weights = new ArrayList<Double>();
38  
39      /**
40       * Parse the SHORTESTROUTE tag.
41       * @param nodeList NodeList; nodeList the top-level nodes of the XML-file
42       * @param parser VissimNetworkLaneParser; 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 parseShortestRouteMix(final NodeList nodeList, final VissimNetworkLaneParser parser)
48              throws SAXException, NetworkException
49      {
50          for (Node node : XMLParser.getNodes(nodeList, "SHORTESTROUTEMIX"))
51          {
52              NamedNodeMap attributes = node.getAttributes();
53              ShortestRouteMixTag shortestRouteMixTag = new ShortestRouteMixTag();
54  
55              if (attributes.getNamedItem("NAME") == null)
56              {
57                  throw new SAXException("SHORTESTROUTEMIX: missing attribute NAME");
58              }
59              shortestRouteMixTag.name = attributes.getNamedItem("NAME").getNodeValue().trim();
60              if (parser.getRouteTags().keySet().contains(shortestRouteMixTag.name))
61              {
62                  throw new SAXException("SHORTESTROUTEMIX: NAME " + shortestRouteMixTag.name + " defined twice");
63              }
64  
65              List<Node> shortestRouteList = XMLParser.getNodes(node.getChildNodes(), "SHORTESTROUTE");
66              if (shortestRouteList.size() == 0)
67              {
68                  throw new SAXException("SHORTESTROUTEMIX: missing tag SHORTESTROUTE");
69              }
70              for (Node shortestRouteNode : shortestRouteList)
71              {
72                  parseRouteMixRouteTag(shortestRouteNode, parser, shortestRouteMixTag);
73              }
74  
75              parser.getShortestRouteMixTags().put(shortestRouteMixTag.name, shortestRouteMixTag);
76          }
77      }
78  
79      /**
80       * Parse the SHORTESTROUTEMIX's SHORTESTROUTE tag.
81       * @param shortestRouteNode Node; the SHORTESTROUTE node to parse
82       * @param parser VissimNetworkLaneParser; the parser with the lists of information
83       * @param shortestRouteMixTag ShortestRouteMixTag; the parent tag
84       * @throws SAXException when parsing of the tag fails
85       * @throws NetworkException when parsing of the tag fails
86       */
87      @SuppressWarnings("checkstyle:needbraces")
88      private static void parseRouteMixRouteTag(final Node shortestRouteNode, final VissimNetworkLaneParser parser,
89              final ShortestRouteMixTag shortestRouteMixTag) throws NetworkException, SAXException
90      {
91          NamedNodeMap attributes = shortestRouteNode.getAttributes();
92  
93          Node shortestRouteName = attributes.getNamedItem("NAME");
94          if (shortestRouteName == null)
95          {
96              throw new NetworkException("SHORTESTROUTEMIX: No SHORTESTROUTE NAME defined");
97          }
98          if (!parser.getRouteTags().containsKey(shortestRouteName.getNodeValue().trim()))
99          {
100             throw new NetworkException("SHORTESTROUTEMIX: " + shortestRouteMixTag.name + " SHORTESTROUTE "
101                     + shortestRouteName.getNodeValue().trim() + " not defined");
102         }
103         shortestRouteMixTag.routes.add(parser.getShortestRouteTags().get(shortestRouteName.getNodeValue().trim()));
104 
105         Node weight = attributes.getNamedItem("WEIGHT");
106         if (weight == null)
107         {
108             throw new NetworkException("SHORTESTROUTEMIX: " + shortestRouteMixTag.name + " SHORTESTROUTE "
109                     + shortestRouteName.getNodeValue().trim() + ": weight not defined");
110         }
111         shortestRouteMixTag.weights.add(Double.parseDouble(weight.getNodeValue()));
112     }
113 
114     /** {@inheritDoc} */
115     @Override
116     public final String toString()
117     {
118         return "ShortestRouteMixTag [name=" + this.name + ", routes=" + this.routes + ", weights=" + this.weights + "]";
119     }
120 
121 }