View Javadoc
1   package org.opentrafficsim.road.network.factory.opendrive;
2   
3   import java.io.Serializable;
4   
5   import org.opentrafficsim.core.network.NetworkException;
6   import org.opentrafficsim.road.network.factory.opendrive.LinkTag.ContactPointEnum;
7   import org.w3c.dom.NamedNodeMap;
8   import org.w3c.dom.Node;
9   import org.xml.sax.SAXException;
10  
11  /**
12   * <p>
13   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
14   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
15   * <p>
16   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
17   * initial version Jul 23, 2015 <br>
18   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19   */
20  class ConnectionTag implements Serializable
21  {
22  
23      /** */
24      private static final long serialVersionUID = 20150723L;
25  
26      /** Id of the lane. */
27      @SuppressWarnings("checkstyle:visibilitymodifier")
28      String id = null;
29  
30      /** Incoming Road id */
31      @SuppressWarnings("checkstyle:visibilitymodifier")
32      String incomingRoad = null;
33  
34      /** Connecting Road id */
35      @SuppressWarnings("checkstyle:visibilitymodifier")
36      String connectingRoad = null;
37  
38      /** Contact point. */
39      @SuppressWarnings("checkstyle:visibilitymodifier")
40      ContactPointEnum contactPoint = null;
41  
42      /** Lane Link From. */
43      @SuppressWarnings("checkstyle:visibilitymodifier")
44      Integer laneLinkFrom = null;
45  
46      /** Lane Link To. */
47      @SuppressWarnings("checkstyle:visibilitymodifier")
48      Integer laneLinkTo = null;
49  
50      /**
51       * Parse the attributes of the road tag. The sub-elements are parsed in separate classes.
52       * @param node Node; the top-level road node
53       * @param parser OpenDriveNetworkLaneParser; the parser with the lists of information
54       * @return the generated RoadTag for further reference
55       * @throws SAXException when parsing of the tag fails
56       * @throws NetworkException when parsing of the tag fails
57       */
58      @SuppressWarnings("checkstyle:needbraces")
59      static ConnectionTag parseConnection(final Node node, final OpenDriveNetworkLaneParser parser)
60              throws SAXException, NetworkException
61      {
62          NamedNodeMap attributes = node.getAttributes();
63          ConnectionTag connectionTag = new ConnectionTag();
64  
65          Node id = attributes.getNamedItem("id");
66          if (id == null)
67              throw new SAXException("LANE: missing attribute id");
68          connectionTag.id = id.getNodeValue().trim();
69  
70          Node incomingRoad = attributes.getNamedItem("incomingRoad");
71          if (incomingRoad == null)
72              throw new SAXException("LANE: missing attribute incomingRoad");
73          connectionTag.incomingRoad = incomingRoad.getNodeValue().trim();
74  
75          Node connectingRoad = attributes.getNamedItem("connectingRoad");
76          if (connectingRoad == null)
77              throw new SAXException("LANE: missing attribute connectingRoad");
78          connectionTag.connectingRoad = connectingRoad.getNodeValue().trim();
79  
80          Node contactPoint = attributes.getNamedItem("contactPoint");
81          if (contactPoint == null)
82              throw new SAXException("LANE: missing attribute contactPoint");
83          else
84          {
85              if ("start".equals(contactPoint.getNodeValue().trim()))
86                  connectionTag.contactPoint = ContactPointEnum.START;
87              else if ("end".equals(contactPoint.getNodeValue().trim()))
88                  connectionTag.contactPoint = ContactPointEnum.END;
89              else
90                  throw new SAXException("contactPoint is neither 'start' nor 'end' but: " + contactPoint.getNodeValue().trim());
91          }
92  
93          for (Node laneLink : XMLParser.getNodes(node.getChildNodes(), "laneLink"))
94          {
95              NamedNodeMap attributes1 = laneLink.getAttributes();
96  
97              Node from = attributes1.getNamedItem("from");
98              connectionTag.laneLinkFrom = Integer.parseInt(from.getNodeValue().trim());
99  
100             Node to = attributes1.getNamedItem("to");
101             connectionTag.laneLinkTo = Integer.parseInt(to.getNodeValue().trim());
102         }
103 
104         return connectionTag;
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     public final String toString()
110     {
111         return "ConnectionTag [id=" + this.id + ", incomingRoad=" + this.incomingRoad + ", connectingRoad="
112                 + this.connectingRoad + ", contactPoint=" + this.contactPoint + ", laneLinkFrom=" + this.laneLinkFrom
113                 + ", laneLinkTo=" + this.laneLinkTo + "]";
114     }
115 }