SUMO - Simulation of Urban MObility
SUMOSAXAttributes.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2007-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Encapsulated SAX-Attributes
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <string>
33 #include <iostream>
34 #include <sstream>
36 #include <utils/common/RGBColor.h>
38 #include <utils/geom/Boundary.h>
40 #include "SUMOSAXAttributes.h"
41 
42 
43 // ===========================================================================
44 // static members
45 // ===========================================================================
47 const std::string SUMOSAXAttributes::ENCODING = " encoding=\"UTF-8\"";
48 
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
53 SUMOSAXAttributes::SUMOSAXAttributes(const std::string& objectType):
54  myObjectType(objectType) {}
55 
56 
58 SUMOSAXAttributes::getSUMOTimeReporting(int attr, const char* objectid,
59  bool& ok, bool report) const {
60  if (!hasAttribute(attr)) {
61  if (report) {
62  emitUngivenError(getName(attr), objectid);
63  }
64  ok = false;
65  return -1;
66  }
67  try {
68  return TIME2STEPS(getFloat(attr));
69  } catch (NumberFormatException&) {
70  if (report) {
71  emitFormatError(getName(attr), "a time value", objectid);
72  }
73  } catch (EmptyData&) {
74  if (report) {
75  emitEmptyError(getName(attr), objectid);
76  }
77  }
78  ok = false;
79  return (SUMOTime) - 1;
80 }
81 
82 
84 SUMOSAXAttributes::getOptSUMOTimeReporting(int attr, const char* objectid,
85  bool& ok, SUMOTime defaultValue, bool report) const {
86  if (!hasAttribute(attr)) {
87  return defaultValue;
88  }
89  try {
90  return (SUMOTime)(getFloat(attr) * 1000.);
91  } catch (NumberFormatException&) {
92  if (report) {
93  emitFormatError(getName(attr), "a real number", objectid);
94  }
95  } catch (EmptyData&) {
96  if (report) {
97  emitEmptyError(getName(attr), objectid);
98  }
99  }
100  ok = false;
101  return (SUMOTime) - 1;
102 }
103 
104 
105 
106 
107 
108 void
109 SUMOSAXAttributes::emitUngivenError(const std::string& attrname, const char* objectid) const {
110  std::ostringstream oss;
111  oss << "Attribute '" << attrname << "' is missing in definition of ";
112  if (objectid == 0 || objectid[0] == 0) {
113  oss << "a " << myObjectType;
114  } else {
115  oss << myObjectType << " '" << objectid << "'";
116  }
117  oss << ".";
118  WRITE_ERROR(oss.str());
119 }
120 
121 
122 void
123 SUMOSAXAttributes::emitEmptyError(const std::string& attrname, const char* objectid) const {
124  std::ostringstream oss;
125  oss << "Attribute '" << attrname << "' in definition of ";
126  if (objectid == 0 || objectid[0] == 0) {
127  oss << "a " << myObjectType;
128  } else {
129  oss << myObjectType << " '" << objectid << "'";
130  }
131  oss << " is empty.";
132  WRITE_ERROR(oss.str());
133 }
134 
135 
136 void
137 SUMOSAXAttributes::emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const {
138  std::ostringstream oss;
139  oss << "Attribute '" << attrname << "' in definition of ";
140  if (objectid == 0 || objectid[0] == 0) {
141  oss << "a " << myObjectType;
142  } else {
143  oss << myObjectType << " '" << objectid << "'";
144  }
145  oss << " is not " << type << ".";
146  WRITE_ERROR(oss.str());
147 }
148 
149 
150 void
151 SUMOSAXAttributes::parseStringVector(const std::string& def, std::vector<std::string>& into) {
152  if (def.find(';') != std::string::npos || def.find(',') != std::string::npos) {
154  WRITE_WARNING("Please note that using ';' and ',' as XML list separators is deprecated.\n From 1.0 onwards, only ' ' will be accepted.");
156  }
157  }
158  StringTokenizer st(def, ";, ", true);
159  while (st.hasNext()) {
160  into.push_back(st.next());
161  }
162 }
163 
164 
165 void
166 SUMOSAXAttributes::parseStringSet(const std::string& def, std::set<std::string>& into) {
167  if (def.find(';') != std::string::npos || def.find(',') != std::string::npos) {
169  WRITE_WARNING("Please note that using ';' and ',' as XML list separators is deprecated.\n From 1.0 onwards, only ' ' will be accepted.");
171  }
172  }
173  StringTokenizer st(def, ";, ", true);
174  while (st.hasNext()) {
175  into.insert(st.next());
176  }
177 }
178 
179 
180 template<> const int invalid_return<int>::value = -1;
181 template<> const std::string invalid_return<int>::type = "int";
182 template<>
183 int SUMOSAXAttributes::getInternal(const int attr) const {
184  return getInt(attr);
185 }
186 
187 
188 template<> const long long int invalid_return<long long int>::value = -1;
189 template<> const std::string invalid_return<long long int>::type = "long";
190 template<>
191 long long int SUMOSAXAttributes::getInternal(const int attr) const {
192  return getLong(attr);
193 }
194 
195 
196 template<> const double invalid_return<double>::value = -1;
197 template<> const std::string invalid_return<double>::type = "float";
198 template<>
199 double SUMOSAXAttributes::getInternal(const int attr) const {
200  return getFloat(attr);
201 }
202 
203 
204 template<> const bool invalid_return<bool>::value = false;
205 template<> const std::string invalid_return<bool>::type = "bool";
206 template<>
207 bool SUMOSAXAttributes::getInternal(const int attr) const {
208  return getBool(attr);
209 }
210 
211 
212 template<> const std::string invalid_return<std::string>::value = "";
213 template<> const std::string invalid_return<std::string>::type = "string";
214 template<>
215 std::string SUMOSAXAttributes::getInternal(const int attr) const {
216  const std::string ret = getString(attr);
217  if (ret == "") {
218  throw EmptyData();
219  }
220  return ret;
221 }
222 
223 
225 template<> const std::string invalid_return<RGBColor>::type = "color";
226 template<>
227 RGBColor SUMOSAXAttributes::getInternal(const int /* attr */) const {
228  return getColor();
229 }
230 
231 
233 template<> const std::string invalid_return<PositionVector>::type = "PositionVector";
234 template<>
236  return getShape(attr);
237 }
238 
239 
241 template<> const std::string invalid_return<Boundary>::type = "Boundary";
242 template<>
244  return getBoundary(attr);
245 }
246 
247 
248 /****************************************************************************/
249 
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
#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:47
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
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.
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:205
virtual double getFloat(int id) const =0
Returns the double-value of the named (by its enum-value) attribute.
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.
long long int SUMOTime
Definition: TraCIDefs.h:51