Main Page   Namespace List   File List   Examples  

XMLTree.hpp

Go to the documentation of this file.
00001 /*
00002  * (C) Copyright 2002, Schlund+Partner AG
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 */
00018 
00019 #ifndef SP_GXML_XMLTREE_HPP
00020 #define SP_GXML_XMLTREE_HPP
00021 
00034 // STDC++
00035 #include <string>
00036 #include <memory>
00037 #include <fstream>
00038 
00039 // Local
00040 #include <sp-gxmlcpp/XMLNodeSet.hpp>
00041 #include <sp-gxmlcpp/XMLException.hpp>
00042 
00043 
00044 /* NOTES
00045    o xmlChar is one byte.
00046    o "xmlChar" is compatible to "char"; xmlChar * Strings are always 0-terminated; (xmlChar *) is compatible to (char *)/string.c_str()
00047    o Simply using char *, string makes us still compatible with: UTF-8, ASCII, ISO-8859-*, ... (basically, all "up-to-8-Bits-Encodings").
00048    o UCS-2/UCS-4 encodings might be a problem.
00049 
00050    UNICODE  ISO 10646    All characters of the planet listed and labeled....
00051 
00052    UCS-2: 16-Bit Encoding of UNICODE.
00053    UCS-4: 32-Bit Encoding of UNICODE.
00054    UTF-8: 8-Bit Encoding of UNICODE. All todays ISO-8859-1 texts qualify as UTF-8. This is what UNIXses will do.
00055 */
00056 
00057 namespace SP
00058 {
00059 namespace GXML
00060 {
00061 
00062 //
00063 // XMLTree
00064 //
00065 class XMLTree
00066 {
00067         /* enum Exceptions
00068                  {
00069                  PARSE_ERR=1,           // XML could not be parsed
00070                  CONTEXT_ERR,           // Could not create (XPath) context for Tree
00071                  NO_NODE_FROM_PATH_ERR, // Could not find node from specified path
00072                  NODE_CREATION_ERR,     // Could not create a node
00073                  NODE_ADDING_ERR,       // Could not add a creted node
00074                  TREE_INVALID_ERR       // Could not add a creted node
00075                  }; */
00076 public:
00077         // Creators for char-Buffer, C++ string, ifstream
00078         XMLTree(const xmlDocPtr doc);
00079         XMLTree(const char * xmlBuffer, int size=-1);  // if size < 0, xmlBuffer must be a valid C-String!
00080         XMLTree(const std::string & xmlString);
00081         XMLTree(std::ifstream & xmlStream);
00082         XMLTree(std::istream & xmlStream);
00083 
00084         ~XMLTree();
00085 
00086         std::string getString( const std::string& xpath );
00087         bool getBool( const std::string& xpath );
00088         double getFloat( const std::string& xpath );
00089 
00090         // REMARK : the returned XMLNodeSet has to be freed by the calling method
00091         std::auto_ptr<XMLNodeSet> getNodeSet( const std::string& xpath );
00092 
00094         // Get internal libxml2 Pointers
00095         //  - this will allow us to call libxml2-Functions on the tree directly.
00096         //    (i.e., helling the abstraction for speed)
00097         xmlDocPtr getDocPtr() const;
00098         xmlXPathContextPtr getXPathContextPtr() const;
00099 
00101         // "Get Methods" based on "XPath"; paths are XPath Expressions;
00102         // all paths can be given an optional position
00103         //  - Methods delivering (xmlChar *) point into the tree
00104         //  - Methods delivering (std::string, int) obviously copy from tree
00105         //  - getValue-Methods will deliver the contents of the first child only
00106         //    e.g.: getValue("/req/X", 0) on
00107         // "<req><X>a b c<subtag>def</subtag></X></req>" returns: "a b c" only.
00108 
00109         // "libxml2"-Functions. Do not use if you want to be abstract.
00110         xmlChar * getXmlCharValue(const xmlChar * path) const;
00111         xmlChar * getXmlCharValue(const std::string & path) const;
00112 
00113         // "C++"-functions
00114         char * getAddrValue(const std::string & path) const;
00115         std::string getValue(const std::string & path) const;
00116 
00117         char * getAddrName(const std::string & path) const;
00118         std::string getName(const std::string & path) const;
00119 
00120         void setValue(const std::string & path, char * value);
00121         void setValue(const std::string & path, const std::string & value);
00122 
00123         /* Obsoleted by XMLDump
00124         // getTreeAsString=recursive: This gets the complete contents of the tag
00125         //  e.g.: getTreeAsString("/req/X", 0) on "<req><X>a b c<subtag>def</subtag>
00126         // </X></req>" returns: "<X>a b c<subtag>def</subtag></X>".
00127         std::string getTreeAsString(const std::string & path) const;
00128         */
00129 
00130         int getCount(const xmlChar * path) const;
00131         int getCount(const char * path) const;
00132         int getCount(const std::string & path) const;
00133 
00135         // "Modifying Methods" based on "XPath"; paths are XPath Expressions
00136         //  - Methods delivering (xmlChar *) point into the tree
00137         //  - Methods delivering (std::string, int) copy from tree
00138         void delTag(const std::string & path);
00139         void addTag(const std::string & path, const std::string & name, const std::string & content);
00140 
00148         void addSiblingTag(const std::string & path, const std::string & name, const std::string & content);
00149 
00157         std::string getSiblingXML( const std::string& path = "/*" );
00158 
00159         // addTree: Adds the whole tree as new child for node
00160         // This will COPY. No other method available from libxml2. We cannot mix trees.
00161         void addTree(const std::string & path, XMLTree * xmlTree);
00162         void addXML(const std::string & path, const std::string & xml);
00163 
00164         std::string getXML(const std::string & path="/") const;
00165 
00166         std::auto_ptr<XMLTree> getTree(const std::string &path="/");
00167 
00169         // Utility functions
00170         //
00171         // Node from Path
00172         static xmlNodePtr nodeFromPath(xmlXPathContextPtr context, const xmlChar * path);
00173         // Node from Path
00174         static xmlNodePtr nodeFromPath(xmlXPathContextPtr context, const std::string & path);
00175 
00176 private:
00177         xmlDocPtr tree_;
00178         xmlXPathContextPtr context_;
00179 
00180         // Constructor helper function
00181         void genTree(const char * xmlBuffer, int size=-1);
00182 
00183         xmlXPathObjectPtr createXPathObject( const std::string& xpath );
00184         void destroyXPathObject( xmlXPathObjectPtr obj );
00185 };
00186 
00187 }}
00188 #endif

Generated on Thu Jun 3 19:20:12 2004 for sp-gxmlcpp by doxygen1.2.15