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 javax.naming.NamingException;
8   
9   import org.djunits.value.vdouble.scalar.Direction;
10  import org.opentrafficsim.core.geometry.OTSPoint3D;
11  import org.opentrafficsim.core.network.NetworkException;
12  import org.opentrafficsim.core.network.OTSNode;
13  import org.opentrafficsim.core.network.factory.xml.units.AngleUnits;
14  import org.opentrafficsim.core.network.factory.xml.units.Coordinates;
15  import org.w3c.dom.NamedNodeMap;
16  import org.w3c.dom.Node;
17  import org.w3c.dom.NodeList;
18  import org.xml.sax.SAXException;
19  
20  /**
21   * <p>
22   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
24   * <p>
25   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
26   * initial version Jul 23, 2015 <br>
27   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
28   */
29  class NodeTag implements Serializable
30  {
31      /** */
32      private static final long serialVersionUID = 20150723L;
33  
34      /** Name. */
35      @SuppressWarnings("checkstyle:visibilitymodifier")
36      String name = null;
37  
38      /** Coordinate (null at first, can be calculated later when connected to a link. */
39      @SuppressWarnings("checkstyle:visibilitymodifier")
40      OTSPoint3D coordinate = null;
41  
42      /** Default direction of the node. 0 is "East", pi/2 = "North". */
43      @SuppressWarnings("checkstyle:visibilitymodifier")
44      Direction direction = null;
45  
46      /** The calculated Node, either through a coordinate or after calculation. */
47      @SuppressWarnings("checkstyle:visibilitymodifier")
48      OTSNode node = null;
49  
50      /**
51       * @param nodeList NodeList; nodeList the top-level nodes of the XML-file
52       * @param parser XmlNetworkLaneParserOld; the parser with the lists of information
53       * @throws SAXException when parsing of GTU tag fails
54       * @throws NetworkException when parsing of GTU tag fails
55       */
56      @SuppressWarnings("checkstyle:needbraces")
57      static void parseNodes(final NodeList nodeList, final XmlNetworkLaneParserOld parser) throws SAXException, NetworkException
58      {
59          for (Node node : XMLParser.getNodes(nodeList, "NODE"))
60          {
61              NamedNodeMap attributes = node.getAttributes();
62              NodeTag nodeTag = new NodeTag();
63  
64              Node name = attributes.getNamedItem("NAME");
65              if (name == null)
66                  throw new SAXException("NODE: missing attribute NAME");
67              nodeTag.name = name.getNodeValue().trim();
68              if (parser.nodeTags.keySet().contains(nodeTag.name))
69                  throw new SAXException("NODE: NAME " + nodeTag.name + " defined twice");
70  
71              if (attributes.getNamedItem("COORDINATE") != null)
72                  nodeTag.coordinate = Coordinates.parseCoordinate(attributes.getNamedItem("COORDINATE").getNodeValue());
73              else
74                  throw new SAXException("NODE: missing attribute COORDINATE");
75  
76              if (attributes.getNamedItem("DIRECTION") != null)
77                  nodeTag.direction = AngleUnits.parseDirection(attributes.getNamedItem("DIRECTION").getNodeValue());
78  
79              parser.nodeTags.put(nodeTag.name, nodeTag);
80  
81              try
82              {
83                  makeOTSNode(nodeTag, parser);
84              }
85              catch (NamingException exception)
86              {
87                  throw new NetworkException(exception);
88              }
89          }
90      }
91  
92      /**
93       * Parse a list of Nodes, e.g. for a ROUTE.
94       * @param nodeNames String; the space separated String with the node names
95       * @param parser XmlNetworkLaneParserOld; the parser with the lists of information
96       * @return a list of NodeTags
97       * @throws SAXException when parsing of the tag fails
98       * @throws NetworkException when parsing of the tag fails
99       */
100     static List<NodeTag> parseNodeList(final String nodeNames, final XmlNetworkLaneParserOld parser)
101             throws SAXException, NetworkException
102     {
103         List<NodeTag> nodeList = new ArrayList<>();
104         String[] ns = nodeNames.split("\\s");
105         for (String s : ns)
106         {
107             if (!parser.nodeTags.containsKey(s))
108             {
109                 throw new SAXException("Node " + s + " from node list [" + nodeNames + "] was not defined");
110             }
111             nodeList.add(parser.nodeTags.get(s));
112         }
113         return nodeList;
114 
115     }
116 
117     /**
118      * @param nodeTag NodeTag; the tag with the info for the node.
119      * @param parser XmlNetworkLaneParserOld; the parser with the lists of information
120      * @return a constructed node
121      * @throws NetworkException when point cannot be instantiated
122      * @throws NamingException when animation context cannot be found.
123      */
124     static OTSNode makeOTSNode(final NodeTag nodeTag, final XmlNetworkLaneParserOld parser)
125             throws NetworkException, NamingException
126     {
127         String id = nodeTag.name;
128         OTSNode node = new OTSNode(parser.network, id, nodeTag.coordinate);
129         nodeTag.node = node;
130         return node;
131     }
132 
133     /** {@inheritDoc} */
134     @Override
135     public String toString()
136     {
137         return "NodeTag [name=" + this.name + ", coordinate=" + this.coordinate + ", node=" + this.node + "]";
138     }
139 
140 }