XercesDomReader.hxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 #ifndef XercesDomReader_hxx
00022 #define XercesDomReader_hxx
00023 
00024 #include "XercesEncodings.hxx"
00025 #include "XercesInitializer.hxx"
00026 #include "XmlStorageErr.hxx"
00027 
00028 #include <xercesc/parsers/XercesDOMParser.hpp>
00029 #include <xercesc/framework/MemBufInputSource.hpp>
00030 #include <xercesc/sax/HandlerBase.hpp>
00031 #include <string>
00032 #include <list>
00033 #include <sstream>
00034 #include "Assert.hxx"
00035 
00036 namespace xercesc = XERCES_CPP_NAMESPACE;
00037 
00038 namespace CLAM
00039 {
00040 
00045 class XercesDomReader : private xercesc::HandlerBase
00046 {
00047         xercesc::XercesDOMParser * parser;
00048         public:
00049                 XercesDomReader()
00050                 {
00051                         // This should be done in the read method itself when the xerces but is solved
00052                         XercesInitializer::require();
00053                         parser = new xercesc::XercesDOMParser();
00054                 }
00055                 ~XercesDomReader()
00056                 {
00057                         // TODO: This is a hack for xerces bug on adoptNode, parser should be local variable
00058                         if (parser)
00059                                 delete parser;
00060                 }
00061                 // TODO: This is a hack parser should be local variable
00062                 xercesc::XercesDOMParser * adoptParser()
00063                 {
00064                         xercesc::XercesDOMParser * temp = parser;
00065                         parser = 0;
00066                         return temp;
00067                 }
00068                 xercesc::DOMDocument * read(std::istream & target)
00069                 {
00070                         if (target.fail()) throw XmlStorageErr("Unable to open the document source");
00071                         parser->setErrorHandler(this);
00072                         parser->setValidationScheme(xercesc::XercesDOMParser::Val_Auto);
00073                         parser->setValidationSchemaFullChecking(true);
00074                         parser->setDoNamespaces(true);
00075                         parser->setDoSchema(true);
00076                         parser->setCreateEntityReferenceNodes(true);
00077 
00078                         std::ostringstream stream;
00079                         char c;
00080                         while (true)
00081                         {
00082                                 target.get(c);
00083                                 if(!target.good()) break;
00084                                 stream.put(c);
00085                         }
00086                         std::string temp = stream.str();
00087                         unsigned length = temp.length();
00088                         const char * documentText = temp.c_str();
00089                         xercesc::MemBufInputSource xercesInputSource (
00090                                 (const XMLByte*)documentText
00091                                 , length
00092                                 , "CLAMParser"
00093                                 , false
00094                                 );
00095 
00096                         xercesInputSource.setCopyBufToStream(false);
00097 
00098                         parser->parse(xercesInputSource);
00099 
00100                         if (parser->getErrorCount())
00101                                 throw XmlStorageErr(
00102                                         (std::string("\nXML Parser Errors:\n")+
00103                                          RecopilaErrors()).c_str());
00104                         xercesc::DOMDocument *doc = parser->getDocument();
00105                         CLAM_ASSERT(doc,"No errors but document not loaded!");
00106                         return doc;
00107                 }
00108         private:
00109                 typedef std::list<std::string> Missatges;
00110                 Missatges _errors;
00111                 void error(const xercesc::SAXParseException& e)
00112                 {
00113                         report("Error", e);
00114                 }
00115 
00116                 void fatalError(const xercesc::SAXParseException& e)
00117                 {
00118                         report("Fatal Error", e);
00119                 }
00120 
00121                 void warning(const xercesc::SAXParseException& e)
00122                 {
00123                         report("Warning", e);
00124                 }
00125 
00126                 void report(const std::string & level, const xercesc::SAXParseException& e)
00127                 {
00128                         std::ostringstream stream;
00129                         stream << level << " at file " << L(e.getSystemId())
00130                                 << ", line " << e.getLineNumber()
00131                                 << ", col " << e.getColumnNumber()
00132                                 << ":\n" << L(e.getMessage());
00133                         _errors.push_back(stream.str());
00134                 }
00135         public:
00136                 std::string RecopilaErrors()
00137                 {
00138                         std::string result;
00139                         Missatges::iterator it = _errors.begin();
00140                         for (; it!=_errors.end(); it++)
00141                                 result += *it + "\n";
00142                         return result;
00143                 }
00144 };
00145 
00146 
00147 
00148 }
00149 #endif//XercesDomReader_hxx
00150 
Generated by  doxygen 1.6.3