2
* libxml++ and this file are copyright (C) 2000 by Ari Johnson
3
* (C) 2002-2004 by the libxml dev team and
4
* are covered by the GNU Lesser General Public License, which should be
5
* included with libxml++ as the file COPYING.
8
#include "libxml++/validators/dtdvalidator.h"
9
#include "libxml++/dtd.h"
10
#include "libxml++/nodes/element.h"
11
#include "libxml++/nodes/textnode.h"
12
#include "libxml++/nodes/commentnode.h"
13
#include "libxml++/keepblanks.h"
14
#include "libxml++/exceptions/internal_error.h"
15
#include "libxml++/io/istreamparserinputbuffer.h"
17
#include <libxml/parserInternals.h>//For xmlCreateFileParserCtxt().
25
DtdValidator::DtdValidator()
30
DtdValidator::DtdValidator(const Glib::ustring& file)
33
parse_subset("",file);
36
DtdValidator::DtdValidator(const Glib::ustring& external,const Glib::ustring& system)
39
parse_subset(external,system);
42
DtdValidator::~DtdValidator()
45
Validator::release_underlying();
48
void DtdValidator::parse_file(const Glib::ustring& filename)
50
parse_subset("",filename);
53
void DtdValidator::parse_subset(const Glib::ustring& external,const Glib::ustring& system)
55
release_underlying(); // Free any existing dtd.
57
xmlDtd* dtd = xmlParseDTD(
58
external.empty() ? 0 : (const xmlChar *)external.c_str(),
59
system.empty() ? 0 : (const xmlChar *)system.c_str());
62
throw parse_error("Dtd could not be parsed");
64
dtd_ = static_cast<Dtd*>(dtd->_private);
67
void DtdValidator::parse_memory(const Glib::ustring& contents)
69
// Prepare an istream with buffer
70
std::istringstream is( contents );
75
void DtdValidator::parse_stream(std::istream& in)
77
release_underlying(); //Free any existing document.
79
IStreamParserInputBuffer ibuff( in );
81
xmlDtd* dtd = xmlIOParseDTD( 0, ibuff.cobj(), XML_CHAR_ENCODING_UTF8 );
84
throw parse_error("Dtd could not be parsed");
86
dtd_ = static_cast<Dtd*>(dtd->_private);
89
void DtdValidator::release_underlying()
93
xmlFreeDtd(dtd_->cobj());
98
DtdValidator::operator bool() const
103
Dtd* DtdValidator::get_dtd()
108
const Dtd* DtdValidator::get_dtd() const
113
bool DtdValidator::validate(const Document* doc)
115
// A context is required at this stage only
117
valid_ = xmlNewValidCtxt();
121
throw internal_error("Couldn't create parsing context");
125
throw internal_error("Document pointer cannot be 0");
129
bool res = (bool)xmlValidateDtd( valid_, (xmlDoc*)doc->cobj(), dtd_->cobj() );
133
check_for_exception();
134
throw validity_error("Document failed Dtd validation");