GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GXmlPI.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GXmlPI.cpp - XML PI node class implementation *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2010-2021 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 GXmlPI.cpp
23  * @brief XML PI node 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 "GXmlPI.hpp"
33 #include "GTools.hpp"
34 
35 /* __ Method name definitions ____________________________________________ */
36 #define G_PARSE "GXmlPI::parse(std::string&)"
37 
38 /* __ Macros _____________________________________________________________ */
39 
40 /* __ Coding definitions _________________________________________________ */
41 
42 /* __ Debug definitions __________________________________________________ */
43 
44 
45 /*==========================================================================
46  = =
47  = Constructors/destructors =
48  = =
49  ==========================================================================*/
50 
51 /***********************************************************************//**
52  * @brief Void constructor
53  ***************************************************************************/
55 {
56  // Initialise private members for clean destruction
57  init_members();
58 
59  // Return
60  return;
61 }
62 
63 
64 /***********************************************************************//**
65  * @brief Copy constructor
66  *
67  * @param[in] node XML Processing Instruction.
68  ***************************************************************************/
69 GXmlPI::GXmlPI(const GXmlPI& node) : GXmlNode(node)
70 {
71  // Initialise private members for clean destruction
72  init_members();
73 
74  // Copy members
75  copy_members(node);
76 
77  // Return
78  return;
79 }
80 
81 
82 /***********************************************************************//**
83  * @brief Segment constructor
84  *
85  * @param[in] segment Text segment.
86  *
87  * Constructs a Processing Instruction from a text @p segment.
88  ***************************************************************************/
89 GXmlPI::GXmlPI(const std::string& segment) : GXmlNode()
90 {
91  // Initialise private members for clean destruction
92  init_members();
93 
94  // Parse segment
95  parse(segment);
96 
97  // Return
98  return;
99 }
100 
101 
102 /***********************************************************************//**
103  * @brief Destructor
104  ***************************************************************************/
106 {
107  // Free members
108  free_members();
109 
110  // Return
111  return;
112 }
113 
114 
115 /*==========================================================================
116  = =
117  = Operators =
118  = =
119  ==========================================================================*/
120 
121 /***********************************************************************//**
122  * @brief Assignment operator
123  *
124  * @param[in] node XML Processing Instruction.
125  * @return XML Processing Instruction.
126  ***************************************************************************/
128 {
129  // Execute only if object is not identical
130  if (this != &node) {
131 
132  // Copy base class members
133  this->GXmlNode::operator=(node);
134 
135  // Free members
136  free_members();
137 
138  // Initialise private members for clean destruction
139  init_members();
140 
141  // Copy members
142  copy_members(node);
143 
144  } // endif: object was not identical
145 
146  // Return
147  return *this;
148 }
149 
150 
151 /*==========================================================================
152  = =
153  = Public methods =
154  = =
155  ==========================================================================*/
156 
157  /***********************************************************************//**
158  * @brief Clear XML Processing Instruction
159  *
160  * Resets the XML Processing Instruction to an initial state.
161  ***************************************************************************/
162 void GXmlPI::clear(void)
163 {
164  // Free class members (base and derived classes, derived class first)
165  free_members();
166  this->GXmlNode::free_members();
167 
168  // Initialise members
169  this->GXmlNode::init_members();
170  init_members();
171 
172  // Return
173  return;
174 }
175 
176 
177 /***********************************************************************//**
178  * @brief Clone XML Processing Instruction
179  *
180  * @return Pointer to deep copy of XML Processing Instruction.
181  ***************************************************************************/
182 GXmlPI* GXmlPI::clone(void) const
183 {
184  // Clone XML PI
185  return new GXmlPI(*this);
186 }
187 
188 
189 /***********************************************************************//**
190  * @brief Write Processing Instruction into URL
191  *
192  * @param[in] url Unified Resource Locator.
193  * @param[in] indent Text indentation (default = 0).
194  *
195  * Writes a Processing Instruction into a @p url object.
196  ***************************************************************************/
197 void GXmlPI::write(GUrl& url, const int& indent) const
198 {
199  // Prepend indentation
200  for (int k = 0; k < indent; ++k) {
201  url.printf(" ");
202  }
203 
204  // Write Processing Instruction into URL
205  url.printf("<?%s?>\n", m_pi.c_str());
206 
207  // Return
208  return;
209 }
210 
211 
212 
213 /***********************************************************************//**
214  * @brief Print XML Processing Instruction
215  *
216  * @param[in] chatter Chattiness (defaults to NORMAL).
217  * @param[in] indent Text indentation (default to 0).
218  * @return String containing XML Processing Instruction.
219  ***************************************************************************/
220 std::string GXmlPI::print(const GChatter& chatter,
221  const int& indent) const
222 {
223  // Initialise result string
224  std::string result;
225 
226  // Continue only if chatter is not silent
227  if (chatter != SILENT) {
228 
229  // Initialise result string
230  result = gammalib::fill(" ", indent);
231 
232  // Append comment to string
233  result.append("GXmlPI::"+m_pi);
234 
235  } // endif: chatter was not silent
236 
237  // Return
238  return result;
239 }
240 
241 
242 /*==========================================================================
243  = =
244  = Private methods =
245  = =
246  ==========================================================================*/
247 
248 /***********************************************************************//**
249  * @brief Initialise class members
250  ***************************************************************************/
252 {
253  // Initialise members
254  m_pi.clear();
255 
256  // Return
257  return;
258 }
259 
260 
261 /***********************************************************************//**
262  * @brief Copy class members
263  *
264  * @param[in] node Processing Instruction.
265  ***************************************************************************/
266 void GXmlPI::copy_members(const GXmlPI& node)
267 {
268  // Copy attributes
269  m_pi = node.m_pi;
270 
271  // Return
272  return;
273 }
274 
275 
276 /***********************************************************************//**
277  * @brief Delete class members
278  ***************************************************************************/
280 {
281  // Return
282  return;
283 }
284 
285 
286 /***********************************************************************//**
287  * @brief Parse comment segment string
288  *
289  * @param[in] segment Segment string.
290  *
291  * @exception GException::invalid_value
292  * XML syntax error.
293  *
294  * Parse the segment string.
295  ***************************************************************************/
296 void GXmlPI::parse(const std::string& segment)
297 {
298  // Get length of segment
299  int n = segment.length();
300 
301  // Check on existence of brackets
302  if (n < 4 || (segment.compare(0,2,"<?") != 0) ||
303  (segment.compare(n-2,2,"?>") != 0)) {
304  std::string msg = "Missing or invalid Processing Instruction brackets "
305  "encountered in XML segment \""+segment+"\". Please "
306  "verify the XML format.";
308  }
309 
310  // Set comment
311  if (n > 4) {
312  m_pi = segment.substr(2, n-4);
313  }
314  else {
315  m_pi.clear();
316  }
317 
318  // Return
319  return;
320 }
Abstract XML node base class.
Definition: GXmlNode.hpp:57
GXmlNode & operator=(const GXmlNode &node)
Assignment operator.
Definition: GXmlNode.cpp:118
virtual void write(GUrl &url, const int &indent=0) const
Write Processing Instruction into URL.
Definition: GXmlPI.cpp:197
XML Processing Instruction node class.
Definition: GXmlPI.hpp:43
void init_members(void)
Initialise class members.
Definition: GXmlNode.cpp:880
void copy_members(const GXmlPI &node)
Copy class members.
Definition: GXmlPI.cpp:266
Gammalib tools definition.
XML PI node class interface definition.
GXmlPI & operator=(const GXmlPI &node)
Assignment operator.
Definition: GXmlPI.cpp:127
void free_members(void)
Delete class members.
Definition: GXmlNode.cpp:921
virtual ~GXmlPI(void)
Destructor.
Definition: GXmlPI.cpp:105
#define G_PARSE
Definition: GXmlPI.cpp:36
Abstract URL base class.
Definition: GUrl.hpp:44
void parse(const std::string &segment)
Parse comment segment string.
Definition: GXmlPI.cpp:296
GXmlPI(void)
Void constructor.
Definition: GXmlPI.cpp:54
virtual std::string print(const GChatter &chatter=NORMAL, const int &indent=0) const
Print XML Processing Instruction.
Definition: GXmlPI.cpp:220
virtual GXmlPI * clone(void) const
Clone XML Processing Instruction.
Definition: GXmlPI.cpp:182
virtual void clear(void)
Clear XML Processing Instruction.
Definition: GXmlPI.cpp:162
GChatter
Definition: GTypemaps.hpp:33
void init_members(void)
Initialise class members.
Definition: GXmlPI.cpp:251
virtual void printf(const char *format,...)=0
std::string m_pi
Processing instruction (without brackets)
Definition: GXmlPI.hpp:76
Exception handler interface definition.
void free_members(void)
Delete class members.
Definition: GXmlPI.cpp:279
std::string fill(const std::string &s, const int &n)
Fill string with n strings of same type.
Definition: GTools.cpp:1044