SUMO - Simulation of Urban MObility
SUMOSAXAttributesImpl_Cached.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // Encapsulated xml-attributes that use a map from string-attr-names to string-attr-values as backend
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
10 // Copyright (C) 2002-2017 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <cassert>
32 #include <xercesc/sax2/Attributes.hpp>
33 #include <xercesc/sax2/DefaultHandler.hpp>
34 #include <xercesc/util/XercesVersion.hpp>
35 #include <xercesc/util/TransService.hpp>
36 #include <xercesc/util/TranscodingException.hpp>
37 #include <utils/common/RGBColor.h>
41 #include <utils/geom/Boundary.h>
45 
46 
47 // ===========================================================================
48 // class definitions
49 // ===========================================================================
51  const std::map<std::string, std::string>& attrs,
52  const std::map<int, std::string>& predefinedTagsMML,
53  const std::string& objectType) :
54  SUMOSAXAttributes(objectType),
55  myAttrs(attrs),
56  myPredefinedTagsMML(predefinedTagsMML) { }
57 
58 
60 }
61 
62 
63 bool
65  std::map<int, std::string>::const_iterator i = myPredefinedTagsMML.find(id);
66  if (i == myPredefinedTagsMML.end()) {
67  return false;
68  }
69  return myAttrs.find((*i).second) != myAttrs.end();
70 }
71 
72 
73 bool
76 }
77 
78 
79 int
82 }
83 
84 
85 long long int
88 }
89 
90 
91 std::string
93  return getAttributeValueSecure(id);
94 }
95 
96 
97 std::string
99  const std::string& str) const {
100  std::string result = getAttributeValueSecure(id);
101  return result.size() == 0 ? str : result;
102 }
103 
104 
105 double
108 }
109 
110 
111 const char*
113  std::map<int, std::string>::const_iterator i = myPredefinedTagsMML.find(id);
114  assert(i != myPredefinedTagsMML.end());
115  return myAttrs.find((*i).second)->second.c_str();
116 }
117 
118 
119 double
120 SUMOSAXAttributesImpl_Cached::getFloat(const std::string& id) const {
121  return TplConvert::_2double(myAttrs.find(id)->second.c_str());
122 }
123 
124 
125 bool
126 SUMOSAXAttributesImpl_Cached::hasAttribute(const std::string& id) const {
127  return myAttrs.find(id) != myAttrs.end();
128 }
129 
130 
131 std::string
133  const std::string& str) const {
134  std::map<std::string, std::string>::const_iterator it = myAttrs.find(id);
135  if (it != myAttrs.end() && it->second != "") {
136  return it->second;
137  } else {
138  return str;
139  }
140 }
141 
142 
146  std::string funcString = getString(SUMO_ATTR_FUNCTION);
147  if (SUMOXMLDefinitions::EdgeFunctions.hasString(funcString)) {
148  return SUMOXMLDefinitions::EdgeFunctions.get(funcString);
149  }
150  ok = false;
151  }
152  return EDGEFUNC_NORMAL;
153 }
154 
155 
159  std::string typeString = getString(SUMO_ATTR_TYPE);
160  if (SUMOXMLDefinitions::NodeTypes.hasString(typeString)) {
161  return SUMOXMLDefinitions::NodeTypes.get(typeString);
162  }
163  ok = false;
164  }
165  return NODETYPE_UNKNOWN;
166 }
167 
168 
169 RGBColor
172 }
173 
174 
177  StringTokenizer st(getString(attr));
178  PositionVector shape;
179  while (st.hasNext()) {
180  StringTokenizer pos(st.next(), ",");
181  if (pos.size() != 2 && pos.size() != 3) {
182  throw FormatException("shape format");
183  }
184  double x = TplConvert::_2double(pos.next().c_str());
185  double y = TplConvert::_2double(pos.next().c_str());
186  if (pos.size() == 2) {
187  shape.push_back(Position(x, y));
188  } else {
189  double z = TplConvert::_2double(pos.next().c_str());
190  shape.push_back(Position(x, y, z));
191  }
192  }
193  return shape;
194 }
195 
196 
197 Boundary
199  std::string def = getString(attr);
200  StringTokenizer st(def, ",");
201  if (st.size() != 4) {
202  throw FormatException("boundary format");
203  }
204  const double xmin = TplConvert::_2double(st.next().c_str());
205  const double ymin = TplConvert::_2double(st.next().c_str());
206  const double xmax = TplConvert::_2double(st.next().c_str());
207  const double ymax = TplConvert::_2double(st.next().c_str());
208  return Boundary(xmin, ymin, xmax, ymax);
209 }
210 
211 
212 std::vector<std::string>
214  std::string def = getString(attr);
215  std::vector<std::string> ret;
216  parseStringVector(def, ret);
217  return ret;
218 }
219 
220 
221 std::string
223  if (myPredefinedTagsMML.find(attr) == myPredefinedTagsMML.end()) {
224  return "?";
225  }
226  return myPredefinedTagsMML.find(attr)->second;
227 }
228 
229 
230 void
232  for (std::map<std::string, std::string>::const_iterator it = myAttrs.begin(); it != myAttrs.end(); ++it) {
233  os << " " << it->first;
234  os << "=\"" << it->second << "\"";
235  }
236 }
237 
238 
242 }
243 
244 /****************************************************************************/
245 
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:168
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:371
static long long int _2long(const E *const data)
converts a char-type array into the long value described by it
Definition: TplConvert.h:200
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:48
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:46
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:297
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...