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 /****************************************************************************/
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 
189  std::string fringeString = getString(SUMO_ATTR_FRINGE);
190  if (SUMOXMLDefinitions::FringeTypeValues.hasString(fringeString)) {
191  return SUMOXMLDefinitions::FringeTypeValues.get(fringeString);
192  }
193  ok = false;
194  }
195  return FRINGE_TYPE_DEFAULT;
196 }
197 
198 RGBColor
201 }
202 
203 
206  StringTokenizer st(getString(attr));
207  PositionVector shape;
208  while (st.hasNext()) {
209  StringTokenizer pos(st.next(), ",");
210  if (pos.size() != 2 && pos.size() != 3) {
211  throw FormatException("shape format");
212  }
213  double x = StringUtils::toDouble(pos.next());
214  double y = StringUtils::toDouble(pos.next());
215  if (pos.size() == 2) {
216  shape.push_back(Position(x, y));
217  } else {
218  double z = StringUtils::toDouble(pos.next());
219  shape.push_back(Position(x, y, z));
220  }
221  }
222  return shape;
223 }
224 
225 
226 Boundary
228  std::string def = getString(attr);
229  StringTokenizer st(def, ",");
230  if (st.size() != 4) {
231  throw FormatException("boundary format");
232  }
233  const double xmin = StringUtils::toDouble(st.next());
234  const double ymin = StringUtils::toDouble(st.next());
235  const double xmax = StringUtils::toDouble(st.next());
236  const double ymax = StringUtils::toDouble(st.next());
237  return Boundary(xmin, ymin, xmax, ymax);
238 }
239 
240 
241 std::string
243  if (myPredefinedTagsMML.find(attr) == myPredefinedTagsMML.end()) {
244  return "?";
245  }
246  return myPredefinedTagsMML.find(attr)->second;
247 }
248 
249 
250 void
252  for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
253  os << " " << StringUtils::transcode(myAttrs.getLocalName(i));
254  os << "=\"" << StringUtils::transcode(myAttrs.getValue(i)) << "\"";
255  }
256 }
257 
258 
259 std::vector<std::string>
261  std::vector<std::string> result;
262  for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
263  result.push_back(StringUtils::transcode(myAttrs.getLocalName(i)));
264  }
265  return result;
266 }
267 
268 
271  std::map<std::string, std::string> attrs;
272  for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
273  attrs[StringUtils::transcode(myAttrs.getLocalName(i))] = StringUtils::transcode(myAttrs.getValue(i));
274  }
276 }
277 
278 /****************************************************************************/
279 
SUMO_ATTR_TYPE
Definition: SUMOXMLDefinitions.h:382
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:58
FringeType
FringeType
algorithms for computing right of way
Definition: SUMOXMLDefinitions.h:1105
SUMOSAXAttributesImpl_Xerces::getName
std::string getName(int attr) const
Converts the given attribute id into a man readable string.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:242
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_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:80
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_Xerces::getColor
RGBColor getColor() const
Returns the value of the named attribute.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:199
SUMOSAXAttributesImpl_Xerces::myAttrs
const XERCES_CPP_NAMESPACE::Attributes & myAttrs
The encapsulated attributes.
Definition: SUMOSAXAttributesImpl_Xerces.h:299
SUMOXMLDefinitions::RightOfWayValues
static StringBijection< RightOfWay > RightOfWayValues
righ of way algorithms
Definition: SUMOXMLDefinitions.h:1372
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
SUMOSAXAttributesImpl_Xerces.h
PositionVector
A list of positions.
Definition: PositionVector.h:46
SUMOSAXAttributesImpl_Xerces::clone
SUMOSAXAttributes * clone() const
return a new deep-copy attributes object
Definition: SUMOSAXAttributesImpl_Xerces.cpp:270
SUMOSAXAttributesImpl_Xerces::~SUMOSAXAttributesImpl_Xerces
virtual ~SUMOSAXAttributesImpl_Xerces()
Destructor.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:53
RGBColor.h
SUMOSAXAttributesImpl_Xerces::getShape
PositionVector getShape(int attr) const
Tries to read given attribute assuming it is a PositionVector.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:205
SUMO_ATTR_FUNCTION
Definition: SUMOXMLDefinitions.h:657
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:92
RGBColor
Definition: RGBColor.h:40
SUMOSAXAttributesImpl_Xerces::getEdgeFunc
SumoXMLEdgeFunc getEdgeFunc(bool &ok) const
Returns the value of the named attribute.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:150
SUMOSAXAttributesImpl_Xerces::getNodeType
SumoXMLNodeType getNodeType(bool &ok) const
Returns the value of the named attribute.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:163
SUMOSAXAttributesImpl_Cached
Encapsulated Xerces-SAX-attributes.
Definition: SUMOSAXAttributesImpl_Cached.h:44
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_Xerces::getBool
bool getBool(int id) const
Returns the bool-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:68
SUMOSAXAttributesImpl_Xerces::getFloat
double getFloat(int id) const
Returns the double-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:104
RGBColor::parseColor
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:177
SUMOSAXAttributesImpl_Xerces::getBoundary
Boundary getBoundary(int attr) const
Tries to read given attribute assuming it is a Boundary.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:227
Boundary
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
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:43
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
SUMOSAXAttributesImpl_Xerces::getFringeType
FringeType getFringeType(bool &ok) const
returns fringe type
Definition: SUMOSAXAttributesImpl_Xerces.cpp:187
EDGEFUNC_NORMAL
Definition: SUMOXMLDefinitions.h:1076
FormatException
Definition: UtilExceptions.h:82
StringTokenizer::size
int size() const
returns the number of existing substrings
Definition: StringTokenizer.cpp:138
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:307
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:132
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_Xerces::myPredefinedTags
const AttrMap & myPredefinedTags
Map of attribute ids to their xerces-representation.
Definition: SUMOSAXAttributesImpl_Xerces.h:304
transcode
std::string transcode(const XMLCh *const qname)
Definition: VehicleEngineHandler.cpp:38
RightOfWay
RightOfWay
algorithms for computing right of way
Definition: SUMOXMLDefinitions.h:1099
SUMOXMLDefinitions::EdgeFunctions
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
Definition: SUMOXMLDefinitions.h:1366
SUMOSAXAttributesImpl_Xerces::getInt
int getInt(int id) const
Returns the int-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:74
SUMOSAXAttributesImpl_Xerces::getAttributeValueSecure
const XMLCh * getAttributeValueSecure(int id) const
Returns Xerces-value of the named attribute.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:110
SUMO_ATTR_RIGHT_OF_WAY
How to compute right of way.
Definition: SUMOXMLDefinitions.h:695
SUMOSAXAttributesImpl_Cached.h
SUMOXMLDefinitions::FringeTypeValues
static StringBijection< FringeType > FringeTypeValues
fringe types
Definition: SUMOXMLDefinitions.h:1375
config.h
SUMOSAXAttributesImpl_Xerces::getAttributeNames
std::vector< std::string > getAttributeNames() const
Retrieves all attribute names.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:260
SUMOSAXAttributesImpl_Xerces::getRightOfWay
RightOfWay getRightOfWay(bool &ok) const
returns rightOfWay method
Definition: SUMOSAXAttributesImpl_Xerces.cpp:175
StringTokenizer.h
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_Xerces::getString
std::string getString(int id) const
Returns the string-value of the named (by its enum-value) attribute.
Definition: SUMOSAXAttributesImpl_Xerces.cpp:86
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:57
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:251
SUMOXMLDefinitions::NodeTypes
static StringBijection< SumoXMLNodeType > NodeTypes
node types
Definition: SUMOXMLDefinitions.h:1363
XMLSubSys.h