SUMO - Simulation of Urban MObility
SUMOSAXAttributesImpl_Cached.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
17 // Encapsulated xml-attributes that use a map from string-attr-names to string-attr-values as backend
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #ifdef _MSC_VER
25 #include <windows_config.h>
26 #else
27 #include <config.h>
28 #endif
29 
30 #include <cassert>
31 #include <xercesc/sax2/Attributes.hpp>
32 #include <xercesc/sax2/DefaultHandler.hpp>
33 #include <xercesc/util/XercesVersion.hpp>
34 #include <xercesc/util/TransService.hpp>
35 #include <xercesc/util/TranscodingException.hpp>
36 #include <utils/common/RGBColor.h>
40 #include <utils/geom/Boundary.h>
44 
45 
46 // ===========================================================================
47 // class definitions
48 // ===========================================================================
50  const std::map<std::string, std::string>& attrs,
51  const std::map<int, std::string>& predefinedTagsMML,
52  const std::string& objectType) :
53  SUMOSAXAttributes(objectType),
54  myAttrs(attrs),
55  myPredefinedTagsMML(predefinedTagsMML) { }
56 
57 
59 }
60 
61 
62 bool
64  std::map<int, std::string>::const_iterator i = myPredefinedTagsMML.find(id);
65  if (i == myPredefinedTagsMML.end()) {
66  return false;
67  }
68  return myAttrs.find((*i).second) != myAttrs.end();
69 }
70 
71 
72 bool
75 }
76 
77 
78 int
81 }
82 
83 
84 long long int
87 }
88 
89 
90 std::string
92  return getAttributeValueSecure(id);
93 }
94 
95 
96 std::string
98  const std::string& str) const {
99  std::string result = getAttributeValueSecure(id);
100  return result.size() == 0 ? str : result;
101 }
102 
103 
104 double
107 }
108 
109 
110 const char*
112  std::map<int, std::string>::const_iterator i = myPredefinedTagsMML.find(id);
113  assert(i != myPredefinedTagsMML.end());
114  return myAttrs.find((*i).second)->second.c_str();
115 }
116 
117 
118 double
119 SUMOSAXAttributesImpl_Cached::getFloat(const std::string& id) const {
120  return TplConvert::_2double(myAttrs.find(id)->second.c_str());
121 }
122 
123 
124 bool
125 SUMOSAXAttributesImpl_Cached::hasAttribute(const std::string& id) const {
126  return myAttrs.find(id) != myAttrs.end();
127 }
128 
129 
130 std::string
132  const std::string& str) const {
133  std::map<std::string, std::string>::const_iterator it = myAttrs.find(id);
134  if (it != myAttrs.end() && it->second != "") {
135  return it->second;
136  } else {
137  return str;
138  }
139 }
140 
141 
145  std::string funcString = getString(SUMO_ATTR_FUNCTION);
146  if (SUMOXMLDefinitions::EdgeFunctions.hasString(funcString)) {
147  return SUMOXMLDefinitions::EdgeFunctions.get(funcString);
148  }
149  ok = false;
150  }
151  return EDGEFUNC_NORMAL;
152 }
153 
154 
158  std::string typeString = getString(SUMO_ATTR_TYPE);
159  if (SUMOXMLDefinitions::NodeTypes.hasString(typeString)) {
160  return SUMOXMLDefinitions::NodeTypes.get(typeString);
161  }
162  ok = false;
163  }
164  return NODETYPE_UNKNOWN;
165 }
166 
167 
168 RGBColor
171 }
172 
173 
176  StringTokenizer st(getString(attr));
177  PositionVector shape;
178  while (st.hasNext()) {
179  StringTokenizer pos(st.next(), ",");
180  if (pos.size() != 2 && pos.size() != 3) {
181  throw FormatException("shape format");
182  }
183  double x = TplConvert::_2double(pos.next().c_str());
184  double y = TplConvert::_2double(pos.next().c_str());
185  if (pos.size() == 2) {
186  shape.push_back(Position(x, y));
187  } else {
188  double z = TplConvert::_2double(pos.next().c_str());
189  shape.push_back(Position(x, y, z));
190  }
191  }
192  return shape;
193 }
194 
195 
196 Boundary
198  std::string def = getString(attr);
199  StringTokenizer st(def, ",");
200  if (st.size() != 4) {
201  throw FormatException("boundary format");
202  }
203  const double xmin = TplConvert::_2double(st.next().c_str());
204  const double ymin = TplConvert::_2double(st.next().c_str());
205  const double xmax = TplConvert::_2double(st.next().c_str());
206  const double ymax = TplConvert::_2double(st.next().c_str());
207  return Boundary(xmin, ymin, xmax, ymax);
208 }
209 
210 
211 std::vector<std::string>
213  std::string def = getString(attr);
214  std::vector<std::string> ret;
215  parseStringVector(def, ret);
216  return ret;
217 }
218 
219 
220 std::string
222  if (myPredefinedTagsMML.find(attr) == myPredefinedTagsMML.end()) {
223  return "?";
224  }
225  return myPredefinedTagsMML.find(attr)->second;
226 }
227 
228 
229 void
231  for (std::map<std::string, std::string>::const_iterator it = myAttrs.begin(); it != myAttrs.end(); ++it) {
232  os << " " << it->first;
233  os << "=\"" << it->second << "\"";
234  }
235 }
236 
237 
241 }
242 
243 /****************************************************************************/
244 
SumoXMLNodeType getNodeType(bool &ok) const
Returns the value of the named attribute.
static StringBijection< SumoXMLNodeType > NodeTypes
node types
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:179
std::string next()
std::vector< std::string > getStringVector(int attr) const
Tries to read given attribute assuming it is a string vector.
static bool _2bool(const E *const data)
converts a 0-terminated char-type array into the boolean value described by it
Definition: TplConvert.h:388
static long long int _2long(const E *const data)
converts a char-type array into the long value described by it
Definition: TplConvert.h:210
const std::string & getObjectType() const
return the objecttype to which these attributes belong
void serialize(std::ostream &os) const
Prints all attribute names and values into the given stream.
long long int getLong(int id) const
Returns the long-value of the named (by its enum-value) attribute.
Boundary getBoundary(int attr) const
Tries to read given attribute assuming it is a Boundary.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:47
RGBColor getColor() const
Returns the value of the named attribute.
PositionVector getShape(int attr) const
Tries to read given attribute assuming it is a PositionVector.
std::string getName(int attr) const
Converts the given attribute id into a man readable string.
const std::map< int, std::string > & myPredefinedTagsMML
Map of attribute ids to their (readable) string-representation.
static void parseStringVector(const std::string &def, std::vector< std::string > &into)
Splits the given string.
Encapsulated SAX-Attributes.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
A list of positions.
int getInt(int id) const
Returns the int-value of the named (by its enum-value) attribute.
T get(const std::string &str) const
double getFloat(int id) const
Returns the double-value of the named (by its enum-value) attribute.
virtual ~SUMOSAXAttributesImpl_Cached()
Destructor.
std::string getStringSecure(int id, const std::string &def) const
Returns the string-value of the named (by its enum-value) attribute.
std::string getString(int id) const
Returns the string-value of the named (by its enum-value) attribute.
bool getBool(int id) const
Returns the bool-value of the named (by its enum-value) attribute.
static int _2int(const E *const data)
converts a char-type array into the integer value described by it
Definition: TplConvert.h:155
SUMOSAXAttributes * clone() const
return a new deep-copy attributes object
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:311
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
const char * getAttributeValueSecure(int id) const
Returns Xerces-value of the named attribute.
SumoXMLEdgeFunc getEdgeFunc(bool &ok) const
Returns the value of the named attribute.
SUMOSAXAttributesImpl_Cached(const std::map< std::string, std::string > &attrs, const std::map< int, std::string > &predefinedTagsMML, const std::string &objectType)
Constructor.
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
std::map< std::string, std::string > myAttrs
The encapsulated attributes.
A color information.
bool hasAttribute(int id) const
Returns the information whether the named (by its enum-value) attribute is within the current list...