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-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 /****************************************************************************/
16 // Encapsulated Xerces-SAX-attributes
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <cassert>
26 #include <xercesc/sax2/Attributes.hpp>
27 #include <xercesc/sax2/DefaultHandler.hpp>
28 #include <utils/common/RGBColor.h>
32 #include <utils/geom/Boundary.h>
34 #include "XMLSubSys.h"
37 
38 
39 // ===========================================================================
40 // class definitions
41 // ===========================================================================
42 SUMOSAXAttributesImpl_Xerces::SUMOSAXAttributesImpl_Xerces(const XERCES_CPP_NAMESPACE::Attributes& attrs,
43  const std::map<int, XMLCh*>& predefinedTags,
44  const std::map<int, std::string>& predefinedTagsMML,
45  const std::string& objectType) :
46  SUMOSAXAttributes(objectType),
47  myAttrs(attrs),
48  myPredefinedTags(predefinedTags),
49  myPredefinedTagsMML(predefinedTagsMML) { }
50 
51 
53 }
54 
55 
56 bool
58  AttrMap::const_iterator i = myPredefinedTags.find(id);
59  if (i == myPredefinedTags.end()) {
60  return false;
61  }
62  return myAttrs.getIndex((*i).second) >= 0;
63 }
64 
65 
66 bool
68  return StringUtils::toBool(getString(id));
69 }
70 
71 
72 int
74  return StringUtils::toInt(getString(id));
75 }
76 
77 
78 long long int
80  return StringUtils::toLong(getString(id));
81 }
82 
83 
84 std::string
87 }
88 
89 
90 std::string
91 SUMOSAXAttributesImpl_Xerces::getStringSecure(int id, const std::string& str) const {
92  const XMLCh* utf16 = getAttributeValueSecure(id);
93  if (XERCES_CPP_NAMESPACE::XMLString::stringLen(utf16) == 0) {
94  // TranscodeToStr and debug_new interact badly in this case;
95  return str;
96  } else {
97  return getString(id);
98  }
99 }
100 
101 
102 double
104  return StringUtils::toDouble(getString(id));
105 }
106 
107 
108 const XMLCh*
110  AttrMap::const_iterator i = myPredefinedTags.find(id);
111  assert(i != myPredefinedTags.end());
112  return myAttrs.getValue((*i).second);
113 }
114 
115 
116 double
117 SUMOSAXAttributesImpl_Xerces::getFloat(const std::string& id) const {
118  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
119  const std::string utf8 = StringUtils::transcode(myAttrs.getValue(t));
120  XERCES_CPP_NAMESPACE::XMLString::release(&t);
121  return StringUtils::toDouble(utf8);
122 }
123 
124 
125 bool
126 SUMOSAXAttributesImpl_Xerces::hasAttribute(const std::string& id) const {
127  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
128  bool result = myAttrs.getIndex(t) >= 0;
129  XERCES_CPP_NAMESPACE::XMLString::release(&t);
130  return result;
131 }
132 
133 
134 std::string
136  const std::string& str) const {
137  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
138  const XMLCh* v = myAttrs.getValue(t);
139  XERCES_CPP_NAMESPACE::XMLString::release(&t);
140  if (v == nullptr) {
141  return str;
142  } else {
143  return StringUtils::transcode(v);
144  }
145 }
146 
147 
151  std::string funcString = getString(SUMO_ATTR_FUNCTION);
152  if (SUMOXMLDefinitions::EdgeFunctions.hasString(funcString)) {
153  return SUMOXMLDefinitions::EdgeFunctions.get(funcString);
154  }
155  ok = false;
156  }
157  return EDGEFUNC_NORMAL;
158 }
159 
160 
164  std::string typeString = getString(SUMO_ATTR_TYPE);
165  if (SUMOXMLDefinitions::NodeTypes.hasString(typeString)) {
166  return SUMOXMLDefinitions::NodeTypes.get(typeString);
167  }
168  ok = false;
169  }
170  return NODETYPE_UNKNOWN;
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 (int i = 0; i < (int)myAttrs.getLength(); ++i) {
252  os << " " << StringUtils::transcode(myAttrs.getLocalName(i));
253  os << "=\"" << StringUtils::transcode(myAttrs.getValue(i)) << "\"";
254  }
255 }
256 
257 
258 std::vector<std::string>
260  std::vector<std::string> result;
261  for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
262  result.push_back(StringUtils::transcode(myAttrs.getLocalName(i)));
263  }
264  return result;
265 }
266 
267 
270  std::map<std::string, std::string> attrs;
271  for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
272  attrs[StringUtils::transcode(myAttrs.getLocalName(i))] = StringUtils::transcode(myAttrs.getValue(i));
273  }
275 }
276 
277 /****************************************************************************/
278 
SUMO_ATTR_TYPE
@ SUMO_ATTR_TYPE
Definition: SUMOXMLDefinitions.h:381
Boundary.h
SUMOSAXAttributesImpl_Xerces::hasAttribute
bool hasAttribute(int id) const
Returns the information whether the named (by its enum-value) attribute is within the current list.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:57
FringeType
FringeType
algorithms for computing right of way
Definition: SUMOXMLDefinitions.h:1110
SUMOSAXAttributesImpl_Xerces::getName
std::string getName(int attr) const
Converts the given attribute id into a man readable string.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:241
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:374
RIGHT_OF_WAY_DEFAULT
@ RIGHT_OF_WAY_DEFAULT
Definition: SUMOXMLDefinitions.h:1105
SUMOSAXAttributesImpl_Xerces::getLong
long long int getLong(int id) const
Returns the long-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:79
StringTokenizer::hasNext
bool hasNext()
returns the information whether further substrings exist
Definition: StringTokenizer.cpp:94
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:345
SUMOSAXAttributesImpl_Xerces::getColor
RGBColor getColor() const
Returns the value of the named attribute.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:198
SUMOSAXAttributesImpl_Xerces::myAttrs
const XERCES_CPP_NAMESPACE::Attributes & myAttrs
The encapsulated attributes.
Definition: SUMOSAXAttributesImpl_Xerces.h:298
SUMOXMLDefinitions::RightOfWayValues
static StringBijection< RightOfWay > RightOfWayValues
righ of way algorithms
Definition: SUMOXMLDefinitions.h:1377
SUMOSAXAttributes::getObjectType
const std::string & getObjectType() const
return the objecttype to which these attributes belong
Definition: SUMOSAXAttributes.h:396
SumoXMLEdgeFunc
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
Definition: SUMOXMLDefinitions.h:1079
SUMO_ATTR_COLOR
@ SUMO_ATTR_COLOR
A color information.
Definition: SUMOXMLDefinitions.h:704
NODETYPE_UNKNOWN
@ NODETYPE_UNKNOWN
Definition: SUMOXMLDefinitions.h:1055
StringTokenizer::next
std::string next()
returns the next substring when it exists. Otherwise the behaviour is undefined
Definition: StringTokenizer.cpp:99
SUMOSAXAttributesImpl_Xerces.h
PositionVector
A list of positions.
Definition: PositionVector.h:45
SUMOSAXAttributesImpl_Xerces::clone
SUMOSAXAttributes * clone() const
return a new deep-copy attributes object
Definition: SUMOSAXAttributesImpl_Xerces.cpp:269
SUMOSAXAttributesImpl_Xerces::~SUMOSAXAttributesImpl_Xerces
virtual ~SUMOSAXAttributesImpl_Xerces()
Destructor.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:52
RGBColor.h
SUMOSAXAttributesImpl_Xerces::getShape
PositionVector getShape(int attr) const
Tries to read given attribute assuming it is a PositionVector.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:204
SUMO_ATTR_FUNCTION
@ SUMO_ATTR_FUNCTION
Definition: SUMOXMLDefinitions.h:659
SUMOSAXAttributesImpl_Xerces::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_Xerces.cpp:91
RGBColor
Definition: RGBColor.h:39
SUMOSAXAttributesImpl_Xerces::getEdgeFunc
SumoXMLEdgeFunc getEdgeFunc(bool &ok) const
Returns the value of the named attribute.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:149
SUMOSAXAttributesImpl_Xerces::getNodeType
SumoXMLNodeType getNodeType(bool &ok) const
Returns the value of the named attribute.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:162
SUMOSAXAttributesImpl_Cached
Encapsulated Xerces-SAX-attributes.
Definition: SUMOSAXAttributesImpl_Cached.h:43
SumoXMLNodeType
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
Definition: SUMOXMLDefinitions.h:1054
StringBijection::get
T get(const std::string &str) const
Definition: StringBijection.h:97
StringTokenizer
Definition: StringTokenizer.h:61
SUMOSAXAttributesImpl_Xerces::getBool
bool getBool(int id) const
Returns the bool-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:67
SUMOSAXAttributesImpl_Xerces::getFloat
double getFloat(int id) const
Returns the double-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:103
RGBColor::parseColor
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:176
SUMOSAXAttributesImpl_Xerces::getBoundary
Boundary getBoundary(int attr) const
Tries to read given attribute assuming it is a Boundary.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:226
Boundary
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:41
SUMOSAXAttributesImpl_Xerces::SUMOSAXAttributesImpl_Xerces
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.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:42
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
SUMOSAXAttributesImpl_Xerces::getFringeType
FringeType getFringeType(bool &ok) const
returns fringe type
Definition: SUMOSAXAttributesImpl_Xerces.cpp:186
EDGEFUNC_NORMAL
@ EDGEFUNC_NORMAL
Definition: SUMOXMLDefinitions.h:1081
FormatException
Definition: UtilExceptions.h:81
StringTokenizer::size
int size() const
returns the number of existing substrings
Definition: StringTokenizer.cpp:137
StringUtils.h
SUMOSAXAttributesImpl_Xerces::myPredefinedTagsMML
const std::map< int, std::string > & myPredefinedTagsMML
Map of attribute ids to their (readable) string-representation.
Definition: SUMOSAXAttributesImpl_Xerces.h:306
StringUtils::transcode
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:136
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:278
SUMOSAXAttributesImpl_Xerces::myPredefinedTags
const AttrMap & myPredefinedTags
Map of attribute ids to their xerces-representation.
Definition: SUMOSAXAttributesImpl_Xerces.h:303
transcode
std::string transcode(const XMLCh *const qname)
Definition: VehicleEngineHandler.cpp:37
RightOfWay
RightOfWay
algorithms for computing right of way
Definition: SUMOXMLDefinitions.h:1104
SUMOXMLDefinitions::EdgeFunctions
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
Definition: SUMOXMLDefinitions.h:1371
SUMOSAXAttributesImpl_Xerces::getInt
int getInt(int id) const
Returns the int-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:73
SUMOSAXAttributesImpl_Xerces::getAttributeValueSecure
const XMLCh * getAttributeValueSecure(int id) const
Returns Xerces-value of the named attribute.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:109
SUMO_ATTR_RIGHT_OF_WAY
@ SUMO_ATTR_RIGHT_OF_WAY
How to compute right of way.
Definition: SUMOXMLDefinitions.h:698
SUMOSAXAttributesImpl_Cached.h
SUMOXMLDefinitions::FringeTypeValues
static StringBijection< FringeType > FringeTypeValues
fringe types
Definition: SUMOXMLDefinitions.h:1380
config.h
SUMOSAXAttributesImpl_Xerces::getAttributeNames
std::vector< std::string > getAttributeNames() const
Retrieves all attribute names.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:259
SUMOSAXAttributesImpl_Xerces::getRightOfWay
RightOfWay getRightOfWay(bool &ok) const
returns rightOfWay method
Definition: SUMOSAXAttributesImpl_Xerces.cpp:174
StringTokenizer.h
FRINGE_TYPE_DEFAULT
@ FRINGE_TYPE_DEFAULT
Definition: SUMOXMLDefinitions.h:1113
SUMO_ATTR_FRINGE
@ SUMO_ATTR_FRINGE
Fringe type of node.
Definition: SUMOXMLDefinitions.h:700
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:297
SUMOSAXAttributesImpl_Xerces::getString
std::string getString(int id) const
Returns the string-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:85
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:56
PositionVector.h
SUMOSAXAttributesImpl_Xerces::serialize
void serialize(std::ostream &os) const
Prints all attribute names and values into the given stream.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:250
SUMOXMLDefinitions::NodeTypes
static StringBijection< SumoXMLNodeType > NodeTypes
node types
Definition: SUMOXMLDefinitions.h:1368
XMLSubSys.h