SUMO - Simulation of Urban MObility
SUMOSAXAttributes.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Encapsulated SAX-Attributes
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2007-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
34 #include <iostream>
35 #include <sstream>
37 #include <utils/common/RGBColor.h>
39 #include <utils/geom/Boundary.h>
41 #include "SUMOSAXAttributes.h"
42 
43 
44 // ===========================================================================
45 // static members
46 // ===========================================================================
48 const std::string SUMOSAXAttributes::ENCODING = " encoding=\"UTF-8\"";
49 
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
54 SUMOSAXAttributes::SUMOSAXAttributes(const std::string& objectType):
55  myObjectType(objectType) {}
56 
57 
59 SUMOSAXAttributes::getSUMOTimeReporting(int attr, const char* objectid,
60  bool& ok, bool report) const {
61  if (!hasAttribute(attr)) {
62  if (report) {
63  emitUngivenError(getName(attr), objectid);
64  }
65  ok = false;
66  return -1;
67  }
68  try {
69  return TIME2STEPS(getFloat(attr));
70  } catch (NumberFormatException&) {
71  if (report) {
72  emitFormatError(getName(attr), "a time value", objectid);
73  }
74  } catch (EmptyData&) {
75  if (report) {
76  emitEmptyError(getName(attr), objectid);
77  }
78  }
79  ok = false;
80  return (SUMOTime) - 1;
81 }
82 
83 
85 SUMOSAXAttributes::getOptSUMOTimeReporting(int attr, const char* objectid,
86  bool& ok, SUMOTime defaultValue, bool report) const {
87  if (!hasAttribute(attr)) {
88  return defaultValue;
89  }
90  try {
91  return (SUMOTime)(getFloat(attr) * 1000.);
92  } catch (NumberFormatException&) {
93  if (report) {
94  emitFormatError(getName(attr), "a real number", objectid);
95  }
96  } catch (EmptyData&) {
97  if (report) {
98  emitEmptyError(getName(attr), objectid);
99  }
100  }
101  ok = false;
102  return (SUMOTime) - 1;
103 }
104 
105 
106 
107 
108 
109 void
110 SUMOSAXAttributes::emitUngivenError(const std::string& attrname, const char* objectid) const {
111  std::ostringstream oss;
112  oss << "Attribute '" << attrname << "' is missing in definition of ";
113  if (objectid == 0 || objectid[0] == 0) {
114  oss << "a " << myObjectType;
115  } else {
116  oss << myObjectType << " '" << objectid << "'";
117  }
118  oss << ".";
119  WRITE_ERROR(oss.str());
120 }
121 
122 
123 void
124 SUMOSAXAttributes::emitEmptyError(const std::string& attrname, const char* objectid) const {
125  std::ostringstream oss;
126  oss << "Attribute '" << attrname << "' in definition of ";
127  if (objectid == 0 || objectid[0] == 0) {
128  oss << "a " << myObjectType;
129  } else {
130  oss << myObjectType << " '" << objectid << "'";
131  }
132  oss << " is empty.";
133  WRITE_ERROR(oss.str());
134 }
135 
136 
137 void
138 SUMOSAXAttributes::emitFormatError(const std::string& attrname, const std::string& type, const char* objectid) const {
139  std::ostringstream oss;
140  oss << "Attribute '" << attrname << "' in definition of ";
141  if (objectid == 0 || objectid[0] == 0) {
142  oss << "a " << myObjectType;
143  } else {
144  oss << myObjectType << " '" << objectid << "'";
145  }
146  oss << " is not " << type << ".";
147  WRITE_ERROR(oss.str());
148 }
149 
150 
151 void
152 SUMOSAXAttributes::parseStringVector(const std::string& def, std::vector<std::string>& into) {
153  if (def.find(';') != std::string::npos || def.find(',') != std::string::npos) {
155  WRITE_WARNING("Please note that using ';' and ',' as XML list separators is deprecated.\n From 1.0 onwards, only ' ' will be accepted.");
157  }
158  }
159  StringTokenizer st(def, ";, ", true);
160  while (st.hasNext()) {
161  into.push_back(st.next());
162  }
163 }
164 
165 
166 void
167 SUMOSAXAttributes::parseStringSet(const std::string& def, std::set<std::string>& into) {
168  if (def.find(';') != std::string::npos || def.find(',') != std::string::npos) {
170  WRITE_WARNING("Please note that using ';' and ',' as XML list separators is deprecated.\n From 1.0 onwards, only ' ' will be accepted.");
172  }
173  }
174  StringTokenizer st(def, ";, ", true);
175  while (st.hasNext()) {
176  into.insert(st.next());
177  }
178 }
179 
180 
181 template<> const int invalid_return<int>::value = -1;
182 template<> const std::string invalid_return<int>::type = "int";
183 template<>
184 int SUMOSAXAttributes::getInternal(const int attr) const {
185  return getInt(attr);
186 }
187 
188 
189 template<> const long long int invalid_return<long long int>::value = -1;
190 template<> const std::string invalid_return<long long int>::type = "long";
191 template<>
192 long long int SUMOSAXAttributes::getInternal(const int attr) const {
193  return getLong(attr);
194 }
195 
196 
197 template<> const double invalid_return<double>::value = -1;
198 template<> const std::string invalid_return<double>::type = "float";
199 template<>
200 double SUMOSAXAttributes::getInternal(const int attr) const {
201  return getFloat(attr);
202 }
203 
204 
205 template<> const bool invalid_return<bool>::value = false;
206 template<> const std::string invalid_return<bool>::type = "bool";
207 template<>
208 bool SUMOSAXAttributes::getInternal(const int attr) const {
209  return getBool(attr);
210 }
211 
212 
213 template<> const std::string invalid_return<std::string>::value = "";
214 template<> const std::string invalid_return<std::string>::type = "string";
215 template<>
216 std::string SUMOSAXAttributes::getInternal(const int attr) const {
217  const std::string ret = getString(attr);
218  if (ret == "") {
219  throw EmptyData();
220  }
221  return ret;
222 }
223 
224 
226 template<> const std::string invalid_return<RGBColor>::type = "color";
227 template<>
228 RGBColor SUMOSAXAttributes::getInternal(const int /* attr */) const {
229  return getColor();
230 }
231 
232 
234 template<> const std::string invalid_return<PositionVector>::type = "PositionVector";
235 template<>
237  return getShape(attr);
238 }
239 
240 
242 template<> const std::string invalid_return<Boundary>::type = "Boundary";
243 template<>
245  return getBoundary(attr);
246 }
247 
248 
249 /****************************************************************************/
250 
SUMOSAXAttributes(const std::string &objectType)
virtual RGBColor getColor() const =0
Returns the value of the named attribute.
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
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
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:48
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
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.
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:206
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.
long long int SUMOTime
Definition: TraCIDefs.h:52