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-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 /****************************************************************************/
15 // Encapsulated xml-attributes that use a map from string-attr-names to string-attr-values as backend
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <cassert>
25 #include <xercesc/sax2/Attributes.hpp>
26 #include <xercesc/sax2/DefaultHandler.hpp>
27 #include <xercesc/util/XercesVersion.hpp>
28 #include <xercesc/util/TransService.hpp>
29 #include <xercesc/util/TranscodingException.hpp>
30 #include <utils/common/RGBColor.h>
34 #include <utils/geom/Boundary.h>
38 
39 
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
44  const std::map<std::string, std::string>& attrs,
45  const std::map<int, std::string>& predefinedTagsMML,
46  const std::string& objectType) :
47  SUMOSAXAttributes(objectType),
48  myAttrs(attrs),
49  myPredefinedTagsMML(predefinedTagsMML) { }
50 
51 
53 }
54 
55 
56 bool
58  std::map<int, std::string>::const_iterator i = myPredefinedTagsMML.find(id);
59  if (i == myPredefinedTagsMML.end()) {
60  return false;
61  }
62  return myAttrs.find((*i).second) != myAttrs.end();
63 }
64 
65 
66 bool
69 }
70 
71 
72 int
75 }
76 
77 
78 long long int
81 }
82 
83 
84 std::string
86  return getAttributeValueSecure(id);
87 }
88 
89 
90 std::string
92  const std::string& str) const {
93  std::string result = getAttributeValueSecure(id);
94  return result.size() == 0 ? str : result;
95 }
96 
97 
98 double
101 }
102 
103 
104 const char*
106  std::map<int, std::string>::const_iterator i = myPredefinedTagsMML.find(id);
107  assert(i != myPredefinedTagsMML.end());
108  return myAttrs.find((*i).second)->second.c_str();
109 }
110 
111 
112 double
113 SUMOSAXAttributesImpl_Cached::getFloat(const std::string& id) const {
114  return TplConvert::_2double(myAttrs.find(id)->second.c_str());
115 }
116 
117 
118 bool
119 SUMOSAXAttributesImpl_Cached::hasAttribute(const std::string& id) const {
120  return myAttrs.find(id) != myAttrs.end();
121 }
122 
123 
124 std::string
126  const std::string& str) const {
127  std::map<std::string, std::string>::const_iterator it = myAttrs.find(id);
128  if (it != myAttrs.end() && it->second != "") {
129  return it->second;
130  } else {
131  return str;
132  }
133 }
134 
135 
139  std::string funcString = getString(SUMO_ATTR_FUNCTION);
140  if (SUMOXMLDefinitions::EdgeFunctions.hasString(funcString)) {
141  return SUMOXMLDefinitions::EdgeFunctions.get(funcString);
142  }
143  ok = false;
144  }
145  return EDGEFUNC_NORMAL;
146 }
147 
148 
152  std::string typeString = getString(SUMO_ATTR_TYPE);
153  if (SUMOXMLDefinitions::NodeTypes.hasString(typeString)) {
154  return SUMOXMLDefinitions::NodeTypes.get(typeString);
155  }
156  ok = false;
157  }
158  return NODETYPE_UNKNOWN;
159 }
160 
161 
162 RGBColor
165 }
166 
167 
170  StringTokenizer st(getString(attr));
171  PositionVector shape;
172  while (st.hasNext()) {
173  StringTokenizer pos(st.next(), ",");
174  if (pos.size() != 2 && pos.size() != 3) {
175  throw FormatException("shape format");
176  }
177  double x = TplConvert::_2double(pos.next().c_str());
178  double y = TplConvert::_2double(pos.next().c_str());
179  if (pos.size() == 2) {
180  shape.push_back(Position(x, y));
181  } else {
182  double z = TplConvert::_2double(pos.next().c_str());
183  shape.push_back(Position(x, y, z));
184  }
185  }
186  return shape;
187 }
188 
189 
190 Boundary
192  std::string def = getString(attr);
193  StringTokenizer st(def, ",");
194  if (st.size() != 4) {
195  throw FormatException("boundary format");
196  }
197  const double xmin = TplConvert::_2double(st.next().c_str());
198  const double ymin = TplConvert::_2double(st.next().c_str());
199  const double xmax = TplConvert::_2double(st.next().c_str());
200  const double ymax = TplConvert::_2double(st.next().c_str());
201  return Boundary(xmin, ymin, xmax, ymax);
202 }
203 
204 
205 std::vector<std::string>
207  std::string def = getString(attr);
208  std::vector<std::string> ret;
209  parseStringVector(def, ret);
210  return ret;
211 }
212 
213 
214 std::string
216  if (myPredefinedTagsMML.find(attr) == myPredefinedTagsMML.end()) {
217  return "?";
218  }
219  return myPredefinedTagsMML.find(attr)->second;
220 }
221 
222 
223 void
225  for (std::map<std::string, std::string>::const_iterator it = myAttrs.begin(); it != myAttrs.end(); ++it) {
226  os << " " << it->first;
227  os << "=\"" << it->second << "\"";
228  }
229 }
230 
231 
235 }
236 
237 /****************************************************************************/
238 
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:173
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:382
static long long int _2long(const E *const data)
converts a char-type array into the long value described by it
Definition: TplConvert.h:204
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:42
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:39
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:149
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:305
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...