SUMO - Simulation of Urban MObility
SUMOSAXAttributes.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Encapsulated SAX-Attributes
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2007-2016 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  if (!hasAttribute(attr)) {
66  if (report) {
67  emitUngivenError(getName(attr), objectid);
68  }
69  ok = false;
70  return -1;
71  }
72  try {
73  return TIME2STEPS(getFloat(attr));
74  } catch (NumberFormatException&) {
75  if (report) {
76  emitFormatError(getName(attr), "a time value", objectid);
77  }
78  } catch (EmptyData&) {
79  if (report) {
80  emitEmptyError(getName(attr), objectid);
81  }
82  }
83  ok = false;
84  return (SUMOTime) - 1;
85 }
86 
87 
89 SUMOSAXAttributes::getOptSUMOTimeReporting(int attr, const char* objectid,
90  bool& ok, SUMOTime defaultValue, bool report) const {
91  if (!hasAttribute(attr)) {
92  return defaultValue;
93  }
94  try {
95  return (SUMOTime)(getFloat(attr) * 1000.);
96  } catch (NumberFormatException&) {
97  if (report) {
98  emitFormatError(getName(attr), "a real number", objectid);
99  }
100  } catch (EmptyData&) {
101  if (report) {
102  emitEmptyError(getName(attr), objectid);
103  }
104  }
105  ok = false;
106  return (SUMOTime) - 1;
107 }
108 
109 
110 
111 
112 
113 void
114 SUMOSAXAttributes::emitUngivenError(const std::string& attrname, const char* objectid) const {
115  std::ostringstream oss;
116  oss << "Attribute '" << attrname << "' is missing in definition of ";
117  if (objectid == 0 || objectid[0] == 0) {
118  oss << "a " << myObjectType;
119  } else {
120  oss << myObjectType << " '" << objectid << "'";
121  }
122  oss << ".";
123  WRITE_ERROR(oss.str());
124 }
125 
126 
127 void
128 SUMOSAXAttributes::emitEmptyError(const std::string& attrname, const char* objectid) const {
129  std::ostringstream oss;
130  oss << "Attribute '" << attrname << "' in definition of ";
131  if (objectid == 0 || objectid[0] == 0) {
132  oss << "a " << myObjectType;
133  } else {
134  oss << myObjectType << " '" << objectid << "'";
135  }
136  oss << " is empty.";
137  WRITE_ERROR(oss.str());
138 }
139 
140 
141 void
142 SUMOSAXAttributes::emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const {
143  std::ostringstream oss;
144  oss << "Attribute '" << attrname << "' in definition of ";
145  if (objectid == 0 || objectid[0] == 0) {
146  oss << "a " << myObjectType;
147  } else {
148  oss << myObjectType << " '" << objectid << "'";
149  }
150  oss << " is not " << type << ".";
151  WRITE_ERROR(oss.str());
152 }
153 
154 
155 void
156 SUMOSAXAttributes::parseStringVector(const std::string& def, std::vector<std::string>& into) {
157  if (def.find(';') != std::string::npos || def.find(',') != std::string::npos) {
159  WRITE_WARNING("Please note that using ';' and ',' as XML list separators is deprecated.\n From 1.0 onwards, only ' ' will be accepted.");
161  }
162  }
163  StringTokenizer st(def, ";, ", true);
164  while (st.hasNext()) {
165  into.push_back(st.next());
166  }
167 }
168 
169 
170 void
171 SUMOSAXAttributes::parseStringSet(const std::string& def, std::set<std::string>& into) {
172  if (def.find(';') != std::string::npos || def.find(',') != std::string::npos) {
174  WRITE_WARNING("Please note that using ';' and ',' as XML list separators is deprecated.\n From 1.0 onwards, only ' ' will be accepted.");
176  }
177  }
178  StringTokenizer st(def, ";, ", true);
179  while (st.hasNext()) {
180  into.insert(st.next());
181  }
182 }
183 
184 
185 template<> const int invalid_return<int>::value = -1;
186 template<> const std::string invalid_return<int>::type = "int";
187 template<>
188 int SUMOSAXAttributes::getInternal(const int attr) const {
189  return getInt(attr);
190 }
191 
192 
193 template<> const long long int invalid_return<long long int>::value = -1;
194 template<> const std::string invalid_return<long long int>::type = "long";
195 template<>
196 long long int SUMOSAXAttributes::getInternal(const int attr) const {
197  return getLong(attr);
198 }
199 
200 
202 template<> const std::string invalid_return<SUMOReal>::type = "float";
203 template<>
205  return getFloat(attr);
206 }
207 
208 
209 template<> const bool invalid_return<bool>::value = false;
210 template<> const std::string invalid_return<bool>::type = "bool";
211 template<>
212 bool SUMOSAXAttributes::getInternal(const int attr) const {
213  return getBool(attr);
214 }
215 
216 
217 template<> const std::string invalid_return<std::string>::value = "";
218 template<> const std::string invalid_return<std::string>::type = "string";
219 template<>
220 std::string SUMOSAXAttributes::getInternal(const int attr) const {
221  const std::string ret = getString(attr);
222  if (ret == "") {
223  throw EmptyData();
224  }
225  return ret;
226 }
227 
228 
230 template<> const std::string invalid_return<RGBColor>::type = "color";
231 template<>
232 RGBColor SUMOSAXAttributes::getInternal(const int /* attr */) const {
233  return getColor();
234 }
235 
236 
238 template<> const std::string invalid_return<PositionVector>::type = "PositionVector";
239 template<>
241  return getShape(attr);
242 }
243 
244 
246 template<> const std::string invalid_return<Boundary>::type = "Boundary";
247 template<>
249  return getBoundary(attr);
250 }
251 
252 
253 /****************************************************************************/
254 
SUMOSAXAttributes(const std::string &objectType)
virtual RGBColor getColor() const =0
Returns the value of the named attribute.
long long int SUMOTime
Definition: SUMOTime.h:43
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
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
static void parseStringVector(const std::string &def, std::vector< std::string > &into)
Splits the given string.
virtual long long int getLong(int id) const =0
Returns the long-value of the named (by its enum-value) attribute.
void emitFormatError(const std::string &attrname, const std::string &type, const char *objectid) const
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.
void emitUngivenError(const std::string &attrname, const char *objectid) const
virtual Boundary getBoundary(int attr) const =0
Tries to read given attribute assuming it is a Boundary.
void emitEmptyError(const std::string &attrname, const char *objectid) const
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
static bool myHaveInformedAboutDeprecatedDivider
Information whether the usage of a deprecated divider was reported.
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
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.
#define SUMOReal
Definition: config.h:213