1 /* Copyright (C) 2014 The libxml++ development team
3 * This file is part of libxml++.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 #ifndef __LIBXMLPP_VALIDATOR_XSDVALIDATOR_H
20 #define __LIBXMLPP_VALIDATOR_XSDVALIDATOR_H
22 #include <libxml++/validators/schemavalidatorbase.h>
23 #include <memory> // std::unique_ptr
35 /** XSD schema validator.
36 * XSD = XML %Schema Definition, a.k.a. XML %Schema or W3C XML %Schema
40 class XsdValidator : public SchemaValidatorBase
45 /** Create a validator and parse a schema definition file.
46 * @param filename The URL of the schema.
47 * @throws xmlpp::parse_error
49 explicit XsdValidator(const std::string& filename);
51 /** Create a validator and parse a schema definition document.
52 * @param document A preparsed document tree, containing the schema definition.
53 * @throws xmlpp::parse_error
55 explicit XsdValidator(const Document* document);
57 /** Create a validator.
58 * @param schema A pointer to the schema to use when validating XML documents.
59 * @param take_ownership If <tt>true</tt>, the validator takes ownership of
60 * the schema. The caller must not delete it.<br>
61 * If <tt>false</tt>, the validator does not take ownership of the schema.
62 * The caller must guarantee that the schema exists as long as the
63 * validator keeps a pointer to it. The caller is responsible for
64 * deleting the schema when it's no longer needed.
66 explicit XsdValidator(XsdSchema* schema, bool take_ownership);
68 ~XsdValidator() override;
70 /** Parse a schema definition file.
71 * If the validator already contains a schema, that schema is released
72 * (deleted if the validator owns the schema).
73 * @param filename The URL of the schema.
74 * @throws xmlpp::parse_error
76 void parse_file(const std::string& filename) override;
78 /** Parse a schema definition from a string.
79 * If the validator already contains a schema, that schema is released
80 * (deleted if the validator owns the schema).
81 * @param contents The schema definition as a string.
82 * @throws xmlpp::parse_error
84 void parse_memory(const Glib::ustring& contents) override;
86 /** Parse a schema definition from a document.
87 * If the validator already contains a schema, that schema is released
88 * (deleted if the validator owns the schema).
89 * @param document A preparsed document tree, containing the schema definition.
90 * @throws xmlpp::parse_error
92 void parse_document(const Document* document) override;
95 * If the validator already contains a schema, that schema is released
96 * (deleted if the validator owns the schema).
97 * @param schema A pointer to the schema to use when validating XML documents.
98 * @param take_ownership If <tt>true</tt>, the validator takes ownership of
99 * the schema. The caller must not delete it.<br>
100 * If <tt>false</tt>, the validator does not take ownership of the schema.
101 * The caller must guarantee that the schema exists as long as the
102 * validator keeps a pointer to it. The caller is responsible for
103 * deleting the schema when it's no longer needed.
105 void set_schema(XsdSchema* schema, bool take_ownership);
107 /** Test whether a schema has been parsed.
114 explicit operator bool() const noexcept override;
117 * @returns A pointer to the schema, or <tt>nullptr</tt>.
119 XsdSchema* get_schema();
122 * @returns A pointer to the schema, or <tt>nullptr</tt>.
124 const XsdSchema* get_schema() const;
126 /** Validate a document, using a previously parsed schema.
127 * @param document Pointer to the document.
128 * @throws xmlpp::internal_error
129 * @throws xmlpp::validity_error
131 void validate(const Document* document) override;
133 /** Validate an XML file, using a previously parsed schema.
134 * @param filename The URL of the XML file.
135 * @throws xmlpp::internal_error
136 * @throws xmlpp::validity_error
138 void validate(const std::string& filename) override;
141 void initialize_context() override;
142 void release_underlying() override;
146 std::unique_ptr<Impl> pimpl_;
151 #endif //__LIBXMLPP_VALIDATOR_XSDVALIDATOR_H