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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // Encapsulated SAX-Attributes
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <string>
26 #include <iostream>
27 #include <sstream>
29 #include <utils/common/RGBColor.h>
32 #include <utils/geom/Boundary.h>
34 #include "SUMOSAXAttributes.h"
35 
36 
37 // ===========================================================================
38 // static members
39 // ===========================================================================
40 const std::string SUMOSAXAttributes::ENCODING = " encoding=\"UTF-8\"";
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
46 SUMOSAXAttributes::SUMOSAXAttributes(const std::string& objectType):
47  myObjectType(objectType) {}
48 
49 
50 const std::string invalid_return<std::string>::value = "";
51 const std::string invalid_return<std::string>::type = "string";
52 template<>
53 std::string SUMOSAXAttributes::getInternal(const int attr) const {
54  const std::string ret = getString(attr);
55  if (ret == "") {
56  throw EmptyData();
57  }
58  return ret;
59 }
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  const std::string val = getInternal<std::string>(attr);
74  return string2time(val);
75  } catch (EmptyData&) {
76  if (report) {
77  emitEmptyError(getName(attr), objectid);
78  }
79  } catch (ProcessError&) {
80  if (report) {
81  emitFormatError(getName(attr), "a time value", objectid);
82  }
83  }
84  ok = false;
85  return (SUMOTime) - 1;
86 }
87 
88 
90 SUMOSAXAttributes::getOptSUMOTimeReporting(int attr, const char* objectid,
91  bool& ok, SUMOTime defaultValue, bool report) const {
92  if (!hasAttribute(attr)) {
93  return defaultValue;
94  }
95  try {
96  const std::string val = getInternal<std::string>(attr);
97  return string2time(val);
98  } catch (EmptyData&) {
99  if (report) {
100  emitEmptyError(getName(attr), objectid);
101  }
102  } catch (ProcessError&) {
103  if (report) {
104  emitFormatError(getName(attr), "a real number", objectid);
105  }
106  }
107  ok = false;
108  return (SUMOTime) - 1;
109 }
110 
111 
112 const std::vector<std::string>
114  const std::vector<std::string>& ret = StringTokenizer(getString(attr)).getVector();
115  if (ret.empty()) {
116  throw EmptyData();
117  }
118  return ret;
119 }
120 
121 
122 const std::vector<std::string>
123 SUMOSAXAttributes::getOptStringVector(int attr, const char* objectid, bool& ok, bool report) const {
124  return getOpt<std::vector<std::string> >(attr, objectid, ok, std::vector<std::string>(), report);
125 }
126 
127 const std::vector<int>
129  const std::vector<std::string>& tmp = StringTokenizer(getString(attr)).getVector();
130  if (tmp.empty()) {
131  throw EmptyData();
132  }
133  std::vector<int> ret;
134  for (const std::string& s : tmp) {
135  ret.push_back(StringUtils::toInt(s));
136  }
137  return ret;
138 }
139 
140 
141 const std::vector<int>
142 SUMOSAXAttributes::getOptIntVector(int attr, const char* objectid, bool& ok, bool report) const {
143  return getOpt<std::vector<int> >(attr, objectid, ok, std::vector<int>(), report);
144 }
145 
146 void
147 SUMOSAXAttributes::emitUngivenError(const std::string& attrname, const char* objectid) const {
148  std::ostringstream oss;
149  oss << "Attribute '" << attrname << "' is missing in definition of ";
150  if (objectid == nullptr || objectid[0] == 0) {
151  oss << "a " << myObjectType;
152  } else {
153  oss << myObjectType << " '" << objectid << "'";
154  }
155  oss << ".";
156  WRITE_ERROR(oss.str());
157 }
158 
159 
160 void
161 SUMOSAXAttributes::emitEmptyError(const std::string& attrname, const char* objectid) const {
162  std::ostringstream oss;
163  oss << "Attribute '" << attrname << "' in definition of ";
164  if (objectid == nullptr || objectid[0] == 0) {
165  oss << "a " << myObjectType;
166  } else {
167  oss << myObjectType << " '" << objectid << "'";
168  }
169  oss << " is empty.";
170  WRITE_ERROR(oss.str());
171 }
172 
173 
174 void
175 SUMOSAXAttributes::emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const {
176  std::ostringstream oss;
177  oss << "Attribute '" << attrname << "' in definition of ";
178  if (objectid == nullptr || objectid[0] == 0) {
179  oss << "a " << myObjectType;
180  } else {
181  oss << myObjectType << " '" << objectid << "'";
182  }
183  oss << " is not " << type << ".";
184  WRITE_ERROR(oss.str());
185 }
186 
187 
188 const int invalid_return<int>::value = -1;
189 const std::string invalid_return<int>::type = "int";
190 template<>
191 int SUMOSAXAttributes::getInternal(const int attr) const {
192  return getInt(attr);
193 }
194 
195 
196 const long long int invalid_return<long long int>::value = -1;
197 const std::string invalid_return<long long int>::type = "long";
198 template<>
199 long long int SUMOSAXAttributes::getInternal(const int attr) const {
200  return getLong(attr);
201 }
202 
203 
204 const double invalid_return<double>::value = -1;
205 const std::string invalid_return<double>::type = "float";
206 template<>
207 double SUMOSAXAttributes::getInternal(const int attr) const {
208  return getFloat(attr);
209 }
210 
211 
212 const bool invalid_return<bool>::value = false;
213 const std::string invalid_return<bool>::type = "bool";
214 template<>
215 bool SUMOSAXAttributes::getInternal(const int attr) const {
216  return getBool(attr);
217 }
218 
219 
221 const std::string invalid_return<RGBColor>::type = "color";
222 template<>
223 RGBColor SUMOSAXAttributes::getInternal(const int /* attr */) const {
224  return getColor();
225 }
226 
227 
229 const std::string invalid_return<PositionVector>::type = "PositionVector";
230 template<>
231 PositionVector SUMOSAXAttributes::getInternal(const int attr) const {
232  return getShape(attr);
233 }
234 
235 
237 const std::string invalid_return<Boundary>::type = "Boundary";
238 template<>
239 Boundary SUMOSAXAttributes::getInternal(const int attr) const {
240  return getBoundary(attr);
241 }
242 
243 
244 const std::vector<std::string> invalid_return<std::vector<std::string> >::value = std::vector<std::string>();
245 const std::string invalid_return<std::vector<std::string> >::type = "StringVector";
246 template<>
247 std::vector<std::string> SUMOSAXAttributes::getInternal(const int attr) const {
248  return getStringVector(attr);
249 }
250 
251 
252 const std::vector<int> invalid_return<std::vector<int> >::value = std::vector<int>();
253 const std::string invalid_return<std::vector<int> >::type = "StringVector";
254 template<>
255 std::vector<int> SUMOSAXAttributes::getInternal(const int attr) const {
256  return getIntVector(attr);
257 }
258 
259 /****************************************************************************/
Boundary.h
SUMOSAXAttributes::getStringVector
const std::vector< std::string > getStringVector(int attr) const
Tries to read given attribute assuming it is a string vector.
Definition: SUMOSAXAttributes.cpp:113
SUMOSAXAttributes::hasAttribute
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
SUMOSAXAttributes::getString
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
SUMOSAXAttributes::getInt
virtual int getInt(int id) const =0
Returns the int-value of the named (by its enum-value) attribute.
SUMOSAXAttributes::getOptIntVector
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<>
Definition: SUMOSAXAttributes.cpp:142
MsgHandler.h
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
SUMOSAXAttributes::myObjectType
std::string myObjectType
the object type to use in error reporting
Definition: SUMOSAXAttributes.h:424
EmptyData
Definition: UtilExceptions.h:68
SUMOSAXAttributes::getBool
virtual bool getBool(int id) const =0
Returns the bool-value of the named (by its enum-value) attribute.
SUMOSAXAttributes::getFloat
virtual double getFloat(int id) const =0
Returns the double-value of the named (by its enum-value) attribute.
SUMOSAXAttributes::getSUMOTimeReporting
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
Definition: SUMOSAXAttributes.cpp:63
PositionVector
A list of positions.
Definition: PositionVector.h:45
SUMOSAXAttributes::getInternal
T getInternal(const int attr) const
SUMOSAXAttributes::emitFormatError
void emitFormatError(const std::string &attrname, const std::string &type, const char *objectid) const
Definition: SUMOSAXAttributes.cpp:175
RGBColor.h
RGBColor
Definition: RGBColor.h:39
SUMOSAXAttributes::getShape
virtual PositionVector getShape(int attr) const =0
Tries to read given attribute assuming it is a PositionVector.
SUMOSAXAttributes::getOptStringVector
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<>
Definition: SUMOSAXAttributes.cpp:123
StringTokenizer
Definition: StringTokenizer.h:61
SUMOSAXAttributes::emitUngivenError
void emitUngivenError(const std::string &attrname, const char *objectid) const
Definition: SUMOSAXAttributes.cpp:147
Boundary
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:41
ProcessError
Definition: UtilExceptions.h:39
SUMOSAXAttributes::getIntVector
const std::vector< int > getIntVector(int attr) const
Tries to read given attribute assuming it is an int vector.
Definition: SUMOSAXAttributes.cpp:128
string2time
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:44
SUMOSAXAttributes::getBoundary
virtual Boundary getBoundary(int attr) const =0
Tries to read given attribute assuming it is a Boundary.
StringUtils.h
SUMOSAXAttributes::getName
virtual std::string getName(int attr) const =0
Converts the given attribute id into a man readable string.
StringUtils::toInt
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter,...
Definition: StringUtils.cpp:278
SUMOSAXAttributes::getOptSUMOTimeReporting
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.
Definition: SUMOSAXAttributes.cpp:90
invalid_return
Definition: SUMOSAXAttributes.h:435
SUMOSAXAttributes::ENCODING
static const std::string ENCODING
The encoding of parsed strings.
Definition: SUMOSAXAttributes.h:407
SUMOSAXAttributes::SUMOSAXAttributes
SUMOSAXAttributes(const std::string &objectType)
Definition: SUMOSAXAttributes.cpp:46
SUMOSAXAttributes::emitEmptyError
void emitEmptyError(const std::string &attrname, const char *objectid) const
Definition: SUMOSAXAttributes.cpp:161
SUMOSAXAttributes.h
StringTokenizer::getVector
std::vector< std::string > getVector()
return vector of strings
Definition: StringTokenizer.cpp:191
config.h
StringTokenizer.h
SUMOSAXAttributes::getLong
virtual long long int getLong(int id) const =0
Returns the long-value of the named (by its enum-value) attribute.
SUMOSAXAttributes::getColor
virtual RGBColor getColor() const =0
Returns the value of the named attribute.
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283
PositionVector.h