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) 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 void
185 SUMOSAXAttributes::parseStringSet(const std::string& def, std::set<std::string>& into) {
186  if (def.find(';') != std::string::npos || def.find(',') != std::string::npos) {
188  WRITE_WARNING("Please note that using ';' and ',' as XML list separators is deprecated.\n From 1.0 onwards, only ' ' will be accepted.");
190  }
191  }
192  StringTokenizer st(def, ";, ", true);
193  while (st.hasNext()) {
194  into.insert(st.next());
195  }
196 }
197 
198 
199 template<> const int invalid_return<int>::value = -1;
200 template<> const std::string invalid_return<int>::type = "int";
201 template<>
202 int SUMOSAXAttributes::getInternal(const int attr) const {
203  return getInt(attr);
204 }
205 
206 
208 template<> const std::string invalid_return<SUMOLong>::type = "long";
209 template<>
211  return getLong(attr);
212 }
213 
214 
216 template<> const std::string invalid_return<SUMOReal>::type = "float";
217 template<>
219  return getFloat(attr);
220 }
221 
222 
223 template<> const bool invalid_return<bool>::value = false;
224 template<> const std::string invalid_return<bool>::type = "bool";
225 template<>
226 bool SUMOSAXAttributes::getInternal(const int attr) const {
227  return getBool(attr);
228 }
229 
230 
231 template<> const std::string invalid_return<std::string>::value = "";
232 template<> const std::string invalid_return<std::string>::type = "string";
233 template<>
234 std::string SUMOSAXAttributes::getInternal(const int attr) const {
235  const std::string ret = getString(attr);
236  if (ret == "") {
237  throw EmptyData();
238  }
239  return ret;
240 }
241 
242 
244 template<> const std::string invalid_return<RGBColor>::type = "color";
245 template<>
246 RGBColor SUMOSAXAttributes::getInternal(const int /* attr */) const {
247  return getColor();
248 }
249 
250 
252 template<> const std::string invalid_return<PositionVector>::type = "PositionVector";
253 template<>
255  return getShape(attr);
256 }
257 
258 
260 template<> const std::string invalid_return<Boundary>::type = "Boundary";
261 template<>
263  return getBoundary(attr);
264 }
265 
266 
267 /****************************************************************************/
268 
SUMOSAXAttributes(const std::string &objectType)
virtual RGBColor getColor() const =0
Returns the value of the named attribute.
static void parseStringSet(const std::string &def, std::set< std::string > &into)
Splits the given string, stores it in a set.
std::string next()
virtual PositionVector getShape(int attr) const =0
Tries to read given attribute assuming it is a PositionVector.
virtual std::string getName(int attr) const =0
Converts the given attribute id into a man readable string.
std::string myObjectType
the object type to use in error reporting
void emitUngivenError(const std::string &attrname, const char *objectid) const
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:196
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
SUMOTime getOptSUMOTimeReporting(int attr, const char *objectid, bool &ok, SUMOTime defaultValue, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
T getInternal(const int attr) const
static void parseStringVector(const std::string &def, std::vector< std::string > &into)
Splits the given string.
virtual SUMOLong getLong(int id) const =0
Returns the long-value of the named (by its enum-value) attribute.
A list of positions.
virtual SUMOReal getFloat(int id) const =0
Returns the SUMOReal-value of the named (by its enum-value) attribute.
static const std::string ENCODING
The encoding of parsed strings.
#define SUMOLong
Definition: config.h:218
virtual Boundary getBoundary(int attr) const =0
Tries to read given attribute assuming it is a Boundary.
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:201
static bool myHaveInformedAboutDeprecatedDivider
Information whether the usage of a deprecated divider was reported.
int SUMOTime
Definition: SUMOTime.h:43
virtual int getInt(int id) const =0
Returns the int-value of the named (by its enum-value) attribute.
virtual bool getBool(int id) const =0
Returns the bool-value of the named (by its enum-value) attribute.
void emitEmptyError(const std::string &attrname, const char *objectid) const
#define SUMOReal
Definition: config.h:221
void emitFormatError(const std::string &attrname, const std::string &type, const char *objectid) const