~ubuntu-branches/ubuntu/precise/libxml++2.6/precise

« back to all changes in this revision

Viewing changes to libxml++/validators/dtdvalidator.cc

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-03-13 15:46:33 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050313154633-iubyqdtu6y3p8915
Tags: 2.10.0-0ubuntu2
added doxygen to the build-depends

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* dtdvalidator.cpp
 
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.
 
6
 */
 
7
 
 
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"
 
16
 
 
17
#include <libxml/parserInternals.h>//For xmlCreateFileParserCtxt().
 
18
 
 
19
#include <sstream>
 
20
#include <iostream>
 
21
 
 
22
namespace xmlpp
 
23
{
 
24
 
 
25
DtdValidator::DtdValidator()
 
26
: dtd_(0)
 
27
{
 
28
}
 
29
 
 
30
DtdValidator::DtdValidator(const Glib::ustring& file)
 
31
: dtd_(0)
 
32
{
 
33
  parse_subset("",file);
 
34
}
 
35
 
 
36
DtdValidator::DtdValidator(const Glib::ustring& external,const Glib::ustring& system)
 
37
: dtd_(0)
 
38
{
 
39
  parse_subset(external,system);
 
40
}
 
41
 
 
42
DtdValidator::~DtdValidator()
 
43
{
 
44
  release_underlying();
 
45
  Validator::release_underlying();
 
46
}
 
47
 
 
48
void DtdValidator::parse_file(const Glib::ustring& filename)
 
49
{
 
50
  parse_subset("",filename);
 
51
}
 
52
 
 
53
void DtdValidator::parse_subset(const Glib::ustring& external,const Glib::ustring& system)
 
54
{
 
55
  release_underlying(); // Free any existing dtd.
 
56
 
 
57
  xmlDtd* dtd = xmlParseDTD(
 
58
    external.empty() ? 0 : (const xmlChar *)external.c_str(),
 
59
    system.empty() ? 0 : (const xmlChar *)system.c_str());
 
60
 
 
61
  if ( ! dtd )
 
62
     throw parse_error("Dtd could not be parsed");
 
63
 
 
64
  dtd_ = static_cast<Dtd*>(dtd->_private);
 
65
}
 
66
 
 
67
void DtdValidator::parse_memory(const Glib::ustring& contents)
 
68
{
 
69
  // Prepare an istream with buffer
 
70
  std::istringstream is( contents );
 
71
 
 
72
  parse_stream( is );
 
73
}
 
74
 
 
75
void DtdValidator::parse_stream(std::istream& in)
 
76
{
 
77
  release_underlying(); //Free any existing document.
 
78
 
 
79
  IStreamParserInputBuffer ibuff( in );
 
80
 
 
81
  xmlDtd* dtd = xmlIOParseDTD( 0, ibuff.cobj(), XML_CHAR_ENCODING_UTF8 );
 
82
 
 
83
  if ( ! dtd )
 
84
     throw parse_error("Dtd could not be parsed");
 
85
 
 
86
  dtd_ = static_cast<Dtd*>(dtd->_private);
 
87
}
 
88
 
 
89
void DtdValidator::release_underlying()
 
90
{
 
91
  if(dtd_)
 
92
  {
 
93
    xmlFreeDtd(dtd_->cobj());
 
94
    dtd_ = 0;
 
95
  }
 
96
}
 
97
 
 
98
DtdValidator::operator bool() const
 
99
{
 
100
  return dtd_ != 0;
 
101
}
 
102
 
 
103
Dtd* DtdValidator::get_dtd()
 
104
{
 
105
  return dtd_;
 
106
}
 
107
 
 
108
const Dtd* DtdValidator::get_dtd() const
 
109
{
 
110
  return dtd_;
 
111
}
 
112
 
 
113
bool DtdValidator::validate(const Document* doc)
 
114
{
 
115
  // A context is required at this stage only
 
116
  if (!valid_)
 
117
    valid_ = xmlNewValidCtxt();
 
118
 
 
119
  if(!valid_)
 
120
  {
 
121
    throw internal_error("Couldn't create parsing context");
 
122
  }
 
123
 
 
124
  if (!doc)
 
125
    throw internal_error("Document pointer cannot be 0");
 
126
 
 
127
  initialize_valid();
 
128
 
 
129
  bool res = (bool)xmlValidateDtd( valid_, (xmlDoc*)doc->cobj(), dtd_->cobj() );
 
130
 
 
131
  if(res == 0)
 
132
  {
 
133
    check_for_exception();
 
134
    throw validity_error("Document failed Dtd validation");
 
135
  }
 
136
 
 
137
  return res;
 
138
}
 
139
 
 
140
} // namespace xmlpp
 
141