SUMO - Simulation of Urban MObility
SUMOSAXAttributesImpl_Xerces.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Encapsulated Xerces-SAX-attributes
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2002-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <cassert>
34 #include <xercesc/sax2/Attributes.hpp>
35 #include <xercesc/sax2/DefaultHandler.hpp>
36 #include <xercesc/util/XercesVersion.hpp>
37 #include <xercesc/util/TransService.hpp>
38 #include <xercesc/util/TranscodingException.hpp>
39 #include <utils/common/RGBColor.h>
42 #include <utils/geom/Boundary.h>
46 
47 
48 // ===========================================================================
49 // class definitions
50 // ===========================================================================
51 SUMOSAXAttributesImpl_Xerces::SUMOSAXAttributesImpl_Xerces(const XERCES_CPP_NAMESPACE::Attributes& attrs,
52  const std::map<int, XMLCh*>& predefinedTags,
53  const std::map<int, std::string>& predefinedTagsMML,
54  const std::string& objectType) :
55  SUMOSAXAttributes(objectType),
56  myAttrs(attrs),
57  myPredefinedTags(predefinedTags),
58  myPredefinedTagsMML(predefinedTagsMML) { }
59 
60 
62 }
63 
64 
65 bool
67  AttrMap::const_iterator i = myPredefinedTags.find(id);
68  if (i == myPredefinedTags.end()) {
69  return false;
70  }
71  return myAttrs.getIndex((*i).second) >= 0;
72 }
73 
74 
75 bool
78 }
79 
80 
81 int
84 }
85 
86 
87 long long int
90 }
91 
92 
93 std::string
95  const XMLCh* utf16 = getAttributeValueSecure(id);
96 #if _XERCES_VERSION < 30100
97  char* t = XERCES_CPP_NAMESPACE::XMLString::transcode(utf16);
98  std::string result(t);
99  XERCES_CPP_NAMESPACE::XMLString::release(&t);
100  return result;
101 #else
102  if (XERCES_CPP_NAMESPACE::XMLString::stringLen(utf16) == 0) {
103  // TranscodeToStr and debug_new interact badly in this case;
104  return "";
105  } else {
106  try {
107  XERCES_CPP_NAMESPACE::TranscodeToStr utf8(utf16, "UTF-8");
108  return TplConvert::_2str(utf8.str(), (unsigned)utf8.length());
109  } catch (XERCES_CPP_NAMESPACE::TranscodingException e) {
110  return "?";
111  }
112  }
113 #endif
114 }
115 
116 
117 std::string
119  const std::string& str) const {
120  const XMLCh* utf16 = getAttributeValueSecure(id);
121 #if _XERCES_VERSION < 30100
122  char* t = XERCES_CPP_NAMESPACE::XMLString::transcode(utf16);
123  std::string result(TplConvert::_2strSec(t, str));
124  XERCES_CPP_NAMESPACE::XMLString::release(&t);
125  return result;
126 #else
127  if (XERCES_CPP_NAMESPACE::XMLString::stringLen(utf16) == 0) {
128  // TranscodeToStr and debug_new interact badly in this case;
129  return "";
130  } else {
131  try {
132  XERCES_CPP_NAMESPACE::TranscodeToStr utf8(utf16, "UTF-8");
133  return TplConvert::_2strSec(utf8.str(), (unsigned)utf8.length(), str);
134  } catch (XERCES_CPP_NAMESPACE::TranscodingException e) {
135  return "?";
136  }
137  }
138 #endif
139 }
140 
141 
142 double
145 }
146 
147 
148 const XMLCh*
150  AttrMap::const_iterator i = myPredefinedTags.find(id);
151  assert(i != myPredefinedTags.end());
152  return myAttrs.getValue((*i).second);
153 }
154 
155 
156 double
157 SUMOSAXAttributesImpl_Xerces::getFloat(const std::string& id) const {
158  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
159  double result = TplConvert::_2double(myAttrs.getValue(t));
160  XERCES_CPP_NAMESPACE::XMLString::release(&t);
161  return result;
162 }
163 
164 
165 bool
166 SUMOSAXAttributesImpl_Xerces::hasAttribute(const std::string& id) const {
167  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
168  bool result = myAttrs.getIndex(t) >= 0;
169  XERCES_CPP_NAMESPACE::XMLString::release(&t);
170  return result;
171 }
172 
173 
174 std::string
176  const std::string& str) const {
177  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
178  std::string result = TplConvert::_2strSec(myAttrs.getValue(t), str);
179  XERCES_CPP_NAMESPACE::XMLString::release(&t);
180  return result;
181 }
182 
183 
187  std::string funcString = getString(SUMO_ATTR_FUNCTION);
188  if (SUMOXMLDefinitions::EdgeFunctions.hasString(funcString)) {
189  return SUMOXMLDefinitions::EdgeFunctions.get(funcString);
190  }
191  ok = false;
192  }
193  return EDGEFUNC_NORMAL;
194 }
195 
196 
200  std::string typeString = getString(SUMO_ATTR_TYPE);
201  if (SUMOXMLDefinitions::NodeTypes.hasString(typeString)) {
202  return SUMOXMLDefinitions::NodeTypes.get(typeString);
203  }
204  ok = false;
205  }
206  return NODETYPE_UNKNOWN;
207 }
208 
209 
210 RGBColor
213 }
214 
215 
218  StringTokenizer st(getString(attr));
219  PositionVector shape;
220  while (st.hasNext()) {
221  StringTokenizer pos(st.next(), ",");
222  if (pos.size() != 2 && pos.size() != 3) {
223  throw FormatException("shape format");
224  }
225  double x = TplConvert::_2double(pos.next().c_str());
226  double y = TplConvert::_2double(pos.next().c_str());
227  if (pos.size() == 2) {
228  shape.push_back(Position(x, y));
229  } else {
230  double z = TplConvert::_2double(pos.next().c_str());
231  shape.push_back(Position(x, y, z));
232  }
233  }
234  return shape;
235 }
236 
237 
238 Boundary
240  std::string def = getString(attr);
241  StringTokenizer st(def, ",");
242  if (st.size() != 4) {
243  throw FormatException("boundary format");
244  }
245  const double xmin = TplConvert::_2double(st.next().c_str());
246  const double ymin = TplConvert::_2double(st.next().c_str());
247  const double xmax = TplConvert::_2double(st.next().c_str());
248  const double ymax = TplConvert::_2double(st.next().c_str());
249  return Boundary(xmin, ymin, xmax, ymax);
250 }
251 
252 
253 std::vector<std::string>
255  std::string def = getString(attr);
256  std::vector<std::string> ret;
257  parseStringVector(def, ret);
258  return ret;
259 }
260 
261 
262 std::string
264  if (myPredefinedTagsMML.find(attr) == myPredefinedTagsMML.end()) {
265  return "?";
266  }
267  return myPredefinedTagsMML.find(attr)->second;
268 }
269 
270 
271 void
273  for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
274  os << " " << TplConvert::_2str(myAttrs.getLocalName(i));
275  os << "=\"" << TplConvert::_2str(myAttrs.getValue(i)) << "\"";
276  }
277 }
278 
279 
282  std::map<std::string, std::string> attrs;
283  for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
284  attrs[TplConvert::_2str(myAttrs.getLocalName(i))] = TplConvert::_2str(myAttrs.getValue(i));
285  }
287 }
288 
289 /****************************************************************************/
290 
static StringBijection< SumoXMLNodeType > NodeTypes
node types
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:168
std::string next()
static bool _2bool(const E *const data)
converts a 0-terminated char-type array into the boolean value described by it
Definition: TplConvert.h:371
int getInt(int id) const
Returns the int-value of the named (by its enum-value) attribute.
static long long int _2long(const E *const data)
converts a char-type array into the long value described by it
Definition: TplConvert.h:200
const std::string & getObjectType() const
return the objecttype to which these attributes belong
Encapsulated Xerces-SAX-attributes.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
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.
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.
Encapsulated SAX-Attributes.
const XERCES_CPP_NAMESPACE::Attributes & myAttrs
The encapsulated attributes.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
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...
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.
static int _2int(const E *const data)
converts a char-type array into the integer value described by it
Definition: TplConvert.h:149
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
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.
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:297
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.
static std::string _2str(const int var)
convert int to string
Definition: TplConvert.h:57
A color information.
static std::string _2strSec(const E *const data, const std::string &def)
converts a 0-terminated char-type array into std::string
Definition: TplConvert.h:128
Boundary getBoundary(int attr) const
Tries to read given attribute assuming it is a Boundary.