00001 /* 00002 Sheep - A Rigid Body Dynamics Engine 00003 Copyright (C) 2001-2004 Francois Beaune 00004 Contact: http://toxicengine.sourceforge.net/ 00005 00006 This file is part of Sheep. 00007 00008 Sheep is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 2 of the License, or 00011 (at your option) any later version. 00012 00013 Sheep is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with Sheep; if not, write to the Free Software 00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 */ 00022 00023 #ifndef SHEEP_MISC_XMLCONFIGFILE_H 00024 #define SHEEP_MISC_XMLCONFIGFILE_H 00025 00026 #include "config.h" 00027 #include "stringutils.h" 00028 00029 #include <xercesc/dom/DOM.hpp> 00030 #include <xercesc/parsers/XercesDOMParser.hpp> 00031 #include <xercesc/sax/ErrorHandler.hpp> 00032 00033 #include <string> 00034 00035 class XPathEvaluator; 00036 class XPathNSResolver; 00037 class XPathResult; 00038 00039 namespace sheep { 00040 00041 class XMLConfigFile { 00042 public: 00043 //! This is the base class for all exceptions thrown by the XMLConfigFile class. 00044 struct LoadErrorException { 00045 LoadErrorException(const std::string &message) : 00046 m_message(message) {} 00047 00048 std::string m_message; 00049 }; 00050 00051 //! This exception is thrown if an error occurs when loading the XML document. 00052 //! It is either an I/O error or, if a schema was provided, a XML document 00053 //! validation error (the XML document could not be validated against the schema). 00054 struct ParseErrorException : public LoadErrorException { 00055 ParseErrorException(const std::string &message) : 00056 LoadErrorException(message) {} 00057 }; 00058 00059 //! This exception is thrown if an error occurs when querying the DOM document 00060 //! corresponding to the XML document. It is likely that the path (given in W3C 00061 //! XPath 1.0 notation) is badly formatted or could not be found in the document. 00062 struct QueryErrorException : public LoadErrorException { 00063 QueryErrorException(const std::string &message) : 00064 LoadErrorException(message) {} 00065 }; 00066 00067 XMLConfigFile( 00068 const std::string &filename, 00069 xercesc::ErrorHandler *error_handler = 0 00070 ) throw(ParseErrorException); 00071 00072 //! Schema file path must be relative to the XML file path. 00073 XMLConfigFile( 00074 const std::string &filename, 00075 const std::string &schema_filename, 00076 xercesc::ErrorHandler *error_handler = 0 00077 ) throw(ParseErrorException); 00078 00079 ~XMLConfigFile(); 00080 00081 //! GetValue<>() methods take a W3C XML Path (XPath) expression as argument. 00082 //! The W3C Recommendation can be found here: http://www.w3.org/TR/xpath. 00083 template<typename T> 00084 T GetValue(const std::string &xpathexpr) const 00085 throw(QueryErrorException, StringUtils::ConversionErrorException); 00086 template<> 00087 bool GetValue<bool>(const std::string &xpathexpr) const 00088 throw(QueryErrorException, StringUtils::ConversionErrorException); 00089 00090 //! Returns true if the element designated by the XPath expression 00091 //! given as argument exists. 00092 int Count(const std::string &xpathexpr) const 00093 throw(QueryErrorException); 00094 00095 private: 00096 xercesc::XercesDOMParser *m_parser; //!< The XML DOM parser which owns the DOM document. 00097 xercesc::DOMElement *m_rootelement; //!< The root element of the DOM tree. 00098 XPathEvaluator *m_evaluator; //!< The XPathEvaluator class is used to create XPathExpression and XPathNSResolver objects. 00099 XPathNSResolver *m_nsresolver; //!< The XPathNSResolver class binds XML namespace prefixes to the full namespace string. 00100 00101 void initialize( 00102 const std::string &filename, 00103 const std::string &schema_filename, 00104 xercesc::ErrorHandler *error_handler 00105 ) throw(ParseErrorException); 00106 00107 XPathResult *query(const std::string &xpathexpr) const 00108 throw(QueryErrorException); 00109 00110 std::string get_string(const std::string &xpathexpr) const 00111 throw(QueryErrorException); 00112 }; 00113 00114 #include "xmlconfigfile.inl" 00115 00116 } 00117 00118 #endif // !SHEEP_MISC_XMLCONFIGFILE_H
1.3.6