00001 /*************************************************************************** 00002 * File: ./XMLHandler.h 00003 * Author: Ronni Grapenthin, NORVULK & HU-BERLIN 00004 * Created: 28.03.2007 00005 * Licence: GPLv2 00006 * 00007 * ######################################################################### 00008 * 00009 * CrusDe, simulation framework for crustal deformation studies 00010 * Copyright (C) 2007 Ronni Grapenthin 00011 * 00012 * This program is free software; you can redistribute it and/or 00013 * modify it under the terms of the GNU General Public License 00014 * as published by the Free Software Foundation; version 2 00015 * of the License. 00016 * 00017 * This program is distributed in the hope that it will be useful, 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 * GNU General Public License for more details. 00021 * 00022 * You should have received a copy of the GNU General Public License 00023 * along with this program; if not, write to the Free Software 00024 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00025 * 00026 ****************************************************************************/ 00027 00028 #ifndef _xml_handler_h 00029 #define _xml_handler_h 00030 00031 //----------------------------------------------------------- 00032 //-------------- X M L INCLUDES ---------------------------- 00033 //----------------------------------------------------------- 00034 #include <xercesc/parsers/XercesDOMParser.hpp> 00035 #include <xercesc/dom/DOM.hpp> 00036 #include <xercesc/dom/DOMDocument.hpp> 00037 #include <xercesc/dom/DOMDocumentType.hpp> 00038 #include <xercesc/dom/DOMElement.hpp> 00039 #include <xercesc/dom/DOMImplementation.hpp> 00040 #include <xercesc/dom/DOMImplementationLS.hpp> 00041 #include <xercesc/dom/DOMNodeIterator.hpp> 00042 #include <xercesc/dom/DOMNodeList.hpp> 00043 #include <xercesc/dom/DOMText.hpp> 00044 #include <xercesc/dom/DOMWriter.hpp> 00045 #include <xercesc/framework/StdOutFormatTarget.hpp> 00046 #include <xercesc/framework/LocalFileFormatTarget.hpp> 00047 #include <xercesc/util/XMLUni.hpp> 00048 #include <xercesc/util/PlatformUtils.hpp> 00049 #include <xercesc/util/XMLString.hpp> 00050 #include <xercesc/util/XMLChar.hpp> 00051 #include <xercesc/util/OutOfMemoryException.hpp> 00052 #include <xercesc/parsers/XercesDOMParser.hpp> 00053 #include <xercesc/util/XMLUni.hpp> 00054 #include "DOMTreeErrorReporter.hpp" 00055 00056 //----------------------------------------------------------- 00057 //-------------- S T D INCLUDES ---------------------------- 00058 //----------------------------------------------------------- 00059 #include <string> 00060 #include <iostream> 00061 #include <cassert> 00062 #include <dlfcn.h> 00063 #include <stdexcept> 00064 #include <list> 00065 #include <sstream> 00066 00067 #include "exceptions.h" 00068 00069 XERCES_CPP_NAMESPACE_USE 00070 00071 using namespace std; 00072 00073 inline std::string stringify(int x) 00074 { 00075 std::ostringstream o; 00076 if (!(o << x)) 00077 throw Conversion(" stringify(int) "); 00078 return o.str(); 00079 } 00080 00081 class StrXML 00082 { 00083 public : 00084 // ----------------------------------------------------------------------- 00085 // Constructors and Destructor 00086 // ----------------------------------------------------------------------- 00087 StrXML(const char* const orig); 00088 StrXML(const XMLCh* const orig); 00089 StrXML( const string& orig ) ; 00090 ~StrXML() throw(); 00091 const XMLCh* xmlStr() const throw(); 00092 const char* cStr() const throw(); 00093 const string cppStr() const throw(); 00094 00095 private : 00096 char* cstring; 00097 XMLCh* xmlstring; 00098 string cppstring; 00099 }; 00100 00105 class XMLCore 00106 { 00107 public: 00108 XMLCore(); /* Constructor */ 00109 ~XMLCore(); /* Destructor */ 00110 }; 00111 00114 class XMLHandler : private XMLCore 00115 { 00116 private: 00117 XercesDOMParser parser; 00118 DOMTreeErrorReporter errReporter; 00119 DOMElement *rootElem; 00120 00121 00123 XMLHandler(const XMLHandler& x); 00124 00125 void fillNodesChildDataList(DOMNode *n, XMLCh *child, list<string> &datalist); 00126 00127 protected: 00128 string xmlFile; 00129 string xmlPath; 00130 00131 DOMDocument *doc; 00132 00133 const StrXML core; 00134 00135 const StrXML TAG_authors; 00136 const StrXML TAG_category; 00137 const StrXML TAG_crustaldecay; 00138 const StrXML TAG_dateAdded; 00139 const StrXML TAG_dependency; 00140 const StrXML TAG_description; 00141 const StrXML TAG_experiment; 00142 const StrXML TAG_file; 00143 const StrXML TAG_filename; 00144 const StrXML TAG_green; 00145 const StrXML TAG_job; 00146 const StrXML TAG_kernel; 00147 const StrXML TAG_load; 00148 const StrXML TAG_loadfunction; 00149 const StrXML TAG_loadhistory; 00150 const StrXML TAG_name; 00151 const StrXML TAG_output; 00152 const StrXML TAG_parameter; 00153 const StrXML TAG_plugin; 00154 const StrXML TAG_postprocessor; 00155 const StrXML TAG_region; 00156 const StrXML TAG_version; 00157 00158 const StrXML ATTR_date; 00159 const StrXML ATTR_gmtime; 00160 const StrXML ATTR_id; 00161 const StrXML ATTR_job; 00162 const StrXML ATTR_localtime; 00163 const StrXML ATTR_name; 00164 const StrXML ATTR_user; 00165 const StrXML ATTR_value; 00166 00167 list<string> getNodesChildDataList(XMLCh *parent, XMLCh *child); 00168 string getChildData(DOMNode *parent, const XMLCh* childname); 00169 void readXML() throw( std::runtime_error ); 00170 void writeXML(bool pretty=false) throw( std::runtime_error ); 00171 int getActualID(); 00172 00173 public: 00174 XMLHandler(const char* path); /* Constructor */ 00175 virtual ~XMLHandler(); /* Destructor */ 00176 00177 DOMNode* getElementByTagName(const XMLCh*); /*returns first element with given name */ 00178 DOMNodeList* getElementListByTagName(const XMLCh*); /*returns all elements with given name*/ 00179 string getValueOfNode(DOMNode*); /*if node == Text Node: return TEXT (<node>TEXT</node>)*/ 00180 string getValueOfNode(DOMNode*, const XMLCh*);/*if node == Element Node: find node with name='name' and return value of 'value' attr*/ 00181 virtual void init()=0; 00182 00183 }; 00184 00185 #endif // _xml_handler_h