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-2020 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  const std::map<SumoXMLAttr, std::string>& attrs,
52  const std::vector<std::string>& predefinedTagsMML,
53  const std::string& objectType) :
54  SUMOSAXAttributes(objectType),
55  myPredefinedTagsMML(predefinedTagsMML) {
56  // parse <SumoXMLAttr, string> to <string, string>
57  for (const auto& i : attrs) {
58  myAttrs[toString(i.first)] = i.second;
59  }
60 }
61 
62 
64 
65 
66 bool
68  assert(id >= 0);
69  assert(id < (int)myPredefinedTagsMML.size());
70  return myAttrs.find(myPredefinedTagsMML[id]) != myAttrs.end();
71 }
72 
73 
74 bool
77 }
78 
79 
80 int
83 }
84 
85 
86 long long int
89 }
90 
91 
92 std::string
94  return getAttributeValueSecure(id);
95 }
96 
97 
98 std::string
99 SUMOSAXAttributesImpl_Cached::getStringSecure(int id, const std::string& str) const {
100  const std::string& result = getAttributeValueSecure(id);
101  return result.size() == 0 ? str : result;
102 }
103 
104 
105 double
108 }
109 
110 
111 const std::string&
113  assert(id >= 0);
114  assert(id < (int)myPredefinedTagsMML.size());
115  return myAttrs.find(myPredefinedTagsMML[id])->second;
116 }
117 
118 
119 double
120 SUMOSAXAttributesImpl_Cached::getFloat(const std::string& id) const {
121  return StringUtils::toDouble(myAttrs.find(id)->second);
122 }
123 
124 
125 bool
126 SUMOSAXAttributesImpl_Cached::hasAttribute(const std::string& id) const {
127  return myAttrs.find(id) != myAttrs.end();
128 }
129 
130 
131 std::string
133  const std::string& str) const {
134  std::map<std::string, std::string>::const_iterator it = myAttrs.find(id);
135  if (it != myAttrs.end() && it->second != "") {
136  return it->second;
137  } else {
138  return str;
139  }
140 }
141 
142 
146  std::string funcString = getString(SUMO_ATTR_FUNCTION);
147  if (SUMOXMLDefinitions::EdgeFunctions.hasString(funcString)) {
148  return SUMOXMLDefinitions::EdgeFunctions.get(funcString);
149  }
150  ok = false;
151  }
153 }
154 
155 
159  std::string typeString = getString(SUMO_ATTR_TYPE);
160  if (SUMOXMLDefinitions::NodeTypes.hasString(typeString)) {
161  return SUMOXMLDefinitions::NodeTypes.get(typeString);
162  }
163  ok = false;
164  }
166 }
167 
168 
172  std::string rowString = getString(SUMO_ATTR_RIGHT_OF_WAY);
173  if (SUMOXMLDefinitions::RightOfWayValues.hasString(rowString)) {
174  return SUMOXMLDefinitions::RightOfWayValues.get(rowString);
175  }
176  ok = false;
177  }
178  return RightOfWay::DEFAULT;
179 }
180 
184  std::string fringeString = getString(SUMO_ATTR_FRINGE);
185  if (SUMOXMLDefinitions::FringeTypeValues.hasString(fringeString)) {
186  return SUMOXMLDefinitions::FringeTypeValues.get(fringeString);
187  }
188  ok = false;
189  }
190  return FringeType::DEFAULT;
191 }
192 
193 RGBColor
196 }
197 
198 
201  StringTokenizer st(getString(attr));
202  PositionVector shape;
203  while (st.hasNext()) {
204  StringTokenizer pos(st.next(), ",");
205  if (pos.size() != 2 && pos.size() != 3) {
206  throw FormatException("shape format");
207  }
208  double x = StringUtils::toDouble(pos.next());
209  double y = StringUtils::toDouble(pos.next());
210  if (pos.size() == 2) {
211  shape.push_back(Position(x, y));
212  } else {
213  double z = StringUtils::toDouble(pos.next());
214  shape.push_back(Position(x, y, z));
215  }
216  }
217  return shape;
218 }
219 
220 
221 Boundary
223  std::string def = getString(attr);
224  StringTokenizer st(def, ",");
225  if (st.size() != 4) {
226  throw FormatException("boundary format");
227  }
228  const double xmin = StringUtils::toDouble(st.next());
229  const double ymin = StringUtils::toDouble(st.next());
230  const double xmax = StringUtils::toDouble(st.next());
231  const double ymax = StringUtils::toDouble(st.next());
232  return Boundary(xmin, ymin, xmax, ymax);
233 }
234 
235 
236 std::string
238  assert(attr >= 0);
239  assert(attr < (int)myPredefinedTagsMML.size());
240  return myPredefinedTagsMML[attr];
241 }
242 
243 
244 void
246  for (std::map<std::string, std::string>::const_iterator it = myAttrs.begin(); it != myAttrs.end(); ++it) {
247  os << " " << it->first;
248  os << "=\"" << it->second << "\"";
249  }
250 }
251 
252 std::vector<std::string>
254  std::vector<std::string> result;
255  for (std::map<std::string, std::string>::const_iterator it = myAttrs.begin(); it != myAttrs.end(); ++it) {
256  result.push_back(it->first);
257  }
258  return result;
259 }
260 
264 }
265 
266 
267 /****************************************************************************/
FringeType
algorithms for computing right of way
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
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
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:36
A list of positions.
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:168
Encapsulated SAX-Attributes.
const std::string & getObjectType() const
return the objecttype to which these attributes belong
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