libcommonc++  0.7
XMLDocument.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_XMLDocument_hxx
24 #define __ccxx_XMLDocument_hxx
25 
26 #include <commonc++/Common.h++>
30 #include <commonc++/String.h++>
31 
32 #include <map>
33 #include <vector>
34 
35 #include <expat.h>
36 
37 namespace ccxx {
38 
39 class XMLElement; // fwd ref
40 
47 {
48  public:
49 
51  XMLDocument();
52 
54  ~XMLDocument();
55 
62  XMLElement& getRoot();
63 
70  const XMLElement& getRoot() const;
71 
80  XMLElement& setRoot(const String& name);
81 
92  XMLElement& find(const String& path);
93 
104  const XMLElement& find(const String& path) const;
105 
113  void read(std::istream& stream);
114 
121  void read(const String& str);
122 
131  void write(std::ostream& stream, uint_t tabWidth = 2) const;
132 
140  void write(String& str, uint_t tabWidth = 2) const;
141 
147  inline bool isEmpty() const
148  { return(_root == NULL); }
149 
155  inline bool operator!() const
156  { return(isEmpty()); }
157 
158  private:
159 
160  const XMLElement& _find(const String& path) const;
161 
162  // RAII class to encapsulate parser object
163  class XMLParser
164  {
165  friend class XMLDocument;
166 
167  public:
168 
169  XMLParser(XMLDocument *owner);
170  ~XMLParser();
171 
172  void parse(const char* data, size_t length, bool done = true);
173 
174  inline void appendCharData(const XML_Char* data, int len)
175  { _charData.append(data, len); }
176 
177  private:
178 
179  XMLDocument* _document;
180  XMLElement* _current;
181  String _charData;
182  XML_Parser _parser;
183  bool _status;
184  };
185 
186  XMLElement* _root;
187 
188  static void _elementStartHandler(void* userData, const XML_Char* name,
189  const XML_Char** attr);
190  static void _elementEndHandler(void* userData, const XML_Char* name);
191  static void _charDataHandler(void* userData, const XML_Char* data, int len);
192  static void _commentHandler(void* userData, const XML_Char* data);
193 
195 };
196 
197 } // namespace ccxx
198 
199 #endif // __ccxx_XMLDocument_hxx
#define COMMONCPPXML_API
Definition: Common.h++:129
An object representing an XML document.
Definition: XMLDocument.h++:46
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
An object representing an XML element.
Definition: XMLElement.h++:51
bool operator!() const
Test if the document is empty.
Definition: XMLDocument.h++:155
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
bool isEmpty() const
Test if the document is empty.
Definition: XMLDocument.h++:147
Definition: AllocationMap.c++:25