Eclipse SUMO - Simulation of Urban MObility
SUMOSAXAttributesImpl_Xerces.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 /****************************************************************************/
20 // Encapsulated Xerces-SAX-attributes
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <cassert>
25 #include <xercesc/sax2/Attributes.hpp>
26 #include <xercesc/sax2/DefaultHandler.hpp>
27 #include <utils/common/RGBColor.h>
31 #include <utils/geom/Boundary.h>
33 #include "XMLSubSys.h"
36 
37 
38 // ===========================================================================
39 // class definitions
40 // ===========================================================================
41 SUMOSAXAttributesImpl_Xerces::SUMOSAXAttributesImpl_Xerces(const XERCES_CPP_NAMESPACE::Attributes& attrs,
42  const std::vector<XMLCh*>& predefinedTags,
43  const std::vector<std::string>& predefinedTagsMML,
44  const std::string& objectType) :
45  SUMOSAXAttributes(objectType),
46  myAttrs(attrs),
47  myPredefinedTags(predefinedTags),
48  myPredefinedTagsMML(predefinedTagsMML) { }
49 
50 
52 }
53 
54 
55 bool
57  assert(id >= 0);
58  assert(id < (int)myPredefinedTags.size());
59  return myAttrs.getIndex(myPredefinedTags[id]) >= 0;
60 }
61 
62 
63 bool
65  return StringUtils::toBool(getString(id));
66 }
67 
68 
69 int
71  return StringUtils::toInt(getString(id));
72 }
73 
74 
75 long long int
77  return StringUtils::toLong(getString(id));
78 }
79 
80 
81 std::string
84 }
85 
86 
87 std::string
88 SUMOSAXAttributesImpl_Xerces::getStringSecure(int id, const std::string& str) const {
89  const XMLCh* utf16 = getAttributeValueSecure(id);
90  if (XERCES_CPP_NAMESPACE::XMLString::stringLen(utf16) == 0) {
91  // TranscodeToStr and debug_new interact badly in this case;
92  return str;
93  } else {
94  return getString(id);
95  }
96 }
97 
98 
99 double
101  return StringUtils::toDouble(getString(id));
102 }
103 
104 
105 const XMLCh*
107  assert(id >= 0);
108  assert(id < (int)myPredefinedTags.size());
109  return myAttrs.getValue(myPredefinedTags[id]);
110 }
111 
112 
113 double
114 SUMOSAXAttributesImpl_Xerces::getFloat(const std::string& id) const {
115  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
116  const std::string utf8 = StringUtils::transcode(myAttrs.getValue(t));
117  XERCES_CPP_NAMESPACE::XMLString::release(&t);
118  return StringUtils::toDouble(utf8);
119 }
120 
121 
122 bool
123 SUMOSAXAttributesImpl_Xerces::hasAttribute(const std::string& id) const {
124  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
125  bool result = myAttrs.getIndex(t) >= 0;
126  XERCES_CPP_NAMESPACE::XMLString::release(&t);
127  return result;
128 }
129 
130 
131 std::string
133  const std::string& str) const {
134  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
135  const XMLCh* v = myAttrs.getValue(t);
136  XERCES_CPP_NAMESPACE::XMLString::release(&t);
137  if (v == nullptr) {
138  return str;
139  } else {
140  return StringUtils::transcode(v);
141  }
142 }
143 
144 
148  std::string funcString = getString(SUMO_ATTR_FUNCTION);
149  if (SUMOXMLDefinitions::EdgeFunctions.hasString(funcString)) {
150  return SUMOXMLDefinitions::EdgeFunctions.get(funcString);
151  }
152  ok = false;
153  }
155 }
156 
157 
161  std::string typeString = getString(SUMO_ATTR_TYPE);
162  if (SUMOXMLDefinitions::NodeTypes.hasString(typeString)) {
163  return SUMOXMLDefinitions::NodeTypes.get(typeString);
164  }
165  ok = false;
166  }
168 }
169 
173  std::string rowString = getString(SUMO_ATTR_RIGHT_OF_WAY);
174  if (SUMOXMLDefinitions::RightOfWayValues.hasString(rowString)) {
175  return SUMOXMLDefinitions::RightOfWayValues.get(rowString);
176  }
177  ok = false;
178  }
179  return RightOfWay::DEFAULT;
180 }
181 
185  std::string fringeString = getString(SUMO_ATTR_FRINGE);
186  if (SUMOXMLDefinitions::FringeTypeValues.hasString(fringeString)) {
187  return SUMOXMLDefinitions::FringeTypeValues.get(fringeString);
188  }
189  ok = false;
190  }
191  return FringeType::DEFAULT;
192 }
193 
194 RGBColor
197 }
198 
199 
202  StringTokenizer st(getString(attr));
203  PositionVector shape;
204  while (st.hasNext()) {
205  StringTokenizer pos(st.next(), ",");
206  if (pos.size() != 2 && pos.size() != 3) {
207  throw FormatException("shape format");
208  }
209  double x = StringUtils::toDouble(pos.next());
210  double y = StringUtils::toDouble(pos.next());
211  if (pos.size() == 2) {
212  shape.push_back(Position(x, y));
213  } else {
214  double z = StringUtils::toDouble(pos.next());
215  shape.push_back(Position(x, y, z));
216  }
217  }
218  return shape;
219 }
220 
221 
222 Boundary
224  std::string def = getString(attr);
225  StringTokenizer st(def, ",");
226  if (st.size() != 4) {
227  throw FormatException("boundary format");
228  }
229  const double xmin = StringUtils::toDouble(st.next());
230  const double ymin = StringUtils::toDouble(st.next());
231  const double xmax = StringUtils::toDouble(st.next());
232  const double ymax = StringUtils::toDouble(st.next());
233  return Boundary(xmin, ymin, xmax, ymax);
234 }
235 
236 
237 std::string
239  assert(attr >= 0);
240  assert(attr < (int)myPredefinedTagsMML.size());
241  return myPredefinedTagsMML[attr];
242 }
243 
244 
245 void
247  for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
248  os << " " << StringUtils::transcode(myAttrs.getLocalName(i));
249  os << "=\"" << StringUtils::transcode(myAttrs.getValue(i)) << "\"";
250  }
251 }
252 
253 
254 std::vector<std::string>
256  std::vector<std::string> result;
257  for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
258  result.push_back(StringUtils::transcode(myAttrs.getLocalName(i)));
259  }
260  return result;
261 }
262 
263 
266  std::map<std::string, std::string> attrs;
267  for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
268  attrs[StringUtils::transcode(myAttrs.getLocalName(i))] = StringUtils::transcode(myAttrs.getValue(i));
269  }
271 }
272 
273 
274 /****************************************************************************/
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 transcode(const XMLCh *const qname)
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
Encapsulated Xerces-SAX-attributes.
const XMLCh * getAttributeValueSecure(int id) const
Returns Xerces-value of the named attribute.
SumoXMLEdgeFunc getEdgeFunc(bool &ok) const
Returns the value of the named attribute.
RightOfWay getRightOfWay(bool &ok) const
returns rightOfWay method
const std::vector< std::string > & myPredefinedTagsMML
Map of attribute ids to their (readable) string-representation.
std::string getName(int attr) const
Converts the given attribute id into a man readable string.
std::vector< std::string > getAttributeNames() const
Retrieves all attribute names.
SumoXMLNodeType getNodeType(bool &ok) const
Returns the value of the named attribute.
const XERCES_CPP_NAMESPACE::Attributes & myAttrs
The encapsulated attributes.
bool getBool(int id) const
Returns the bool-value of the named (by its enum-value) attribute.
RGBColor getColor() const
Returns the value of the named 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
SUMOSAXAttributesImpl_Xerces(const XERCES_CPP_NAMESPACE::Attributes &attrs, const std::vector< XMLCh * > &predefinedTags, const std::vector< std::string > &predefinedTagsMML, const std::string &objectType)
Constructor.
double getFloat(int id) const
Returns the double-value of the named (by its enum-value) attribute.
PositionVector getShape(int attr) const
Tries to read given attribute assuming it is a PositionVector.
std::string getString(int id) const
Returns the string-value of the named (by its enum-value) attribute.
const AttrMap & myPredefinedTags
Map of attribute ids to their xerces-representation.
void serialize(std::ostream &os) const
Prints all attribute names and values into the given stream.
int getInt(int id) const
Returns the int-value of the named (by its enum-value) attribute.
std::string getStringSecure(int id, const std::string &def) const
Returns the string-value of the named (by its enum-value) attribute.
Boundary getBoundary(int attr) const
Tries to read given attribute assuming it is a Boundary.
SUMOSAXAttributes * clone() const
return a new deep-copy attributes object
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 std::string transcode(const XMLCh *const data)
converts a 0-terminated XMLCh* array (usually UTF-16, stemming from Xerces) into std::string in UTF-8
Definition: StringUtils.h:133
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