Eclipse 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-2022 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // Encapsulated SAX-Attributes
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <string>
25 #include <iostream>
26 #include <sstream>
28 #include <utils/common/RGBColor.h>
31 #include <utils/geom/Boundary.h>
33 #include "SUMOSAXAttributes.h"
34 
35 
36 // ===========================================================================
37 // static members
38 // ===========================================================================
39 const std::string SUMOSAXAttributes::ENCODING = " encoding=\"UTF-8\"";
40 
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
45 SUMOSAXAttributes::SUMOSAXAttributes(const std::string& objectType):
46  myObjectType(objectType) {}
47 
48 
49 const std::string invalid_return<std::string>::value = "";
50 const std::string invalid_return<std::string>::type = "string";
51 template<>
52 std::string SUMOSAXAttributes::getInternal(const int attr) const {
53  const std::string ret = getString(attr);
54  if (ret == "") {
55  throw EmptyData();
56  }
57  return ret;
58 }
59 
60 
62 SUMOSAXAttributes::getSUMOTimeReporting(int attr, const char* objectid,
63  bool& ok, bool report) const {
64  if (!hasAttribute(attr)) {
65  if (report) {
66  emitUngivenError(getName(attr), objectid);
67  }
68  ok = false;
69  return -1;
70  }
71  try {
72  const std::string val = getInternal<std::string>(attr);
73  return string2time(val);
74  } catch (EmptyData&) {
75  if (report) {
76  emitEmptyError(getName(attr), objectid);
77  }
78  } catch (ProcessError&) {
79  if (report) {
80  emitFormatError(getName(attr), "a time value", 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  const std::string val = getInternal<std::string>(attr);
96  return string2time(val);
97  } catch (EmptyData&) {
98  if (report) {
99  emitEmptyError(getName(attr), objectid);
100  }
101  } catch (ProcessError&) {
102  if (report) {
103  emitFormatError(getName(attr), "a real number", objectid);
104  }
105  }
106  ok = false;
107  return (SUMOTime) - 1;
108 }
109 
110 
111 const std::vector<std::string>
113  const std::vector<std::string>& ret = StringTokenizer(getString(attr)).getVector();
114  if (ret.empty()) {
115  throw EmptyData();
116  }
117  return ret;
118 }
119 
120 
121 const std::vector<std::string>
122 SUMOSAXAttributes::getOptStringVector(int attr, const char* objectid, bool& ok, bool report) const {
123  return getOpt<std::vector<std::string> >(attr, objectid, ok, std::vector<std::string>(), report);
124 }
125 
126 const std::vector<int>
128  const std::vector<std::string>& tmp = StringTokenizer(getString(attr)).getVector();
129  if (tmp.empty()) {
130  throw EmptyData();
131  }
132  std::vector<int> ret;
133  for (const std::string& s : tmp) {
134  ret.push_back(StringUtils::toInt(s));
135  }
136  return ret;
137 }
138 
139 
140 const std::vector<int>
141 SUMOSAXAttributes::getOptIntVector(int attr, const char* objectid, bool& ok, bool report) const {
142  return getOpt<std::vector<int> >(attr, objectid, ok, std::vector<int>(), report);
143 }
144 
145 void
146 SUMOSAXAttributes::emitUngivenError(const std::string& attrname, const char* objectid) const {
147  std::ostringstream oss;
148  oss << "Attribute '" << attrname << "' is missing in definition of ";
149  if (objectid == nullptr || objectid[0] == 0) {
150  oss << "a " << myObjectType;
151  } else {
152  oss << myObjectType << " '" << objectid << "'";
153  }
154  oss << ".";
155  WRITE_ERROR(oss.str());
156 }
157 
158 
159 void
160 SUMOSAXAttributes::emitEmptyError(const std::string& attrname, const char* objectid) const {
161  std::ostringstream oss;
162  oss << "Attribute '" << attrname << "' in definition of ";
163  if (objectid == nullptr || objectid[0] == 0) {
164  oss << "a " << myObjectType;
165  } else {
166  oss << myObjectType << " '" << objectid << "'";
167  }
168  oss << " is empty.";
169  WRITE_ERROR(oss.str());
170 }
171 
172 
173 void
174 SUMOSAXAttributes::emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const {
175  std::ostringstream oss;
176  oss << "Attribute '" << attrname << "' in definition of ";
177  if (objectid == nullptr || objectid[0] == 0) {
178  oss << "a " << myObjectType;
179  } else {
180  oss << myObjectType << " '" << objectid << "'";
181  }
182  oss << " is not " << type << ".";
183  WRITE_ERROR(oss.str());
184 }
185 
186 
187 const int invalid_return<int>::value = -1;
188 const std::string invalid_return<int>::type = "int";
189 template<>
190 int SUMOSAXAttributes::getInternal(const int attr) const {
191  return getInt(attr);
192 }
193 
194 
195 const long long int invalid_return<long long int>::value = -1;
196 const std::string invalid_return<long long int>::type = "long";
197 template<>
198 long long int SUMOSAXAttributes::getInternal(const int attr) const {
199  return getLong(attr);
200 }
201 
202 
203 const double invalid_return<double>::value = -1;
204 const std::string invalid_return<double>::type = "float";
205 template<>
206 double SUMOSAXAttributes::getInternal(const int attr) const {
207  return getFloat(attr);
208 }
209 
210 
211 const bool invalid_return<bool>::value = false;
212 const std::string invalid_return<bool>::type = "bool";
213 template<>
214 bool SUMOSAXAttributes::getInternal(const int attr) const {
215  return getBool(attr);
216 }
217 
218 
220 const std::string invalid_return<RGBColor>::type = "color";
221 template<>
222 RGBColor SUMOSAXAttributes::getInternal(const int /* attr */) const {
223  return getColor();
224 }
225 
226 
228 const std::string invalid_return<Position>::type = "Position";
229 template<>
230 Position SUMOSAXAttributes::getInternal(const int attr) const {
231  return getPosition(attr);
232 }
233 
234 
236 const std::string invalid_return<PositionVector>::type = "PositionVector";
237 template<>
238 PositionVector SUMOSAXAttributes::getInternal(const int attr) const {
239  return getShape(attr);
240 }
241 
242 
244 const std::string invalid_return<Boundary>::type = "Boundary";
245 template<>
246 Boundary SUMOSAXAttributes::getInternal(const int attr) const {
247  return getBoundary(attr);
248 }
249 
250 
251 const std::vector<std::string> invalid_return<std::vector<std::string> >::value = std::vector<std::string>();
252 const std::string invalid_return<std::vector<std::string> >::type = "StringVector";
253 template<>
254 std::vector<std::string> SUMOSAXAttributes::getInternal(const int attr) const {
255  return getStringVector(attr);
256 }
257 
258 
259 const std::vector<int> invalid_return<std::vector<int> >::value = std::vector<int>();
260 const std::string invalid_return<std::vector<int> >::type = "StringVector";
261 template<>
262 std::vector<int> SUMOSAXAttributes::getInternal(const int attr) const {
263  return getIntVector(attr);
264 }
265 
266 
267 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:288
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:45
long long int SUMOTime
Definition: SUMOTime.h:32
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:39
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
A list of positions.
virtual bool getBool(int id) const =0
Returns the bool-value of the named (by its enum-value) attribute.
virtual int getInt(int id) const =0
Returns the int-value of the named (by its enum-value) attribute.
virtual RGBColor getColor() const =0
Returns the value of the named attribute.
virtual PositionVector getShape(int attr) const =0
Tries to read given attribute assuming it is a PositionVector.
virtual Position getPosition(int attr) const =0
Tries to read given attribute assuming it is a Position.
const std::vector< std::string > getOptStringVector(int attr, const char *objectid, bool &ok, bool report=true) const
convenience function to avoid the default argument and the template stuff at getOpt<>
SUMOSAXAttributes(const std::string &objectType)
const std::vector< std::string > getStringVector(int attr) const
Tries to read given attribute assuming it is a string vector.
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
const std::vector< int > getIntVector(int attr) const
Tries to read given attribute assuming it is an int vector.
void emitFormatError(const std::string &attrname, const std::string &type, const char *objectid) const
virtual double getFloat(int id) const =0
Returns the double-value of the named (by its enum-value) attribute.
virtual std::string getName(int attr) const =0
Converts the given attribute id into a man readable string.
void emitEmptyError(const std::string &attrname, const char *objectid) const
std::string myObjectType
the object type to use in error reporting
static const std::string ENCODING
The encoding of parsed strings.
virtual Boundary getBoundary(int attr) const =0
Tries to read given attribute assuming it is a Boundary.
virtual long long int getLong(int id) const =0
Returns the long-value of the named (by its enum-value) attribute.
const std::vector< int > getOptIntVector(int attr, const char *objectid, bool &ok, bool report=true) const
convenience function to avoid the default argument and the template stuff at getOpt<>
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.
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
std::vector< std::string > getVector()
return vector of strings
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...