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-2018 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 <xercesc/util/XercesVersion.hpp>
30 #include <xercesc/util/TransService.hpp>
31 #include <xercesc/util/TranscodingException.hpp>
32 #include <utils/common/RGBColor.h>
35 #include <utils/geom/Boundary.h>
39 
40 
41 // ===========================================================================
42 // class definitions
43 // ===========================================================================
44 SUMOSAXAttributesImpl_Xerces::SUMOSAXAttributesImpl_Xerces(const XERCES_CPP_NAMESPACE::Attributes& attrs,
45  const std::map<int, XMLCh*>& predefinedTags,
46  const std::map<int, std::string>& predefinedTagsMML,
47  const std::string& objectType) :
48  SUMOSAXAttributes(objectType),
49  myAttrs(attrs),
50  myPredefinedTags(predefinedTags),
51  myPredefinedTagsMML(predefinedTagsMML) { }
52 
53 
55 }
56 
57 
58 bool
60  AttrMap::const_iterator i = myPredefinedTags.find(id);
61  if (i == myPredefinedTags.end()) {
62  return false;
63  }
64  return myAttrs.getIndex((*i).second) >= 0;
65 }
66 
67 
68 bool
71 }
72 
73 
74 int
77 }
78 
79 
80 long long int
83 }
84 
85 
86 std::string
88  const XMLCh* utf16 = getAttributeValueSecure(id);
89 #if _XERCES_VERSION < 30100
90  char* t = XERCES_CPP_NAMESPACE::XMLString::transcode(utf16);
91  std::string result(t);
92  XERCES_CPP_NAMESPACE::XMLString::release(&t);
93  return result;
94 #else
95  if (XERCES_CPP_NAMESPACE::XMLString::stringLen(utf16) == 0) {
96  // TranscodeToStr and debug_new interact badly in this case;
97  return "";
98  } else {
99  try {
100  XERCES_CPP_NAMESPACE::TranscodeToStr utf8(utf16, "UTF-8");
101  return TplConvert::_2str(utf8.str(), (unsigned)utf8.length());
102  } catch (XERCES_CPP_NAMESPACE::TranscodingException&) {
103  return "?";
104  }
105  }
106 #endif
107 }
108 
109 
110 std::string
112  const std::string& str) const {
113  const XMLCh* utf16 = getAttributeValueSecure(id);
114 #if _XERCES_VERSION < 30100
115  char* t = XERCES_CPP_NAMESPACE::XMLString::transcode(utf16);
116  std::string result(TplConvert::_2strSec(t, str));
117  XERCES_CPP_NAMESPACE::XMLString::release(&t);
118  return result;
119 #else
120  if (XERCES_CPP_NAMESPACE::XMLString::stringLen(utf16) == 0) {
121  // TranscodeToStr and debug_new interact badly in this case;
122  return "";
123  } else {
124  try {
125  XERCES_CPP_NAMESPACE::TranscodeToStr utf8(utf16, "UTF-8");
126  return TplConvert::_2strSec(utf8.str(), (unsigned)utf8.length(), str);
127  } catch (XERCES_CPP_NAMESPACE::TranscodingException&) {
128  return "?";
129  }
130  }
131 #endif
132 }
133 
134 
135 double
138 }
139 
140 
141 const XMLCh*
143  AttrMap::const_iterator i = myPredefinedTags.find(id);
144  assert(i != myPredefinedTags.end());
145  return myAttrs.getValue((*i).second);
146 }
147 
148 
149 double
150 SUMOSAXAttributesImpl_Xerces::getFloat(const std::string& id) const {
151  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
152  double result = TplConvert::_2double(myAttrs.getValue(t));
153  XERCES_CPP_NAMESPACE::XMLString::release(&t);
154  return result;
155 }
156 
157 
158 bool
159 SUMOSAXAttributesImpl_Xerces::hasAttribute(const std::string& id) const {
160  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
161  bool result = myAttrs.getIndex(t) >= 0;
162  XERCES_CPP_NAMESPACE::XMLString::release(&t);
163  return result;
164 }
165 
166 
167 std::string
169  const std::string& str) const {
170  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
171  std::string result = TplConvert::_2strSec(myAttrs.getValue(t), str);
172  XERCES_CPP_NAMESPACE::XMLString::release(&t);
173  return result;
174 }
175 
176 
180  std::string funcString = getString(SUMO_ATTR_FUNCTION);
181  if (SUMOXMLDefinitions::EdgeFunctions.hasString(funcString)) {
182  return SUMOXMLDefinitions::EdgeFunctions.get(funcString);
183  }
184  ok = false;
185  }
186  return EDGEFUNC_NORMAL;
187 }
188 
189 
193  std::string typeString = getString(SUMO_ATTR_TYPE);
194  if (SUMOXMLDefinitions::NodeTypes.hasString(typeString)) {
195  return SUMOXMLDefinitions::NodeTypes.get(typeString);
196  }
197  ok = false;
198  }
199  return NODETYPE_UNKNOWN;
200 }
201 
202 
203 RGBColor
206 }
207 
208 
211  StringTokenizer st(getString(attr));
212  PositionVector shape;
213  while (st.hasNext()) {
214  StringTokenizer pos(st.next(), ",");
215  if (pos.size() != 2 && pos.size() != 3) {
216  throw FormatException("shape format");
217  }
218  double x = TplConvert::_2double(pos.next().c_str());
219  double y = TplConvert::_2double(pos.next().c_str());
220  if (pos.size() == 2) {
221  shape.push_back(Position(x, y));
222  } else {
223  double z = TplConvert::_2double(pos.next().c_str());
224  shape.push_back(Position(x, y, z));
225  }
226  }
227  return shape;
228 }
229 
230 
231 Boundary
233  std::string def = getString(attr);
234  StringTokenizer st(def, ",");
235  if (st.size() != 4) {
236  throw FormatException("boundary format");
237  }
238  const double xmin = TplConvert::_2double(st.next().c_str());
239  const double ymin = TplConvert::_2double(st.next().c_str());
240  const double xmax = TplConvert::_2double(st.next().c_str());
241  const double ymax = TplConvert::_2double(st.next().c_str());
242  return Boundary(xmin, ymin, xmax, ymax);
243 }
244 
245 
246 std::vector<std::string>
248  std::string def = getString(attr);
249  std::vector<std::string> ret;
250  parseStringVector(def, ret);
251  return ret;
252 }
253 
254 
255 std::string
257  if (myPredefinedTagsMML.find(attr) == myPredefinedTagsMML.end()) {
258  return "?";
259  }
260  return myPredefinedTagsMML.find(attr)->second;
261 }
262 
263 
264 void
266  for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
267  os << " " << TplConvert::_2str(myAttrs.getLocalName(i));
268  os << "=\"" << TplConvert::_2str(myAttrs.getValue(i)) << "\"";
269  }
270 }
271 
272 
275  std::map<std::string, std::string> attrs;
276  for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
277  attrs[TplConvert::_2str(myAttrs.getLocalName(i))] = TplConvert::_2str(myAttrs.getValue(i));
278  }
280 }
281 
282 /****************************************************************************/
283 
static StringBijection< SumoXMLNodeType > NodeTypes
node types
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:173
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:382
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:204
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: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.
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: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...
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:305
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:50
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:126
Boundary getBoundary(int attr) const
Tries to read given attribute assuming it is a Boundary.