Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/xml/SUMOSAXAttributesImpl_Xerces.h
169678 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2007-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.h
15
/// @author Daniel Krajzewicz
16
/// @author Jakob Erdmann
17
/// @author Michael Behrisch
18
/// @date Fri, 30 Mar 2007
19
///
20
// Encapsulated Xerces-SAX-attributes
21
/****************************************************************************/
22
#pragma once
23
#include <config.h>
24
25
#include <string>
26
#include <map>
27
#include <iostream>
28
#include <xercesc/sax2/Attributes.hpp>
29
#include <utils/common/SUMOTime.h>
30
#include <utils/common/StringUtils.h>
31
#include "SUMOSAXAttributes.h"
32
33
34
// ===========================================================================
35
// class definitions
36
// ===========================================================================
37
/**
38
* @class SUMOSAXAttributesImpl_Xerces
39
* @brief Encapsulated Xerces-SAX-attributes
40
*
41
* @see SUMOSAXAttributes
42
*/
43
class SUMOSAXAttributesImpl_Xerces : public SUMOSAXAttributes {
44
public:
45
/** @brief Constructor
46
*
47
* @param[in] attrs The encapsulated xerces-attributes
48
* @param[in] predefinedTags Map of attribute ids to their xerces-representation
49
* @param[in] predefinedTagsMML Map of attribute ids to their (readable) string-representation
50
*/
51
SUMOSAXAttributesImpl_Xerces(const XERCES_CPP_NAMESPACE::Attributes& attrs,
52
const std::vector<XMLCh*>& predefinedTags,
53
const std::vector<std::string>& predefinedTagsMML,
54
const std::string& objectType);
55
56
57
/// @brief Destructor
58
virtual ~SUMOSAXAttributesImpl_Xerces();
59
60
61
62
63
/// @name methods for retrieving attribute values
64
/// @{
65
66
/** @brief Returns the information whether the named (by its enum-value) attribute is within the current list
67
*
68
* @param[in] id The id of the searched attribute
69
* @return Whether the attribute is within the attributes
70
*/
71
bool hasAttribute(int id) const;
72
73
74
/**
75
* @brief Returns the string-value of the named (by its enum-value) attribute
76
*
77
* Tries to retrieve the attribute from the attribute list. The retrieved
78
* attribute (which may be 0) is then parsed using TplConvert<XMLCh>::_2str.
79
* If the attribute is ==0, TplConvert<XMLCh>::_2str throws an
80
* EmptyData-exception which is passed.
81
*
82
* @param[in] id The id of the attribute to return the value of
83
* @return The attribute's value as a string, if it could be read and parsed
84
* @exception EmptyData If the attribute is not known or the attribute value is an empty string
85
*/
86
std::string getString(int id, bool* isPresent = nullptr) const;
87
88
/**
89
* @brief Returns the string-value of the named (by its enum-value) attribute
90
*
91
* Tries to retrieve the attribute from the attribute list. The retrieved
92
* attribute (which may be 0) is then parsed using TplConvert<XMLCh>::_2strSec.
93
* If the attribute is ==0, TplConvert<XMLCh>::_2strSec returns the default value.
94
*
95
* @param[in] id The id of the attribute to return the value of
96
* @param[in] def The default value to return if the attribute is not in attributes
97
* @return The attribute's value as a string, if it could be read and parsed
98
* @exception EmptyData If the attribute is not known or the attribute value is an empty string
99
*/
100
std::string getStringSecure(int id, const std::string& def) const;
101
102
/**
103
* @brief Returns the information whether the named attribute is within the current list
104
*/
105
bool hasAttribute(const std::string& id) const;
106
107
/**
108
* @brief Returns the double-value of the named attribute
109
*
110
* Tries to retrieve the attribute from the attribute list. The retrieved
111
* attribute (which may be 0) is then parsed using TplConvert<XMLCh>::_2double.
112
* If the attribute is empty or ==0, TplConvert<XMLCh>::_2double throws an
113
* EmptyData-exception which is passed.
114
* If the value can not be parsed to a double, TplConvert<XMLCh>::_2double throws a
115
* NumberFormatException-exception which is passed.
116
*
117
* @param[in] id The name of the attribute to return the value of
118
* @return The attribute's value as a float, if it could be read and parsed
119
* @exception EmptyData If the attribute is not known or the attribute value is an empty string
120
* @exception NumberFormatException If the attribute value can not be parsed to an double
121
*/
122
double getFloat(const std::string& id) const;
123
124
/**
125
* @brief Returns the string-value of the named (by its enum-value) attribute
126
*
127
* Tries to retrieve the attribute from the attribute list.
128
* If the attribute is ==0, TplConvert<XMLCh>::_2strSec returns the default value.
129
* @param[in] id The name of the attribute to return the value of
130
* @param[in] def The default value to return if the attribute is not in attributes
131
* @return The attribute's value as a string, if it could be read and parsed
132
*/
133
std::string getStringSecure(const std::string& id,
134
const std::string& def) const;
135
//}
136
137
/** @brief Converts the given attribute id into a man readable string
138
*
139
* Returns a "?" if the attribute is not known.
140
*
141
* @param[in] attr The id of the attribute to return the name of
142
* @return The name of the described attribute
143
*/
144
std::string getName(int attr) const;
145
146
147
/** @brief Prints all attribute names and values into the given stream
148
*
149
* @param[in] os The stream to use
150
*/
151
void serialize(std::ostream& os) const;
152
153
/** @brief Retrieves all attribute names
154
*/
155
std::vector<std::string> getAttributeNames() const;
156
157
/// @brief return a new deep-copy attributes object
158
SUMOSAXAttributes* clone() const;
159
160
private:
161
/** @brief Returns Xerces-value of the named attribute
162
*
163
* It is assumed that this attribute is within the stored attributes.
164
* @param[in] id The id of the attribute to retrieve the vale of
165
* @return The xerces-value of the attribute
166
*/
167
const XMLCh* getAttributeValueSecure(int id) const;
168
169
170
private:
171
/// @brief The encapsulated attributes
172
const XERCES_CPP_NAMESPACE::Attributes& myAttrs;
173
174
/// @brief Definition of a map of attribute ids to their xerces-representation
175
typedef std::vector<XMLCh*> AttrMap;
176
/// @brief Map of attribute ids to their xerces-representation
177
const AttrMap& myPredefinedTags;
178
179
/// @brief Map of attribute ids to their (readable) string-representation
180
const std::vector<std::string>& myPredefinedTagsMML;
181
182
private:
183
/// @brief Invalidated copy constructor.
184
SUMOSAXAttributesImpl_Xerces(const SUMOSAXAttributesImpl_Xerces& src);
185
186
/// @brief Invalidated assignment operator.
187
SUMOSAXAttributesImpl_Xerces& operator=(const SUMOSAXAttributesImpl_Xerces& src);
188
189
190
};
191
192