2 * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and
3 * are covered by the GNU Lesser General Public License, which should be
4 * included with libxml++ as the file COPYING.
7 #include <libxml++/dtd.h>
8 #include <libxml++/exceptions/parse_error.h>
9 #include <libxml++/io/istreamparserinputbuffer.h>
11 #include <libxml/tree.h>
18 Impl() : dtd(nullptr), is_dtd_owner(false) {}
29 Dtd::Dtd(_xmlDtd* dtd, bool take_ownership)
35 pimpl_->dtd->_private = this;
36 pimpl_->is_dtd_owner = take_ownership;
40 Dtd::Dtd(const std::string& filename)
43 parse_subset("", filename);
46 Dtd::Dtd(const Glib::ustring& external, const Glib::ustring& system)
49 parse_subset(external, system);
57 void Dtd::parse_file(const std::string& filename)
59 parse_subset("", filename);
62 void Dtd::parse_subset(const Glib::ustring& external, const Glib::ustring& system)
64 release_underlying(); // Free any existing dtd.
67 auto dtd = xmlParseDTD(
68 external.empty() ? nullptr : (const xmlChar*)external.c_str(),
69 system.empty() ? nullptr : (const xmlChar*)system.c_str());
73 throw parse_error("Dtd could not be parsed.\n" + format_xml_error());
77 pimpl_->dtd->_private = this;
78 pimpl_->is_dtd_owner = true;
81 void Dtd::parse_memory(const Glib::ustring& contents)
83 // Prepare an istream with buffer
84 std::istringstream is(contents);
89 void Dtd::parse_stream(std::istream& in)
91 release_underlying(); // Free any existing dtd.
94 IStreamParserInputBuffer ibuff(in);
96 auto dtd = xmlIOParseDTD(nullptr, ibuff.cobj(), XML_CHAR_ENCODING_UTF8);
100 throw parse_error("Dtd could not be parsed.\n" + format_xml_error());
104 pimpl_->dtd->_private = this;
105 pimpl_->is_dtd_owner = true;
108 Glib::ustring Dtd::get_name() const
110 return (pimpl_->dtd && pimpl_->dtd->name) ? (const char*)pimpl_->dtd->name : "";
113 Glib::ustring Dtd::get_external_id() const
115 return (pimpl_->dtd && pimpl_->dtd->ExternalID) ? (const char*)pimpl_->dtd->ExternalID : "";
118 Glib::ustring Dtd::get_system_id() const
120 return (pimpl_->dtd && pimpl_->dtd->SystemID) ? (const char*)pimpl_->dtd->SystemID : "";
128 const _xmlDtd* Dtd::cobj() const
133 void Dtd::release_underlying()
137 pimpl_->dtd->_private = nullptr;
138 if (pimpl_->is_dtd_owner)
140 xmlFreeDtd(pimpl_->dtd);
141 pimpl_->is_dtd_owner = false;
143 pimpl_->dtd = nullptr;