Imported Upstream version 2.91.2
[platform/upstream/libxml++.git] / libxml++ / validators / xsdvalidator.h
1 /* Copyright (C) 2014 The libxml++ development team
2  *
3  * This file is part of libxml++.
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #ifndef __LIBXMLPP_VALIDATOR_XSDVALIDATOR_H
20 #define __LIBXMLPP_VALIDATOR_XSDVALIDATOR_H
21
22 #include <libxml++/validators/schemavalidatorbase.h>
23 #include <memory> // std::unique_ptr
24
25 namespace Glib
26 {
27 class ustring;
28 }
29
30 namespace xmlpp
31 {
32 class Document;
33 class XsdSchema;
34
35 /** XSD schema validator.
36  * XSD = XML %Schema Definition, a.k.a. XML %Schema or W3C XML %Schema
37  *
38  * @newin{2,38}
39  */
40 class XsdValidator : public SchemaValidatorBase
41 {
42 public:
43   XsdValidator();
44
45   /** Create a validator and parse a schema definition file.
46    * @param filename The URL of the schema.
47    * @throws xmlpp::parse_error
48    */
49   explicit XsdValidator(const std::string& filename);
50
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
54    */
55   explicit XsdValidator(const Document* document);
56
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.
65    */
66   explicit XsdValidator(XsdSchema* schema, bool take_ownership);
67
68   ~XsdValidator() override;
69
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
75    */
76   void parse_file(const std::string& filename) override;
77
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
83    */
84   void parse_memory(const Glib::ustring& contents) override;
85
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
91    */
92   void parse_document(const Document* document) override;
93
94   /** Set a schema.
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.
104    */
105   void set_schema(XsdSchema* schema, bool take_ownership);
106
107   /** Test whether a schema has been parsed.
108    * For instance
109    * @code
110    * if (xsd_validator)
111    *   do_something();
112    * @endcode
113    */
114   explicit operator bool() const noexcept override;
115
116   /** Get the schema.
117    * @returns A pointer to the schema, or <tt>nullptr</tt>.
118    */
119   XsdSchema* get_schema();
120
121   /** Get the schema.
122    * @returns A pointer to the schema, or <tt>nullptr</tt>.
123    */
124   const XsdSchema* get_schema() const;
125
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
130    */
131   void validate(const Document* document) override;
132
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
137    */
138   void validate(const std::string& filename) override;
139
140 protected:
141   void initialize_context() override;
142   void release_underlying() override;
143
144 private:
145   struct Impl;
146   std::unique_ptr<Impl> pimpl_;
147 };
148
149 } // namespace xmlpp
150
151 #endif //__LIBXMLPP_VALIDATOR_XSDVALIDATOR_H