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-2016 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>
45 
46 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 
51 // ===========================================================================
52 // class definitions
53 // ===========================================================================
54 SUMOSAXAttributesImpl_Xerces::SUMOSAXAttributesImpl_Xerces(const XERCES_CPP_NAMESPACE::Attributes& attrs,
55  const std::map<int, XMLCh*>& predefinedTags,
56  const std::map<int, std::string>& predefinedTagsMML,
57  const std::string& objectType) :
58  SUMOSAXAttributes(objectType),
59  myAttrs(attrs),
60  myPredefinedTags(predefinedTags),
61  myPredefinedTagsMML(predefinedTagsMML) { }
62 
63 
65 }
66 
67 
68 bool
70  AttrMap::const_iterator i = myPredefinedTags.find(id);
71  if (i == myPredefinedTags.end()) {
72  return false;
73  }
74  return myAttrs.getIndex((*i).second) >= 0;
75 }
76 
77 
78 bool
81 }
82 
83 
84 int
87 }
88 
89 
90 long long int
93 }
94 
95 
96 std::string
98  const XMLCh* utf16 = getAttributeValueSecure(id);
99 #if _XERCES_VERSION < 30100
100  char* t = XERCES_CPP_NAMESPACE::XMLString::transcode(utf16);
101  std::string result(t);
102  XERCES_CPP_NAMESPACE::XMLString::release(&t);
103  return result;
104 #else
105  if (XERCES_CPP_NAMESPACE::XMLString::stringLen(utf16) == 0) {
106  // TranscodeToStr and debug_new interact badly in this case;
107  return "";
108  } else {
109  try {
110  XERCES_CPP_NAMESPACE::TranscodeToStr utf8(utf16, "UTF-8");
111  return TplConvert::_2str(utf8.str(), (unsigned)utf8.length());
112  } catch (XERCES_CPP_NAMESPACE::TranscodingException e) {
113  return "?";
114  }
115  }
116 #endif
117 }
118 
119 
120 std::string
122  const std::string& str) const {
123  const XMLCh* utf16 = getAttributeValueSecure(id);
124 #if _XERCES_VERSION < 30100
125  char* t = XERCES_CPP_NAMESPACE::XMLString::transcode(utf16);
126  std::string result(TplConvert::_2strSec(t, str));
127  XERCES_CPP_NAMESPACE::XMLString::release(&t);
128  return result;
129 #else
130  if (XERCES_CPP_NAMESPACE::XMLString::stringLen(utf16) == 0) {
131  // TranscodeToStr and debug_new interact badly in this case;
132  return "";
133  } else {
134  try {
135  XERCES_CPP_NAMESPACE::TranscodeToStr utf8(utf16, "UTF-8");
136  return TplConvert::_2strSec(utf8.str(), (unsigned)utf8.length(), str);
137  } catch (XERCES_CPP_NAMESPACE::TranscodingException e) {
138  return "?";
139  }
140  }
141 #endif
142 }
143 
144 
145 SUMOReal
148 }
149 
150 
151 const XMLCh*
153  AttrMap::const_iterator i = myPredefinedTags.find(id);
154  assert(i != myPredefinedTags.end());
155  return myAttrs.getValue((*i).second);
156 }
157 
158 
159 SUMOReal
160 SUMOSAXAttributesImpl_Xerces::getFloat(const std::string& id) const {
161  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
162  SUMOReal result = TplConvert::_2SUMOReal(myAttrs.getValue(t));
163  XERCES_CPP_NAMESPACE::XMLString::release(&t);
164  return result;
165 }
166 
167 
168 bool
169 SUMOSAXAttributesImpl_Xerces::hasAttribute(const std::string& id) const {
170  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
171  bool result = myAttrs.getIndex(t) >= 0;
172  XERCES_CPP_NAMESPACE::XMLString::release(&t);
173  return result;
174 }
175 
176 
177 std::string
179  const std::string& str) const {
180  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
181  std::string result = TplConvert::_2strSec(myAttrs.getValue(t), str);
182  XERCES_CPP_NAMESPACE::XMLString::release(&t);
183  return result;
184 }
185 
186 
190  std::string funcString = getString(SUMO_ATTR_FUNCTION);
191  if (SUMOXMLDefinitions::EdgeFunctions.hasString(funcString)) {
192  return SUMOXMLDefinitions::EdgeFunctions.get(funcString);
193  }
194  ok = false;
195  }
196  return EDGEFUNC_NORMAL;
197 }
198 
199 
203  std::string typeString = getString(SUMO_ATTR_TYPE);
204  if (SUMOXMLDefinitions::NodeTypes.hasString(typeString)) {
205  return SUMOXMLDefinitions::NodeTypes.get(typeString);
206  }
207  ok = false;
208  }
209  return NODETYPE_UNKNOWN;
210 }
211 
212 
213 RGBColor
216 }
217 
218 
221  StringTokenizer st(getString(attr));
222  PositionVector shape;
223  while (st.hasNext()) {
224  StringTokenizer pos(st.next(), ",");
225  if (pos.size() != 2 && pos.size() != 3) {
226  throw FormatException("shape format");
227  }
228  SUMOReal x = TplConvert::_2SUMOReal(pos.next().c_str());
229  SUMOReal y = TplConvert::_2SUMOReal(pos.next().c_str());
230  if (pos.size() == 2) {
231  shape.push_back(Position(x, y));
232  } else {
233  SUMOReal z = TplConvert::_2SUMOReal(pos.next().c_str());
234  shape.push_back(Position(x, y, z));
235  }
236  }
237  return shape;
238 }
239 
240 
241 Boundary
243  std::string def = getString(attr);
244  StringTokenizer st(def, ",");
245  if (st.size() != 4) {
246  throw FormatException("boundary format");
247  }
248  const SUMOReal xmin = TplConvert::_2SUMOReal(st.next().c_str());
249  const SUMOReal ymin = TplConvert::_2SUMOReal(st.next().c_str());
250  const SUMOReal xmax = TplConvert::_2SUMOReal(st.next().c_str());
251  const SUMOReal ymax = TplConvert::_2SUMOReal(st.next().c_str());
252  return Boundary(xmin, ymin, xmax, ymax);
253 }
254 
255 
256 std::vector<std::string>
258  std::string def = getString(attr);
259  std::vector<std::string> ret;
260  parseStringVector(def, ret);
261  return ret;
262 }
263 
264 
265 std::string
267  if (myPredefinedTagsMML.find(attr) == myPredefinedTagsMML.end()) {
268  return "?";
269  }
270  return myPredefinedTagsMML.find(attr)->second;
271 }
272 
273 
274 void
276  for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
277  os << " " << TplConvert::_2str(myAttrs.getLocalName(i));
278  os << "=\"" << TplConvert::_2str(myAttrs.getValue(i)) << "\"";
279  }
280 }
281 
282 
283 /****************************************************************************/
284 
SumoXMLEdgeFunc getEdgeFunc(bool &ok) const
Returns the value of the named attribute.
static StringBijection< SumoXMLNodeType > NodeTypes
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:172
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.
std::string next()
SumoXMLNodeType getNodeType(bool &ok) const
Returns the value of the named attribute.
long long int getLong(int id) const
Returns the long-value of the named (by its enum-value) attribute.
static SUMOReal _2SUMOReal(const E *const data)
converts a char-type array into the SUMOReal value described by it
Definition: TplConvert.h:290
static bool _2bool(const E *const data)
converts a 0-terminated char-type array into the boolean value described by it
Definition: TplConvert.h:364
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.
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
bool getBool(int id) const
Returns the bool-value of the named (by its enum-value) attribute.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
const std::map< int, std::string > & myPredefinedTagsMML
Map of attribute ids to their (readable) string-representation.
RGBColor getColor() const
Returns the value of the named attribute.
virtual ~SUMOSAXAttributesImpl_Xerces()
Destructor.
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.
std::string getStringSecure(int id, const std::string &def) const
Returns the string-value of the named (by its enum-value) attribute.
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.
SUMOReal getFloat(int id) const
Returns the SUMOReal-value of the named (by its enum-value) attribute.
int getInt(int id) const
Returns the int-value of the named (by its enum-value) attribute.
PositionVector getShape(int attr) const
Tries to read given attribute assuming it is a PositionVector.
std::string getName(int attr) const
Converts the given attribute id into a man readable string.
void serialize(std::ostream &os) const
Prints all attribute names and values into the given stream.
Boundary getBoundary(int attr) const
Tries to read given attribute assuming it is a Boundary.
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 ...
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
T get(const std::string &str) const
#define SUMOReal
Definition: config.h:213
std::vector< std::string > getStringVector(int attr) const
Tries to read given attribute assuming it is a string vector.
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
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
const XMLCh * getAttributeValueSecure(int id) const
Returns Xerces-value of the named attribute.