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-sim.org/
12 // Copyright (C) 2007-2014 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 TIME2STEPS(getFloat(attr));
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 || objectid[0] == 0) {
126  oss << "a " << myObjectType;
127  } else {
128  oss << myObjectType << " '" << objectid << "'";
129  }
130  oss << ".";
131  WRITE_ERROR(oss.str());
132 }
133 
134 
135 void
136 SUMOSAXAttributes::emitEmptyError(const std::string& attrname, const char* objectid) const {
137  std::ostringstream oss;
138  oss << "Attribute '" << attrname << "' in definition of ";
139  if (objectid == 0 || objectid[0] == 0) {
140  oss << "a " << myObjectType;
141  } else {
142  oss << myObjectType << " '" << objectid << "'";
143  }
144  oss << " is empty.";
145  WRITE_ERROR(oss.str());
146 }
147 
148 
149 void
150 SUMOSAXAttributes::emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const {
151  std::ostringstream oss;
152  oss << "Attribute '" << attrname << "' in definition of ";
153  if (objectid == 0 || objectid[0] == 0) {
154  oss << "a " << myObjectType;
155  } else {
156  oss << myObjectType << " '" << objectid << "'";
157  }
158  oss << " is not " << type << ".";
159  WRITE_ERROR(oss.str());
160 }
161 
162 
163 void
164 SUMOSAXAttributes::parseStringVector(const std::string& def, std::vector<std::string>& into) {
165  if (def.find(';') != std::string::npos || def.find(',') != std::string::npos) {
167  WRITE_WARNING("Please note that using ';' and ',' as XML list separators is deprecated.\n From 1.0 onwards, only ' ' will be accepted.");
169  }
170  }
171  StringTokenizer st(def, ";, ", true);
172  while (st.hasNext()) {
173  into.push_back(st.next());
174  }
175 }
176 
177 
178 void
179 SUMOSAXAttributes::parseStringSet(const std::string& def, std::set<std::string>& into) {
180  if (def.find(';') != std::string::npos || def.find(',') != std::string::npos) {
182  WRITE_WARNING("Please note that using ';' and ',' as XML list separators is deprecated.\n From 1.0 onwards, only ' ' will be accepted.");
184  }
185  }
186  StringTokenizer st(def, ";, ", true);
187  while (st.hasNext()) {
188  into.insert(st.next());
189  }
190 }
191 
192 
193 template<> const int invalid_return<int>::value = -1;
194 template<> const std::string invalid_return<int>::type = "int";
195 template<>
196 int SUMOSAXAttributes::getInternal(const int attr) const {
197  return getInt(attr);
198 }
199 
200 
202 template<> const std::string invalid_return<SUMOLong>::type = "long";
203 template<>
204 SUMOLong SUMOSAXAttributes::getInternal(const int attr) const {
205  return getLong(attr);
206 }
207 
208 
210 template<> const std::string invalid_return<SUMOReal>::type = "float";
211 template<>
212 SUMOReal SUMOSAXAttributes::getInternal(const int attr) const {
213  return getFloat(attr);
214 }
215 
216 
217 template<> const bool invalid_return<bool>::value = false;
218 template<> const std::string invalid_return<bool>::type = "bool";
219 template<>
220 bool SUMOSAXAttributes::getInternal(const int attr) const {
221  return getBool(attr);
222 }
223 
224 
225 template<> const std::string invalid_return<std::string>::value = "";
226 template<> const std::string invalid_return<std::string>::type = "string";
227 template<>
228 std::string SUMOSAXAttributes::getInternal(const int attr) const {
229  const std::string ret = getString(attr);
230  if (ret == "") {
231  throw EmptyData();
232  }
233  return ret;
234 }
235 
236 
238 template<> const std::string invalid_return<RGBColor>::type = "color";
239 template<>
240 RGBColor SUMOSAXAttributes::getInternal(const int /* attr */) const {
241  return getColor();
242 }
243 
244 
246 template<> const std::string invalid_return<PositionVector>::type = "PositionVector";
247 template<>
248 PositionVector SUMOSAXAttributes::getInternal(const int attr) const {
249  return getShape(attr);
250 }
251 
252 
254 template<> const std::string invalid_return<Boundary>::type = "Boundary";
255 template<>
256 Boundary SUMOSAXAttributes::getInternal(const int attr) const {
257  return getBoundary(attr);
258 }
259 
260 
261 /****************************************************************************/
262