Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/xml/SUMOSAXAttributesImpl_Cached.cpp
169678 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2002-2025 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
/****************************************************************************/
14
/// @file SUMOSAXAttributesImpl_Cached.cpp
15
/// @author Jakob Erdmann
16
/// @date Dec 2016
17
///
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>
29
#include <utils/common/StringTokenizer.h>
30
#include <utils/common/StringUtils.h>
31
#include <utils/common/StringBijection.h>
32
#include <utils/geom/Boundary.h>
33
#include <utils/geom/PositionVector.h>
34
#include "SUMOSAXAttributesImpl_Cached.h"
35
#include "SUMOSAXAttributesImpl_Cached.h"
36
37
38
// ===========================================================================
39
// class definitions
40
// ===========================================================================
41
SUMOSAXAttributesImpl_Cached::SUMOSAXAttributesImpl_Cached(
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
50
SUMOSAXAttributesImpl_Cached::~SUMOSAXAttributesImpl_Cached() { }
51
52
53
bool
54
SUMOSAXAttributesImpl_Cached::hasAttribute(int id) const {
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&
80
SUMOSAXAttributesImpl_Cached::getAttributeValueSecure(int id) const {
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
100
SUMOSAXAttributesImpl_Cached::getStringSecure(const std::string& id,
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
112
SUMOSAXAttributesImpl_Cached::getName(int attr) const {
113
assert(attr >= 0);
114
assert(attr < (int)myPredefinedTagsMML.size());
115
return myPredefinedTagsMML[attr];
116
}
117
118
119
void
120
SUMOSAXAttributesImpl_Cached::serialize(std::ostream& os) const {
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>
128
SUMOSAXAttributesImpl_Cached::getAttributeNames() const {
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
136
SUMOSAXAttributes*
137
SUMOSAXAttributesImpl_Cached::clone() const {
138
return new SUMOSAXAttributesImpl_Cached(myAttrs, myPredefinedTagsMML, getObjectType());
139
}
140
141
142
/****************************************************************************/
143
144