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 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 
51 // ===========================================================================
52 // class definitions
53 // ===========================================================================
55  const std::map<std::string, std::string>& attrs,
56  const std::map<int, std::string>& predefinedTagsMML,
57  const std::string& objectType) :
58  SUMOSAXAttributes(objectType),
59  myAttrs(attrs),
60  myPredefinedTagsMML(predefinedTagsMML) { }
61 
62 
64 }
65 
66 
67 bool
69  std::map<int, std::string>::const_iterator i = myPredefinedTagsMML.find(id);
70  if (i == myPredefinedTagsMML.end()) {
71  return false;
72  }
73  return myAttrs.find((*i).second) != myAttrs.end();
74 }
75 
76 
77 bool
80 }
81 
82 
83 int
86 }
87 
88 
89 long long int
92 }
93 
94 
95 std::string
97  return getAttributeValueSecure(id);
98 }
99 
100 
101 std::string
103  const std::string& str) const {
104  std::string result = getAttributeValueSecure(id);
105  return result.size() == 0 ? str : result;
106 }
107 
108 
109 SUMOReal
112 }
113 
114 
115 const char*
117  std::map<int, std::string>::const_iterator i = myPredefinedTagsMML.find(id);
118  assert(i != myPredefinedTagsMML.end());
119  return myAttrs.find((*i).second)->second.c_str();
120 }
121 
122 
123 SUMOReal
124 SUMOSAXAttributesImpl_Cached::getFloat(const std::string& id) const {
125  return TplConvert::_2SUMOReal(myAttrs.find(id)->second.c_str());
126 }
127 
128 
129 bool
130 SUMOSAXAttributesImpl_Cached::hasAttribute(const std::string& id) const {
131  return myAttrs.find(id) != myAttrs.end();
132 }
133 
134 
135 std::string
137  const std::string& str) const {
138  std::map<std::string, std::string>::const_iterator it = myAttrs.find(id);
139  if (it != myAttrs.end() && it->second != "") {
140  return it->second;
141  } else {
142  return str;
143  }
144 }
145 
146 
150  std::string funcString = getString(SUMO_ATTR_FUNCTION);
151  if (SUMOXMLDefinitions::EdgeFunctions.hasString(funcString)) {
152  return SUMOXMLDefinitions::EdgeFunctions.get(funcString);
153  }
154  ok = false;
155  }
156  return EDGEFUNC_NORMAL;
157 }
158 
159 
163  std::string typeString = getString(SUMO_ATTR_TYPE);
164  if (SUMOXMLDefinitions::NodeTypes.hasString(typeString)) {
165  return SUMOXMLDefinitions::NodeTypes.get(typeString);
166  }
167  ok = false;
168  }
169  return NODETYPE_UNKNOWN;
170 }
171 
172 
173 RGBColor
176 }
177 
178 
181  StringTokenizer st(getString(attr));
182  PositionVector shape;
183  while (st.hasNext()) {
184  StringTokenizer pos(st.next(), ",");
185  if (pos.size() != 2 && pos.size() != 3) {
186  throw FormatException("shape format");
187  }
188  SUMOReal x = TplConvert::_2SUMOReal(pos.next().c_str());
189  SUMOReal y = TplConvert::_2SUMOReal(pos.next().c_str());
190  if (pos.size() == 2) {
191  shape.push_back(Position(x, y));
192  } else {
193  SUMOReal z = TplConvert::_2SUMOReal(pos.next().c_str());
194  shape.push_back(Position(x, y, z));
195  }
196  }
197  return shape;
198 }
199 
200 
201 Boundary
203  std::string def = getString(attr);
204  StringTokenizer st(def, ",");
205  if (st.size() != 4) {
206  throw FormatException("boundary format");
207  }
208  const SUMOReal xmin = TplConvert::_2SUMOReal(st.next().c_str());
209  const SUMOReal ymin = TplConvert::_2SUMOReal(st.next().c_str());
210  const SUMOReal xmax = TplConvert::_2SUMOReal(st.next().c_str());
211  const SUMOReal ymax = TplConvert::_2SUMOReal(st.next().c_str());
212  return Boundary(xmin, ymin, xmax, ymax);
213 }
214 
215 
216 std::vector<std::string>
218  std::string def = getString(attr);
219  std::vector<std::string> ret;
220  parseStringVector(def, ret);
221  return ret;
222 }
223 
224 
225 std::string
227  if (myPredefinedTagsMML.find(attr) == myPredefinedTagsMML.end()) {
228  return "?";
229  }
230  return myPredefinedTagsMML.find(attr)->second;
231 }
232 
233 
234 void
236  for (std::map<std::string, std::string>::const_iterator it = myAttrs.begin(); it != myAttrs.end(); ++it) {
237  os << " " << it->first;
238  os << "=\"" << it->second << "\"";
239  }
240 }
241 
242 
246 }
247 
248 /****************************************************************************/
249 
RGBColor getColor() const
Returns the value of the named attribute.
static StringBijection< SumoXMLNodeType > NodeTypes
static RGBColor parseColor(std::string coldef)
Parses a color information.
Definition: RGBColor.cpp:172
int getInt(int id) const
Returns the int-value of the named (by its enum-value) attribute.
PositionVector getShape(int attr) const
Tries to read given attribute assuming it is a PositionVector.
std::string next()
static SUMOReal _2SUMOReal(const E *const data)
converts a char-type array into the SUMOReal value described by it
Definition: TplConvert.h:297
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
std::string getString(int id) const
Returns the string-value of the named (by its enum-value) attribute.
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.
SumoXMLNodeType getNodeType(bool &ok) const
Returns the value of the named attribute.
SUMOSAXAttributes * clone() const
return a new deep-copy attributes object
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
long long int getLong(int id) const
Returns the long-value of the named (by its enum-value) attribute.
bool hasAttribute(int id) const
Returns the information whether the named (by its enum-value) attribute is within the current list...
std::vector< std::string > getStringVector(int attr) const
Tries to read given attribute assuming it is a string vector.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
std::string getStringSecure(int id, const std::string &def) const
Returns the string-value of the named (by its enum-value) attribute.
const std::string & getObjectType() const
return the objecttype to which these attributes belong
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.
bool getBool(int id) const
Returns the bool-value of the named (by its enum-value) attribute.
std::string getName(int attr) const
Converts the given attribute id into a man readable string.
virtual ~SUMOSAXAttributesImpl_Cached()
Destructor.
static int _2int(const E *const data)
converts a char-type array into the integer value described by it
Definition: TplConvert.h:149
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
Boundary getBoundary(int attr) const
Tries to read given attribute assuming it is a Boundary.
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
T get(const std::string &str) const
#define SUMOReal
Definition: config.h:208
SUMOSAXAttributesImpl_Cached(const std::map< std::string, std::string > &attrs, const std::map< int, std::string > &predefinedTagsMML, const std::string &objectType)
Constructor.
void serialize(std::ostream &os) const
Prints all attribute names and values into the given stream.
SUMOReal getFloat(int id) const
Returns the SUMOReal-value of the named (by its enum-value) attribute.
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
std::map< std::string, std::string > myAttrs
The encapsulated attributes.
A color information.