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 /****************************************************************************/
17 // Encapsulated SAX-Attributes
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <string>
27 #include <iostream>
28 #include <sstream>
30 #include <utils/common/RGBColor.h>
33 #include <utils/geom/Boundary.h>
35 #include "SUMOSAXAttributes.h"
36 
37 
38 // ===========================================================================
39 // static members
40 // ===========================================================================
41 const std::string SUMOSAXAttributes::ENCODING = " encoding=\"UTF-8\"";
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
47 SUMOSAXAttributes::SUMOSAXAttributes(const std::string& objectType):
48  myObjectType(objectType) {}
49 
50 
51 const std::string invalid_return<std::string>::value = "";
52 const std::string invalid_return<std::string>::type = "string";
53 template<>
54 std::string SUMOSAXAttributes::getInternal(const int attr) const {
55  const std::string ret = getString(attr);
56  if (ret == "") {
57  throw EmptyData();
58  }
59  return ret;
60 }
61 
62 
64 SUMOSAXAttributes::getSUMOTimeReporting(int attr, const char* objectid,
65  bool& ok, bool report) const {
66  if (!hasAttribute(attr)) {
67  if (report) {
68  emitUngivenError(getName(attr), objectid);
69  }
70  ok = false;
71  return -1;
72  }
73  try {
74  const std::string val = getInternal<std::string>(attr);
75  return string2time(val);
76  } catch (EmptyData&) {
77  if (report) {
78  emitEmptyError(getName(attr), objectid);
79  }
80  } catch (ProcessError&) {
81  if (report) {
82  emitFormatError(getName(attr), "a time value", objectid);
83  }
84  }
85  ok = false;
86  return (SUMOTime) - 1;
87 }
88 
89 
91 SUMOSAXAttributes::getOptSUMOTimeReporting(int attr, const char* objectid,
92  bool& ok, SUMOTime defaultValue, bool report) const {
93  if (!hasAttribute(attr)) {
94  return defaultValue;
95  }
96  try {
97  const std::string val = getInternal<std::string>(attr);
98  return string2time(val);
99  } catch (EmptyData&) {
100  if (report) {
101  emitEmptyError(getName(attr), objectid);
102  }
103  } catch (ProcessError&) {
104  if (report) {
105  emitFormatError(getName(attr), "a real number", objectid);
106  }
107  }
108  ok = false;
109  return (SUMOTime) - 1;
110 }
111 
112 
113 const std::vector<std::string>
115  const std::vector<std::string>& ret = StringTokenizer(getString(attr)).getVector();
116  if (ret.empty()) {
117  throw EmptyData();
118  }
119  return ret;
120 }
121 
122 
123 const std::vector<std::string>
124 SUMOSAXAttributes::getOptStringVector(int attr, const char* objectid, bool& ok, bool report) const {
125  return getOpt<std::vector<std::string> >(attr, objectid, ok, std::vector<std::string>(), report);
126 }
127 
128 const std::vector<int>
130  const std::vector<std::string>& tmp = StringTokenizer(getString(attr)).getVector();
131  if (tmp.empty()) {
132  throw EmptyData();
133  }
134  std::vector<int> ret;
135  for (const std::string& s : tmp) {
136  ret.push_back(StringUtils::toInt(s));
137  }
138  return ret;
139 }
140 
141 
142 const std::vector<int>
143 SUMOSAXAttributes::getOptIntVector(int attr, const char* objectid, bool& ok, bool report) const {
144  return getOpt<std::vector<int> >(attr, objectid, ok, std::vector<int>(), report);
145 }
146 
147 void
148 SUMOSAXAttributes::emitUngivenError(const std::string& attrname, const char* objectid) const {
149  std::ostringstream oss;
150  oss << "Attribute '" << attrname << "' is missing in definition of ";
151  if (objectid == nullptr || objectid[0] == 0) {
152  oss << "a " << myObjectType;
153  } else {
154  oss << myObjectType << " '" << objectid << "'";
155  }
156  oss << ".";
157  WRITE_ERROR(oss.str());
158 }
159 
160 
161 void
162 SUMOSAXAttributes::emitEmptyError(const std::string& attrname, const char* objectid) const {
163  std::ostringstream oss;
164  oss << "Attribute '" << attrname << "' in definition of ";
165  if (objectid == nullptr || objectid[0] == 0) {
166  oss << "a " << myObjectType;
167  } else {
168  oss << myObjectType << " '" << objectid << "'";
169  }
170  oss << " is empty.";
171  WRITE_ERROR(oss.str());
172 }
173 
174 
175 void
176 SUMOSAXAttributes::emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const {
177  std::ostringstream oss;
178  oss << "Attribute '" << attrname << "' in definition of ";
179  if (objectid == nullptr || objectid[0] == 0) {
180  oss << "a " << myObjectType;
181  } else {
182  oss << myObjectType << " '" << objectid << "'";
183  }
184  oss << " is not " << type << ".";
185  WRITE_ERROR(oss.str());
186 }
187 
188 
189 const int invalid_return<int>::value = -1;
190 const std::string invalid_return<int>::type = "int";
191 template<>
192 int SUMOSAXAttributes::getInternal(const int attr) const {
193  return getInt(attr);
194 }
195 
196 
197 const long long int invalid_return<long long int>::value = -1;
198 const std::string invalid_return<long long int>::type = "long";
199 template<>
200 long long int SUMOSAXAttributes::getInternal(const int attr) const {
201  return getLong(attr);
202 }
203 
204 
205 const double invalid_return<double>::value = -1;
206 const std::string invalid_return<double>::type = "float";
207 template<>
208 double SUMOSAXAttributes::getInternal(const int attr) const {
209  return getFloat(attr);
210 }
211 
212 
213 const bool invalid_return<bool>::value = false;
214 const std::string invalid_return<bool>::type = "bool";
215 template<>
216 bool SUMOSAXAttributes::getInternal(const int attr) const {
217  return getBool(attr);
218 }
219 
220 
222 const std::string invalid_return<RGBColor>::type = "color";
223 template<>
224 RGBColor SUMOSAXAttributes::getInternal(const int /* attr */) const {
225  return getColor();
226 }
227 
228 
230 const std::string invalid_return<PositionVector>::type = "PositionVector";
231 template<>
233  return getShape(attr);
234 }
235 
236 
238 const std::string invalid_return<Boundary>::type = "Boundary";
239 template<>
241  return getBoundary(attr);
242 }
243 
244 
245 const std::vector<std::string> invalid_return<std::vector<std::string> >::value = std::vector<std::string>();
246 const std::string invalid_return<std::vector<std::string> >::type = "StringVector";
247 template<>
248 std::vector<std::string> SUMOSAXAttributes::getInternal(const int attr) const {
249  return getStringVector(attr);
250 }
251 
252 
253 const std::vector<int> invalid_return<std::vector<int> >::value = std::vector<int>();
254 const std::string invalid_return<std::vector<int> >::type = "StringVector";
255 template<>
256 std::vector<int> SUMOSAXAttributes::getInternal(const int attr) const {
257  return getIntVector(attr);
258 }
259 
260 /****************************************************************************/
SUMOSAXAttributes(const std::string &objectType)
const std::vector< int > getIntVector(int attr) const
Tries to read given attribute assuming it is an int vector.
virtual RGBColor getColor() const =0
Returns the value of the named attribute.
long long int SUMOTime
Definition: SUMOTime.h:35
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
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:42
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
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.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:42
void emitUngivenError(const std::string &attrname, const char *objectid) const
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<> ...
virtual Boundary getBoundary(int attr) const =0
Tries to read given attribute assuming it is a Boundary.
static int toInt(const std::string &sData)
converts a string into the integer value described by it by calling the char-type converter...
void emitEmptyError(const std::string &attrname, const char *objectid) const
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<> ...
std::vector< std::string > getVector()
return vector of strings
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
virtual double getFloat(int id) const =0
Returns the double-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.
const std::vector< std::string > getStringVector(int attr) const
Tries to read given attribute assuming it is a string vector.
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.