1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2019 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 /****************************************************************************/
10 /// @file SUMOSAXAttributesImpl_Cached.cpp
11 /// @author Jakob Erdmann
12 /// @date Dec 2016
13 /// @version $Id$
14 ///
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>
31 #include <utils/common/StringTokenizer.h>
32 #include <utils/common/StringUtils.h>
33 #include <utils/common/StringBijection.h>
34 #include <utils/geom/Boundary.h>
35 #include <utils/geom/PositionVector.h>
36 #include "SUMOSAXAttributesImpl_Cached.h"
37 #include "SUMOSAXAttributesImpl_Cached.h"
38
39
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
SUMOSAXAttributesImpl_Cached(const std::map<std::string,std::string> & attrs,const std::map<int,std::string> & predefinedTagsMML,const std::string & objectType)43 SUMOSAXAttributesImpl_Cached::SUMOSAXAttributesImpl_Cached(
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
SUMOSAXAttributesImpl_Cached(const std::map<SumoXMLAttr,std::string> & attrs,const std::map<int,std::string> & predefinedTagsMML,const std::string & objectType)52 SUMOSAXAttributesImpl_Cached::SUMOSAXAttributesImpl_Cached(
53 const std::map<SumoXMLAttr, std::string>& attrs,
54 const std::map<int, std::string>& predefinedTagsMML,
55 const std::string& objectType) :
56 SUMOSAXAttributes(objectType),
57 myPredefinedTagsMML(predefinedTagsMML) {
58 // parse <SumoXMLAttr, string> to <string, string>
59 for (const auto& i : attrs) {
60 myAttrs[toString(i.first)] = i.second;
61 }
62 }
63
64
~SUMOSAXAttributesImpl_Cached()65 SUMOSAXAttributesImpl_Cached::~SUMOSAXAttributesImpl_Cached() { }
66
67
68 bool
hasAttribute(int id) const69 SUMOSAXAttributesImpl_Cached::hasAttribute(int id) const {
70 std::map<int, std::string>::const_iterator i = myPredefinedTagsMML.find(id);
71 if (i == myPredefinedTagsMML.end()) {
72 return false;
73 }
74 return myAttrs.find((*i).second) != myAttrs.end();
75 }
76
77
78 bool
getBool(int id) const79 SUMOSAXAttributesImpl_Cached::getBool(int id) const {
80 return StringUtils::toBool(getAttributeValueSecure(id));
81 }
82
83
84 int
getInt(int id) const85 SUMOSAXAttributesImpl_Cached::getInt(int id) const {
86 return StringUtils::toInt(getAttributeValueSecure(id));
87 }
88
89
90 long long int
getLong(int id) const91 SUMOSAXAttributesImpl_Cached::getLong(int id) const {
92 return StringUtils::toLong(getAttributeValueSecure(id));
93 }
94
95
96 std::string
getString(int id) const97 SUMOSAXAttributesImpl_Cached::getString(int id) const {
98 return getAttributeValueSecure(id);
99 }
100
101
102 std::string
getStringSecure(int id,const std::string & str) const103 SUMOSAXAttributesImpl_Cached::getStringSecure(int id, const std::string& str) const {
104 const std::string& result = getAttributeValueSecure(id);
105 return result.size() == 0 ? str : result;
106 }
107
108
109 double
getFloat(int id) const110 SUMOSAXAttributesImpl_Cached::getFloat(int id) const {
111 return StringUtils::toDouble(getAttributeValueSecure(id));
112 }
113
114
115 const std::string&
getAttributeValueSecure(int id) const116 SUMOSAXAttributesImpl_Cached::getAttributeValueSecure(int id) const {
117 std::map<int, std::string>::const_iterator i = myPredefinedTagsMML.find(id);
118 assert(i != myPredefinedTagsMML.end());
119 return myAttrs.find(i->second)->second;
120 }
121
122
123 double
getFloat(const std::string & id) const124 SUMOSAXAttributesImpl_Cached::getFloat(const std::string& id) const {
125 return StringUtils::toDouble(myAttrs.find(id)->second);
126 }
127
128
129 bool
hasAttribute(const std::string & id) const130 SUMOSAXAttributesImpl_Cached::hasAttribute(const std::string& id) const {
131 return myAttrs.find(id) != myAttrs.end();
132 }
133
134
135 std::string
getStringSecure(const std::string & id,const std::string & str) const136 SUMOSAXAttributesImpl_Cached::getStringSecure(const std::string& id,
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
147 SumoXMLEdgeFunc
getEdgeFunc(bool & ok) const148 SUMOSAXAttributesImpl_Cached::getEdgeFunc(bool& ok) const {
149 if (hasAttribute(SUMO_ATTR_FUNCTION)) {
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
160 SumoXMLNodeType
getNodeType(bool & ok) const161 SUMOSAXAttributesImpl_Cached::getNodeType(bool& ok) const {
162 if (hasAttribute(SUMO_ATTR_TYPE)) {
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 RightOfWay
getRightOfWay(bool & ok) const174 SUMOSAXAttributesImpl_Cached::getRightOfWay(bool& ok) const {
175 if (hasAttribute(SUMO_ATTR_RIGHT_OF_WAY)) {
176 std::string rowString = getString(SUMO_ATTR_RIGHT_OF_WAY);
177 if (SUMOXMLDefinitions::RightOfWayValues.hasString(rowString)) {
178 return SUMOXMLDefinitions::RightOfWayValues.get(rowString);
179 }
180 ok = false;
181 }
182 return RIGHT_OF_WAY_DEFAULT;
183 }
184
185 FringeType
getFringeType(bool & ok) const186 SUMOSAXAttributesImpl_Cached::getFringeType(bool& ok) const {
187 if (hasAttribute(SUMO_ATTR_FRINGE)) {
188 std::string fringeString = getString(SUMO_ATTR_FRINGE);
189 if (SUMOXMLDefinitions::FringeTypeValues.hasString(fringeString)) {
190 return SUMOXMLDefinitions::FringeTypeValues.get(fringeString);
191 }
192 ok = false;
193 }
194 return FRINGE_TYPE_DEFAULT;
195 }
196
197 RGBColor
getColor() const198 SUMOSAXAttributesImpl_Cached::getColor() const {
199 return RGBColor::parseColor(getString(SUMO_ATTR_COLOR));
200 }
201
202
203 PositionVector
getShape(int attr) const204 SUMOSAXAttributesImpl_Cached::getShape(int attr) const {
205 StringTokenizer st(getString(attr));
206 PositionVector shape;
207 while (st.hasNext()) {
208 StringTokenizer pos(st.next(), ",");
209 if (pos.size() != 2 && pos.size() != 3) {
210 throw FormatException("shape format");
211 }
212 double x = StringUtils::toDouble(pos.next());
213 double y = StringUtils::toDouble(pos.next());
214 if (pos.size() == 2) {
215 shape.push_back(Position(x, y));
216 } else {
217 double z = StringUtils::toDouble(pos.next());
218 shape.push_back(Position(x, y, z));
219 }
220 }
221 return shape;
222 }
223
224
225 Boundary
getBoundary(int attr) const226 SUMOSAXAttributesImpl_Cached::getBoundary(int attr) const {
227 std::string def = getString(attr);
228 StringTokenizer st(def, ",");
229 if (st.size() != 4) {
230 throw FormatException("boundary format");
231 }
232 const double xmin = StringUtils::toDouble(st.next());
233 const double ymin = StringUtils::toDouble(st.next());
234 const double xmax = StringUtils::toDouble(st.next());
235 const double ymax = StringUtils::toDouble(st.next());
236 return Boundary(xmin, ymin, xmax, ymax);
237 }
238
239
240 std::string
getName(int attr) const241 SUMOSAXAttributesImpl_Cached::getName(int attr) const {
242 if (myPredefinedTagsMML.find(attr) == myPredefinedTagsMML.end()) {
243 return "?";
244 }
245 return myPredefinedTagsMML.find(attr)->second;
246 }
247
248
249 void
serialize(std::ostream & os) const250 SUMOSAXAttributesImpl_Cached::serialize(std::ostream& os) const {
251 for (std::map<std::string, std::string>::const_iterator it = myAttrs.begin(); it != myAttrs.end(); ++it) {
252 os << " " << it->first;
253 os << "=\"" << it->second << "\"";
254 }
255 }
256
257 std::vector<std::string>
getAttributeNames() const258 SUMOSAXAttributesImpl_Cached::getAttributeNames() const {
259 std::vector<std::string> result;
260 for (std::map<std::string, std::string>::const_iterator it = myAttrs.begin(); it != myAttrs.end(); ++it) {
261 result.push_back(it->first);
262 }
263 return result;
264 }
265
266 SUMOSAXAttributes*
clone() const267 SUMOSAXAttributesImpl_Cached::clone() const {
268 return new SUMOSAXAttributesImpl_Cached(myAttrs, myPredefinedTagsMML, getObjectType());
269 }
270
271 /****************************************************************************/
272
273