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-2019 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  const std::map<SumoXMLAttr, std::string>& attrs,
54  const std::map<int, std::string>& predefinedTagsMML,
55  const std::string& objectType) :
56  SUMOSAXAttributes(objectType),
57  myPredefinedTagsMML(predefinedTagsMML) {
58  // parse <SumoXMLAttr, string> to <string, string>
59  for (const auto& i : attrs) {
60  myAttrs[toString(i.first)] = i.second;
61  }
62 }
63 
64 
66 
67 
68 bool
70  std::map<int, std::string>::const_iterator i = myPredefinedTagsMML.find(id);
71  if (i == myPredefinedTagsMML.end()) {
72  return false;
73  }
74  return myAttrs.find((*i).second) != myAttrs.end();
75 }
76 
77 
78 bool
81 }
82 
83 
84 int
87 }
88 
89 
90 long long int
93 }
94 
95 
96 std::string
98  return getAttributeValueSecure(id);
99 }
100 
101 
102 std::string
103 SUMOSAXAttributesImpl_Cached::getStringSecure(int id, const std::string& str) const {
104  const std::string& result = getAttributeValueSecure(id);
105  return result.size() == 0 ? str : result;
106 }
107 
108 
109 double
112 }
113 
114 
115 const std::string&
117  std::map<int, std::string>::const_iterator i = myPredefinedTagsMML.find(id);
118  assert(i != myPredefinedTagsMML.end());
119  return myAttrs.find(i->second)->second;
120 }
121 
122 
123 double
124 SUMOSAXAttributesImpl_Cached::getFloat(const std::string& id) const {
125  return StringUtils::toDouble(myAttrs.find(id)->second);
126 }
127 
128 
129 bool
130 SUMOSAXAttributesImpl_Cached::hasAttribute(const std::string& id) const {
131  return myAttrs.find(id) != myAttrs.end();
132 }
133 
134 
135 std::string
137  const std::string& str) const {
138  std::map<std::string, std::string>::const_iterator it = myAttrs.find(id);
139  if (it != myAttrs.end() && it->second != "") {
140  return it->second;
141  } else {
142  return str;
143  }
144 }
145 
146 
150  std::string funcString = getString(SUMO_ATTR_FUNCTION);
151  if (SUMOXMLDefinitions::EdgeFunctions.hasString(funcString)) {
152  return SUMOXMLDefinitions::EdgeFunctions.get(funcString);
153  }
154  ok = false;
155  }
156  return EDGEFUNC_NORMAL;
157 }
158 
159 
163  std::string typeString = getString(SUMO_ATTR_TYPE);
164  if (SUMOXMLDefinitions::NodeTypes.hasString(typeString)) {
165  return SUMOXMLDefinitions::NodeTypes.get(typeString);
166  }
167  ok = false;
168  }
169  return NODETYPE_UNKNOWN;
170 }
171 
172 
176  std::string rowString = getString(SUMO_ATTR_RIGHT_OF_WAY);
177  if (SUMOXMLDefinitions::RightOfWayValues.hasString(rowString)) {
178  return SUMOXMLDefinitions::RightOfWayValues.get(rowString);
179  }
180  ok = false;
181  }
182  return RIGHT_OF_WAY_DEFAULT;
183 }
184 
188  std::string fringeString = getString(SUMO_ATTR_FRINGE);
189  if (SUMOXMLDefinitions::FringeTypeValues.hasString(fringeString)) {
190  return SUMOXMLDefinitions::FringeTypeValues.get(fringeString);
191  }
192  ok = false;
193  }
194  return FRINGE_TYPE_DEFAULT;
195 }
196 
197 RGBColor
200 }
201 
202 
205  StringTokenizer st(getString(attr));
206  PositionVector shape;
207  while (st.hasNext()) {
208  StringTokenizer pos(st.next(), ",");
209  if (pos.size() != 2 && pos.size() != 3) {
210  throw FormatException("shape format");
211  }
212  double x = StringUtils::toDouble(pos.next());
213  double y = StringUtils::toDouble(pos.next());
214  if (pos.size() == 2) {
215  shape.push_back(Position(x, y));
216  } else {
217  double z = StringUtils::toDouble(pos.next());
218  shape.push_back(Position(x, y, z));
219  }
220  }
221  return shape;
222 }
223 
224 
225 Boundary
227  std::string def = getString(attr);
228  StringTokenizer st(def, ",");
229  if (st.size() != 4) {
230  throw FormatException("boundary format");
231  }
232  const double xmin = StringUtils::toDouble(st.next());
233  const double ymin = StringUtils::toDouble(st.next());
234  const double xmax = StringUtils::toDouble(st.next());
235  const double ymax = StringUtils::toDouble(st.next());
236  return Boundary(xmin, ymin, xmax, ymax);
237 }
238 
239 
240 std::string
242  if (myPredefinedTagsMML.find(attr) == myPredefinedTagsMML.end()) {
243  return "?";
244  }
245  return myPredefinedTagsMML.find(attr)->second;
246 }
247 
248 
249 void
251  for (std::map<std::string, std::string>::const_iterator it = myAttrs.begin(); it != myAttrs.end(); ++it) {
252  os << " " << it->first;
253  os << "=\"" << it->second << "\"";
254  }
255 }
256 
257 std::vector<std::string>
259  std::vector<std::string> result;
260  for (std::map<std::string, std::string>::const_iterator it = myAttrs.begin(); it != myAttrs.end(); ++it) {
261  result.push_back(it->first);
262  }
263  return result;
264 }
265 
269 }
270 
271 /****************************************************************************/
272 
SUMO_ATTR_TYPE
Definition: SUMOXMLDefinitions.h:382
Boundary.h
FringeType
FringeType
algorithms for computing right of way
Definition: SUMOXMLDefinitions.h:1105
SUMOSAXAttributesImpl_Cached::getFringeType
FringeType getFringeType(bool &ok) const
returns fringe type
Definition: SUMOSAXAttributesImpl_Cached.cpp:186
SUMOSAXAttributesImpl_Cached::getBoundary
Boundary getBoundary(int attr) const
Tries to read given attribute assuming it is a Boundary.
Definition: SUMOSAXAttributesImpl_Cached.cpp:226
StringUtils::toBool
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter
Definition: StringUtils.cpp:342
RIGHT_OF_WAY_DEFAULT
Definition: SUMOXMLDefinitions.h:1100
SUMOSAXAttributesImpl_Cached::getFloat
double getFloat(int id) const
Returns the double-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Cached.cpp:110
SUMOSAXAttributesImpl_Cached::getNodeType
SumoXMLNodeType getNodeType(bool &ok) const
Returns the value of the named attribute.
Definition: SUMOSAXAttributesImpl_Cached.cpp:161
StringBijection.h
SUMOSAXAttributesImpl_Cached::getStringSecure
std::string getStringSecure(int id, const std::string &def) const
Returns the string-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Cached.cpp:103
StringTokenizer::hasNext
bool hasNext()
returns the information whether further substrings exist
Definition: StringTokenizer.cpp:95
StringUtils::toDouble
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
Definition: StringUtils.cpp:313
SUMOSAXAttributesImpl_Cached::getString
std::string getString(int id) const
Returns the string-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Cached.cpp:97
SUMOSAXAttributesImpl_Cached::getBool
bool getBool(int id) const
Returns the bool-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Cached.cpp:79
SUMOXMLDefinitions::RightOfWayValues
static StringBijection< RightOfWay > RightOfWayValues
righ of way algorithms
Definition: SUMOXMLDefinitions.h:1372
SUMOSAXAttributesImpl_Cached::myAttrs
std::map< std::string, std::string > myAttrs
The encapsulated attributes.
Definition: SUMOSAXAttributesImpl_Cached.h:286
SUMOSAXAttributes::getObjectType
const std::string & getObjectType() const
return the objecttype to which these attributes belong
Definition: SUMOSAXAttributes.h:397
SumoXMLEdgeFunc
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
Definition: SUMOXMLDefinitions.h:1074
SUMO_ATTR_COLOR
A color information.
Definition: SUMOXMLDefinitions.h:701
NODETYPE_UNKNOWN
Definition: SUMOXMLDefinitions.h:1050
StringTokenizer::next
std::string next()
returns the next substring when it exists. Otherwise the behaviour is undefined
Definition: StringTokenizer.cpp:100
PositionVector
A list of positions.
Definition: PositionVector.h:46
SUMOSAXAttributesImpl_Cached::hasAttribute
bool hasAttribute(int id) const
Returns the information whether the named (by its enum-value) attribute is within the current list.
Definition: SUMOSAXAttributesImpl_Cached.cpp:69
RGBColor.h
SUMO_ATTR_FUNCTION
Definition: SUMOXMLDefinitions.h:657
RGBColor
Definition: RGBColor.h:40
SUMOSAXAttributesImpl_Cached::SUMOSAXAttributesImpl_Cached
SUMOSAXAttributesImpl_Cached(const std::map< std::string, std::string > &attrs, const std::map< int, std::string > &predefinedTagsMML, const std::string &objectType)
Constructor.
Definition: SUMOSAXAttributesImpl_Cached.cpp:43
SUMOSAXAttributesImpl_Cached::getEdgeFunc
SumoXMLEdgeFunc getEdgeFunc(bool &ok) const
Returns the value of the named attribute.
Definition: SUMOSAXAttributesImpl_Cached.cpp:148
SUMOSAXAttributesImpl_Cached::clone
SUMOSAXAttributes * clone() const
return a new deep-copy attributes object
Definition: SUMOSAXAttributesImpl_Cached.cpp:267
SumoXMLNodeType
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
Definition: SUMOXMLDefinitions.h:1049
StringBijection::get
T get(const std::string &str) const
Definition: StringBijection.h:98
StringTokenizer
Definition: StringTokenizer.h:62
SUMOSAXAttributesImpl_Cached::getRightOfWay
RightOfWay getRightOfWay(bool &ok) const
returns rightOfWay method
Definition: SUMOSAXAttributesImpl_Cached.cpp:174
RGBColor::parseColor
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:177
Boundary
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
SUMOSAXAttributesImpl_Cached::getInt
int getInt(int id) const
Returns the int-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Cached.cpp:85
EDGEFUNC_NORMAL
Definition: SUMOXMLDefinitions.h:1076
FormatException
Definition: UtilExceptions.h:82
SUMOSAXAttributesImpl_Cached::getLong
long long int getLong(int id) const
Returns the long-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Cached.cpp:91
StringTokenizer::size
int size() const
returns the number of existing substrings
Definition: StringTokenizer.cpp:138
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
SUMOSAXAttributesImpl_Cached::getName
std::string getName(int attr) const
Converts the given attribute id into a man readable string.
Definition: SUMOSAXAttributesImpl_Cached.cpp:241
StringUtils.h
StringUtils::toInt
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...
Definition: StringUtils.cpp:246
SUMOSAXAttributesImpl_Cached::getAttributeValueSecure
const std::string & getAttributeValueSecure(int id) const
Returns Xerces-value of the named attribute.
Definition: SUMOSAXAttributesImpl_Cached.cpp:116
RightOfWay
RightOfWay
algorithms for computing right of way
Definition: SUMOXMLDefinitions.h:1099
SUMOXMLDefinitions::EdgeFunctions
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
Definition: SUMOXMLDefinitions.h:1366
SUMO_ATTR_RIGHT_OF_WAY
How to compute right of way.
Definition: SUMOXMLDefinitions.h:695
SUMOSAXAttributesImpl_Cached.h
SUMOSAXAttributesImpl_Cached::serialize
void serialize(std::ostream &os) const
Prints all attribute names and values into the given stream.
Definition: SUMOSAXAttributesImpl_Cached.cpp:250
SUMOXMLDefinitions::FringeTypeValues
static StringBijection< FringeType > FringeTypeValues
fringe types
Definition: SUMOXMLDefinitions.h:1375
config.h
StringTokenizer.h
SUMOSAXAttributesImpl_Cached::getAttributeNames
std::vector< std::string > getAttributeNames() const
Retrieves all attribute names.
Definition: SUMOSAXAttributesImpl_Cached.cpp:258
FRINGE_TYPE_DEFAULT
Definition: SUMOXMLDefinitions.h:1108
SUMO_ATTR_FRINGE
Fringe type of node.
Definition: SUMOXMLDefinitions.h:697
StringUtils::toLong
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,...
Definition: StringUtils.cpp:265
SUMOSAXAttributesImpl_Cached::~SUMOSAXAttributesImpl_Cached
~SUMOSAXAttributesImpl_Cached()
Destructor.
Definition: SUMOSAXAttributesImpl_Cached.cpp:65
SUMOSAXAttributesImpl_Cached::getShape
PositionVector getShape(int attr) const
Tries to read given attribute assuming it is a PositionVector.
Definition: SUMOSAXAttributesImpl_Cached.cpp:204
SUMOSAXAttributesImpl_Cached::getColor
RGBColor getColor() const
Returns the value of the named attribute.
Definition: SUMOSAXAttributesImpl_Cached.cpp:198
SUMOSAXAttributesImpl_Cached::myPredefinedTagsMML
const std::map< int, std::string > & myPredefinedTagsMML
Map of attribute ids to their (readable) string-representation.
Definition: SUMOSAXAttributesImpl_Cached.h:289
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:57
PositionVector.h
SUMOXMLDefinitions::NodeTypes
static StringBijection< SumoXMLNodeType > NodeTypes
node types
Definition: SUMOXMLDefinitions.h:1363