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>
41 
42 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 
47 // ===========================================================================
48 // class definitions
49 // ===========================================================================
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
76  return TplConvert<XMLCh>::_2bool(getAttributeValueSecure(id));
77 }
78 
79 
80 bool
82  return TplConvertSec<XMLCh>::_2boolSec(getAttributeValueSecure(id), val);
83 }
84 
85 
86 int
88  return TplConvert<XMLCh>::_2int(getAttributeValueSecure(id));
89 }
90 
91 
92 int
94  int def) const throw(EmptyData, NumberFormatException) {
95  return TplConvertSec<XMLCh>::_2intSec(getAttributeValueSecure(id), def);
96 }
97 
98 
99 long
101  return TplConvert<XMLCh>::_2long(getAttributeValueSecure(id));
102 }
103 
104 
105 std::string
107  const XMLCh* utf16 = getAttributeValueSecure(id);
108 #if _XERCES_VERSION < 30000
109  return TplConvert<XMLCh>::_2str(utf16);
110 #else
111  if (XMLString::stringLen(utf16) == 0) {
112  // TranscodeToStr and debug_new interact badly in this case;
113  return "";
114  } else {
115  TranscodeToStr utf8(utf16, "UTF-8");
116  return TplConvert<XMLByte>::_2str(utf8.str(), (unsigned)utf8.length());
117  }
118 #endif
119 }
120 
121 
122 std::string
124  const std::string& str) const throw(EmptyData) {
125  const XMLCh* utf16 = getAttributeValueSecure(id);
126 #if _XERCES_VERSION < 30000
127  return TplConvertSec<XMLCh>::_2strSec(utf16, str);
128 #else
129  if (XMLString::stringLen(utf16) == 0) {
130  // TranscodeToStr and debug_new interact badly in this case;
131  return "";
132  } else {
133  TranscodeToStr utf8(utf16, "UTF-8");
134  return TplConvertSec<XMLByte>::_2strSec(utf8.str(), (int)utf8.length(), str);
135  }
136 #endif
137 }
138 
139 
140 SUMOReal
142  return TplConvert<XMLCh>::_2SUMOReal(getAttributeValueSecure(id));
143 }
144 
145 
146 SUMOReal
148  SUMOReal def) const throw(EmptyData, NumberFormatException) {
149  return TplConvertSec<XMLCh>::_2SUMORealSec(getAttributeValueSecure(id), def);
150 }
151 
152 
153 const XMLCh*
155  AttrMap::const_iterator i = myPredefinedTags.find(id);
156  assert(i != myPredefinedTags.end());
157  return myAttrs.getValue((*i).second);
158 }
159 
160 
161 SUMOReal
163  XMLCh* t = XMLString::transcode(id.c_str());
164  SUMOReal result = TplConvert<XMLCh>::_2SUMOReal(myAttrs.getValue(t));
165  XMLString::release(&t);
166  return result;
167 }
168 
169 
170 bool
171 SUMOSAXAttributesImpl_Xerces::hasAttribute(const std::string& id) const {
172  XMLCh* t = XMLString::transcode(id.c_str());
173  bool result = myAttrs.getIndex(t) >= 0;
174  XMLString::release(&t);
175  return result;
176 }
177 
178 
179 std::string
181  const std::string& str) const {
182  XMLCh* t = XMLString::transcode(id.c_str());
183  std::string result = TplConvertSec<XMLCh>::_2strSec(myAttrs.getValue(t), str);
184  XMLString::release(&t);
185  return result;
186 }
187 
188 
189 std::string
191  if (myPredefinedTagsMML.find(attr) == myPredefinedTagsMML.end()) {
192  return "?";
193  }
194  return myPredefinedTagsMML.find(attr)->second;
195 }
196 
197 
198 /****************************************************************************/
199