CLAM-Development  1.4.0
LibXmlDomReader.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 LibXmlDomReader_hxx
22 #define LibXmlDomReader_hxx
23 
24 #include "XmlStorageErr.hxx"
25 
26 #include <string>
27 #include <list>
28 #include <sstream>
29 #include "Assert.hxx"
30 #include <libxml++/parsers/domparser.h>
31 
32 namespace CLAM
33 {
34 
40 {
41  xmlpp::DomParser * parser;
42  public:
44  {
45  // TODO: This is a hack for xerces bug on adoptNode, parser should be local variable
46  // This should be done in the read method itself when the xerces but is solved
47  parser = new xmlpp::DomParser;
48  }
50  {
51  // TODO: This is a hack for xerces bug on adoptNode, parser should be local variable
52  if (parser) delete parser;
53  }
54  // TODO: This is a hack parser should be local variable
55  xmlpp::DomParser * adoptParser()
56  {
57  xmlpp::DomParser * temp = parser;
58  parser = 0;
59  return temp;
60  }
61  xmlpp::Document * read(std::istream & target)
62  {
63  if (target.fail()) throw XmlStorageErr("Unable to open the document source");
64  parser->set_validate(false);
65  parser->set_substitute_entities();
66  try
67  {
68  parser->parse_stream(target);
69  }
70  catch (xmlpp::parse_error & e)
71  {
72  throw XmlStorageErr(
73  std::string("\nXML Parser Errors:\n")+e.what()+"\n");
74  }
75  xmlpp::Document *doc = parser->get_document();
76  CLAM_ASSERT(doc,"No errors but document not loaded!");
77  return doc;
78  }
79 };
80 
81 
82 
83 }
84 #endif//LibXmlDomReader_hxx
85