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-2018 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>
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 void
113 SUMOSAXAttributes::emitUngivenError(const std::string& attrname, const char* objectid) const {
114  std::ostringstream oss;
115  oss << "Attribute '" << attrname << "' is missing in definition of ";
116  if (objectid == nullptr || objectid[0] == 0) {
117  oss << "a " << myObjectType;
118  } else {
119  oss << myObjectType << " '" << objectid << "'";
120  }
121  oss << ".";
122  WRITE_ERROR(oss.str());
123 }
124 
125 
126 void
127 SUMOSAXAttributes::emitEmptyError(const std::string& attrname, const char* objectid) const {
128  std::ostringstream oss;
129  oss << "Attribute '" << attrname << "' in definition of ";
130  if (objectid == nullptr || objectid[0] == 0) {
131  oss << "a " << myObjectType;
132  } else {
133  oss << myObjectType << " '" << objectid << "'";
134  }
135  oss << " is empty.";
136  WRITE_ERROR(oss.str());
137 }
138 
139 
140 void
141 SUMOSAXAttributes::emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const {
142  std::ostringstream oss;
143  oss << "Attribute '" << attrname << "' in definition of ";
144  if (objectid == nullptr || objectid[0] == 0) {
145  oss << "a " << myObjectType;
146  } else {
147  oss << myObjectType << " '" << objectid << "'";
148  }
149  oss << " is not " << type << ".";
150  WRITE_ERROR(oss.str());
151 }
152 
153 
154 void
155 SUMOSAXAttributes::parseStringVector(const std::string& def, std::vector<std::string>& into) {
156  StringTokenizer st(def, " ", true);
157  while (st.hasNext()) {
158  into.push_back(st.next());
159  }
160 }
161 
162 
163 void
164 SUMOSAXAttributes::parseStringSet(const std::string& def, std::set<std::string>& into) {
165  StringTokenizer st(def, " ", true);
166  while (st.hasNext()) {
167  into.insert(st.next());
168  }
169 }
170 
171 
172 const int invalid_return<int>::value = -1;
173 const std::string invalid_return<int>::type = "int";
174 template<>
175 int SUMOSAXAttributes::getInternal(const int attr) const {
176  return getInt(attr);
177 }
178 
179 
180 const long long int invalid_return<long long int>::value = -1;
181 const std::string invalid_return<long long int>::type = "long";
182 template<>
183 long long int SUMOSAXAttributes::getInternal(const int attr) const {
184  return getLong(attr);
185 }
186 
187 
188 const double invalid_return<double>::value = -1;
189 const std::string invalid_return<double>::type = "float";
190 template<>
191 double SUMOSAXAttributes::getInternal(const int attr) const {
192  return getFloat(attr);
193 }
194 
195 
196 const bool invalid_return<bool>::value = false;
197 const std::string invalid_return<bool>::type = "bool";
198 template<>
199 bool SUMOSAXAttributes::getInternal(const int attr) const {
200  return getBool(attr);
201 }
202 
203 
205 const std::string invalid_return<RGBColor>::type = "color";
206 template<>
207 RGBColor SUMOSAXAttributes::getInternal(const int /* attr */) const {
208  return getColor();
209 }
210 
211 
213 const std::string invalid_return<PositionVector>::type = "PositionVector";
214 template<>
216  return getShape(attr);
217 }
218 
219 
221 const std::string invalid_return<Boundary>::type = "Boundary";
222 template<>
224  return getBoundary(attr);
225 }
226 
227 
228 /****************************************************************************/
SUMOSAXAttributes(const std::string &objectType)
virtual RGBColor getColor() const =0
Returns the value of the named attribute.
long long int SUMOTime
Definition: SUMOTime.h:36
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
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.
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.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:42
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:247
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.
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.