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 
static StringBijection< RightOfWay > RightOfWayValues
righ of way algorithms
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
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
std::vector< std::string > getAttributeNames() const
Retrieves all attribute names.
Encapsulated Xerces-SAX-attributes.
bool hasNext()
returns the information whether further substrings exist
RightOfWay getRightOfWay(bool &ok) const
returns rightOfWay method
std::string transcode(const XMLCh *const qname)
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:132
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.
How to compute right of way.
int size() const
returns the number of existing substrings
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.
FringeType getFringeType(bool &ok) const
returns fringe type
std::string getStringSecure(int id, const std::string &def) const
Returns the string-value of the named (by its enum-value) attribute.
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
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.
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.
FringeType
algorithms for computing right of way
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
RGBColor getColor() const
Returns the value of the named attribute.
Fringe type of node.
A color information.
Boundary getBoundary(int attr) const
Tries to read given attribute assuming it is a Boundary.