File: schemavalidator.cc

package info (click to toggle)
libxml%2B%2B2.6 2.40.1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 8,048 kB
  • ctags: 1,443
  • sloc: sh: 11,363; cpp: 7,695; xml: 996; perl: 289; makefile: 219
file content (232 lines) | stat: -rw-r--r-- 5,246 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* dtdvalidator.cpp
 * libxml++ and this file are copyright (C) 2000 by Ari Johnson
 * (C) 2002-2004 by the libxml dev team and
 * are covered by the GNU Lesser General Public License, which should be
 * included with libxml++ as the file COPYING.
 */

#include <libxml++config.h> // LIBXMLXX_DISABLE_DEPRECATED

#ifndef LIBXMLXX_DISABLE_DEPRECATED

#include "libxml++/validators/schemavalidator.h"
#include "libxml++/schema.h"

#include <libxml/xmlschemas.h>
#include <libxml/xmlschemastypes.h>

#include <sstream>
#include <iostream>

namespace
{
  // This class simply holds a xmlSchemaParserCtxtPtr and releases it on
  // destruction. This way, we make sure we don't leak it in either case,
  // even when an exception is thrown.
  class XmlSchemaParserContextHolder
  {
  public:
    XmlSchemaParserContextHolder(xmlSchemaParserCtxtPtr ptr): ptr_(ptr) {}
    ~XmlSchemaParserContextHolder() { xmlSchemaFreeParserCtxt(ptr_); }

  private:
    xmlSchemaParserCtxtPtr ptr_;
  };
}

namespace xmlpp
{

SchemaValidator::SchemaValidator()
: schema_(nullptr)
, embbeded_shema_(false)
, ctxt_(nullptr)
{
}

SchemaValidator::SchemaValidator(const Glib::ustring& file)
: schema_(nullptr)
, embbeded_shema_(false)
, ctxt_(nullptr)
{
  parse_file( file );
}

SchemaValidator::SchemaValidator(Document& document)
: schema_(nullptr)
, embbeded_shema_(false)
, ctxt_(nullptr)
{
  parse_document( document );
}

SchemaValidator::SchemaValidator(Schema* schema)
: schema_(schema)
, embbeded_shema_(false)
, ctxt_(nullptr)
{
}

SchemaValidator::~SchemaValidator()
{
  release_underlying();
  Validator::release_underlying();
}

void SchemaValidator::parse_context(_xmlSchemaParserCtxt* context)
{
  if (!context)
    throw parse_error("Could not create schema parser context\n" + format_xml_error());

  release_underlying(); // Free any existing schema.

  auto schema = xmlSchemaParse( context );
  if ( ! schema )
    throw parse_error("Schema could not be parsed\n" + format_xml_error());

  schema->_private = new Schema(schema);

  schema_ = static_cast<Schema*>(schema->_private);
  embbeded_shema_ = true;
}

void SchemaValidator::parse_file(const Glib::ustring& filename)
{
  xmlResetLastError();
  auto ctx = xmlSchemaNewParserCtxt( filename.c_str() );
  XmlSchemaParserContextHolder holder(ctx);
  parse_context( ctx );
}

void SchemaValidator::parse_memory(const Glib::ustring& contents)
{
  xmlResetLastError();
  auto ctx = xmlSchemaNewMemParserCtxt( contents.c_str(), contents.bytes() );
  XmlSchemaParserContextHolder holder(ctx);
  parse_context( ctx );
}

void SchemaValidator::parse_document(Document& document)
{
  xmlResetLastError();
  auto ctx = xmlSchemaNewDocParserCtxt( document.cobj() );
  XmlSchemaParserContextHolder holder(ctx);
  parse_context( ctx );
}

void SchemaValidator::set_schema(Schema* schema)
{
  release_underlying();
  schema_ = schema;
  embbeded_shema_ = false;
}

void SchemaValidator::release_underlying()
{
  if(ctxt_)
  {
    xmlSchemaFreeValidCtxt( ctxt_ );
    ctxt_ = nullptr;
  }
  if(schema_)
  {
    if(embbeded_shema_)
      delete schema_;
    schema_ = nullptr;
  }
}

SchemaValidator::operator bool() const
{
  return schema_ != nullptr;
}

Schema* SchemaValidator::get_schema()
{
  return schema_;
}

const Schema* SchemaValidator::get_schema() const
{
  return schema_;
}

void SchemaValidator::initialize_valid()
{
  xmlSchemaSetValidErrors(ctxt_, &callback_validity_error, &callback_validity_warning, this);
  Validator::initialize_valid();
}


bool SchemaValidator::validate(const Document* doc)
{
  if (!doc)
    throw internal_error("Document pointer cannot be 0");

  if (!schema_)
    throw internal_error("Must have a schema to validate document");

  // A context is required at this stage only
  if (!ctxt_)
    ctxt_ = xmlSchemaNewValidCtxt( schema_->cobj() );

  if(!ctxt_)
  {
    throw internal_error("Couldn't create validating context");
  }

  xmlResetLastError();
  initialize_valid();

  const int res = xmlSchemaValidateDoc( ctxt_, (xmlDoc*)doc->cobj() );

  if(res != 0)
  {
    check_for_exception();

    auto error_str = format_xml_error();
    if (error_str.empty())
      error_str = "Error code from xmlSchemaValidateDoc(): " + Glib::ustring::format(res);
    throw validity_error("Document failed schema validation\n" + error_str);
  }

  return res == 0;
}

bool SchemaValidator::validate(const Glib::ustring& file)
{
  if (file.empty())
    throw internal_error("File path must not be empty");

  if (!schema_)
    throw internal_error("Must have a schema to validate document");

  // A context is required at this stage only
  if (!ctxt_)
    ctxt_ = xmlSchemaNewValidCtxt( schema_->cobj() );

  if(!ctxt_)
  {
    throw internal_error("Couldn't create validating context");
  }

  xmlResetLastError();
  initialize_valid();

  const int res = xmlSchemaValidateFile( ctxt_, file.c_str(), 0 );

  if(res != 0)
  {
    check_for_exception();

    auto error_str = format_xml_error();
    if (error_str.empty())
      error_str = "Error code from xmlSchemaValidateFile(): " + Glib::ustring::format(res);
    throw validity_error("Document failed schema validation\n" + error_str);
  }

  return res == 0;
}

} // namespace xmlpp
#endif // LIBXMLXX_DISABLE_DEPRECATED