XercesDomReader.hxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00052 XercesInitializer::require();
00053 parser = new xercesc::XercesDOMParser();
00054 }
00055 ~XercesDomReader()
00056 {
00057
00058 if (parser)
00059 delete parser;
00060 }
00061
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