Path: blob/main/src/utils/xml/SUMOSAXAttributesImpl_Cached.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_Cached.cpp14/// @author Jakob Erdmann15/// @date Dec 201616///17// Encapsulated xml-attributes that use a map from string-attr-names to string-attr-values as backend18/****************************************************************************/19#include <config.h>2021#include <cassert>22#include <xercesc/sax2/Attributes.hpp>23#include <xercesc/sax2/DefaultHandler.hpp>24#include <xercesc/util/XercesVersion.hpp>25#include <xercesc/util/TransService.hpp>26#include <xercesc/util/TranscodingException.hpp>27#include <utils/common/RGBColor.h>28#include <utils/common/StringTokenizer.h>29#include <utils/common/StringUtils.h>30#include <utils/common/StringBijection.h>31#include <utils/geom/Boundary.h>32#include <utils/geom/PositionVector.h>33#include "SUMOSAXAttributesImpl_Cached.h"34#include "SUMOSAXAttributesImpl_Cached.h"353637// ===========================================================================38// class definitions39// ===========================================================================40SUMOSAXAttributesImpl_Cached::SUMOSAXAttributesImpl_Cached(41const std::map<std::string, std::string>& attrs,42const std::vector<std::string>& predefinedTagsMML,43const std::string& objectType) :44SUMOSAXAttributes(objectType),45myAttrs(attrs),46myPredefinedTagsMML(predefinedTagsMML) { }474849SUMOSAXAttributesImpl_Cached::~SUMOSAXAttributesImpl_Cached() { }505152bool53SUMOSAXAttributesImpl_Cached::hasAttribute(int id) const {54assert(id >= 0);55assert(id < (int)myPredefinedTagsMML.size());56return myAttrs.find(myPredefinedTagsMML[id]) != myAttrs.end();57}585960std::string61SUMOSAXAttributesImpl_Cached::getString(int id, bool* isPresent) const {62const auto it = myAttrs.find(myPredefinedTagsMML[id]);63if (it != myAttrs.end()) {64return it->second;65}66*isPresent = false;67return "";68}697071std::string72SUMOSAXAttributesImpl_Cached::getStringSecure(int id, const std::string& str) const {73const std::string& result = getAttributeValueSecure(id);74return result.size() == 0 ? str : result;75}767778const std::string&79SUMOSAXAttributesImpl_Cached::getAttributeValueSecure(int id) const {80assert(id >= 0);81assert(id < (int)myPredefinedTagsMML.size());82return myAttrs.find(myPredefinedTagsMML[id])->second;83}848586double87SUMOSAXAttributesImpl_Cached::getFloat(const std::string& id) const {88return StringUtils::toDouble(myAttrs.find(id)->second);89}909192bool93SUMOSAXAttributesImpl_Cached::hasAttribute(const std::string& id) const {94return myAttrs.find(id) != myAttrs.end();95}969798std::string99SUMOSAXAttributesImpl_Cached::getStringSecure(const std::string& id,100const std::string& str) const {101std::map<std::string, std::string>::const_iterator it = myAttrs.find(id);102if (it != myAttrs.end() && it->second != "") {103return it->second;104} else {105return str;106}107}108109110std::string111SUMOSAXAttributesImpl_Cached::getName(int attr) const {112assert(attr >= 0);113assert(attr < (int)myPredefinedTagsMML.size());114return myPredefinedTagsMML[attr];115}116117118void119SUMOSAXAttributesImpl_Cached::serialize(std::ostream& os) const {120for (std::map<std::string, std::string>::const_iterator it = myAttrs.begin(); it != myAttrs.end(); ++it) {121os << " " << it->first;122os << "=\"" << it->second << "\"";123}124}125126std::vector<std::string>127SUMOSAXAttributesImpl_Cached::getAttributeNames() const {128std::vector<std::string> result;129for (std::map<std::string, std::string>::const_iterator it = myAttrs.begin(); it != myAttrs.end(); ++it) {130result.push_back(it->first);131}132return result;133}134135SUMOSAXAttributes*136SUMOSAXAttributesImpl_Cached::clone() const {137return new SUMOSAXAttributesImpl_Cached(myAttrs, myPredefinedTagsMML, getObjectType());138}139140141/****************************************************************************/142143144