Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/xml/SUMOSAXAttributesImpl_Xerces.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_Xerces.cpp
15
/// @author Daniel Krajzewicz
16
/// @author Jakob Erdmann
17
/// @author Michael Behrisch
18
/// @date Sept 2002
19
///
20
// Encapsulated Xerces-SAX-attributes
21
/****************************************************************************/
22
#include <config.h>
23
24
#include <cassert>
25
#include <xercesc/sax2/Attributes.hpp>
26
#include <xercesc/sax2/DefaultHandler.hpp>
27
#include <utils/common/RGBColor.h>
28
#include <utils/common/StringTokenizer.h>
29
#include <utils/common/StringUtils.h>
30
#include <utils/common/StringUtils.h>
31
#include <utils/geom/Boundary.h>
32
#include <utils/geom/PositionVector.h>
33
#include "XMLSubSys.h"
34
#include "SUMOSAXAttributesImpl_Xerces.h"
35
#include "SUMOSAXAttributesImpl_Cached.h"
36
37
38
// ===========================================================================
39
// class definitions
40
// ===========================================================================
41
SUMOSAXAttributesImpl_Xerces::SUMOSAXAttributesImpl_Xerces(const XERCES_CPP_NAMESPACE::Attributes& attrs,
42
const std::vector<XMLCh*>& predefinedTags,
43
const std::vector<std::string>& predefinedTagsMML,
44
const std::string& objectType) :
45
SUMOSAXAttributes(objectType),
46
myAttrs(attrs),
47
myPredefinedTags(predefinedTags),
48
myPredefinedTagsMML(predefinedTagsMML) { }
49
50
51
SUMOSAXAttributesImpl_Xerces::~SUMOSAXAttributesImpl_Xerces() {
52
}
53
54
55
bool
56
SUMOSAXAttributesImpl_Xerces::hasAttribute(int id) const {
57
assert(id >= 0);
58
assert(id < (int)myPredefinedTags.size());
59
return myAttrs.getIndex(myPredefinedTags[id]) >= 0;
60
}
61
62
63
std::string
64
SUMOSAXAttributesImpl_Xerces::getString(int id, bool* isPresent) const {
65
const XMLCh* const xString = getAttributeValueSecure(id);
66
if (xString != nullptr) {
67
return StringUtils::transcode(getAttributeValueSecure(id));
68
}
69
*isPresent = false;
70
return "";
71
}
72
73
74
std::string
75
SUMOSAXAttributesImpl_Xerces::getStringSecure(int id, const std::string& str) const {
76
const XMLCh* utf16 = getAttributeValueSecure(id);
77
if (XERCES_CPP_NAMESPACE::XMLString::stringLen(utf16) == 0) {
78
// TranscodeToStr and debug_new interact badly in this case;
79
return str;
80
} else {
81
return getString(id);
82
}
83
}
84
85
86
const XMLCh*
87
SUMOSAXAttributesImpl_Xerces::getAttributeValueSecure(int id) const {
88
assert(id >= 0);
89
assert(id < (int)myPredefinedTags.size());
90
return myAttrs.getValue(myPredefinedTags[id]);
91
}
92
93
94
double
95
SUMOSAXAttributesImpl_Xerces::getFloat(const std::string& id) const {
96
XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
97
const std::string utf8 = StringUtils::transcode(myAttrs.getValue(t));
98
XERCES_CPP_NAMESPACE::XMLString::release(&t);
99
return StringUtils::toDouble(utf8);
100
}
101
102
103
bool
104
SUMOSAXAttributesImpl_Xerces::hasAttribute(const std::string& id) const {
105
XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
106
bool result = myAttrs.getIndex(t) >= 0;
107
XERCES_CPP_NAMESPACE::XMLString::release(&t);
108
return result;
109
}
110
111
112
std::string
113
SUMOSAXAttributesImpl_Xerces::getStringSecure(const std::string& id,
114
const std::string& str) const {
115
XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
116
const XMLCh* v = myAttrs.getValue(t);
117
XERCES_CPP_NAMESPACE::XMLString::release(&t);
118
if (v == nullptr) {
119
return str;
120
} else {
121
return StringUtils::transcode(v);
122
}
123
}
124
125
126
std::string
127
SUMOSAXAttributesImpl_Xerces::getName(int attr) const {
128
assert(attr >= 0);
129
assert(attr < (int)myPredefinedTagsMML.size());
130
return myPredefinedTagsMML[attr];
131
}
132
133
134
void
135
SUMOSAXAttributesImpl_Xerces::serialize(std::ostream& os) const {
136
for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
137
os << " " << StringUtils::transcode(myAttrs.getLocalName(i));
138
os << "=\"" << StringUtils::transcode(myAttrs.getValue(i)) << "\"";
139
}
140
}
141
142
143
std::vector<std::string>
144
SUMOSAXAttributesImpl_Xerces::getAttributeNames() const {
145
std::vector<std::string> result;
146
for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
147
result.push_back(StringUtils::transcode(myAttrs.getLocalName(i)));
148
}
149
return result;
150
}
151
152
153
SUMOSAXAttributes*
154
SUMOSAXAttributesImpl_Xerces::clone() const {
155
std::map<std::string, std::string> attrs;
156
for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
157
attrs[StringUtils::transcode(myAttrs.getLocalName(i))] = StringUtils::transcode(myAttrs.getValue(i));
158
}
159
return new SUMOSAXAttributesImpl_Cached(attrs, myPredefinedTagsMML, getObjectType());
160
}
161
162
163
/****************************************************************************/
164
165