Eclipse 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.dev/sumo
3 // Copyright (C) 2002-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // Encapsulated xml-attributes that use a map from string-attr-names to string-attr-values as backend
19 /****************************************************************************/
20 #include <config.h>
21 
22 #include <cassert>
23 #include <xercesc/sax2/Attributes.hpp>
24 #include <xercesc/sax2/DefaultHandler.hpp>
25 #include <xercesc/util/XercesVersion.hpp>
26 #include <xercesc/util/TransService.hpp>
27 #include <xercesc/util/TranscodingException.hpp>
28 #include <utils/common/RGBColor.h>
32 #include <utils/geom/Boundary.h>
36 
37 
38 // ===========================================================================
39 // class definitions
40 // ===========================================================================
42  const std::map<std::string, std::string>& attrs,
43  const std::vector<std::string>& predefinedTagsMML,
44  const std::string& objectType) :
45  SUMOSAXAttributes(objectType),
46  myAttrs(attrs),
47  myPredefinedTagsMML(predefinedTagsMML) { }
48 
49 
51 
52 
53 bool
55  assert(id >= 0);
56  assert(id < (int)myPredefinedTagsMML.size());
57  return myAttrs.find(myPredefinedTagsMML[id]) != myAttrs.end();
58 }
59 
60 
61 std::string
62 SUMOSAXAttributesImpl_Cached::getString(int id, bool* isPresent) const {
63  const auto it = myAttrs.find(myPredefinedTagsMML[id]);
64  if (it != myAttrs.end()) {
65  return it->second;
66  }
67  *isPresent = false;
68  return "";
69 }
70 
71 
72 std::string
73 SUMOSAXAttributesImpl_Cached::getStringSecure(int id, const std::string& str) const {
74  const std::string& result = getAttributeValueSecure(id);
75  return result.size() == 0 ? str : result;
76 }
77 
78 
79 const std::string&
81  assert(id >= 0);
82  assert(id < (int)myPredefinedTagsMML.size());
83  return myAttrs.find(myPredefinedTagsMML[id])->second;
84 }
85 
86 
87 double
88 SUMOSAXAttributesImpl_Cached::getFloat(const std::string& id) const {
89  return StringUtils::toDouble(myAttrs.find(id)->second);
90 }
91 
92 
93 bool
94 SUMOSAXAttributesImpl_Cached::hasAttribute(const std::string& id) const {
95  return myAttrs.find(id) != myAttrs.end();
96 }
97 
98 
99 std::string
101  const std::string& str) const {
102  std::map<std::string, std::string>::const_iterator it = myAttrs.find(id);
103  if (it != myAttrs.end() && it->second != "") {
104  return it->second;
105  } else {
106  return str;
107  }
108 }
109 
110 
111 std::string
113  assert(attr >= 0);
114  assert(attr < (int)myPredefinedTagsMML.size());
115  return myPredefinedTagsMML[attr];
116 }
117 
118 
119 void
121  for (std::map<std::string, std::string>::const_iterator it = myAttrs.begin(); it != myAttrs.end(); ++it) {
122  os << " " << it->first;
123  os << "=\"" << it->second << "\"";
124  }
125 }
126 
127 std::vector<std::string>
129  std::vector<std::string> result;
130  for (std::map<std::string, std::string>::const_iterator it = myAttrs.begin(); it != myAttrs.end(); ++it) {
131  result.push_back(it->first);
132  }
133  return result;
134 }
135 
139 }
140 
141 
142 /****************************************************************************/
Encapsulated SAX-Attributes.
const std::string & getObjectType() const
return the objecttype to which these attributes belong
std::string getName(int attr) const
Converts the given attribute id into a man readable string.
std::string getStringSecure(int id, const std::string &def) const
Returns the string-value of the named (by its enum-value) attribute.
std::map< std::string, std::string > myAttrs
The encapsulated attributes.
double getFloat(const std::string &id) const
Returns the double-value of the named attribute.
SUMOSAXAttributesImpl_Cached(const std::map< std::string, std::string > &attrs, const std::vector< std::string > &predefinedTagsMML, const std::string &objectType)
Constructor.
const std::string & getAttributeValueSecure(int id) const
Returns Xerces-value of the named attribute.
void serialize(std::ostream &os) const
Prints all attribute names and values into the given stream.
std::vector< std::string > getAttributeNames() const
Retrieves all attribute names.
std::string getString(int id, bool *isPresent=nullptr) const
Returns the string-value of the named (by its enum-value) attribute.
SUMOSAXAttributes * clone() const
return a new deep-copy attributes object
const std::vector< std::string > & myPredefinedTagsMML
Map of attribute ids to their (readable) string-representation.
bool hasAttribute(int id) const
Returns the information whether the named (by its enum-value) attribute is within the current list.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter