7a4c15cd5d65973275b01c04fa6a72e77dfacb1b
[platform/upstream/libxml++.git] / libxml++ / validators / relaxngvalidator.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  * This file was adapted from schemavalidator.h by Fugro Intersite B.V./Tjalling Hattink
19  */
20
21 #ifndef __LIBXMLPP_VALIDATOR_RELAXNGVALIDATOR_H
22 #define __LIBXMLPP_VALIDATOR_RELAXNGVALIDATOR_H
23
24 #include <libxml++/validators/schemavalidatorbase.h>
25 #include <memory> // std::auto_ptr
26
27 namespace Glib
28 {
29 class LIBXMLPP_API ustring;
30 }
31
32 namespace xmlpp
33 {
34 class LIBXMLPP_API Document;
35 class LIBXMLPP_API RelaxNGSchema;
36
37 /** RelaxNG schema validator.
38  * RelaxNG = REgular LAnguage for XML Next Generation
39  *
40  * @newin{2,38}
41  */
42 class LIBXMLPP_API RelaxNGValidator : public SchemaValidatorBase
43 {
44 public:
45   RelaxNGValidator();
46
47   /** Create a validator and parse a schema definition file.
48    * The schema must be defined with XML syntax (.rng file). The compact syntax
49    * (.rnc file) is not supported.
50    * @param filename The URL of the schema.
51    * @throws xmlpp::parse_error
52    */
53   explicit RelaxNGValidator(const Glib::ustring& filename);
54
55   /** Create a validator and parse a schema definition document.
56    * @param document A preparsed document tree, containing the schema definition.
57    * @throws xmlpp::parse_error
58    */
59   explicit RelaxNGValidator(const Document* document);
60
61   /** Create a validator.
62    * @param schema A pointer to the schema to use when validating XML documents.
63    * @param take_ownership If <tt>true</tt>, the validator takes ownership of
64    *        the schema. The caller must not delete it.<br>
65    *        If <tt>false</tt>, the validator does not take ownership of the schema.
66    *        The caller must guarantee that the schema exists as long as the
67    *        validator keeps a pointer to it. The caller is responsible for
68    *        deleting the schema when it's no longer needed.
69    */
70   explicit RelaxNGValidator(RelaxNGSchema* schema, bool take_ownership);
71
72   ~RelaxNGValidator() override;
73
74   //TODO: Remove virtuals when we can break ABI,
75   //or really put these in the base class.
76
77   /** Parse a schema definition file.
78    * The schema must be defined with XML syntax (.rng file). The compact syntax
79    * (.rnc file) is not supported.
80    *
81    * If the validator already contains a schema, that schema is released
82    * (deleted if the validator owns the schema).
83    * @param filename The URL of the schema.
84    * @throws xmlpp::parse_error
85    */
86   void parse_file(const Glib::ustring& filename) override;
87
88   /** Parse a schema definition from a string.
89    * The schema must be defined with XML syntax. The compact syntax is not supported.
90    *
91    * If the validator already contains a schema, that schema is released
92    * (deleted if the validator owns the schema).
93    * @param contents The schema definition as a string.
94    * @throws xmlpp::parse_error
95    */
96   void parse_memory(const Glib::ustring& contents) override;
97
98   /** Parse a schema definition from a document.
99    * If the validator already contains a schema, that schema is released
100    * (deleted if the validator owns the schema).
101    * @param document A preparsed document tree, containing the schema definition.
102    * @throws xmlpp::parse_error
103    */
104   void parse_document(const Document* document) override;
105
106   /** Set a schema.
107    * If the validator already contains a schema, that schema is released
108    * (deleted if the validator owns the schema).
109    * @param schema A pointer to the schema to use when validating XML documents.
110    * @param take_ownership If <tt>true</tt>, the validator takes ownership of
111    *        the schema. The caller must not delete it.<br>
112    *        If <tt>false</tt>, the validator does not take ownership of the schema.
113    *        The caller must guarantee that the schema exists as long as the
114    *        validator keeps a pointer to it. The caller is responsible for
115    *        deleting the schema when it's no longer needed.
116    */
117   void set_schema(RelaxNGSchema* schema, bool take_ownership);
118
119   /** Test whether a schema has been parsed.
120    * For instance
121    * @code
122    * if (relaxng_validator)
123    *   do_something();
124    * @endcode
125    */
126   operator BoolExpr() const override;
127
128   /** Get the schema.
129    * @returns A pointer to the schema, or <tt>nullptr</tt>.
130    */
131   RelaxNGSchema* get_schema();
132
133   /** Get the schema.
134    * @returns A pointer to the schema, or <tt>nullptr</tt>.
135    */
136   const RelaxNGSchema* get_schema() const;
137
138   /** Validate a document, using a previously parsed schema.
139    * @param document Pointer to the document.
140    * @throws xmlpp::internal_error
141    * @throws xmlpp::validity_error
142    */
143   void validate(const Document* document) override;
144
145   /** Validate an XML file, using a previously parsed schema.
146    * @param filename The URL of the XML file.
147    * @throws xmlpp::internal_error
148    * @throws xmlpp::parse_error
149    * @throws xmlpp::validity_error
150    */
151   void validate(const Glib::ustring& filename) override;
152
153 protected:
154   void initialize_valid() override;
155   void release_underlying() override;
156
157 private:
158   struct Impl;
159   std::auto_ptr<Impl> pimpl_;
160 };
161
162 } // namespace xmlpp
163
164 #endif //__LIBXMLPP_VALIDATOR_RELAXNGVALIDATOR_H