Line data Source code
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 15748244 : SUMOSAXAttributesImpl_Xerces::SUMOSAXAttributesImpl_Xerces(const XERCES_CPP_NAMESPACE::Attributes& attrs,
42 : const std::vector<XMLCh*>& predefinedTags,
43 : const std::vector<std::string>& predefinedTagsMML,
44 15748244 : const std::string& objectType) :
45 : SUMOSAXAttributes(objectType),
46 15748244 : myAttrs(attrs),
47 15748244 : myPredefinedTags(predefinedTags),
48 15748244 : myPredefinedTagsMML(predefinedTagsMML) { }
49 :
50 :
51 15748547 : SUMOSAXAttributesImpl_Xerces::~SUMOSAXAttributesImpl_Xerces() {
52 15748547 : }
53 :
54 :
55 : bool
56 32321887 : SUMOSAXAttributesImpl_Xerces::hasAttribute(int id) const {
57 : assert(id >= 0);
58 : assert(id < (int)myPredefinedTags.size());
59 32321887 : return myAttrs.getIndex(myPredefinedTags[id]) >= 0;
60 : }
61 :
62 :
63 : std::string
64 117195465 : SUMOSAXAttributesImpl_Xerces::getString(int id, bool* isPresent) const {
65 117195465 : const XMLCh* const xString = getAttributeValueSecure(id);
66 117195465 : if (xString != nullptr) {
67 60819523 : return StringUtils::transcode(getAttributeValueSecure(id));
68 : }
69 56375942 : *isPresent = false;
70 56375942 : return "";
71 : }
72 :
73 :
74 : std::string
75 990787 : SUMOSAXAttributesImpl_Xerces::getStringSecure(int id, const std::string& str) const {
76 990787 : const XMLCh* utf16 = getAttributeValueSecure(id);
77 979783 : if (XERCES_CPP_NAMESPACE::XMLString::stringLen(utf16) == 0) {
78 : // TranscodeToStr and debug_new interact badly in this case;
79 : return str;
80 : } else {
81 817466 : return getString(id);
82 : }
83 : }
84 :
85 :
86 : const XMLCh*
87 179005775 : SUMOSAXAttributesImpl_Xerces::getAttributeValueSecure(int id) const {
88 : assert(id >= 0);
89 : assert(id < (int)myPredefinedTags.size());
90 179005775 : return myAttrs.getValue(myPredefinedTags[id]);
91 : }
92 :
93 :
94 : double
95 5837 : SUMOSAXAttributesImpl_Xerces::getFloat(const std::string& id) const {
96 5837 : XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
97 5837 : const std::string utf8 = StringUtils::transcode(myAttrs.getValue(t));
98 5837 : XERCES_CPP_NAMESPACE::XMLString::release(&t);
99 11674 : return StringUtils::toDouble(utf8);
100 : }
101 :
102 :
103 : bool
104 509255 : SUMOSAXAttributesImpl_Xerces::hasAttribute(const std::string& id) const {
105 509255 : XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
106 509255 : bool result = myAttrs.getIndex(t) >= 0;
107 509255 : XERCES_CPP_NAMESPACE::XMLString::release(&t);
108 509255 : return result;
109 : }
110 :
111 :
112 : std::string
113 469777 : SUMOSAXAttributesImpl_Xerces::getStringSecure(const std::string& id,
114 : const std::string& str) const {
115 469777 : XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());
116 469777 : const XMLCh* v = myAttrs.getValue(t);
117 469777 : XERCES_CPP_NAMESPACE::XMLString::release(&t);
118 469777 : if (v == nullptr) {
119 : return str;
120 : } else {
121 465309 : return StringUtils::transcode(v);
122 : }
123 : }
124 :
125 :
126 : std::string
127 1262 : SUMOSAXAttributesImpl_Xerces::getName(int attr) const {
128 : assert(attr >= 0);
129 : assert(attr < (int)myPredefinedTagsMML.size());
130 1262 : return myPredefinedTagsMML[attr];
131 : }
132 :
133 :
134 : void
135 136 : SUMOSAXAttributesImpl_Xerces::serialize(std::ostream& os) const {
136 460 : for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
137 648 : os << " " << StringUtils::transcode(myAttrs.getLocalName(i));
138 972 : os << "=\"" << StringUtils::transcode(myAttrs.getValue(i)) << "\"";
139 : }
140 136 : }
141 :
142 :
143 : std::vector<std::string>
144 0 : SUMOSAXAttributesImpl_Xerces::getAttributeNames() const {
145 : std::vector<std::string> result;
146 0 : for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
147 0 : result.push_back(StringUtils::transcode(myAttrs.getLocalName(i)));
148 : }
149 0 : return result;
150 0 : }
151 :
152 :
153 : SUMOSAXAttributes*
154 13351 : SUMOSAXAttributesImpl_Xerces::clone() const {
155 : std::map<std::string, std::string> attrs;
156 94675 : for (int i = 0; i < (int)myAttrs.getLength(); ++i) {
157 162648 : attrs[StringUtils::transcode(myAttrs.getLocalName(i))] = StringUtils::transcode(myAttrs.getValue(i));
158 : }
159 26702 : return new SUMOSAXAttributesImpl_Cached(attrs, myPredefinedTagsMML, getObjectType());
160 : }
161 :
162 :
163 : /****************************************************************************/
|