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 // ===========================================================================
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 
63 
65 SUMOSAXAttributes::getSUMOTimeReporting(int attr, const char* objectid,
66  bool& ok, bool report) const {
67  if (!hasAttribute(attr)) {
68  if (report) {
69  emitUngivenError(getName(attr), objectid);
70  }
71  ok = false;
72  return -1;
73  }
74  try {
75  const std::string val = getInternal<std::string>(attr);
76  return string2time(val);
77  } catch (ProcessError&) {
78  if (report) {
79  emitFormatError(getName(attr), "a time value", objectid);
80  }
81  } catch (EmptyData&) {
82  if (report) {
83  emitEmptyError(getName(attr), objectid);
84  }
85  }
86  ok = false;
87  return (SUMOTime) - 1;
88 }
89 
90 
92 SUMOSAXAttributes::getOptSUMOTimeReporting(int attr, const char* objectid,
93  bool& ok, SUMOTime defaultValue, bool report) const {
94  if (!hasAttribute(attr)) {
95  return defaultValue;
96  }
97  try {
98  const std::string val = getInternal<std::string>(attr);
99  return string2time(val);
100  } catch (ProcessError&) {
101  if (report) {
102  emitFormatError(getName(attr), "a real number", objectid);
103  }
104  } catch (EmptyData&) {
105  if (report) {
106  emitEmptyError(getName(attr), objectid);
107  }
108  }
109  ok = false;
110  return (SUMOTime) - 1;
111 }
112 
113 
114 
115 
116 
117 void
118 SUMOSAXAttributes::emitUngivenError(const std::string& attrname, const char* objectid) const {
119  std::ostringstream oss;
120  oss << "Attribute '" << attrname << "' is missing in definition of ";
121  if (objectid == 0 || objectid[0] == 0) {
122  oss << "a " << myObjectType;
123  } else {
124  oss << myObjectType << " '" << objectid << "'";
125  }
126  oss << ".";
127  WRITE_ERROR(oss.str());
128 }
129 
130 
131 void
132 SUMOSAXAttributes::emitEmptyError(const std::string& attrname, const char* objectid) const {
133  std::ostringstream oss;
134  oss << "Attribute '" << attrname << "' in definition of ";
135  if (objectid == 0 || objectid[0] == 0) {
136  oss << "a " << myObjectType;
137  } else {
138  oss << myObjectType << " '" << objectid << "'";
139  }
140  oss << " is empty.";
141  WRITE_ERROR(oss.str());
142 }
143 
144 
145 void
146 SUMOSAXAttributes::emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const {
147  std::ostringstream oss;
148  oss << "Attribute '" << attrname << "' in definition of ";
149  if (objectid == 0 || objectid[0] == 0) {
150  oss << "a " << myObjectType;
151  } else {
152  oss << myObjectType << " '" << objectid << "'";
153  }
154  oss << " is not " << type << ".";
155  WRITE_ERROR(oss.str());
156 }
157 
158 
159 void
160 SUMOSAXAttributes::parseStringVector(const std::string& def, std::vector<std::string>& into) {
161  if (def.find(';') != std::string::npos || def.find(',') != std::string::npos) {
163  WRITE_WARNING("Please note that using ';' and ',' as XML list separators is deprecated.\n From 1.0 onwards, only ' ' will be accepted.");
165  }
166  }
167  StringTokenizer st(def, ";, ", true);
168  while (st.hasNext()) {
169  into.push_back(st.next());
170  }
171 }
172 
173 
174 void
175 SUMOSAXAttributes::parseStringSet(const std::string& def, std::set<std::string>& into) {
176  if (def.find(';') != std::string::npos || def.find(',') != std::string::npos) {
178  WRITE_WARNING("Please note that using ';' and ',' as XML list separators is deprecated.\n From 1.0 onwards, only ' ' will be accepted.");
180  }
181  }
182  StringTokenizer st(def, ";, ", true);
183  while (st.hasNext()) {
184  into.insert(st.next());
185  }
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 /****************************************************************************/
246 
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
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:242
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:248
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.