View Javadoc
1   package org.opentrafficsim.road.network.factory.xml.old;
2   
3   import java.io.Serializable;
4   import java.net.URI;
5   import java.net.URISyntaxException;
6   
7   import org.djunits.unit.SpeedUnit;
8   import org.djunits.value.vdouble.scalar.Speed;
9   import org.opentrafficsim.core.network.NetworkException;
10  import org.opentrafficsim.core.network.factory.xml.units.Distributions;
11  import org.opentrafficsim.core.units.distributions.ContinuousDistDoubleScalar;
12  import org.opentrafficsim.road.network.factory.xml.old.CrossSectionElementTag.ElementType;
13  import org.w3c.dom.NamedNodeMap;
14  import org.w3c.dom.Node;
15  import org.xml.sax.SAXException;
16  
17  /**
18   * <p>
19   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
20   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
21   * <p>
22   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
23   * initial version Jul 23, 2015 <br>
24   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
25   */
26  class ListGeneratorTag implements Serializable
27  {
28      /** */
29      private static final long serialVersionUID = 20150723L;
30  
31      /** URI of the list. */
32      @SuppressWarnings("checkstyle:visibilitymodifier")
33      URI uri = null;
34  
35      /** Position of the sink on the link, relative to the design line, stored as a string to parse when the length is known. */
36      @SuppressWarnings("checkstyle:visibilitymodifier")
37      String positionStr = null;
38  
39      /** GTU tag. */
40      @SuppressWarnings("checkstyle:visibilitymodifier")
41      GTUTag gtuTag = null;
42  
43      /** GTU mix tag. */
44      @SuppressWarnings("checkstyle:visibilitymodifier")
45      GTUMixTag gtuMixTag = null;
46  
47      /** Initial speed. */
48      @SuppressWarnings("checkstyle:visibilitymodifier")
49      ContinuousDistDoubleScalar.Rel<Speed, SpeedUnit> initialSpeedDist = null;
50  
51      /** GTU colorer. */
52      @SuppressWarnings("checkstyle:visibilitymodifier")
53      String gtuColorer;
54  
55      /**
56       * Parse the LISTGENERATOR tag.
57       * @param node Node; the LISTGENERATOR node to parse
58       * @param parser XmlNetworkLaneParserOld; the parser with the lists of information
59       * @param linkTag LinkTag; the parent LINK tag
60       * @throws SAXException when parsing of the tag fails
61       * @throws NetworkException when parsing of the tag fails
62       */
63      @SuppressWarnings("checkstyle:needbraces")
64      static void parseListGenerator(final Node node, final XmlNetworkLaneParserOld parser, final LinkTag linkTag)
65              throws SAXException, NetworkException
66      {
67          NamedNodeMap attributes = node.getAttributes();
68          ListGeneratorTag listGeneratorTag = new ListGeneratorTag();
69  
70          if (attributes.getNamedItem("URI") == null)
71              throw new SAXException("LISTGENERATOR: missing attribute URI" + " for link " + linkTag.name);
72          String uriStr = attributes.getNamedItem("URI").getNodeValue().trim();
73          try
74          {
75              listGeneratorTag.uri = new URI(uriStr);
76          }
77          catch (URISyntaxException exception)
78          {
79              throw new NetworkException("LISTGENERATOR: URI " + uriStr + " is not valid", exception);
80          }
81  
82          if (attributes.getNamedItem("LANE") == null)
83              throw new SAXException("LISTGENERATOR: missing attribute LANE" + " for link " + linkTag.name);
84          String laneName = attributes.getNamedItem("LANE").getNodeValue().trim();
85          if (linkTag.roadLayoutTag == null)
86              throw new NetworkException("LISTGENERATOR: LANE " + laneName + " no ROADTYPE for link " + linkTag.name);
87          CrossSectionElementTag cseTag = linkTag.roadLayoutTag.cseTags.get(laneName);
88          if (cseTag == null)
89              throw new NetworkException("LISTGENERATOR: LANE " + laneName + " not found in elements of link " + linkTag.name
90                      + " - roadtype " + linkTag.roadLayoutTag.name);
91          if (cseTag.elementType != ElementType.LANE)
92              throw new NetworkException("LISTGENERATOR: LANE " + laneName + " not a real GTU lane for link " + linkTag.name
93                      + " - roadtype " + linkTag.roadLayoutTag.name);
94          if (linkTag.generatorTags.containsKey(laneName))
95              throw new SAXException("LISTGENERATOR for LANE with NAME " + laneName + " defined twice");
96  
97          Node position = attributes.getNamedItem("POSITION");
98          if (position == null)
99              throw new NetworkException("LISTGENERATOR: POSITION element not found in elements of link " + linkTag.name
100                     + " - roadtype " + linkTag.roadLayoutTag.name);
101         listGeneratorTag.positionStr = position.getNodeValue().trim();
102 
103         if (attributes.getNamedItem("GTU") != null)
104         {
105             String gtuName = attributes.getNamedItem("GTU").getNodeValue().trim();
106             if (!parser.gtuTags.containsKey(gtuName))
107                 throw new NetworkException(
108                         "LISTGENERATOR: LANE " + laneName + " GTU " + gtuName + " in link " + linkTag.name + " not defined");
109             listGeneratorTag.gtuTag = parser.gtuTags.get(gtuName);
110         }
111 
112         if (attributes.getNamedItem("GTUMIX") != null)
113         {
114             String gtuMixName = attributes.getNamedItem("GTUMIX").getNodeValue().trim();
115             if (!parser.gtuMixTags.containsKey(gtuMixName))
116                 throw new NetworkException("LISTGENERATOR: LANE " + laneName + " GTUMIX " + gtuMixName + " in link "
117                         + linkTag.name + " not defined");
118             listGeneratorTag.gtuMixTag = parser.gtuMixTags.get(gtuMixName);
119         }
120 
121         if (listGeneratorTag.gtuTag == null && listGeneratorTag.gtuMixTag == null)
122             throw new SAXException("LISTGENERATOR: missing attribute GTU or GTUMIX for Lane with NAME " + laneName + " of link "
123                     + linkTag.name);
124 
125         if (listGeneratorTag.gtuTag != null && listGeneratorTag.gtuMixTag != null)
126             throw new SAXException("LISTGENERATOR: both attribute GTU and GTUMIX defined for Lane with NAME " + laneName
127                     + " of link " + linkTag.name);
128 
129         Node initialSpeed = attributes.getNamedItem("INITIALSPEED");
130         if (initialSpeed == null)
131             throw new SAXException("LISTGENERATOR: missing attribute INITIALSPEED");
132         listGeneratorTag.initialSpeedDist = Distributions.parseSpeedDist(initialSpeed.getNodeValue());
133 
134         // TODO GTUColorer
135 
136         linkTag.listGeneratorTags.put(laneName, listGeneratorTag);
137     }
138 
139     /** {@inheritDoc} */
140     @Override
141     public final String toString()
142     {
143         return "ListGeneratorTag [uri=" + this.uri + ", positionStr=" + this.positionStr + ", gtuTag=" + this.gtuTag
144                 + ", gtuMixTag=" + this.gtuMixTag + ", initialSpeedDist=" + this.initialSpeedDist + ", gtuColorer="
145                 + this.gtuColorer + "]";
146     }
147 }