SUMO - Simulation of Urban MObility
SUMOSAXAttributesImpl_Cached.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // Encapsulated xml-attributes that use a map from string-attr-names to string-attr-values as backend
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <cassert>
25 #include <xercesc/sax2/Attributes.hpp>
26 #include <xercesc/sax2/DefaultHandler.hpp>
27 #include <xercesc/util/XercesVersion.hpp>
28 #include <xercesc/util/TransService.hpp>
29 #include <xercesc/util/TranscodingException.hpp>
30 #include <utils/common/RGBColor.h>
34 #include <utils/geom/Boundary.h>
38 
39 
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
44  const std::map<std::string, std::string>& attrs,
45  const std::map<int, std::string>& predefinedTagsMML,
46  const std::string& objectType) :
47  SUMOSAXAttributes(objectType),
48  myAttrs(attrs),
49  myPredefinedTagsMML(predefinedTagsMML) { }
50 
51 
53 }
54 
55 
56 bool
58  std::map<int, std::string>::const_iterator i = myPredefinedTagsMML.find(id);
59  if (i == myPredefinedTagsMML.end()) {
60  return false;
61  }
62  return myAttrs.find((*i).second) != myAttrs.end();
63 }
64 
65 
66 bool
69 }
70 
71 
72 int
75 }
76 
77 
78 long long int
81 }
82 
83 
84 std::string
86  return getAttributeValueSecure(id);
87 }
88 
89 
90 std::string
91 SUMOSAXAttributesImpl_Cached::getStringSecure(int id, const std::string& str) const {
92  const std::string& result = getAttributeValueSecure(id);
93  return result.size() == 0 ? str : result;
94 }
95 
96 
97 double
100 }
101 
102 
103 const std::string&
105  std::map<int, std::string>::const_iterator i = myPredefinedTagsMML.find(id);
106  assert(i != myPredefinedTagsMML.end());
107  return myAttrs.find(i->second)->second;
108 }
109 
110 
111 double
112 SUMOSAXAttributesImpl_Cached::getFloat(const std::string& id) const {
113  return StringUtils::toDouble(myAttrs.find(id)->second);
114 }
115 
116 
117 bool
118 SUMOSAXAttributesImpl_Cached::hasAttribute(const std::string& id) const {
119  return myAttrs.find(id) != myAttrs.end();
120 }
121 
122 
123 std::string
125  const std::string& str) const {
126  std::map<std::string, std::string>::const_iterator it = myAttrs.find(id);
127  if (it != myAttrs.end() && it->second != "") {
128  return it->second;
129  } else {
130  return str;
131  }
132 }
133 
134 
138  std::string funcString = getString(SUMO_ATTR_FUNCTION);
139  if (SUMOXMLDefinitions::EdgeFunctions.hasString(funcString)) {
140  return SUMOXMLDefinitions::EdgeFunctions.get(funcString);
141  }
142  ok = false;
143  }
144  return EDGEFUNC_NORMAL;
145 }
146 
147 
151  std::string typeString = getString(SUMO_ATTR_TYPE);
152  if (SUMOXMLDefinitions::NodeTypes.hasString(typeString)) {
153  return SUMOXMLDefinitions::NodeTypes.get(typeString);
154  }
155  ok = false;
156  }
157  return NODETYPE_UNKNOWN;
158 }
159 
160 
164  std::string rowString = getString(SUMO_ATTR_RIGHT_OF_WAY);
165  if (SUMOXMLDefinitions::RightOfWayValues.hasString(rowString)) {
166  return SUMOXMLDefinitions::RightOfWayValues.get(rowString);
167  }
168  ok = false;
169  }
170  return RIGHT_OF_WAY_DEFAULT;
171 }
172 
173 
174 RGBColor
177 }
178 
179 
182  StringTokenizer st(getString(attr));
183  PositionVector shape;
184  while (st.hasNext()) {
185  StringTokenizer pos(st.next(), ",");
186  if (pos.size() != 2 && pos.size() != 3) {
187  throw FormatException("shape format");
188  }
189  double x = StringUtils::toDouble(pos.next());
190  double y = StringUtils::toDouble(pos.next());
191  if (pos.size() == 2) {
192  shape.push_back(Position(x, y));
193  } else {
194  double z = StringUtils::toDouble(pos.next());
195  shape.push_back(Position(x, y, z));
196  }
197  }
198  return shape;
199 }
200 
201 
202 Boundary
204  std::string def = getString(attr);
205  StringTokenizer st(def, ",");
206  if (st.size() != 4) {
207  throw FormatException("boundary format");
208  }
209  const double xmin = StringUtils::toDouble(st.next());
210  const double ymin = StringUtils::toDouble(st.next());
211  const double xmax = StringUtils::toDouble(st.next());
212  const double ymax = StringUtils::toDouble(st.next());
213  return Boundary(xmin, ymin, xmax, ymax);
214 }
215 
216 
217 std::vector<std::string>
219  std::string def = getString(attr);
220  std::vector<std::string> ret;
221  parseStringVector(def, ret);
222  return ret;
223 }
224 
225 
226 std::string
228  if (myPredefinedTagsMML.find(attr) == myPredefinedTagsMML.end()) {
229  return "?";
230  }
231  return myPredefinedTagsMML.find(attr)->second;
232 }
233 
234 
235 void
237  for (std::map<std::string, std::string>::const_iterator it = myAttrs.begin(); it != myAttrs.end(); ++it) {
238  os << " " << it->first;
239  os << "=\"" << it->second << "\"";
240  }
241 }
242 
243 
247 }
248 
249 /****************************************************************************/
250 
static StringBijection< RightOfWay > RightOfWayValues
lane spread functions
SumoXMLNodeType getNodeType(bool &ok) const
Returns the value of the named attribute.
static StringBijection< SumoXMLNodeType > NodeTypes
node types
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:177
std::string next()
std::vector< std::string > getStringVector(int attr) const
Tries to read given attribute assuming it is a string vector.
const std::string & getObjectType() const
return the objecttype to which these attributes belong
void serialize(std::ostream &os) const
Prints all attribute names and values into the given stream.
long long int getLong(int id) const
Returns the long-value of the named (by its enum-value) attribute.
RightOfWay
algorithms for computing right of way
Boundary getBoundary(int attr) const
Tries to read given attribute assuming it is a Boundary.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter ...
RGBColor getColor() const
Returns the value of the named attribute.
const std::string & getAttributeValueSecure(int id) const
Returns Xerces-value of the named attribute.
PositionVector getShape(int attr) const
Tries to read given attribute assuming it is a PositionVector.
std::string getName(int attr) const
Converts the given attribute id into a man readable string.
RightOfWay getRightOfWay(bool &ok) const
returns rightOfWay method
const std::map< int, std::string > & myPredefinedTagsMML
Map of attribute ids to their (readable) string-representation.
static void parseStringVector(const std::string &def, std::vector< std::string > &into)
Splits the given string.
How to compute right of way.
Encapsulated SAX-Attributes.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter ...
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
A list of positions.
int getInt(int id) const
Returns the int-value of the named (by its enum-value) attribute.
T get(const std::string &str) const
double getFloat(int id) const
Returns the double-value of the named (by its enum-value) attribute.
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter...
virtual ~SUMOSAXAttributesImpl_Cached()
Destructor.
std::string getStringSecure(int id, const std::string &def) const
Returns the string-value of the named (by its enum-value) attribute.
std::string getString(int id) const
Returns the string-value of the named (by its enum-value) attribute.
bool getBool(int id) const
Returns the bool-value of the named (by its enum-value) attribute.
SUMOSAXAttributes * clone() const
return a new deep-copy attributes object
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
static long long int toLong(const std::string &sData)
converts a string into the long value described by it by calling the char-type converter, which
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
SumoXMLEdgeFunc getEdgeFunc(bool &ok) const
Returns the value of the named attribute.
SUMOSAXAttributesImpl_Cached(const std::map< std::string, std::string > &attrs, const std::map< int, std::string > &predefinedTagsMML, const std::string &objectType)
Constructor.
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
std::map< std::string, std::string > myAttrs
The encapsulated attributes.
A color information.
bool hasAttribute(int id) const
Returns the information whether the named (by its enum-value) attribute is within the current list...