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.sourceforge.net/
12 // Copyright (C) 2001-2012 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
79  return TplConvert::_2bool(getAttributeValueSecure(id));
80 }
81 
82 
83 bool
85  return TplConvert::_2boolSec(getAttributeValueSecure(id), val);
86 }
87 
88 
89 int
92 }
93 
94 
95 int
97  int def) const {
99 }
100 
101 
102 SUMOLong
105 }
106 
107 
108 std::string
110  const XMLCh* utf16 = getAttributeValueSecure(id);
111 #if _XERCES_VERSION < 30100
112  char* t = XERCES_CPP_NAMESPACE::XMLString::transcode(utf16);
113  std::string result(t);
114  XERCES_CPP_NAMESPACE::XMLString::release(&t);
115  return result;
116 #else
117  if (XERCES_CPP_NAMESPACE::XMLString::stringLen(utf16) == 0) {
118  // TranscodeToStr and debug_new interact badly in this case;
119  return "";
120  } else {
121  XERCES_CPP_NAMESPACE::TranscodeToStr utf8(utf16, "UTF-8");
122  return TplConvert::_2str(utf8.str(), (unsigned)utf8.length());
123  }
124 #endif
125 }
126 
127 
128 std::string
130  const std::string& str) const throw(EmptyData) {
131  const XMLCh* utf16 = getAttributeValueSecure(id);
132 #if _XERCES_VERSION < 30100
133  char* t = XERCES_CPP_NAMESPACE::XMLString::transcode(utf16);
134  std::string result(TplConvert::_2strSec(t, str));
135  XERCES_CPP_NAMESPACE::XMLString::release(&t);
136  return result;
137 #else
138  if (XERCES_CPP_NAMESPACE::XMLString::stringLen(utf16) == 0) {
139  // TranscodeToStr and debug_new interact badly in this case;
140  return "";
141  } else {
142  XERCES_CPP_NAMESPACE::TranscodeToStr utf8(utf16, "UTF-8");
143  return TplConvert::_2strSec(utf8.str(), (unsigned)utf8.length(), str);
144  }
145 #endif
146 }
147 
148 
149 SUMOReal
152 }
153 
154 
155 SUMOReal
157  SUMOReal def) const {
159 }
160 
161 
162 const XMLCh*
164  AttrMap::const_iterator i = myPredefinedTags.find(id);
165  assert(i != myPredefinedTags.end());
166  return myAttrs.getValue((*i).second);
167 }
168 
169 
170 SUMOReal
171 SUMOSAXAttributesImpl_Xerces::getFloat(const std::string& id) const {
172  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
173  SUMOReal result = TplConvert::_2SUMOReal(myAttrs.getValue(t));
174  XERCES_CPP_NAMESPACE::XMLString::release(&t);
175  return result;
176 }
177 
178 
179 bool
180 SUMOSAXAttributesImpl_Xerces::hasAttribute(const std::string& id) const {
181  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
182  bool result = myAttrs.getIndex(t) >= 0;
183  XERCES_CPP_NAMESPACE::XMLString::release(&t);
184  return result;
185 }
186 
187 
188 std::string
190  const std::string& str) const {
191  XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
192  std::string result = TplConvert::_2strSec(myAttrs.getValue(t), str);
193  XERCES_CPP_NAMESPACE::XMLString::release(&t);
194  return result;
195 }
196 
197 
201  std::string funcString = getString(SUMO_ATTR_FUNCTION);
202  if (SUMOXMLDefinitions::EdgeFunctions.hasString(funcString)) {
203  return SUMOXMLDefinitions::EdgeFunctions.get(funcString);
204  }
205  ok = false;
206  }
207  return EDGEFUNC_NORMAL;
208 }
209 
210 
214  std::string typeString = getString(SUMO_ATTR_TYPE);
215  if (SUMOXMLDefinitions::NodeTypes.hasString(typeString)) {
216  return SUMOXMLDefinitions::NodeTypes.get(typeString);
217  }
218  ok = false;
219  }
220  return NODETYPE_UNKNOWN;
221 }
222 
223 
224 RGBColor
225 SUMOSAXAttributesImpl_Xerces::getColorReporting(const char* objectid, bool& ok) const {
226  try {
228  } catch (NumberFormatException&) {
229  } catch (EmptyData&) {
230  }
231  ok = false;
232  emitFormatError("color", "a valid color", objectid);
233  return RGBColor();
234 }
235 
236 
238 SUMOSAXAttributesImpl_Xerces::getShapeReporting(int attr, const char* objectid, bool& ok,
239  bool allowEmpty) const {
240  std::string shpdef = getOptStringReporting(attr, objectid, ok, "");
241  if (shpdef == "") {
242  if (!allowEmpty) {
243  emitEmptyError(getName(attr), objectid);
244  ok = false;
245  }
246  return PositionVector();
247  }
248  StringTokenizer st(shpdef, " ");
249  PositionVector shape;
250  while (st.hasNext()) {
251  StringTokenizer pos(st.next(), ",");
252  if (pos.size() != 2 && pos.size() != 3) {
253  emitFormatError(getName(attr), "x,y or x,y,z", objectid);
254  ok = false;
255  return PositionVector();
256  }
257  try {
258  SUMOReal x = TplConvert::_2SUMOReal(pos.next().c_str());
259  SUMOReal y = TplConvert::_2SUMOReal(pos.next().c_str());
260  if (pos.size() == 2) {
261  shape.push_back(Position(x, y));
262  } else {
263  SUMOReal z = TplConvert::_2SUMOReal(pos.next().c_str());
264  shape.push_back(Position(x, y, z));
265  }
266  } catch (NumberFormatException&) {
267  emitFormatError(getName(attr), "all numeric position entries", objectid);
268  ok = false;
269  return PositionVector();
270  } catch (EmptyData&) {
271  emitFormatError(getName(attr), "all valid entries", objectid);
272  ok = false;
273  return PositionVector();
274  }
275  }
276  return shape;
277 }
278 
279 
280 Boundary
281 SUMOSAXAttributesImpl_Xerces::getBoundaryReporting(int attr, const char* objectid, bool& ok) const {
282  std::string def = getStringReporting(attr, objectid, ok);
283  StringTokenizer st(def, ",");
284  if (st.size() != 4) {
285  emitFormatError(getName(attr), "a valid number of entries", objectid);
286  ok = false;
287  return Boundary();
288  }
289  try {
290  const SUMOReal xmin = TplConvert::_2SUMOReal(st.next().c_str());
291  const SUMOReal ymin = TplConvert::_2SUMOReal(st.next().c_str());
292  const SUMOReal xmax = TplConvert::_2SUMOReal(st.next().c_str());
293  const SUMOReal ymax = TplConvert::_2SUMOReal(st.next().c_str());
294  return Boundary(xmin, ymin, xmax, ymax);
295  } catch (NumberFormatException&) {
296  emitFormatError(getName(attr), "all numeric entries", objectid);
297  } catch (EmptyData&) {
298  emitFormatError(getName(attr), "all valid entries", objectid);
299  }
300  ok = false;
301  return Boundary();
302 }
303 
304 
305 std::string
307  if (myPredefinedTagsMML.find(attr) == myPredefinedTagsMML.end()) {
308  return "?";
309  }
310  return myPredefinedTagsMML.find(attr)->second;
311 }
312 
313 
314 void
316  for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
317  os << " " << TplConvert::_2str(myAttrs.getLocalName(i));
318  os << "=\"" << TplConvert::_2str(myAttrs.getValue(i)) << "\"";
319  }
320 }
321 
322 
323 /****************************************************************************/
324