CLAM-Development  1.4.0
XercesDomReader.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 #ifndef XercesDomReader_hxx
22 #define XercesDomReader_hxx
23 
24 #include "XercesEncodings.hxx"
25 #include "XercesInitializer.hxx"
26 #include "XmlStorageErr.hxx"
27 
28 #include <xercesc/parsers/XercesDOMParser.hpp>
29 #include <xercesc/framework/MemBufInputSource.hpp>
30 #include <xercesc/sax/HandlerBase.hpp>
31 #include <string>
32 #include <list>
33 #include <sstream>
34 #include "Assert.hxx"
35 
36 namespace xercesc = XERCES_CPP_NAMESPACE;
37 
38 namespace CLAM
39 {
40 
45 class XercesDomReader : private xercesc::HandlerBase
46 {
47  xercesc::XercesDOMParser * parser;
48  public:
50  {
51  // This should be done in the read method itself when the xerces but is solved
53  parser = new xercesc::XercesDOMParser();
54  }
56  {
57  // TODO: This is a hack for xerces bug on adoptNode, parser should be local variable
58  if (parser)
59  delete parser;
60  }
61  // TODO: This is a hack parser should be local variable
62  xercesc::XercesDOMParser * adoptParser()
63  {
64  xercesc::XercesDOMParser * temp = parser;
65  parser = 0;
66  return temp;
67  }
68  xercesc::DOMDocument * read(std::istream & target)
69  {
70  if (target.fail()) throw XmlStorageErr("Unable to open the document source");
71  parser->setErrorHandler(this);
72  parser->setValidationScheme(xercesc::XercesDOMParser::Val_Auto);
73  parser->setValidationSchemaFullChecking(true);
74  parser->setDoNamespaces(true);
75  parser->setDoSchema(true);
76  parser->setCreateEntityReferenceNodes(true);
77 
78  std::ostringstream stream;
79  char c;
80  while (true)
81  {
82  target.get(c);
83  if(!target.good()) break;
84  stream.put(c);
85  }
86  std::string temp = stream.str();
87  unsigned length = temp.length();
88  const char * documentText = temp.c_str();
89  xercesc::MemBufInputSource xercesInputSource (
90  (const XMLByte*)documentText
91  , length
92  , "CLAMParser"
93  , false
94  );
95 
96  xercesInputSource.setCopyBufToStream(false);
97 
98  parser->parse(xercesInputSource);
99 
100  if (parser->getErrorCount())
101  throw XmlStorageErr(
102  (std::string("\nXML Parser Errors:\n")+
103  RecopilaErrors()).c_str());
104  xercesc::DOMDocument *doc = parser->getDocument();
105  CLAM_ASSERT(doc,"No errors but document not loaded!");
106  return doc;
107  }
108  private:
109  typedef std::list<std::string> Missatges;
110  Missatges _errors;
111  void error(const xercesc::SAXParseException& e)
112  {
113  report("Error", e);
114  }
115 
116  void fatalError(const xercesc::SAXParseException& e)
117  {
118  report("Fatal Error", e);
119  }
120 
121  void warning(const xercesc::SAXParseException& e)
122  {
123  report("Warning", e);
124  }
125 
126  void report(const std::string & level, const xercesc::SAXParseException& e)
127  {
128  std::ostringstream stream;
129  stream << level << " at file " << L(e.getSystemId())
130  << ", line " << e.getLineNumber()
131  << ", col " << e.getColumnNumber()
132  << ":\n" << L(e.getMessage());
133  _errors.push_back(stream.str());
134  }
135  public:
136  std::string RecopilaErrors()
137  {
138  std::string result;
139  Missatges::iterator it = _errors.begin();
140  for (; it!=_errors.end(); it++)
141  result += *it + "\n";
142  return result;
143  }
144 };
145 
146 
147 
148 }
149 #endif//XercesDomReader_hxx
150