View Javadoc
1   package org.opentrafficsim.road.network.factory.xml.old;
2   
3   import java.io.Serializable;
4   
5   import org.djunits.unit.LengthUnit;
6   import org.djunits.unit.SpeedUnit;
7   import org.djunits.value.vdouble.scalar.Length;
8   import org.djunits.value.vdouble.scalar.Speed;
9   import org.opentrafficsim.core.gtu.GTUException;
10  import org.opentrafficsim.core.gtu.GTUType;
11  import org.opentrafficsim.core.network.NetworkException;
12  import org.opentrafficsim.core.network.factory.xml.units.Distributions;
13  import org.opentrafficsim.core.units.distributions.ContinuousDistDoubleScalar;
14  import org.w3c.dom.NamedNodeMap;
15  import org.w3c.dom.Node;
16  import org.w3c.dom.NodeList;
17  import org.xml.sax.SAXException;
18  
19  /**
20   * GTU Tag.
21   * 
22   * <pre>
23   * {@code
24    <xsd:element name="GTU">
25      <xsd:complexType>
26        <xsd:attribute name="NAME" type="xsd:string" use="required" />
27        <xsd:attribute name="GTUTYPE" type="xsd:string" use="required" />
28        <xsd:attribute name="LENGTH" type="LENGTHDISTTYPE" use="required" />
29        <xsd:attribute name="WIDTH" type="LENGTHDISTTYPE" use="required" />
30        <xsd:attribute name="MAXSPEED" type="SPEEDDISTTYPE" use="required" />
31        <xsd:attribute ref="xml:base" />
32      </xsd:complexType>
33    </xsd:element>
34   * }
35   * </pre>
36   * <p>
37   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
38   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
39   * <p>
40   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
41   * initial version Jul 23, 2015 <br>
42   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
43   */
44  public class GTUTag implements Serializable
45  {
46      /** */
47      private static final long serialVersionUID = 20150723L;
48  
49      /** Name. */
50      @SuppressWarnings("checkstyle:visibilitymodifier")
51      String name = null;
52  
53      /** Type. */
54      @SuppressWarnings("checkstyle:visibilitymodifier")
55      GTUType gtuType = null;
56  
57      /** GTU length. */
58      @SuppressWarnings("checkstyle:visibilitymodifier")
59      ContinuousDistDoubleScalar.Rel<Length, LengthUnit> lengthDist = null;
60  
61      /** GTU width. */
62      @SuppressWarnings("checkstyle:visibilitymodifier")
63      ContinuousDistDoubleScalar.Rel<Length, LengthUnit> widthDist = null;
64  
65      /** Maximum speed. */
66      @SuppressWarnings("checkstyle:visibilitymodifier")
67      ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> maxSpeedDist = null;
68  
69      /**
70       * @param nodeList NodeList; nodeList the top-level nodes of the XML-file
71       * @param parser XmlNetworkLaneParserOld; the parser with the lists of information
72       * @throws SAXException when parsing of GTU tag fails
73       * @throws NetworkException when parsing of GTU tag fails
74       * @throws GTUException if GTUType defined twice
75       */
76      @SuppressWarnings("checkstyle:needbraces")
77      static void parseGTUs(final NodeList nodeList, final XmlNetworkLaneParserOld parser)
78              throws SAXException, NetworkException, GTUException
79      {
80          for (Node node : XMLParser.getNodes(nodeList, "GTU"))
81          {
82              NamedNodeMap attributes = node.getAttributes();
83              GTUTag gtuTag = new GTUTag();
84  
85              Node name = attributes.getNamedItem("NAME");
86              if (name == null)
87                  throw new SAXException("GTU: missing attribute NAME");
88              gtuTag.name = name.getNodeValue().trim();
89              if (parser.gtuTags.keySet().contains(gtuTag.name))
90                  throw new SAXException("GTU: NAME " + gtuTag.name + " defined twice");
91  
92              Node gtuType = attributes.getNamedItem("GTUTYPE");
93              if (gtuType == null)
94                  throw new SAXException("GTU: missing attribute GTUTYPE");
95              if (!parser.gtuTypes.containsKey(gtuType.getNodeValue().trim()))
96                  throw new SAXException("GTU: GTUTYPE " + gtuType.getNodeValue().trim() + " not defined");
97              gtuTag.gtuType = parser.gtuTypes.get(gtuType.getNodeValue().trim());
98  
99              Node length = attributes.getNamedItem("LENGTH");
100             if (length == null)
101                 throw new SAXException("GTU: missing attribute LENGTH");
102             gtuTag.lengthDist = Distributions.parseLengthDist(length.getNodeValue());
103 
104             Node width = attributes.getNamedItem("WIDTH");
105             if (width == null)
106                 throw new SAXException("GTU: missing attribute WIDTH");
107             gtuTag.widthDist = Distributions.parseLengthDist(width.getNodeValue());
108 
109             Node maxSpeed = attributes.getNamedItem("MAXSPEED");
110             if (maxSpeed == null)
111                 throw new SAXException("GTU: missing attribute MAXSPEED");
112             gtuTag.maxSpeedDist = Distributions.parseSpeedDist(maxSpeed.getNodeValue());
113 
114             parser.gtuTags.put(gtuTag.name, gtuTag);
115         }
116     }
117 
118     /** {@inheritDoc} */
119     @Override
120     public final String toString()
121     {
122         return "GTUTag [name=" + this.name + ", gtuType=" + this.gtuType + ", lengthDist=" + this.lengthDist + ", widthDist="
123                 + this.widthDist + ", followingModel=" + ", maxSpeedDist=" + this.maxSpeedDist + "]";
124     }
125 
126 }