Eclipse 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-2022 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // Encapsulated xml-attributes that use a map from string-attr-names to string-attr-values as backend
19 /****************************************************************************/
20 #include <config.h>
21 
22 #include <cassert>
23 #include <xercesc/sax2/Attributes.hpp>
24 #include <xercesc/sax2/DefaultHandler.hpp>
25 #include <xercesc/util/XercesVersion.hpp>
26 #include <xercesc/util/TransService.hpp>
27 #include <xercesc/util/TranscodingException.hpp>
28 #include <utils/common/RGBColor.h>
32 #include <utils/geom/Boundary.h>
36 
37 
38 // ===========================================================================
39 // class definitions
40 // ===========================================================================
42  const std::map<std::string, std::string>& attrs,
43  const std::vector<std::string>& predefinedTagsMML,
44  const std::string& objectType) :
45  SUMOSAXAttributes(objectType),
46  myAttrs(attrs),
47  myPredefinedTagsMML(predefinedTagsMML) { }
48 
49 
51 
52 
53 bool
55  assert(id >= 0);
56  assert(id < (int)myPredefinedTagsMML.size());
57  return myAttrs.find(myPredefinedTagsMML[id]) != myAttrs.end();
58 }
59 
60 
61 bool
64 }
65 
66 
67 int
70 }
71 
72 
73 long long int
76 }
77 
78 
79 std::string
81  return getAttributeValueSecure(id);
82 }
83 
84 
85 std::string
86 SUMOSAXAttributesImpl_Cached::getStringSecure(int id, const std::string& str) const {
87  const std::string& result = getAttributeValueSecure(id);
88  return result.size() == 0 ? str : result;
89 }
90 
91 
92 double
95 }
96 
97 
98 const std::string&
100  assert(id >= 0);
101  assert(id < (int)myPredefinedTagsMML.size());
102  return myAttrs.find(myPredefinedTagsMML[id])->second;
103 }
104 
105 
106 double
107 SUMOSAXAttributesImpl_Cached::getFloat(const std::string& id) const {
108  return StringUtils::toDouble(myAttrs.find(id)->second);
109 }
110 
111 
112 bool
113 SUMOSAXAttributesImpl_Cached::hasAttribute(const std::string& id) const {
114  return myAttrs.find(id) != myAttrs.end();
115 }
116 
117 
118 std::string
120  const std::string& str) const {
121  std::map<std::string, std::string>::const_iterator it = myAttrs.find(id);
122  if (it != myAttrs.end() && it->second != "") {
123  return it->second;
124  } else {
125  return str;
126  }
127 }
128 
129 
133  std::string funcString = getString(SUMO_ATTR_FUNCTION);
134  if (SUMOXMLDefinitions::EdgeFunctions.hasString(funcString)) {
135  return SUMOXMLDefinitions::EdgeFunctions.get(funcString);
136  }
137  ok = false;
138  }
140 }
141 
142 
146  std::string typeString = getString(SUMO_ATTR_TYPE);
147  if (SUMOXMLDefinitions::NodeTypes.hasString(typeString)) {
148  return SUMOXMLDefinitions::NodeTypes.get(typeString);
149  }
150  ok = false;
151  }
153 }
154 
155 
159  std::string rowString = getString(SUMO_ATTR_RIGHT_OF_WAY);
160  if (SUMOXMLDefinitions::RightOfWayValues.hasString(rowString)) {
161  return SUMOXMLDefinitions::RightOfWayValues.get(rowString);
162  }
163  ok = false;
164  }
165  return RightOfWay::DEFAULT;
166 }
167 
171  std::string fringeString = getString(SUMO_ATTR_FRINGE);
172  if (SUMOXMLDefinitions::FringeTypeValues.hasString(fringeString)) {
173  return SUMOXMLDefinitions::FringeTypeValues.get(fringeString);
174  }
175  ok = false;
176  }
177  return FringeType::DEFAULT;
178 }
179 
180 RGBColor
183 }
184 
185 
186 Position
188  // declare string tokenizer
189  StringTokenizer st(getString(attr));
190  // check StringTokenizer
191  while (st.hasNext()) {
192  // obtain position
193  StringTokenizer pos(st.next(), ",");
194  // check that position has X-Y or X-Y-Z
195  if ((pos.size() != 2) && (pos.size() != 3)) {
196  throw FormatException("position format");
197  }
198  // obtain x and y
199  double x = StringUtils::toDouble(pos.next());
200  double y = StringUtils::toDouble(pos.next());
201  // check if return a X-Y or a X-Y-Z Position
202  if (pos.size() == 2) {
203  return Position(x, y);
204  } else {
205  // obtain z
206  double z = StringUtils::toDouble(pos.next());
207  return Position(x, y, z);
208  }
209  }
210  // empty positions aren't allowed
211  throw FormatException("position format");
212 }
213 
214 
217  StringTokenizer st(getString(attr));
218  PositionVector shape;
219  while (st.hasNext()) {
220  StringTokenizer pos(st.next(), ",");
221  if (pos.size() != 2 && pos.size() != 3) {
222  throw FormatException("shape format");
223  }
224  double x = StringUtils::toDouble(pos.next());
225  double y = StringUtils::toDouble(pos.next());
226  if (pos.size() == 2) {
227  shape.push_back(Position(x, y));
228  } else {
229  double z = StringUtils::toDouble(pos.next());
230  shape.push_back(Position(x, y, z));
231  }
232  }
233  return shape;
234 }
235 
236 
237 Boundary
239  std::string def = getString(attr);
240  StringTokenizer st(def, ",");
241  if (st.size() != 4) {
242  throw FormatException("boundary format");
243  }
244  const double xmin = StringUtils::toDouble(st.next());
245  const double ymin = StringUtils::toDouble(st.next());
246  const double xmax = StringUtils::toDouble(st.next());
247  const double ymax = StringUtils::toDouble(st.next());
248  return Boundary(xmin, ymin, xmax, ymax);
249 }
250 
251 
252 std::string
254  assert(attr >= 0);
255  assert(attr < (int)myPredefinedTagsMML.size());
256  return myPredefinedTagsMML[attr];
257 }
258 
259 
260 void
262  for (std::map<std::string, std::string>::const_iterator it = myAttrs.begin(); it != myAttrs.end(); ++it) {
263  os << " " << it->first;
264  os << "=\"" << it->second << "\"";
265  }
266 }
267 
268 std::vector<std::string>
270  std::vector<std::string> result;
271  for (std::map<std::string, std::string>::const_iterator it = myAttrs.begin(); it != myAttrs.end(); ++it) {
272  result.push_back(it->first);
273  }
274  return result;
275 }
276 
280 }
281 
282 
283 /****************************************************************************/
FringeType
classifying boundary nodes
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
RightOfWay
algorithms for computing right of way
@ SUMO_ATTR_FRINGE
Fringe type of node.
@ SUMO_ATTR_TYPE
@ SUMO_ATTR_COLOR
A color information.
@ SUMO_ATTR_RIGHT_OF_WAY
How to compute right of way.
@ SUMO_ATTR_FUNCTION
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:39
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
A list of positions.
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:236
Encapsulated SAX-Attributes.
const std::string & getObjectType() const
return the objecttype to which these attributes belong
Position getPosition(int attr) const
Tries to read given attribute assuming it is a Position.
std::string getString(int id) const
Returns the string-value of the named (by its enum-value) attribute.
std::string getName(int attr) const
Converts the given attribute id into a man readable string.
RightOfWay getRightOfWay(bool &ok) const
returns rightOfWay method
PositionVector getShape(int attr) const
Tries to read given attribute assuming it is a PositionVector.
SumoXMLEdgeFunc getEdgeFunc(bool &ok) const
Returns the value of the named attribute.
bool getBool(int id) const
Returns the bool-value of the named (by its enum-value) attribute.
long long int getLong(int id) const
Returns the long-value of the named (by its enum-value) attribute.
FringeType getFringeType(bool &ok) const
returns fringe type
Boundary getBoundary(int attr) const
Tries to read given attribute assuming it is a Boundary.
std::string getStringSecure(int id, const std::string &def) const
Returns the string-value of the named (by its enum-value) attribute.
std::map< std::string, std::string > myAttrs
The encapsulated attributes.
SUMOSAXAttributesImpl_Cached(const std::map< std::string, std::string > &attrs, const std::vector< std::string > &predefinedTagsMML, const std::string &objectType)
Constructor.
SumoXMLNodeType getNodeType(bool &ok) const
Returns the value of the named attribute.
const std::string & getAttributeValueSecure(int id) const
Returns Xerces-value of the named attribute.
int getInt(int id) const
Returns the int-value of the named (by its enum-value) attribute.
void serialize(std::ostream &os) const
Prints all attribute names and values into the given stream.
std::vector< std::string > getAttributeNames() const
Retrieves all attribute names.
RGBColor getColor() const
Returns the value of the named attribute.
SUMOSAXAttributes * clone() const
return a new deep-copy attributes object
const std::vector< std::string > & myPredefinedTagsMML
Map of attribute ids to their (readable) string-representation.
double getFloat(int id) const
Returns the double-value of the named (by its enum-value) attribute.
bool hasAttribute(int id) const
Returns the information whether the named (by its enum-value) attribute is within the current list.
static StringBijection< SumoXMLNodeType > NodeTypes
node types
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
static StringBijection< RightOfWay > RightOfWayValues
righ of way algorithms
static StringBijection< FringeType > FringeTypeValues
fringe types
T get(const std::string &str) const
int size() const
returns the number of existing substrings
bool hasNext()
returns the information whether further substrings exist
std::string next()
returns the next substring when it exists. Otherwise the behaviour is undefined
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,...
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter