GammaLib  2.1.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GXmlComment.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GXmlComment.cpp - XML comment 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 GXmlComment.cpp
23  * @brief XML comment 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 "GXmlComment.hpp"
33 #include "GTools.hpp"
34 
35 /* __ Method name definitions ____________________________________________ */
36 #define G_PARSE "GXmlComment::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 members
57  init_members();
58 
59  // Return
60  return;
61 }
62 
63 
64 /***********************************************************************//**
65  * @brief Copy constructor
66  *
67  * @param[in] node XML comment.
68  ***************************************************************************/
70 {
71  // Initialise members
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 segement.
86  *
87  * Constructs a comment from the text given in @p segment.
88  ***************************************************************************/
89 GXmlComment::GXmlComment(const std::string& segment) : GXmlNode()
90 {
91  // Initialise members
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 comment.
125  * @return XML comment.
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 comment
159  *
160  * Resets the XML comment to an clean initial state.
161  ***************************************************************************/
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 comment
179  *
180  * @return Pointer to deep copy of XML comment.
181  ***************************************************************************/
183 {
184  // Clone comment
185  return new GXmlComment(*this);
186 }
187 
188 
189 /***********************************************************************//**
190  * @brief Write comment into URL
191  *
192  * @param[in] url Unified Resource Locator.
193  * @param[in] indent Text indentation (default = 0).
194  *
195  * Writes the XML comment into a @p url object.
196  ***************************************************************************/
197 void GXmlComment::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 comment into file
205  url.printf("<!--%s-->\n", m_comment.c_str());
206 
207  // Return
208  return;
209 }
210 
211 
212 /***********************************************************************//**
213  * @brief Print XML comment
214  *
215  * @param[in] chatter Chattiness (defaults to NORMAL).
216  * @param[in] indent Text indentation (default to 0).
217  * @return String containing XML comment
218  ***************************************************************************/
219 std::string GXmlComment::print(const GChatter& chatter,
220  const int& indent) const
221 {
222  // Initialise result string
223  std::string result;
224 
225  // Continue only if chatter is not silent
226  if (chatter != SILENT) {
227 
228  // Initialise result string
229  result = gammalib::fill(" ", indent);
230 
231  // Append comment to string
232  result.append("GXmlComment::"+m_comment);
233 
234  } // endif: chatter was not silent
235 
236  // Return
237  return result;
238 }
239 
240 
241 /*==========================================================================
242  = =
243  = Private methods =
244  = =
245  ==========================================================================*/
246 
247 /***********************************************************************//**
248  * @brief Initialise class members
249  ***************************************************************************/
251 {
252  // Initialise members
253  m_comment.clear();
254 
255  // Return
256  return;
257 }
258 
259 
260 /***********************************************************************//**
261  * @brief Copy class members
262  *
263  * @param[in] node XML comment.
264  ***************************************************************************/
266 {
267  // Copy members
268  m_comment = node.m_comment;
269 
270  // Return
271  return;
272 }
273 
274 
275 /***********************************************************************//**
276  * @brief Delete class members
277  ***************************************************************************/
279 {
280  // Return
281  return;
282 }
283 
284 
285 /***********************************************************************//**
286  * @brief Parse comment segment string
287  *
288  * @param[in] segment Segment string.
289  *
290  * @exception GException::invalid_value
291  * XML syntax error.
292  *
293  * Parse the segment string and extract the comment.
294  *
295  * @todo Check validity of characters in comment string
296  ***************************************************************************/
297 void GXmlComment::parse(const std::string& segment)
298 {
299  // Initialise comment string
300  m_comment.clear();
301 
302  // Get length of segment
303  int n = segment.length();
304 
305  // Do nothing if string is empty
306  if (n > 0) {
307 
308  // If string starts with brackets then check that the brackets are
309  // valid comment brackets
310  if (segment[0] == '<') {
311  if (n < 7 || (segment.compare(0,4,"<!--") != 0) ||
312  (segment.compare(n-3,3,"-->") != 0)) {
313  std::string msg = "Missing or invalid comment brackets "
314  "encountered in XML segment \""+segment+
315  "\". Please verify the XML format.";
317  }
318  else {
319  m_comment = segment.substr(4, n-7);
320  }
321  }
322  else {
323  m_comment = segment;
324  }
325 
326  //@todo Check validity of characters comment string
327 
328  } // endif: string is not empty
329 
330  // Return
331  return;
332 }
Abstract XML node base class.
Definition: GXmlNode.hpp:57
virtual ~GXmlComment(void)
Destructor.
XML comment node class interface definition.
void init_members(void)
Initialise class members.
GXmlNode & operator=(const GXmlNode &node)
Assignment operator.
Definition: GXmlNode.cpp:118
virtual std::string print(const GChatter &chatter=NORMAL, const int &indent=0) const
Print XML comment.
void init_members(void)
Initialise class members.
Definition: GXmlNode.cpp:880
Gammalib tools definition.
void copy_members(const GXmlComment &node)
Copy class members.
#define G_PARSE
Definition: GXmlComment.cpp:36
void free_members(void)
Delete class members.
Definition: GXmlNode.cpp:921
std::string m_comment
Comment (excluding brackets)
Definition: GXmlComment.hpp:77
void parse(const std::string &segment)
Parse comment segment string.
Abstract URL base class.
Definition: GUrl.hpp:44
virtual GXmlComment * clone(void) const
Clone XML comment.
GChatter
Definition: GTypemaps.hpp:33
GXmlComment & operator=(const GXmlComment &node)
Assignment operator.
virtual void printf(const char *format,...)=0
virtual void write(GUrl &url, const int &indent=0) const
Write comment into URL.
GXmlComment(void)
Void constructor.
Definition: GXmlComment.cpp:54
void free_members(void)
Delete class members.
virtual void clear(void)
Clear XML comment.
XML comment node class.
Definition: GXmlComment.hpp:44
Exception handler interface definition.
std::string fill(const std::string &s, const int &n)
Fill string with n strings of same type.
Definition: GTools.cpp:1044