Path: blob/main/src/utils/xml/SUMOSAXAttributesImpl_Xerces.h
169678 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2007-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.h14/// @author Daniel Krajzewicz15/// @author Jakob Erdmann16/// @author Michael Behrisch17/// @date Fri, 30 Mar 200718///19// Encapsulated Xerces-SAX-attributes20/****************************************************************************/21#pragma once22#include <config.h>2324#include <string>25#include <map>26#include <iostream>27#include <xercesc/sax2/Attributes.hpp>28#include <utils/common/SUMOTime.h>29#include <utils/common/StringUtils.h>30#include "SUMOSAXAttributes.h"313233// ===========================================================================34// class definitions35// ===========================================================================36/**37* @class SUMOSAXAttributesImpl_Xerces38* @brief Encapsulated Xerces-SAX-attributes39*40* @see SUMOSAXAttributes41*/42class SUMOSAXAttributesImpl_Xerces : public SUMOSAXAttributes {43public:44/** @brief Constructor45*46* @param[in] attrs The encapsulated xerces-attributes47* @param[in] predefinedTags Map of attribute ids to their xerces-representation48* @param[in] predefinedTagsMML Map of attribute ids to their (readable) string-representation49*/50SUMOSAXAttributesImpl_Xerces(const XERCES_CPP_NAMESPACE::Attributes& attrs,51const std::vector<XMLCh*>& predefinedTags,52const std::vector<std::string>& predefinedTagsMML,53const std::string& objectType);545556/// @brief Destructor57virtual ~SUMOSAXAttributesImpl_Xerces();5859606162/// @name methods for retrieving attribute values63/// @{6465/** @brief Returns the information whether the named (by its enum-value) attribute is within the current list66*67* @param[in] id The id of the searched attribute68* @return Whether the attribute is within the attributes69*/70bool hasAttribute(int id) const;717273/**74* @brief Returns the string-value of the named (by its enum-value) attribute75*76* Tries to retrieve the attribute from the attribute list. The retrieved77* attribute (which may be 0) is then parsed using TplConvert<XMLCh>::_2str.78* If the attribute is ==0, TplConvert<XMLCh>::_2str throws an79* EmptyData-exception which is passed.80*81* @param[in] id The id of the attribute to return the value of82* @return The attribute's value as a string, if it could be read and parsed83* @exception EmptyData If the attribute is not known or the attribute value is an empty string84*/85std::string getString(int id, bool* isPresent = nullptr) const;8687/**88* @brief Returns the string-value of the named (by its enum-value) attribute89*90* Tries to retrieve the attribute from the attribute list. The retrieved91* attribute (which may be 0) is then parsed using TplConvert<XMLCh>::_2strSec.92* If the attribute is ==0, TplConvert<XMLCh>::_2strSec returns the default value.93*94* @param[in] id The id of the attribute to return the value of95* @param[in] def The default value to return if the attribute is not in attributes96* @return The attribute's value as a string, if it could be read and parsed97* @exception EmptyData If the attribute is not known or the attribute value is an empty string98*/99std::string getStringSecure(int id, const std::string& def) const;100101/**102* @brief Returns the information whether the named attribute is within the current list103*/104bool hasAttribute(const std::string& id) const;105106/**107* @brief Returns the double-value of the named attribute108*109* Tries to retrieve the attribute from the attribute list. The retrieved110* attribute (which may be 0) is then parsed using TplConvert<XMLCh>::_2double.111* If the attribute is empty or ==0, TplConvert<XMLCh>::_2double throws an112* EmptyData-exception which is passed.113* If the value can not be parsed to a double, TplConvert<XMLCh>::_2double throws a114* NumberFormatException-exception which is passed.115*116* @param[in] id The name of the attribute to return the value of117* @return The attribute's value as a float, if it could be read and parsed118* @exception EmptyData If the attribute is not known or the attribute value is an empty string119* @exception NumberFormatException If the attribute value can not be parsed to an double120*/121double getFloat(const std::string& id) const;122123/**124* @brief Returns the string-value of the named (by its enum-value) attribute125*126* Tries to retrieve the attribute from the attribute list.127* If the attribute is ==0, TplConvert<XMLCh>::_2strSec returns the default value.128* @param[in] id The name of the attribute to return the value of129* @param[in] def The default value to return if the attribute is not in attributes130* @return The attribute's value as a string, if it could be read and parsed131*/132std::string getStringSecure(const std::string& id,133const std::string& def) const;134//}135136/** @brief Converts the given attribute id into a man readable string137*138* Returns a "?" if the attribute is not known.139*140* @param[in] attr The id of the attribute to return the name of141* @return The name of the described attribute142*/143std::string getName(int attr) const;144145146/** @brief Prints all attribute names and values into the given stream147*148* @param[in] os The stream to use149*/150void serialize(std::ostream& os) const;151152/** @brief Retrieves all attribute names153*/154std::vector<std::string> getAttributeNames() const;155156/// @brief return a new deep-copy attributes object157SUMOSAXAttributes* clone() const;158159private:160/** @brief Returns Xerces-value of the named attribute161*162* It is assumed that this attribute is within the stored attributes.163* @param[in] id The id of the attribute to retrieve the vale of164* @return The xerces-value of the attribute165*/166const XMLCh* getAttributeValueSecure(int id) const;167168169private:170/// @brief The encapsulated attributes171const XERCES_CPP_NAMESPACE::Attributes& myAttrs;172173/// @brief Definition of a map of attribute ids to their xerces-representation174typedef std::vector<XMLCh*> AttrMap;175/// @brief Map of attribute ids to their xerces-representation176const AttrMap& myPredefinedTags;177178/// @brief Map of attribute ids to their (readable) string-representation179const std::vector<std::string>& myPredefinedTagsMML;180181private:182/// @brief Invalidated copy constructor.183SUMOSAXAttributesImpl_Xerces(const SUMOSAXAttributesImpl_Xerces& src);184185/// @brief Invalidated assignment operator.186SUMOSAXAttributesImpl_Xerces& operator=(const SUMOSAXAttributesImpl_Xerces& src);187188189};190191192