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 
static StringBijection< RightOfWay > RightOfWayValues
righ of way algorithms
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()
returns the next substring when it exists. Otherwise the behaviour is undefined
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.
bool hasNext()
returns the information whether further substrings exist
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
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
std::vector< std::string > getAttributeNames() const
Retrieves all attribute names.
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.
How to compute right of way.
int size() const
returns the number of existing substrings
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
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...
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
static StringBijection< FringeType > FringeTypeValues
fringe types
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...
FringeType
algorithms for computing right of way
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.
Fringe type of node.
A color information.
bool hasAttribute(int id) const
Returns the information whether the named (by its enum-value) attribute is within the current list...