GammaLib  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GXmlAttribute.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GXmlAttribute.cpp - XML attribute class implementation *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2010-2016 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 "GException.hpp"
32 #include "GTools.hpp"
33 #include "GXmlAttribute.hpp"
34 
35 /* __ Method name definitions ____________________________________________ */
36 
37 /* __ Macros _____________________________________________________________ */
38 
39 /* __ Coding definitions _________________________________________________ */
40 
41 /* __ Debug definitions __________________________________________________ */
42 
43 
44 /*==========================================================================
45  = =
46  = Constructors/destructors =
47  = =
48  ==========================================================================*/
49 
50 /***********************************************************************//**
51  * @brief Void constructor
52  ***************************************************************************/
54 {
55  // Initialise members
56  init_members();
57 
58  // Return
59  return;
60 }
61 
62 
63 /***********************************************************************//**
64  * @brief Copy constructor
65  *
66  * @param[in] attr Element attribute.
67  ***************************************************************************/
69 {
70  // Initialise members
71  init_members();
72 
73  // Copy members
74  copy_members(attr);
75 
76  // Return
77  return;
78 }
79 
80 
81 /***********************************************************************//**
82  * @brief Name-Value pair constructor
83  *
84  * @param[in] name Attribute name.
85  * @param[in] value Attribute value.
86  *
87  * Construct attribute form a @p name and a @p value. Predefined entities
88  * (e.g. &quot;) in attribute values are automatically converted into normal
89  * characters. The constructor strips any existing " or ' leading and
90  * trailing hyphens from the @p value string.
91  ***************************************************************************/
92 GXmlAttribute::GXmlAttribute(const std::string& name, const std::string& value)
93 {
94  // Initialise members
95  init_members();
96 
97  // Create working copy of attribute value
98  std::string v(value);
99 
100  // Strip any pair of leading and trailing hyphens
101  int n = v.length();
102  if (n >= 2) {
103  if (((v[0] == '"') && (v[n-1] == '"')) ||
104  ((v[0] == '\'') && (v[n-1] == '\''))) {
105  if (n > 2) {
106  v = v.substr(1, n-2);
107  }
108  else {
109  v = "";
110  }
111  }
112  }
113 
114  // Set attribute
115  this->name(name);
116  this->value(gammalib::xml2str(v));
117 
118  // Return
119  return;
120 }
121 
122 
123 /***********************************************************************//**
124  * @brief Destructor
125  ***************************************************************************/
127 {
128  // Free members
129  free_members();
130 
131  // Return
132  return;
133 }
134 
135 
136 /*==========================================================================
137  = =
138  = Operators =
139  = =
140  ==========================================================================*/
141 
142 /***********************************************************************//**
143  * @brief Assignment operator
144  *
145  * @param[in] attr Element attribute.
146  * @return Element attribute.
147  ***************************************************************************/
149 {
150  // Execute only if object is not identical
151  if (this != &attr) {
152 
153  // Free members
154  free_members();
155 
156  // Initialise members
157  init_members();
158 
159  // Copy members
160  copy_members(attr);
161 
162  } // endif: object was not identical
163 
164  // Return
165  return *this;
166 }
167 
168 
169 /*==========================================================================
170  = =
171  = Public methods =
172  = =
173  ==========================================================================*/
174 
175  /***********************************************************************//**
176  * @brief Clear element attribute
177  *
178  * Resets element attribute to a clean initial state.
179  ***************************************************************************/
181 {
182  // Free members
183  free_members();
184 
185  // Initialise members
186  init_members();
187 
188  // Return
189  return;
190 }
191 
192 
193 /***********************************************************************//**
194  * @brief Clone element attribute
195  *
196  * @return Pointer to deep copy of an element attribute.
197  ***************************************************************************/
199 {
200  // Clone XML attribute
201  return new GXmlAttribute(*this);
202 }
203 
204 
205 /***********************************************************************//**
206  * @brief Write attribute into URL
207  *
208  * @param[in] url Unified Resource Locator.
209  *
210  * Writes the element attribute into the @p url. Special characters are
211  * automatically transformed into predefined entities (e.g. &quot;).
212  ***************************************************************************/
213 void GXmlAttribute::write(GUrl& url) const
214 {
215  // Convert value into XML format and add hyphens
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 
221  // Return
222  return;
223 }
224 
225 
226 /***********************************************************************//**
227  * @brief Print element attribute
228  *
229  * @param[in] chatter Chattiness.
230  * @return String containing element attribute.
231  ***************************************************************************/
232 std::string GXmlAttribute::print(const GChatter& chatter) const
233 {
234  // Initialise result string
235  std::string result;
236 
237  // Continue only if chatter is not silent
238  if (chatter != SILENT) {
239 
240  // Append attribute to string
241  result.append(" "+m_name+"=\""+m_value+"\"");
242 
243  } // endif: chatter was not silent
244 
245  // Return
246  return result;
247 }
248 
249 
250 /*==========================================================================
251  = =
252  = Private methods =
253  = =
254  ==========================================================================*/
255 
256 /***********************************************************************//**
257  * @brief Initialise class members
258  ***************************************************************************/
260 {
261  // Initialise members
262  m_name.clear();
263  m_value.clear();
264 
265  // Return
266  return;
267 }
268 
269 
270 /***********************************************************************//**
271  * @brief Copy class members
272  *
273  * @param[in] attr Element attribute.
274  ***************************************************************************/
276 {
277  // Copy attributes
278  m_name = attr.m_name;
279  m_value = attr.m_value;
280 
281  // Return
282  return;
283 }
284 
285 
286 /***********************************************************************//**
287  * @brief Delete class members
288  ***************************************************************************/
290 {
291  // Return
292  return;
293 }
void copy_members(const GXmlAttribute &attr)
Copy class members.
XML attribute class.
Gammalib tools definition.
std::string m_value
Attribute value.
void clear(void)
Clear element attribute.
void write(GUrl &url) const
Write attribute into URL.
Abstract URL base class.
Definition: GUrl.hpp:44
virtual ~GXmlAttribute(void)
Destructor.
const std::string & name(void) const
Return attribute name.
std::string print(const GChatter &chatter=NORMAL) const
Print element attribute.
GChatter
Definition: GTypemaps.hpp:33
GXmlAttribute * clone(void) const
Clone element attribute.
GXmlAttribute & operator=(const GXmlAttribute &attr)
Assignment operator.
virtual void printf(const char *format,...)=0
std::string xml2str(const std::string &arg)
Convert XML character references in string to characters.
Definition: GTools.cpp:1416
std::string m_name
Attribute name.
GXmlAttribute(void)
Void constructor.
const std::string & value(void) const
Return attribute value.
void free_members(void)
Delete class members.
std::string str2xml(const std::string &arg)
Convert special characters in string to XML character references.
Definition: GTools.cpp:1549
void init_members(void)
Initialise class members.
XML attribute class interface definition.