libcommonc++  0.7
XMLElement.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library 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 GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_XMLElement_hxx
24 #define __ccxx_XMLElement_hxx
25 
26 #include <commonc++/Common.h++>
28 #include <commonc++/String.h++>
29 
30 #include <vector>
31 
32 namespace ccxx {
33 
34 class XMLElement; // fwd decl
35 
52 {
53  friend class XMLDocument;
55  friend class XMLElementIterator;
56 
57  public:
58 
60  inline String getName() const
61  { return(_name); }
62 
71  void setAttribute(const String& name, const String& value);
72 
81  void setAttribute(const String& name, int value);
82 
91  void setAttribute(const String& name, const int64_t& value);
92 
101  void setAttribute(const String& name, const double& value);
102 
111  void setAttribute(const String& name, bool value);
112 
119  XMLElement& getParent();
120 
127  const XMLElement& getParent() const;
128 
140  bool getAttribute(const String& name, String& value,
141  const String& defaultValue = "") const;
142 
154  bool getAttribute(const String& name, int& value, int defaultValue = 0)
155  const;
156 
168  bool getAttribute(const String& name, int64_t& value,
169  const int64_t& defaultValue = 0) const;
170 
182  bool getAttribute(const String& name, bool& value,
183  bool defaultValue = false) const;
184 
196  bool getAttribute(const String& name, double& value,
197  const double& defaultValue = 0.0) const;
198 
204  void getAttributeNames(StringVec& names) const;
205 
207  void removeAttributes();
208 
215  bool hasAttributes() const;
216 
224  inline bool hasContent() const
225  { return(_content.getLength() > 0); }
226 
232  inline void setContent(const String& content)
233  { _content = content; }
234 
240  inline const String getContent() const
241  { return(_content); }
242 
249  bool hasChildren() const;
250 
256  uint_t getChildCount() const;
257 
265  XMLElement& addChild(const String& name);
266 
275  XMLElement& getChild(const String& name = String::null);
276 
285  const XMLElement& getChild(const String& name = String::null) const;
286 
294  XMLElement& getChild(uint_t index);
295 
303  const XMLElement& getChild(uint_t index) const;
304 
314  bool hasChild(const String& name) const;
315 
324  XMLElement& operator[](const String& name);
325 
334  XMLElement& operator[](const char* name);
335 
344  inline const XMLElement& operator[](const String& name) const
345  { return(_getChild(name)); }
346 
355  inline const XMLElement& operator[](const char* name) const
356  { return(_getChild(name)); }
357 
366  XMLElement& operator[](int index);
367 
376  const XMLElement& operator[](int index) const;
377 
379  void removeChildren();
380 
387  void removeChildren(const String& name);
388 
393  XMLElement& operator=(bool val);
394 
399  XMLElement& operator=(long val);
400 
405  XMLElement& operator=(unsigned long val);
406 
411  XMLElement& operator=(const int64_t& val);
412 
417  XMLElement& operator=(const uint64_t& val);
418 
423  XMLElement& operator=(float val);
424 
429  XMLElement& operator=(const double& val);
430 
432  XMLElement& operator=(const String& val);
433 
435  bool toBool() const;
436 
438  int toInt() const;
439 
441  uint_t toUInt() const;
442 
444  int64_t toInt64() const;
445 
447  uint64_t toUInt64() const;
448 
450  float toFloat() const;
451 
453  double toDouble() const;
454 
456  inline String toString() const
457  { return(getContent()); }
458 
460  inline bool isNull() const
461  { return(_name.isNull()); }
462 
464  inline operator const void*() const
465  { return(isNull() ? NULL : this); }
466 
468  inline bool operator!() const
469  { return(isNull()); }
470 
472  void write(std::ostream& stream, int depth, uint_t tabWidth) const;
473 
475  static void validateName(const String& name);
476 
478  static XMLElement null;
479 
480  private:
481 
482  const XMLElement& _getChild(const String& name = String::null) const;
483  const XMLElement& _getChild(uint_t index) const;
484 
485  String _name;
486  String _content;
487 
488  class XMLAttributeMap; // fwd decl
489 
490  XMLAttributeMap* _attrs;
491  XMLElement* _firstChild;
492  XMLElement* _lastChild;
493  XMLElement* _next;
494  XMLElement* _parent;
495 
496  XMLElement(const String& name, XMLElement* parent = NULL);
497  ~XMLElement();
498 
500 };
501 
502 } // namespace ccxx
503 
504 #endif // __ccxx_XMLElement_hxx
An XMLElement const iterator.
Definition: XMLElementConstIterator.h++:40
An XMLElement iterator.
Definition: XMLElementIterator.h++:40
bool isNull() const
Test if the element is a "null" element.
Definition: XMLElement.h++:460
const XMLElement & operator[](const String &name) const
Get the first child with the given name.
Definition: XMLElement.h++:344
A String vector.
Definition: String.h++:1159
String toString() const
Return the element&#39;s content as a String.
Definition: XMLElement.h++:456
String getName() const
Get the name of the element.
Definition: XMLElement.h++:60
#define COMMONCPPXML_API
Definition: Common.h++:129
An object representing an XML document.
Definition: XMLDocument.h++:46
bool operator!() const
Test if the element is a "null" element.
Definition: XMLElement.h++:468
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
An object representing an XML element.
Definition: XMLElement.h++:51
const String getContent() const
Get the element&#39;s content.
Definition: XMLElement.h++:240
void setContent(const String &content)
Set the element&#39;s content.
Definition: XMLElement.h++:232
static XMLElement null
The "null" element.
Definition: XMLElement.h++:478
A flexible, reference counted, copy-on-write, thread-safe, nullable string.
Definition: String.h++:50
#define CCXX_COPY_DECLS(CLASS)
Inlines declarations of a copy constructor and assignment operator for the class CLASS.
Definition: Common.h++:295
static const String null
The null string.
Definition: String.h++:1128
Definition: AllocationMap.c++:25
const XMLElement & operator[](const char *name) const
Get the first child with the given name.
Definition: XMLElement.h++:355
bool hasContent() const
Test if the element has content.
Definition: XMLElement.h++:224