Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
SUMOSAXAttributesImpl_Xerces.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-2023 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/****************************************************************************/
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>
31#include <utils/geom/Boundary.h>
33#include "XMLSubSys.h"
36
37
38// ===========================================================================
39// class definitions
40// ===========================================================================
41SUMOSAXAttributesImpl_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
53
54
55bool
57 assert(id >= 0);
58 assert(id < (int)myPredefinedTags.size());
59 return myAttrs.getIndex(myPredefinedTags[id]) >= 0;
60}
61
62
63std::string
64SUMOSAXAttributesImpl_Xerces::getString(int id, bool* isPresent) const {
65 const XMLCh* const xString = getAttributeValueSecure(id);
66 if (xString != nullptr) {
68 }
69 *isPresent = false;
70 return "";
71}
72
73
74std::string
75SUMOSAXAttributesImpl_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
86const XMLCh*
88 assert(id >= 0);
89 assert(id < (int)myPredefinedTags.size());
90 return myAttrs.getValue(myPredefinedTags[id]);
91}
92
93
94double
95SUMOSAXAttributesImpl_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
103bool
104SUMOSAXAttributesImpl_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
112std::string
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
126std::string
128 assert(attr >= 0);
129 assert(attr < (int)myPredefinedTagsMML.size());
130 return myPredefinedTagsMML[attr];
131}
132
133
134void
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
143std::vector<std::string>
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
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 }
160}
161
162
163/****************************************************************************/
Encapsulated SAX-Attributes.
const std::string & getObjectType() const
return the objecttype to which these attributes belong
Encapsulated Xerces-SAX-attributes.
const XMLCh * getAttributeValueSecure(int id) const
Returns Xerces-value of the named attribute.
std::string getString(int id, bool *isPresent=nullptr) const
Returns the string-value of the named (by its enum-value) attribute.
const std::vector< std::string > & myPredefinedTagsMML
Map of attribute ids to their (readable) string-representation.
std::string getName(int attr) const
Converts the given attribute id into a man readable string.
std::vector< std::string > getAttributeNames() const
Retrieves all attribute names.
const XERCES_CPP_NAMESPACE::Attributes & myAttrs
The encapsulated attributes.
SUMOSAXAttributesImpl_Xerces(const XERCES_CPP_NAMESPACE::Attributes &attrs, const std::vector< XMLCh * > &predefinedTags, const std::vector< std::string > &predefinedTagsMML, const std::string &objectType)
Constructor.
double getFloat(const std::string &id) const
Returns the double-value of the named attribute.
const AttrMap & myPredefinedTags
Map of attribute ids to their xerces-representation.
void serialize(std::ostream &os) const
Prints all attribute names and values into the given stream.
std::string getStringSecure(int id, const std::string &def) const
Returns the string-value of the named (by its enum-value) attribute.
SUMOSAXAttributes * clone() const
return a new deep-copy attributes object
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
static std::string transcode(const XMLCh *const data)
converts a 0-terminated XMLCh* array (usually UTF-16, stemming from Xerces) into std::string in UTF-8