GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GXmlDocument.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GXmlDocument.cpp - XML document node class implementation *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2010-2018 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 GXmlDocument.cpp
23  * @brief XML document node class implementation
24  * @author Juergen Knoedlseder
25  */
26 
27 /* __ Includes ___________________________________________________________ */
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include "GXmlDocument.hpp"
32 #include "GTools.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 private members
55  init_members();
56 
57  // Return
58  return;
59 }
60 
61 
62 /***********************************************************************//**
63  * @brief Constructor
64  *
65  * @param[in] filename Filename.
66  * @param[in] version Version string.
67  * @param[in] encoding Encoding string.
68  * @param[in] standalone Standalone string.
69  ***************************************************************************/
71  const std::string& version,
72  const std::string& encoding,
73  const std::string& standalone) : GXmlNode()
74 {
75  // Initialise private members
76  init_members();
77 
78  // Set members
79  this->filename(filename);
80  this->version(version);
81  this->encoding(encoding);
82  this->standalone(standalone);
83 
84  // Return
85  return;
86 }
87 
88 
89 /***********************************************************************//**
90  * @brief Copy constructor
91  *
92  * @param[in] node XML document.
93  ***************************************************************************/
95 {
96  // Initialise private members for clean destruction
97  init_members();
98 
99  // Copy members
100  copy_members(node);
101 
102  // Return
103  return;
104 }
105 
106 
107 /***********************************************************************//**
108  * @brief Destructor
109  ***************************************************************************/
111 {
112  // Free members
113  free_members();
114 
115  // Return
116  return;
117 }
118 
119 
120 /*==========================================================================
121  = =
122  = Operators =
123  = =
124  ==========================================================================*/
125 
126 /***********************************************************************//**
127  * @brief Assignment operator
128  *
129  * @param[in] node XML document.
130  * @return XML document.
131  ***************************************************************************/
133 {
134  // Execute only if object is not identical
135  if (this != &node) {
136 
137  // Copy base class members
138  this->GXmlNode::operator=(node);
139 
140  // Free members
141  free_members();
142 
143  // Initialise private members for clean destruction
144  init_members();
145 
146  // Copy members
147  copy_members(node);
148 
149  } // endif: object was not identical
150 
151  // Return
152  return *this;
153 }
154 
155 
156 /*==========================================================================
157  = =
158  = Public methods =
159  = =
160  ==========================================================================*/
161 
162  /***********************************************************************//**
163  * @brief Clear XML document
164  *
165  * Resets the XML document to a clean initial state.
166  ***************************************************************************/
168 {
169  // Free class members (base and derived classes, derived class first)
170  free_members();
171  this->GXmlNode::free_members();
172 
173  // Initialise members
174  this->GXmlNode::init_members();
175  init_members();
176 
177  // Return
178  return;
179 }
180 
181 
182 /***********************************************************************//**
183  * @brief Clone XML document
184  *
185  * @return Pointer to deep copy of XML document.
186  ***************************************************************************/
188 {
189  // Clone document
190  return new GXmlDocument(*this);
191 }
192 
193 
194 /***********************************************************************//**
195  * @brief Write XML document into URL
196  *
197  * @param[in] url Unified Resource Locator.
198  * @param[in] indent Text indentation (default = 0).
199  *
200  * Writes the XML document into a @p url object.
201  ***************************************************************************/
202 void GXmlDocument::write(GUrl& url, const int& indent) const
203 {
204  // Write document header into URL
205  url.printf("<?xml");
206  m_version.write(url);
207  m_encoding.write(url);
208  m_standalone.write(url);
209  url.printf("?>\n");
210 
211  // Write children into URL
212  for (int i = 0; i < m_nodes.size(); ++i) {
213  m_nodes[i]->write(url, indent);
214  }
215 
216  // Return
217  return;
218 }
219 
220 
221 /***********************************************************************//**
222  * @brief Print XML document
223  *
224  * @param[in] chatter Chattiness (defaults to NORMAL).
225  * @param[in] indent Text indentation (default to 0).
226  * @return String containing XML document.
227  ***************************************************************************/
228 std::string GXmlDocument::print(const GChatter& chatter,
229  const int& indent) const
230 {
231  // Initialise result string
232  std::string result;
233 
234  // Continue only if chatter is not silent
235  if (chatter != SILENT) {
236 
237  // Initialise result string
238  result = gammalib::fill(" ", indent);
239 
240  // Append document to string
241  result.append("GXmlDocument::");
242  result.append("version=\"" + version() +"\"");
243  result.append(" encoding=\"" + encoding() +"\"");
244  result.append(" standalone=\"" + standalone() +"\"");
245 
246  // Append children
247  for (int i = 0; i < m_nodes.size(); ++i) {
248  result.append("\n" + m_nodes[i]->print(chatter, indent));
249  }
250 
251  } // endif: chatter was not silent
252 
253  // Return
254  return result;
255 }
256 
257 
258 /*==========================================================================
259  = =
260  = Private methods =
261  = =
262  ==========================================================================*/
263 
264 /***********************************************************************//**
265  * @brief Initialise class members
266  ***************************************************************************/
268 {
269  // Initialise members
270  m_filename.clear();
271  m_version.clear();
272  m_encoding.clear();
274  m_version.name("version");
275  m_encoding.name("encoding");
276  m_standalone.name("standalone");
277  m_version.value("1.0");
278  m_encoding.value("UTF-8");
279  m_standalone.value("no");
280 
281  // Return
282  return;
283 }
284 
285 
286 /***********************************************************************//**
287  * @brief Copy class members
288  *
289  * @param[in] node Object from which members which should be copied.
290  ***************************************************************************/
292 {
293  // Copy attributes
294  m_filename = node.m_filename;
295  m_version = node.m_version;
296  m_encoding = node.m_encoding;
297  m_standalone = node.m_standalone;
298 
299  // Return
300  return;
301 }
302 
303 
304 /***********************************************************************//**
305  * @brief Delete class members
306  ***************************************************************************/
308 {
309  // Return
310  return;
311 }
Abstract XML node base class.
Definition: GXmlNode.hpp:57
GXmlNode & operator=(const GXmlNode &node)
Assignment operator.
Definition: GXmlNode.cpp:118
std::vector< GXmlNode * > m_nodes
Pointer to child nodes.
Definition: GXmlNode.hpp:123
std::string standalone(void) const
Return standalone.
void init_members(void)
Initialise class members.
Definition: GXmlNode.cpp:948
Gammalib tools definition.
virtual void write(GUrl &url, const int &indent=0) const
Write XML document into URL.
virtual ~GXmlDocument(void)
Destructor.
void free_members(void)
Delete class members.
Definition: GXmlNode.cpp:989
void clear(void)
Clear element attribute.
void write(GUrl &url) const
Write attribute into URL.
GXmlAttribute m_version
XML version (&quot;1.0&quot;, &quot;1.1&quot;)
Abstract URL base class.
Definition: GUrl.hpp:44
Filename class.
Definition: GFilename.hpp:62
const std::string & name(void) const
Return attribute name.
std::string version(void) const
Return version.
void copy_members(const GXmlDocument &node)
Copy class members.
virtual GXmlDocument * clone(void) const
Clone XML document.
GChatter
Definition: GTypemaps.hpp:33
XML document node class interface definition.
virtual void printf(const char *format,...)=0
XML document node class.
GFilename m_filename
Name of XML file.
GXmlDocument(void)
Void constructor.
void clear(void)
Clear file name.
Definition: GFilename.cpp:188
GXmlDocument & operator=(const GXmlDocument &node)
Assignment operator.
const std::string & value(void) const
Return attribute value.
GXmlAttribute m_standalone
Standalone (&quot;yes&quot;, &quot;no&quot;)
std::string encoding(void) const
Return encoding.
const GFilename & filename(void) const
Return filename.
GXmlAttribute m_encoding
Encoding (e.g. &quot;UTF-8&quot;)
void free_members(void)
Delete class members.
std::string fill(const std::string &s, const int &n)
Fill string with n strings of same type.
Definition: GTools.cpp:923
virtual std::string print(const GChatter &chatter=NORMAL, const int &indent=0) const
Print XML document.
virtual void clear(void)
Clear XML document.
void init_members(void)
Initialise class members.