Imported Upstream version 2.91.2
[platform/upstream/libxml++.git] / libxml++ / validators / dtdvalidator.h
1 /* dtdvalidator.h
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 #ifndef __LIBXMLPP_VALIDATORS_DTDVALIDATOR_H
9 #define __LIBXMLPP_VALIDATORS_DTDVALIDATOR_H
10
11 #include <libxml++/validators/validator.h>
12 #include <libxml++/dtd.h>
13 #include <libxml++/document.h>
14
15 namespace xmlpp {
16
17 /** XML DTD validator.
18  * DTD = Document Type Definition
19  */
20 class DtdValidator : public Validator
21 {
22 public:
23   DtdValidator();
24
25   /** Create a validator and parse an external subset (DTD file) immediately.
26    * @param filename The URL of the DTD.
27    * @throws xmlpp::parse_error
28    */
29   explicit DtdValidator(const std::string& filename);
30
31   /** Create a validator and parse an external subset (DTD file) immediately.
32    * @param external The external ID of the DTD.
33    * @param system The URL of the DTD.
34    * @throws xmlpp::parse_error
35    */
36   explicit DtdValidator(const Glib::ustring& external, const Glib::ustring& system);
37
38   /** Create a validator.
39    *
40    * @newin{3,0}
41    *
42    * @param dtd A pointer to the DTD to use when validating XML documents.
43    * @param take_ownership If <tt>true</tt>, the validator takes ownership of
44    *        the DTD. The caller must not delete it.<br>
45    *        If <tt>false</tt>, the validator does not take ownership of the DTD.
46    *        The caller must guarantee that the DTD exists as long as the
47    *        validator keeps a pointer to it. The caller is responsible for
48    *        deleting the DTD when it's no longer needed.
49    */
50   explicit DtdValidator(Dtd* dtd, bool take_ownership);
51
52   ~DtdValidator() override;
53
54   /** Parse an external subset (DTD file).
55    * If the validator already contains a DTD, that DTD is deleted.
56    * @param filename The URL of the DTD.
57    * @throws xmlpp::parse_error
58    */
59   void parse_file(const std::string& filename) override;
60
61   /** Parse an external subset (DTD file).
62    * If the validator already contains a DTD, that DTD is deleted.
63    * @param external The external ID of the DTD.
64    * @param system The URL of the DTD.
65    * @throws xmlpp::parse_error
66    */
67   void parse_subset(const Glib::ustring& external, const Glib::ustring& system);
68
69   /** Parse a DTD from a string.
70    * If the validator already contains a DTD, that DTD is deleted.
71    * @param contents The DTD as a string.
72    * @throws xmlpp::parse_error
73    */
74   void parse_memory(const Glib::ustring& contents) override;
75
76   /** Parse a DTD from a stream.
77    * If the validator already contains a DTD, that DTD is deleted.
78    * @param in The stream.
79    * @throws xmlpp::parse_error
80    */
81   void parse_stream(std::istream& in);
82
83   /** Set a DTD.
84    * If the validator already contains a DTD, that DTD is released
85    * (deleted if the validator owns the DTD).
86    * @param dtd A pointer to the DTD to use when validating XML documents.
87    * @param take_ownership If <tt>true</tt>, the validator takes ownership of
88    *        the DTD. The caller must not delete it.<br>
89    *        If <tt>false</tt>, the validator does not take ownership of the DTD.
90    *        The caller must guarantee that the DTD exists as long as the
91    *        validator keeps a pointer to it. The caller is responsible for
92    *        deleting the DTD when it's no longer needed.
93    */
94   void set_dtd(Dtd* dtd, bool take_ownership);
95
96   /** Test whether a DTD has been parsed.
97    * For instance
98    * @code
99    * if (validator)
100    *   do_something();
101    * @endcode
102    */
103   explicit operator bool() const noexcept override;
104
105   /** Get the parsed DTD.
106    * @returns A pointer to the parsed DTD, or <tt>nullptr</tt>.
107    */
108   Dtd* get_dtd();
109
110   /** Get the parsed DTD.
111    * @returns A pointer to the parsed DTD, or <tt>nullptr</tt>.
112    */
113   const Dtd* get_dtd() const;
114
115   /** Validate a document, using a previously parsed DTD.
116    * The internal subset (if present) is de-coupled (i.e. not used),
117    * which could give problems if ID or IDREF is present.
118    * @param document Pointer to the document.
119    * @throws xmlpp::internal_error
120    * @throws xmlpp::validity_error
121    */
122   void validate(const Document* document) override;
123
124 protected:
125   void initialize_context() override;
126   void release_underlying() override;
127
128 private:
129   struct Impl;
130   std::unique_ptr<Impl> pimpl_;
131 };
132
133 } // namespace xmlpp
134
135 #endif //__LIBXMLPP_VALIDATORS_DTDVALIDATOR_H