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   * GTUMIX Tag.
15   * 
16   * <pre>
17   * {@code
18    <xsd:element name="GTUMIX">
19      <xsd:complexType>
20        <xsd:sequence>
21          <xsd:element name="GTU" minOccurs="1" maxOccurs="unbounded">
22            <xsd:complexType>
23              <xsd:attribute name="NAME" type="xsd:string" use="required" />
24              <xsd:attribute name="WEIGHT" type="xsd:double" use="required" />
25            </xsd:complexType>
26          </xsd:element>
27        </xsd:sequence>
28        <xsd:attribute name="NAME" type="xsd:string" use="required" />
29        <xsd:attribute ref="xml:base" />
30      </xsd:complexType>
31    </xsd:element>
32   * }
33   * </pre>
34   * <p>
35   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
36   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
37   * <p>
38   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
39   * initial version Jul 23, 2015 <br>
40   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
41   */
42  class GTUMixTag implements Serializable
43  {
44      /** */
45      private static final long serialVersionUID = 20150723L;
46  
47      /** Name. */
48      @SuppressWarnings("checkstyle:visibilitymodifier")
49      String name = null;
50  
51      /** GTUs. */
52      @SuppressWarnings("checkstyle:visibilitymodifier")
53      List<GTUTag> gtus = new ArrayList<GTUTag>();
54  
55      /** Weights. */
56      @SuppressWarnings("checkstyle:visibilitymodifier")
57      List<Double> weights = new ArrayList<Double>();
58  
59      /**
60       * Parse the GTUMIX tag.
61       * @param nodeList NodeList; nodeList the top-level nodes of the XML-file
62       * @param parser XmlNetworkLaneParserOld; the parser with the lists of information
63       * @throws SAXException when parsing of the tag fails
64       * @throws NetworkException when parsing of the tag fails
65       */
66      @SuppressWarnings("checkstyle:needbraces")
67      static void parseGTUMix(final NodeList nodeList, final XmlNetworkLaneParserOld parser) throws SAXException, NetworkException
68      {
69          for (Node node : XMLParser.getNodes(nodeList, "GTUMIX"))
70          {
71              NamedNodeMap attributes = node.getAttributes();
72              GTUMixTag gtuMixTag = new GTUMixTag();
73  
74              Node name = attributes.getNamedItem("NAME");
75              if (name == null)
76                  throw new SAXException("GTUMIX: missing attribute NAME");
77              gtuMixTag.name = name.getNodeValue().trim();
78              if (parser.gtuMixTags.keySet().contains(gtuMixTag.name))
79                  throw new SAXException("GTUMIX: NAME " + gtuMixTag.name + " defined twice");
80  
81              List<Node> gtuList = XMLParser.getNodes(node.getChildNodes(), "GTU");
82              if (gtuList.size() == 0)
83                  throw new SAXException("GTUMIX: missing tag GTU");
84              for (Node gtuNode : gtuList)
85              {
86                  parseGTUMixGTUTag(gtuNode, parser, gtuMixTag);
87              }
88  
89              parser.gtuMixTags.put(gtuMixTag.name, gtuMixTag);
90          }
91      }
92  
93      /**
94       * Parse the GTUMIX's GTU tag.
95       * @param gtuNode Node; the GTU node to parse
96       * @param parser XmlNetworkLaneParserOld; the parser with the lists of information
97       * @param gtuMixTag GTUMixTag; the parent tag
98       * @throws SAXException when parsing of the tag fails
99       * @throws NetworkException when parsing of the tag fails
100      */
101     @SuppressWarnings("checkstyle:needbraces")
102     private static void parseGTUMixGTUTag(final Node gtuNode, final XmlNetworkLaneParserOld parser, final GTUMixTag gtuMixTag)
103             throws NetworkException, SAXException
104     {
105         NamedNodeMap attributes = gtuNode.getAttributes();
106 
107         Node gtuName = attributes.getNamedItem("NAME");
108         if (gtuName == null)
109             throw new NetworkException("GTUMIX: No GTU NAME defined");
110         if (!parser.gtuTags.containsKey(gtuName.getNodeValue().trim()))
111             throw new NetworkException("GTUMIX: " + gtuMixTag.name + " GTU " + gtuName.getNodeValue().trim() + " not defined");
112         gtuMixTag.gtus.add(parser.gtuTags.get(gtuName.getNodeValue().trim()));
113 
114         Node weight = attributes.getNamedItem("WEIGHT");
115         if (weight == null)
116             throw new NetworkException(
117                     "GTUMIX: " + gtuMixTag.name + " GTU " + gtuName.getNodeValue().trim() + ": weight not defined");
118         gtuMixTag.weights.add(Double.parseDouble(weight.getNodeValue()));
119     }
120 
121     /** {@inheritDoc} */
122     @Override
123     public final String toString()
124     {
125         return "GTUMixTag [name=" + this.name + ", gtus=" + this.gtus + ", weights=" + this.weights + "]";
126     }
127 
128 }