SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-sim.org/
12 // Copyright (C) 2002-2014 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 <utils/common/RGBColor.h>
41 #include <utils/geom/Boundary.h>
44 
45 #ifdef CHECK_MEMORY_LEAKS
46 #include <foreign/nvwa/debug_new.h>
47 #endif // CHECK_MEMORY_LEAKS
48 
49 
50 // ===========================================================================
51 // class definitions
52 // ===========================================================================
53 SUMOSAXAttributesImpl_Xerces::SUMOSAXAttributesImpl_Xerces(const XERCES_CPP_NAMESPACE::Attributes& attrs,
54  const std::map<int, XMLCh*>& predefinedTags,
55  const std::map<int, std::string>& predefinedTagsMML,
56  const std::string& objectType) :
57  SUMOSAXAttributes(objectType),
58  myAttrs(attrs),
59  myPredefinedTags(predefinedTags),
60  myPredefinedTagsMML(predefinedTagsMML) { }
61 
62 
64 }
65 
66 
67 bool
69  AttrMap::const_iterator i = myPredefinedTags.find(id);
70  if (i == myPredefinedTags.end()) {
71  return false;
72  }
73  return myAttrs.getIndex((*i).second) >= 0;
74 }
75 
76 
77 bool
80 }
81 
82 
83 int
86 }
87 
88 
92 }
93 
94 
95 std::string
97  const XMLCh* utf16 = getAttributeValueSecure(id);
98 #if _XERCES_VERSION < 30100
99  char* t = XERCES_CPP_NAMESPACE::XMLString::transcode(utf16);
100  std::string result(t);
101  XERCES_CPP_NAMESPACE::XMLString::release(&t);
102  return result;
103 #else
104  if (XERCES_CPP_NAMESPACE::XMLString::stringLen(utf16) == 0) {
105  // TranscodeToStr and debug_new interact badly in this case;
106  return "";
107  } else {
108  XERCES_CPP_NAMESPACE::TranscodeToStr utf8(utf16, "UTF-8");
109  return TplConvert::_2str(utf8.str(), (unsigned)utf8.length());
110  }
111 #endif
112 }
113 
114 
115 std::string
117  const std::string& str) const {
118  const XMLCh* utf16 = getAttributeValueSecure(id);
119 #if _XERCES_VERSION < 30100
120  char* t = XERCES_CPP_NAMESPACE::XMLString::transcode(utf16);
121  std::string result(TplConvert::_2strSec(t, str));
122  XERCES_CPP_NAMESPACE::XMLString::release(&t);
123  return result;
124 #else
125  if (XERCES_CPP_NAMESPACE::XMLString::stringLen(utf16) == 0) {
126  // TranscodeToStr and debug_new interact badly in this case;
127  return "";
128  } else {
129  XERCES_CPP_NAMESPACE::TranscodeToStr utf8(utf16, "UTF-8");
130  return TplConvert::_2strSec(utf8.str(), (unsigned)utf8.length(), str);
131  }
132 #endif
133 }
134 
135 
136 SUMOReal
139 }
140 
141 
142 const XMLCh*
144  AttrMap::const_iterator i = myPredefinedTags.find(id);
145  assert(i != myPredefinedTags.end());
146  return myAttrs.getValue((*i).second);
147 }
148 
149 
150 SUMOReal
151 SUMOSAXAttributesImpl_Xerces::getFloat(const std::string& id) const {
152  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
153  SUMOReal result = TplConvert::_2SUMOReal(myAttrs.getValue(t));
154  XERCES_CPP_NAMESPACE::XMLString::release(&t);
155  return result;
156 }
157 
158 
159 bool
160 SUMOSAXAttributesImpl_Xerces::hasAttribute(const std::string& id) const {
161  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
162  bool result = myAttrs.getIndex(t) >= 0;
163  XERCES_CPP_NAMESPACE::XMLString::release(&t);
164  return result;
165 }
166 
167 
168 std::string
170  const std::string& str) const {
171  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
172  std::string result = TplConvert::_2strSec(myAttrs.getValue(t), str);
173  XERCES_CPP_NAMESPACE::XMLString::release(&t);
174  return result;
175 }
176 
177 
181  std::string funcString = getString(SUMO_ATTR_FUNCTION);
182  if (SUMOXMLDefinitions::EdgeFunctions.hasString(funcString)) {
183  return SUMOXMLDefinitions::EdgeFunctions.get(funcString);
184  }
185  ok = false;
186  }
187  return EDGEFUNC_NORMAL;
188 }
189 
190 
194  std::string typeString = getString(SUMO_ATTR_TYPE);
195  if (SUMOXMLDefinitions::NodeTypes.hasString(typeString)) {
196  return SUMOXMLDefinitions::NodeTypes.get(typeString);
197  }
198  ok = false;
199  }
200  return NODETYPE_UNKNOWN;
201 }
202 
203 
204 RGBColor
207 }
208 
209 
212  StringTokenizer st(getString(attr));
213  PositionVector shape;
214  while (st.hasNext()) {
215  StringTokenizer pos(st.next(), ",");
216  if (pos.size() != 2 && pos.size() != 3) {
217  throw FormatException("shape format");
218  }
219  SUMOReal x = TplConvert::_2SUMOReal(pos.next().c_str());
220  SUMOReal y = TplConvert::_2SUMOReal(pos.next().c_str());
221  if (pos.size() == 2) {
222  shape.push_back(Position(x, y));
223  } else {
224  SUMOReal z = TplConvert::_2SUMOReal(pos.next().c_str());
225  shape.push_back(Position(x, y, z));
226  }
227  }
228  return shape;
229 }
230 
231 
232 Boundary
234  std::string def = getString(attr);
235  StringTokenizer st(def, ",");
236  if (st.size() != 4) {
237  throw FormatException("boundary format");
238  }
239  const SUMOReal xmin = TplConvert::_2SUMOReal(st.next().c_str());
240  const SUMOReal ymin = TplConvert::_2SUMOReal(st.next().c_str());
241  const SUMOReal xmax = TplConvert::_2SUMOReal(st.next().c_str());
242  const SUMOReal ymax = TplConvert::_2SUMOReal(st.next().c_str());
243  return Boundary(xmin, ymin, xmax, ymax);
244 }
245 
246 
247 std::vector<std::string>
249  std::string def = getString(attr);
250  std::vector<std::string> ret;
251  parseStringVector(def, ret);
252  return ret;
253 }
254 
255 
256 std::string
258  if (myPredefinedTagsMML.find(attr) == myPredefinedTagsMML.end()) {
259  return "?";
260  }
261  return myPredefinedTagsMML.find(attr)->second;
262 }
263 
264 
265 void
267  for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
268  os << " " << TplConvert::_2str(myAttrs.getLocalName(i));
269  os << "=\"" << TplConvert::_2str(myAttrs.getValue(i)) << "\"";
270  }
271 }
272 
273 
274 /****************************************************************************/
275 
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:168
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.
static SUMOReal _2SUMOReal(const E *const data)
Definition: TplConvert.h:223
static bool _2bool(const E *const data)
Definition: TplConvert.h:282
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.
bool getBool(int id) const
Returns the bool-value of the named (by its enum-value) attribute.
static SUMOLong _2long(const E *const data)
Definition: TplConvert.h:142
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.
static std::string _2str(const E *const data)
Definition: TplConvert.h:56
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.
#define SUMOLong
Definition: config.h:212
size_t size() const
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.
void push_back(const PositionVector &p)
Appends all positions from the given vector.
SUMOLong getLong(int id) const
Returns the long-value of the named (by its enum-value) attribute.
static int _2int(const E *const data)
Definition: TplConvert.h:114
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:215
std::vector< std::string > getStringVector(int attr) const
Tries to read given attribute assuming it is a string vector.
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
A color information.
static std::string _2strSec(const E *const data, const std::string &def)
Definition: TplConvert.h:302
const XMLCh * getAttributeValueSecure(int id) const
Returns Xerces-value of the named attribute.