XMLDocument.h++
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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;
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
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 };
00196
00197 #endif // __ccxx_XMLDocument_hxx
00198
00199