Path: blob/main/src/utils/xml/SUMOSAXAttributesImpl_Xerces.cpp
169678 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2002-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file SUMOSAXAttributesImpl_Xerces.cpp14/// @author Daniel Krajzewicz15/// @author Jakob Erdmann16/// @author Michael Behrisch17/// @date Sept 200218///19// Encapsulated Xerces-SAX-attributes20/****************************************************************************/21#include <config.h>2223#include <cassert>24#include <xercesc/sax2/Attributes.hpp>25#include <xercesc/sax2/DefaultHandler.hpp>26#include <utils/common/RGBColor.h>27#include <utils/common/StringTokenizer.h>28#include <utils/common/StringUtils.h>29#include <utils/common/StringUtils.h>30#include <utils/geom/Boundary.h>31#include <utils/geom/PositionVector.h>32#include "XMLSubSys.h"33#include "SUMOSAXAttributesImpl_Xerces.h"34#include "SUMOSAXAttributesImpl_Cached.h"353637// ===========================================================================38// class definitions39// ===========================================================================40SUMOSAXAttributesImpl_Xerces::SUMOSAXAttributesImpl_Xerces(const XERCES_CPP_NAMESPACE::Attributes& attrs,41const std::vector<XMLCh*>& predefinedTags,42const std::vector<std::string>& predefinedTagsMML,43const std::string& objectType) :44SUMOSAXAttributes(objectType),45myAttrs(attrs),46myPredefinedTags(predefinedTags),47myPredefinedTagsMML(predefinedTagsMML) { }484950SUMOSAXAttributesImpl_Xerces::~SUMOSAXAttributesImpl_Xerces() {51}525354bool55SUMOSAXAttributesImpl_Xerces::hasAttribute(int id) const {56assert(id >= 0);57assert(id < (int)myPredefinedTags.size());58return myAttrs.getIndex(myPredefinedTags[id]) >= 0;59}606162std::string63SUMOSAXAttributesImpl_Xerces::getString(int id, bool* isPresent) const {64const XMLCh* const xString = getAttributeValueSecure(id);65if (xString != nullptr) {66return StringUtils::transcode(getAttributeValueSecure(id));67}68*isPresent = false;69return "";70}717273std::string74SUMOSAXAttributesImpl_Xerces::getStringSecure(int id, const std::string& str) const {75const XMLCh* utf16 = getAttributeValueSecure(id);76if (XERCES_CPP_NAMESPACE::XMLString::stringLen(utf16) == 0) {77// TranscodeToStr and debug_new interact badly in this case;78return str;79} else {80return getString(id);81}82}838485const XMLCh*86SUMOSAXAttributesImpl_Xerces::getAttributeValueSecure(int id) const {87assert(id >= 0);88assert(id < (int)myPredefinedTags.size());89return myAttrs.getValue(myPredefinedTags[id]);90}919293double94SUMOSAXAttributesImpl_Xerces::getFloat(const std::string& id) const {95XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());96const std::string utf8 = StringUtils::transcode(myAttrs.getValue(t));97XERCES_CPP_NAMESPACE::XMLString::release(&t);98return StringUtils::toDouble(utf8);99}100101102bool103SUMOSAXAttributesImpl_Xerces::hasAttribute(const std::string& id) const {104XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());105bool result = myAttrs.getIndex(t) >= 0;106XERCES_CPP_NAMESPACE::XMLString::release(&t);107return result;108}109110111std::string112SUMOSAXAttributesImpl_Xerces::getStringSecure(const std::string& id,113const std::string& str) const {114XMLCh* t = XERCES_CPP_NAMESPACE::XMLString::transcode(id.c_str());115const XMLCh* v = myAttrs.getValue(t);116XERCES_CPP_NAMESPACE::XMLString::release(&t);117if (v == nullptr) {118return str;119} else {120return StringUtils::transcode(v);121}122}123124125std::string126SUMOSAXAttributesImpl_Xerces::getName(int attr) const {127assert(attr >= 0);128assert(attr < (int)myPredefinedTagsMML.size());129return myPredefinedTagsMML[attr];130}131132133void134SUMOSAXAttributesImpl_Xerces::serialize(std::ostream& os) const {135for (int i = 0; i < (int)myAttrs.getLength(); ++i) {136os << " " << StringUtils::transcode(myAttrs.getLocalName(i));137os << "=\"" << StringUtils::transcode(myAttrs.getValue(i)) << "\"";138}139}140141142std::vector<std::string>143SUMOSAXAttributesImpl_Xerces::getAttributeNames() const {144std::vector<std::string> result;145for (int i = 0; i < (int)myAttrs.getLength(); ++i) {146result.push_back(StringUtils::transcode(myAttrs.getLocalName(i)));147}148return result;149}150151152SUMOSAXAttributes*153SUMOSAXAttributesImpl_Xerces::clone() const {154std::map<std::string, std::string> attrs;155for (int i = 0; i < (int)myAttrs.getLength(); ++i) {156attrs[StringUtils::transcode(myAttrs.getLocalName(i))] = StringUtils::transcode(myAttrs.getValue(i));157}158return new SUMOSAXAttributesImpl_Cached(attrs, myPredefinedTagsMML, getObjectType());159}160161162/****************************************************************************/163164165