00001 #ifndef PhysicsTools_MVATrainer_XMLDocument_h 00002 #define PhysicsTools_MVATrainer_XMLDocument_h 00003 00004 #include <string> 00005 #include <memory> 00006 00007 #include <xercesc/dom/DOM.hpp> 00008 #include <xercesc/dom/DOMImplementation.hpp> 00009 #include <xercesc/parsers/XercesDOMParser.hpp> 00010 #include <xercesc/sax/HandlerBase.hpp> 00011 #include <xercesc/dom/DOMDocument.hpp> 00012 00013 class XMLDocument { 00014 public: 00015 XMLDocument(const std::string &fileName, bool write = false); 00016 ~XMLDocument(); 00017 00018 inline XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument * 00019 getDocument() const { return doc; } 00020 00021 inline XERCES_CPP_NAMESPACE_QUALIFIER DOMElement * 00022 getRootNode() const { return rootNode; } 00023 00024 XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument * 00025 createDocument(const std::string &root); 00026 00027 static bool hasAttribute(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *elem, 00028 const char *name); 00029 template<typename T> 00030 static T readAttribute(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *elem, 00031 const char *name); 00032 template<typename T> 00033 static T readAttribute(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *elem, 00034 const char *name, const T &defValue); 00035 template<typename T> 00036 static void writeAttribute(XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *elem, 00037 const char *name, const T &value); 00038 00039 template<typename T> 00040 static T readContent(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node); 00041 template<typename T> 00042 static void writeContent(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *node, 00043 XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc, 00044 const T &value); 00045 00046 private: 00047 class XercesPlatform { 00048 public: 00049 XercesPlatform(); 00050 ~XercesPlatform(); 00051 00052 private: 00053 // do not make any kind of copies 00054 XercesPlatform(const XercesPlatform &orig); 00055 XercesPlatform &operator = (const XercesPlatform &orig); 00056 00057 static unsigned int instances; 00058 }; 00059 00060 void openForRead(const std::string &fileName); 00061 void openForWrite(const std::string &fileName); 00062 00063 std::auto_ptr<XercesPlatform> platform; 00064 00065 std::string fileName; 00066 bool write; 00067 00068 std::auto_ptr<XERCES_CPP_NAMESPACE_QUALIFIER XercesDOMParser> parser; 00069 std::auto_ptr<XERCES_CPP_NAMESPACE_QUALIFIER HandlerBase> errHandler; 00070 XERCES_CPP_NAMESPACE_QUALIFIER DOMImplementation *impl; 00071 00072 XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *doc; 00073 XERCES_CPP_NAMESPACE_QUALIFIER DOMElement *rootNode; 00074 }; 00075 00076 #include "PhysicsTools/MVATrainer/interface/XMLDocument.icc" 00077 00078 #endif // PhysicsTools_MVATrainer_XMLDocument_h