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-2018 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 /****************************************************************************/
17 // Encapsulated Xerces-SAX-attributes
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <cassert>
27 #include <xercesc/sax2/Attributes.hpp>
28 #include <xercesc/sax2/DefaultHandler.hpp>
29 #include <utils/common/RGBColor.h>
33 #include <utils/geom/Boundary.h>
35 #include "XMLSubSys.h"
38 
39 
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
43 SUMOSAXAttributesImpl_Xerces::SUMOSAXAttributesImpl_Xerces(const XERCES_CPP_NAMESPACE::Attributes& attrs,
44  const std::map<int, XMLCh*>& predefinedTags,
45  const std::map<int, std::string>& predefinedTagsMML,
46  const std::string& objectType) :
47  SUMOSAXAttributes(objectType),
48  myAttrs(attrs),
49  myPredefinedTags(predefinedTags),
50  myPredefinedTagsMML(predefinedTagsMML) { }
51 
52 
54 }
55 
56 
57 bool
59  AttrMap::const_iterator i = myPredefinedTags.find(id);
60  if (i == myPredefinedTags.end()) {
61  return false;
62  }
63  return myAttrs.getIndex((*i).second) >= 0;
64 }
65 
66 
67 bool
69  return StringUtils::toBool(getString(id));
70 }
71 
72 
73 int
75  return StringUtils::toInt(getString(id));
76 }
77 
78 
79 long long int
81  return StringUtils::toLong(getString(id));
82 }
83 
84 
85 std::string
88 }
89 
90 
91 std::string
92 SUMOSAXAttributesImpl_Xerces::getStringSecure(int id, const std::string& str) const {
93  const XMLCh* utf16 = getAttributeValueSecure(id);
94  if (XERCES_CPP_NAMESPACE::XMLString::stringLen(utf16) == 0) {
95  // TranscodeToStr and debug_new interact badly in this case;
96  return str;
97  } else {
98  return getString(id);
99  }
100 }
101 
102 
103 double
105  return StringUtils::toDouble(getString(id));
106 }
107 
108 
109 const XMLCh*
111  AttrMap::const_iterator i = myPredefinedTags.find(id);
112  assert(i != myPredefinedTags.end());
113  return myAttrs.getValue((*i).second);
114 }
115 
116 
117 double
118 SUMOSAXAttributesImpl_Xerces::getFloat(const std::string& id) const {
119  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
120  const std::string utf8 = StringUtils::transcode(myAttrs.getValue(t));
121  XERCES_CPP_NAMESPACE::XMLString::release(&t);
122  return StringUtils::toDouble(utf8);
123 }
124 
125 
126 bool
127 SUMOSAXAttributesImpl_Xerces::hasAttribute(const std::string& id) const {
128  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
129  bool result = myAttrs.getIndex(t) >= 0;
130  XERCES_CPP_NAMESPACE::XMLString::release(&t);
131  return result;
132 }
133 
134 
135 std::string
137  const std::string& str) const {
138  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
139  const XMLCh* v = myAttrs.getValue(t);
140  XERCES_CPP_NAMESPACE::XMLString::release(&t);
141  if (v == nullptr) {
142  return str;
143  } else {
144  return StringUtils::transcode(v);
145  }
146 }
147 
148 
152  std::string funcString = getString(SUMO_ATTR_FUNCTION);
153  if (SUMOXMLDefinitions::EdgeFunctions.hasString(funcString)) {
154  return SUMOXMLDefinitions::EdgeFunctions.get(funcString);
155  }
156  ok = false;
157  }
158  return EDGEFUNC_NORMAL;
159 }
160 
161 
165  std::string typeString = getString(SUMO_ATTR_TYPE);
166  if (SUMOXMLDefinitions::NodeTypes.hasString(typeString)) {
167  return SUMOXMLDefinitions::NodeTypes.get(typeString);
168  }
169  ok = false;
170  }
171  return NODETYPE_UNKNOWN;
172 }
173 
177  std::string rowString = getString(SUMO_ATTR_RIGHT_OF_WAY);
178  if (SUMOXMLDefinitions::RightOfWayValues.hasString(rowString)) {
179  return SUMOXMLDefinitions::RightOfWayValues.get(rowString);
180  }
181  ok = false;
182  }
183  return RIGHT_OF_WAY_DEFAULT;
184 }
185 
186 
187 RGBColor
190 }
191 
192 
195  StringTokenizer st(getString(attr));
196  PositionVector shape;
197  while (st.hasNext()) {
198  StringTokenizer pos(st.next(), ",");
199  if (pos.size() != 2 && pos.size() != 3) {
200  throw FormatException("shape format");
201  }
202  double x = StringUtils::toDouble(pos.next());
203  double y = StringUtils::toDouble(pos.next());
204  if (pos.size() == 2) {
205  shape.push_back(Position(x, y));
206  } else {
207  double z = StringUtils::toDouble(pos.next());
208  shape.push_back(Position(x, y, z));
209  }
210  }
211  return shape;
212 }
213 
214 
215 Boundary
217  std::string def = getString(attr);
218  StringTokenizer st(def, ",");
219  if (st.size() != 4) {
220  throw FormatException("boundary format");
221  }
222  const double xmin = StringUtils::toDouble(st.next());
223  const double ymin = StringUtils::toDouble(st.next());
224  const double xmax = StringUtils::toDouble(st.next());
225  const double ymax = StringUtils::toDouble(st.next());
226  return Boundary(xmin, ymin, xmax, ymax);
227 }
228 
229 
230 std::vector<std::string>
232  std::string def = getString(attr);
233  std::vector<std::string> ret;
234  parseStringVector(def, ret);
235  return ret;
236 }
237 
238 
239 std::string
241  if (myPredefinedTagsMML.find(attr) == myPredefinedTagsMML.end()) {
242  return "?";
243  }
244  return myPredefinedTagsMML.find(attr)->second;
245 }
246 
247 
248 void
250  for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
251  os << " " << StringUtils::transcode(myAttrs.getLocalName(i));
252  os << "=\"" << StringUtils::transcode(myAttrs.getValue(i)) << "\"";
253  }
254 }
255 
256 
259  std::map<std::string, std::string> attrs;
260  for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
261  attrs[StringUtils::transcode(myAttrs.getLocalName(i))] = StringUtils::transcode(myAttrs.getValue(i));
262  }
264 }
265 
266 /****************************************************************************/
267 
static StringBijection< RightOfWay > RightOfWayValues
lane spread functions
static StringBijection< SumoXMLNodeType > NodeTypes
node types
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:177
std::string next()
int getInt(int id) const
Returns the int-value of the named (by its enum-value) attribute.
const std::string & getObjectType() const
return the objecttype to which these attributes belong
Encapsulated Xerces-SAX-attributes.
RightOfWay getRightOfWay(bool &ok) const
returns rightOfWay method
RightOfWay
algorithms for computing right of way
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
double getFloat(int id) const
Returns the double-value of the named (by its enum-value) attribute.
const std::map< int, std::string > & myPredefinedTagsMML
Map of attribute ids to their (readable) string-representation.
static bool toBool(const std::string &sData)
converts a string into the bool 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
SUMOSAXAttributes * clone() const
return a new deep-copy attributes object
virtual ~SUMOSAXAttributesImpl_Xerces()
Destructor.
PositionVector getShape(int attr) const
Tries to read given attribute assuming it is a PositionVector.
const AttrMap & myPredefinedTags
Map of attribute ids to their xerces-representation.
static void parseStringVector(const std::string &def, std::vector< std::string > &into)
Splits the given string.
How to compute right of way.
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 ...
const XERCES_CPP_NAMESPACE::Attributes & myAttrs
The encapsulated attributes.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
A list of positions.
T get(const std::string &str) const
SumoXMLNodeType getNodeType(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.
bool hasAttribute(int id) const
Returns the information whether the named (by its enum-value) attribute is within the current list...
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 getString(int id) const
Returns the string-value of the named (by its enum-value) attribute.
SumoXMLEdgeFunc getEdgeFunc(bool &ok) const
Returns the value of the named attribute.
std::string getStringSecure(int id, const std::string &def) const
Returns the string-value of the named (by its enum-value) attribute.
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
SUMOSAXAttributesImpl_Xerces(const XERCES_CPP_NAMESPACE::Attributes &attrs, const std::map< int, XMLCh *> &predefinedTags, const std::map< int, std::string > &predefinedTagsMML, const std::string &objectType)
Constructor.
long long int getLong(int id) const
Returns the long-value of the named (by its enum-value) attribute.
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
std::string getName(int attr) const
Converts the given attribute id into a man readable string.
std::vector< std::string > getStringVector(int attr) const
Tries to read given attribute assuming it is a string vector.
const XMLCh * getAttributeValueSecure(int id) const
Returns Xerces-value of the named attribute.
void serialize(std::ostream &os) const
Prints all attribute names and values into the given stream.
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
RGBColor getColor() const
Returns the value of the named attribute.
A color information.
Boundary getBoundary(int attr) const
Tries to read given attribute assuming it is a Boundary.