SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SUMOSAXAttributes.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Encapsulated SAX-Attributes
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2013 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 <string>
34 #include <iostream>
35 #include <sstream>
37 #include <utils/common/RGBColor.h>
39 #include <utils/geom/Boundary.h>
41 #include "SUMOSAXAttributes.h"
42 
43 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 
48 // ===========================================================================
49 // static members
50 // ===========================================================================
52 const std::string SUMOSAXAttributes::ENCODING = " encoding=\"UTF-8\"";
53 
54 
55 // ===========================================================================
56 // method definitions
57 // ===========================================================================
58 SUMOSAXAttributes::SUMOSAXAttributes(const std::string& objectType):
59  myObjectType(objectType) {}
60 
61 
63 SUMOSAXAttributes::getSUMOTimeReporting(int attr, const char* objectid,
64  bool& ok, bool report) const {
65 #ifdef HAVE_SUBSECOND_TIMESTEPS
66  if (!hasAttribute(attr)) {
67  if (report) {
68  emitUngivenError(getName(attr), objectid);
69  }
70  ok = false;
71  return -1;
72  }
73  try {
74  return (SUMOTime)(getFloat(attr) * 1000.);
75  } catch (NumberFormatException&) {
76  if (report) {
77  emitFormatError(getName(attr), "a time value", objectid);
78  }
79  } catch (EmptyData&) {
80  if (report) {
81  emitEmptyError(getName(attr), objectid);
82  }
83  }
84  ok = false;
85  return (SUMOTime) - 1;
86 #else
87  return get<int>(attr, objectid, ok, report);
88 #endif
89 }
90 
91 
93 SUMOSAXAttributes::getOptSUMOTimeReporting(int attr, const char* objectid,
94  bool& ok, SUMOTime defaultValue, bool report) const {
95 #ifdef HAVE_SUBSECOND_TIMESTEPS
96  if (!hasAttribute(attr)) {
97  return defaultValue;
98  }
99  try {
100  return (SUMOTime)(getFloat(attr) * 1000.);
101  } catch (NumberFormatException&) {
102  if (report) {
103  emitFormatError(getName(attr), "a real number", objectid);
104  }
105  } catch (EmptyData&) {
106  if (report) {
107  emitEmptyError(getName(attr), objectid);
108  }
109  }
110  ok = false;
111  return (SUMOTime) - 1;
112 #else
113  return getOpt<int>(attr, objectid, ok, defaultValue, report);
114 #endif
115 }
116 
117 
118 
119 
120 
121 void
122 SUMOSAXAttributes::emitUngivenError(const std::string& attrname, const char* objectid) const {
123  std::ostringstream oss;
124  oss << "Attribute '" << attrname << "' is missing in definition of ";
125  if (objectid == 0) {
126  oss << "a ";
127  }
128  oss << myObjectType;
129  if (objectid != 0) {
130  oss << " '" << objectid << "'";
131  }
132  oss << ".";
133  WRITE_ERROR(oss.str());
134 }
135 
136 
137 void
138 SUMOSAXAttributes::emitEmptyError(const std::string& attrname, const char* objectid) const {
139  std::ostringstream oss;
140  oss << "Attribute '" << attrname << "' in definition of ";
141  if (objectid == 0) {
142  oss << "a ";
143  }
144  oss << myObjectType;
145  if (objectid != 0) {
146  oss << " '" << objectid << "'";
147  }
148  oss << " is empty.";
149  WRITE_ERROR(oss.str());
150 }
151 
152 
153 void
154 SUMOSAXAttributes::emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const {
155  std::ostringstream oss;
156  oss << "Attribute '" << attrname << "' in definition of ";
157  if (objectid == 0) {
158  oss << "a ";
159  }
160  oss << myObjectType;
161  if (objectid != 0) {
162  oss << " '" << objectid << "'";
163  }
164  oss << " is not " << type << ".";
165  WRITE_ERROR(oss.str());
166 }
167 
168 
169 void
170 SUMOSAXAttributes::parseStringVector(const std::string& def, std::vector<std::string>& into) {
171  if (def.find(';') != std::string::npos || def.find(',') != std::string::npos) {
173  WRITE_WARNING("Please note that using ';' and ',' as XML list separators is deprecated.\n From 1.0 onwards, only ' ' will be accepted.");
175  }
176  }
177  StringTokenizer st(def, ";, ", true);
178  while (st.hasNext()) {
179  into.push_back(st.next());
180  }
181 }
182 
183 
184 template<> const int invalid_return<int>::value = -1;
185 template<> const std::string invalid_return<int>::type = "int";
186 template<>
187 int SUMOSAXAttributes::getInternal(const int attr) const {
188  return getInt(attr);
189 }
190 
191 
193 template<> const std::string invalid_return<SUMOLong>::type = "long";
194 template<>
195 SUMOLong SUMOSAXAttributes::getInternal(const int attr) const {
196  return getLong(attr);
197 }
198 
199 
201 template<> const std::string invalid_return<SUMOReal>::type = "float";
202 template<>
203 SUMOReal SUMOSAXAttributes::getInternal(const int attr) const {
204  return getFloat(attr);
205 }
206 
207 
208 template<> const bool invalid_return<bool>::value = false;
209 template<> const std::string invalid_return<bool>::type = "bool";
210 template<>
211 bool SUMOSAXAttributes::getInternal(const int attr) const {
212  return getBool(attr);
213 }
214 
215 
216 template<> const std::string invalid_return<std::string>::value = "";
217 template<> const std::string invalid_return<std::string>::type = "string";
218 template<>
219 std::string SUMOSAXAttributes::getInternal(const int attr) const {
220  const std::string ret = getString(attr);
221  if (ret == "") {
222  throw EmptyData();
223  }
224  return ret;
225 }
226 
227 
229 template<> const std::string invalid_return<RGBColor>::type = "color";
230 template<>
231 RGBColor SUMOSAXAttributes::getInternal(const int attr) const {
232  return getColor();
233 }
234 
235 
237 template<> const std::string invalid_return<PositionVector>::type = "PositionVector";
238 template<>
239 PositionVector SUMOSAXAttributes::getInternal(const int attr) const {
240  return getShape(attr);
241 }
242 
243 
245 template<> const std::string invalid_return<Boundary>::type = "Boundary";
246 template<>
247 Boundary SUMOSAXAttributes::getInternal(const int attr) const {
248  return getBoundary(attr);
249 }
250 
251 
252 /****************************************************************************/
253