XMLDocument.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2012  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020    ---------------------------------------------------------------------------
00021 */
00022 
00023 #ifndef __ccxx_XMLDocument_hxx
00024 #define __ccxx_XMLDocument_hxx
00025 
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/String.h++>
00028 #include <commonc++/IOException.h++>
00029 #include <commonc++/InvalidArgumentException.h++>
00030 #include <commonc++/ParseException.h++>
00031 
00032 #include <map>
00033 #include <vector>
00034 
00035 #include <expat.h>
00036 
00037 namespace ccxx {
00038 
00039 class XMLElement; // fwd ref
00040 
00046 class COMMONCPPXML_API XMLDocument
00047 {
00048   public:
00049 
00054   XMLDocument(const String& encoding = "us-ascii");
00055 
00057   ~XMLDocument() throw();
00058 
00064   XMLElement & getRoot();
00065 
00071   const XMLElement & getRoot() const;
00072 
00080   XMLElement & setRoot(const String &name) throw(InvalidArgumentException);
00081 
00091   XMLElement & find(const String &path) throw(InvalidArgumentException);
00092 
00102   const XMLElement & find(const String &path) const
00103     throw(InvalidArgumentException);
00104 
00111   void read(std::istream &stream) throw(ParseException, IOException);
00112 
00118   void read(const String &str) throw(ParseException);
00119 
00127   void write(std::ostream &stream, uint_t tabWidth = 2) const
00128     throw(IOException);
00129 
00136   void write(String &str, uint_t tabWidth = 2) const;
00137 
00142   inline bool isEmpty() const throw()
00143   { return(_root == NULL); }
00144 
00149   inline bool operator!() const throw()
00150   { return(isEmpty()); }
00151 
00152   private:
00153 
00154   const XMLElement & _find(const String &path) const
00155     throw(InvalidArgumentException);
00156 
00157   // RAII class to encapsulate parser object
00158   class XMLParser
00159   {
00160     friend class XMLDocument;
00161 
00162     public:
00163 
00164     XMLParser(XMLDocument *owner, const String& encoding = "us-ascii");
00165     ~XMLParser();
00166 
00167     void parse(const char *data, size_t length, bool done = true)
00168       throw(ParseException);
00169 
00170     inline void appendCharData(const XML_Char *data, int len)
00171     { _charData.append(data, len); }
00172 
00173     private:
00174 
00175     XMLDocument *_document;
00176     XMLElement *_current;
00177     String _charData;
00178     XML_Parser _parser;
00179     bool _status;
00180   };
00181 
00182   String _encoding;
00183   XMLElement *_root;
00184 
00185   static void _elementStartHandler(void *userData, const XML_Char *name,
00186                                    const XML_Char **attr);
00187   static void _elementEndHandler(void *userData, const XML_Char *name);
00188   static void _charDataHandler(void *userData, const XML_Char *data,
00189                                int len);
00190   static void _commentHandler(void *userData, const XML_Char *data);
00191 
00192   CCXX_COPY_DECLS(XMLDocument);
00193 };
00194 
00195 }; // namespace ccxx
00196 
00197 #endif // __ccxx_XMLDocument_hxx
00198 
00199 /* end of header file */
Generated on Sat Nov 26 16:49:07 2011 for libcommonc++ by  doxygen 1.6.3