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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Encapsulated Xerces-SAX-attributes
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <cassert>
33 #include <xercesc/sax2/Attributes.hpp>
34 #include <xercesc/sax2/DefaultHandler.hpp>
35 #include <xercesc/util/XercesVersion.hpp>
36 #include <xercesc/util/TransService.hpp>
37 #include <xercesc/util/TranscodingException.hpp>
38 #include <utils/common/RGBColor.h>
41 #include <utils/geom/Boundary.h>
45 
46 
47 // ===========================================================================
48 // class definitions
49 // ===========================================================================
50 SUMOSAXAttributesImpl_Xerces::SUMOSAXAttributesImpl_Xerces(const XERCES_CPP_NAMESPACE::Attributes& attrs,
51  const std::map<int, XMLCh*>& predefinedTags,
52  const std::map<int, std::string>& predefinedTagsMML,
53  const std::string& objectType) :
54  SUMOSAXAttributes(objectType),
55  myAttrs(attrs),
56  myPredefinedTags(predefinedTags),
57  myPredefinedTagsMML(predefinedTagsMML) { }
58 
59 
61 }
62 
63 
64 bool
66  AttrMap::const_iterator i = myPredefinedTags.find(id);
67  if (i == myPredefinedTags.end()) {
68  return false;
69  }
70  return myAttrs.getIndex((*i).second) >= 0;
71 }
72 
73 
74 bool
77 }
78 
79 
80 int
83 }
84 
85 
86 long long int
89 }
90 
91 
92 std::string
94  const XMLCh* utf16 = getAttributeValueSecure(id);
95 #if _XERCES_VERSION < 30100
96  char* t = XERCES_CPP_NAMESPACE::XMLString::transcode(utf16);
97  std::string result(t);
98  XERCES_CPP_NAMESPACE::XMLString::release(&t);
99  return result;
100 #else
101  if (XERCES_CPP_NAMESPACE::XMLString::stringLen(utf16) == 0) {
102  // TranscodeToStr and debug_new interact badly in this case;
103  return "";
104  } else {
105  try {
106  XERCES_CPP_NAMESPACE::TranscodeToStr utf8(utf16, "UTF-8");
107  return TplConvert::_2str(utf8.str(), (unsigned)utf8.length());
108  } catch (XERCES_CPP_NAMESPACE::TranscodingException e) {
109  return "?";
110  }
111  }
112 #endif
113 }
114 
115 
116 std::string
118  const std::string& str) const {
119  const XMLCh* utf16 = getAttributeValueSecure(id);
120 #if _XERCES_VERSION < 30100
121  char* t = XERCES_CPP_NAMESPACE::XMLString::transcode(utf16);
122  std::string result(TplConvert::_2strSec(t, str));
123  XERCES_CPP_NAMESPACE::XMLString::release(&t);
124  return result;
125 #else
126  if (XERCES_CPP_NAMESPACE::XMLString::stringLen(utf16) == 0) {
127  // TranscodeToStr and debug_new interact badly in this case;
128  return "";
129  } else {
130  try {
131  XERCES_CPP_NAMESPACE::TranscodeToStr utf8(utf16, "UTF-8");
132  return TplConvert::_2strSec(utf8.str(), (unsigned)utf8.length(), str);
133  } catch (XERCES_CPP_NAMESPACE::TranscodingException e) {
134  return "?";
135  }
136  }
137 #endif
138 }
139 
140 
141 double
144 }
145 
146 
147 const XMLCh*
149  AttrMap::const_iterator i = myPredefinedTags.find(id);
150  assert(i != myPredefinedTags.end());
151  return myAttrs.getValue((*i).second);
152 }
153 
154 
155 double
156 SUMOSAXAttributesImpl_Xerces::getFloat(const std::string& id) const {
157  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
158  double result = TplConvert::_2double(myAttrs.getValue(t));
159  XERCES_CPP_NAMESPACE::XMLString::release(&t);
160  return result;
161 }
162 
163 
164 bool
165 SUMOSAXAttributesImpl_Xerces::hasAttribute(const std::string& id) const {
166  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
167  bool result = myAttrs.getIndex(t) >= 0;
168  XERCES_CPP_NAMESPACE::XMLString::release(&t);
169  return result;
170 }
171 
172 
173 std::string
175  const std::string& str) const {
176  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
177  std::string result = TplConvert::_2strSec(myAttrs.getValue(t), str);
178  XERCES_CPP_NAMESPACE::XMLString::release(&t);
179  return result;
180 }
181 
182 
186  std::string funcString = getString(SUMO_ATTR_FUNCTION);
187  if (SUMOXMLDefinitions::EdgeFunctions.hasString(funcString)) {
188  return SUMOXMLDefinitions::EdgeFunctions.get(funcString);
189  }
190  ok = false;
191  }
192  return EDGEFUNC_NORMAL;
193 }
194 
195 
199  std::string typeString = getString(SUMO_ATTR_TYPE);
200  if (SUMOXMLDefinitions::NodeTypes.hasString(typeString)) {
201  return SUMOXMLDefinitions::NodeTypes.get(typeString);
202  }
203  ok = false;
204  }
205  return NODETYPE_UNKNOWN;
206 }
207 
208 
209 RGBColor
212 }
213 
214 
217  StringTokenizer st(getString(attr));
218  PositionVector shape;
219  while (st.hasNext()) {
220  StringTokenizer pos(st.next(), ",");
221  if (pos.size() != 2 && pos.size() != 3) {
222  throw FormatException("shape format");
223  }
224  double x = TplConvert::_2double(pos.next().c_str());
225  double y = TplConvert::_2double(pos.next().c_str());
226  if (pos.size() == 2) {
227  shape.push_back(Position(x, y));
228  } else {
229  double z = TplConvert::_2double(pos.next().c_str());
230  shape.push_back(Position(x, y, z));
231  }
232  }
233  return shape;
234 }
235 
236 
237 Boundary
239  std::string def = getString(attr);
240  StringTokenizer st(def, ",");
241  if (st.size() != 4) {
242  throw FormatException("boundary format");
243  }
244  const double xmin = TplConvert::_2double(st.next().c_str());
245  const double ymin = TplConvert::_2double(st.next().c_str());
246  const double xmax = TplConvert::_2double(st.next().c_str());
247  const double ymax = TplConvert::_2double(st.next().c_str());
248  return Boundary(xmin, ymin, xmax, ymax);
249 }
250 
251 
252 std::vector<std::string>
254  std::string def = getString(attr);
255  std::vector<std::string> ret;
256  parseStringVector(def, ret);
257  return ret;
258 }
259 
260 
261 std::string
263  if (myPredefinedTagsMML.find(attr) == myPredefinedTagsMML.end()) {
264  return "?";
265  }
266  return myPredefinedTagsMML.find(attr)->second;
267 }
268 
269 
270 void
272  for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
273  os << " " << TplConvert::_2str(myAttrs.getLocalName(i));
274  os << "=\"" << TplConvert::_2str(myAttrs.getValue(i)) << "\"";
275  }
276 }
277 
278 
281  std::map<std::string, std::string> attrs;
282  for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
283  attrs[TplConvert::_2str(myAttrs.getLocalName(i))] = TplConvert::_2str(myAttrs.getValue(i));
284  }
286 }
287 
288 /****************************************************************************/
289 
static StringBijection< SumoXMLNodeType > NodeTypes
node types
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:179
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:388
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:210
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:47
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:45
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:155
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:311
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:56
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:132
Boundary getBoundary(int attr) const
Tries to read given attribute assuming it is a Boundary.