View Javadoc
1   package org.opentrafficsim.road.network.factory.xml;
2   
3   import static org.opentrafficsim.core.gtu.GTUType.VEHICLE;
4   
5   import java.io.Serializable;
6   
7   import org.opentrafficsim.core.gtu.GTUException;
8   import org.opentrafficsim.core.gtu.GTUType;
9   import org.opentrafficsim.core.network.NetworkException;
10  import org.w3c.dom.NamedNodeMap;
11  import org.w3c.dom.Node;
12  import org.w3c.dom.NodeList;
13  import org.xml.sax.SAXException;
14  
15  /**
16   * The XML tag for the GTUType.
17   * 
18   * <pre>
19   * {@code
20    <xsd:element name="GTUTYPE">
21      <xsd:complexType>
22        <xsd:attribute name="NAME" type="xsd:string" use="required" />
23        <xsd:attribute ref="xml:base" />
24      </xsd:complexType>
25    </xsd:element>
26   * }
27   * </pre>
28   * <p>
29   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
30   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
31   * <p>
32   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
33   * initial version Jul 23, 2015 <br>
34   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
35   */
36  class GTUTypeTag implements Serializable
37  {
38      /** */
39      private static final long serialVersionUID = 20160925L;
40  
41      /** Name. */
42      @SuppressWarnings("checkstyle:visibilitymodifier")
43      String name = null;
44  
45      /**
46       * @param nodeList nodeList the top-level nodes of the XML-file
47       * @param parser the parser with the lists of information
48       * @throws SAXException when parsing of GTU tag fails
49       * @throws NetworkException when parsing of GTU tag fails
50       * @throws GTUException if GTUType defined twice
51       */
52      @SuppressWarnings("checkstyle:needbraces")
53      static void parseGTUTypes(final NodeList nodeList, final XmlNetworkLaneParser parser)
54              throws SAXException, NetworkException, GTUException
55      {
56          for (Node node : XMLParser.getNodes(nodeList, "GTUTYPE"))
57          {
58              NamedNodeMap attributes = node.getAttributes();
59              GTUTypeTag gtuTypeTag = new GTUTypeTag();
60  
61              Node name = attributes.getNamedItem("NAME");
62              if (name == null)
63                  throw new SAXException("GTUTYPE: missing attribute NAME");
64              gtuTypeTag.name = name.getNodeValue().trim();
65              if (parser.gtuTypes.keySet().contains(gtuTypeTag.name))
66                  throw new SAXException("GTUTYPE: NAME " + gtuTypeTag.name + " defined twice");
67              // TODO super type (car, bus, truck) in xml definition
68              GTUType gtuType = new GTUType(gtuTypeTag.name, VEHICLE);
69              parser.gtuTypes.put(gtuTypeTag.name, gtuType);
70          }
71      }
72  
73      /** {@inheritDoc} */
74      @Override
75      public final String toString()
76      {
77          return "GTUTypeTag [name=" + this.name + "]";
78      }
79  
80  }