GammaLib 2.2.0.dev
Loading...
Searching...
No Matches
GXmlAttribute.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * GXmlAttribute.cpp - XML attribute class implementation *
3 * ----------------------------------------------------------------------- *
4 * copyright (C) 2010-2026 by Juergen Knoedlseder *
5 * ----------------------------------------------------------------------- *
6 * *
7 * This program is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19 * *
20 ***************************************************************************/
21/**
22 * @file GXmlAttribute.cpp
23 * @brief XML attribute class implementation
24 * @author Juergen Knoedlseder
25 */
26
27/* __ Includes ___________________________________________________________ */
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include "GTools.hpp"
32#include "GXmlAttribute.hpp"
33
34/* __ Method name definitions ____________________________________________ */
35
36/* __ Macros _____________________________________________________________ */
37
38/* __ Coding definitions _________________________________________________ */
39
40/* __ Debug definitions __________________________________________________ */
41
42
43/*==========================================================================
44 = =
45 = Constructors/destructors =
46 = =
47 ==========================================================================*/
48
49/***********************************************************************//**
50 * @brief Void constructor
51 ***************************************************************************/
53{
54 // Initialise members
56
57 // Return
58 return;
59}
60
61
62/***********************************************************************//**
63 * @brief Copy constructor
64 *
65 * @param[in] attr Element attribute.
66 ***************************************************************************/
68{
69 // Initialise members
71
72 // Copy members
73 copy_members(attr);
74
75 // Return
76 return;
77}
78
79
80/***********************************************************************//**
81 * @brief Name-Value pair constructor
82 *
83 * @param[in] name Attribute name.
84 * @param[in] value Attribute value.
85 *
86 * Construct attribute form a @p name and a @p value. Predefined entities
87 * (e.g. &quot;) in attribute values are automatically converted into normal
88 * characters. The constructor strips any existing " or ' leading and
89 * trailing hyphens from the @p value string.
90 ***************************************************************************/
91GXmlAttribute::GXmlAttribute(const std::string& name, const std::string& value)
92{
93 // Initialise members
95
96 // Create working copy of attribute value
97 std::string v(value);
98
99 // Strip any pair of leading and trailing hyphens
100 int n = v.length();
101 if (n >= 2) {
102 if (((v[0] == '"') && (v[n-1] == '"')) ||
103 ((v[0] == '\'') && (v[n-1] == '\''))) {
104 if (n > 2) {
105 v = v.substr(1, n-2);
106 }
107 else {
108 v = "";
109 }
110 }
111 }
112
113 // Set attribute
114 this->name(name);
115 this->value(gammalib::xml2str(v));
116
117 // Return
118 return;
119}
120
121
122/***********************************************************************//**
123 * @brief Destructor
124 ***************************************************************************/
126{
127 // Free members
128 free_members();
129
130 // Return
131 return;
132}
133
134
135/*==========================================================================
136 = =
137 = Operators =
138 = =
139 ==========================================================================*/
140
141/***********************************************************************//**
142 * @brief Assignment operator
143 *
144 * @param[in] attr Element attribute.
145 * @return Element attribute.
146 ***************************************************************************/
148{
149 // Execute only if object is not identical
150 if (this != &attr) {
151
152 // Free members
153 free_members();
154
155 // Initialise members
156 init_members();
157
158 // Copy members
159 copy_members(attr);
160
161 } // endif: object was not identical
162
163 // Return
164 return *this;
165}
166
167
168/*==========================================================================
169 = =
170 = Public methods =
171 = =
172 ==========================================================================*/
173
174 /***********************************************************************//**
175 * @brief Clear element attribute
176 *
177 * Resets element attribute to a clean initial state.
178 ***************************************************************************/
180{
181 // Free members
182 free_members();
183
184 // Initialise members
185 init_members();
186
187 // Return
188 return;
189}
190
191
192/***********************************************************************//**
193 * @brief Clone element attribute
194 *
195 * @return Pointer to deep copy of an element attribute.
196 ***************************************************************************/
198{
199 // Clone XML attribute
200 return new GXmlAttribute(*this);
201}
202
203
204/***********************************************************************//**
205 * @brief Write attribute into URL
206 *
207 * @param[in] url Unified Resource Locator.
208 *
209 * Writes the element attribute into the @p url. Special characters are
210 * automatically transformed into predefined entities (e.g. &quot;).
211 ***************************************************************************/
213{
214 // Convert value into XML format
215 //std::string value = "\""+gammalib::str2xml(m_value)+"\"";
216 std::string value = gammalib::str2xml(m_value);
217
218 // Write attribute into URL
219 //url.printf(" %s=%s", m_name.c_str(), value.c_str());
220 // The above code led to a missing " in case that the element finishes
221 // with a > after that attribute. This is probably related to a bug (or
222 // a feature) in the underlying std::vfprintf code.
223
224 // Write attribute into URL
225 url.printf(" ");
226 url.printf(m_name.c_str());
227 url.printf("=\"");
228 url.printf(value.c_str());
229 url.printf("\"");
230
231 // Return
232 return;
233}
234
235
236/***********************************************************************//**
237 * @brief Print element attribute
238 *
239 * @param[in] chatter Chattiness.
240 * @return String containing element attribute.
241 ***************************************************************************/
242std::string GXmlAttribute::print(const GChatter& chatter) const
243{
244 // Initialise result string
245 std::string result;
246
247 // Continue only if chatter is not silent
248 if (chatter != SILENT) {
249
250 // Append attribute to string
251 result.append(" "+m_name+"=\""+m_value+"\"");
252
253 } // endif: chatter was not silent
254
255 // Return
256 return result;
257}
258
259
260/*==========================================================================
261 = =
262 = Private methods =
263 = =
264 ==========================================================================*/
265
266/***********************************************************************//**
267 * @brief Initialise class members
268 ***************************************************************************/
270{
271 // Initialise members
272 m_name.clear();
273 m_value.clear();
274
275 // Return
276 return;
277}
278
279
280/***********************************************************************//**
281 * @brief Copy class members
282 *
283 * @param[in] attr Element attribute.
284 ***************************************************************************/
286{
287 // Copy attributes
288 m_name = attr.m_name;
289 m_value = attr.m_value;
290
291 // Return
292 return;
293}
294
295
296/***********************************************************************//**
297 * @brief Delete class members
298 ***************************************************************************/
300{
301 // Return
302 return;
303}
Gammalib tools definition.
GChatter
Definition GTypemaps.hpp:33
@ SILENT
Definition GTypemaps.hpp:34
XML attribute class interface definition.
Abstract URL base class.
Definition GUrl.hpp:44
virtual void printf(const char *format,...)=0
XML attribute class.
void init_members(void)
Initialise class members.
GXmlAttribute & operator=(const GXmlAttribute &attr)
Assignment operator.
virtual ~GXmlAttribute(void)
Destructor.
std::string m_name
Attribute name.
void copy_members(const GXmlAttribute &attr)
Copy class members.
void free_members(void)
Delete class members.
std::string print(const GChatter &chatter=NORMAL) const
Print element attribute.
std::string m_value
Attribute value.
const std::string & value(void) const
Return attribute value.
GXmlAttribute(void)
Void constructor.
void write(GUrl &url) const
Write attribute into URL.
GXmlAttribute * clone(void) const
Clone element attribute.
const std::string & name(void) const
Return attribute name.
void clear(void)
Clear element attribute.
std::string xml2str(const std::string &arg)
Convert XML character references in string to characters.
Definition GTools.cpp:1449
std::string str2xml(const std::string &arg)
Convert special characters in string to XML character references.
Definition GTools.cpp:1600