|
| 1 | +/* |
| 2 | + * libdigidocpp |
| 3 | + * |
| 4 | + * This library is free software; you can redistribute it and/or |
| 5 | + * modify it under the terms of the GNU Lesser General Public |
| 6 | + * License as published by the Free Software Foundation; either |
| 7 | + * version 2.1 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This library is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + * Lesser General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Lesser General Public |
| 15 | + * License along with this library; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + * |
| 18 | + */ |
| 19 | + |
| 20 | +#pragma once |
| 21 | + |
| 22 | +#include "util/log.h" |
| 23 | + |
| 24 | +#include <libxml/parser.h> |
| 25 | +#include <libxml/xmlschemas.h> |
| 26 | + |
| 27 | +#include <memory> |
| 28 | + |
| 29 | +namespace digidoc { |
| 30 | + |
| 31 | +template<typename> struct unique_xml; |
| 32 | +template<class T> |
| 33 | +struct unique_xml<void(T *)> |
| 34 | +{ |
| 35 | + using type = std::unique_ptr<T,void(*)(T *)>; |
| 36 | +}; |
| 37 | + |
| 38 | +template<typename T> |
| 39 | +using unique_xml_t = typename unique_xml<T>::type; |
| 40 | + |
| 41 | +template<class T, typename D> |
| 42 | +constexpr std::unique_ptr<T, D> make_unique_ptr(T *p, D d) noexcept |
| 43 | +{ |
| 44 | + return {p, d}; |
| 45 | +} |
| 46 | + |
| 47 | +template<class T> |
| 48 | +struct XMLElem |
| 49 | +{ |
| 50 | + using value_type = T; |
| 51 | + using pointer = value_type*; |
| 52 | + using sv = std::string_view; |
| 53 | + using pcxmlChar = const xmlChar *; |
| 54 | + |
| 55 | + template<class C> |
| 56 | + constexpr static C find(C n, xmlElementType type) noexcept |
| 57 | + { |
| 58 | + for(; n; n = n->next) |
| 59 | + if(n->type == type) |
| 60 | + return n; |
| 61 | + return {}; |
| 62 | + } |
| 63 | + |
| 64 | + constexpr static sv to_string_view(const xmlChar *str) noexcept |
| 65 | + { |
| 66 | + return str ? sv(sv::const_pointer(str)) : sv(); |
| 67 | + } |
| 68 | + |
| 69 | + constexpr sv name() const noexcept |
| 70 | + { |
| 71 | + return to_string_view(d ? d->name : nullptr); |
| 72 | + } |
| 73 | + |
| 74 | + constexpr sv ns() const noexcept |
| 75 | + { |
| 76 | + return to_string_view(d && d->ns ? d->ns->href : nullptr); |
| 77 | + } |
| 78 | + |
| 79 | + constexpr operator bool() const noexcept |
| 80 | + { |
| 81 | + return bool(d); |
| 82 | + } |
| 83 | + |
| 84 | + constexpr auto& operator++() noexcept |
| 85 | + { |
| 86 | + d = d ? find(d->next, d->type) : nullptr; |
| 87 | + return *this; |
| 88 | + } |
| 89 | + |
| 90 | + constexpr operator sv() const noexcept |
| 91 | + { |
| 92 | + auto text = find(d ? d->children : nullptr, XML_TEXT_NODE); |
| 93 | + return to_string_view(text ? text->content : nullptr); |
| 94 | + } |
| 95 | + |
| 96 | + pointer d{}; |
| 97 | +}; |
| 98 | + |
| 99 | +struct XMLNode: public XMLElem<xmlNode> |
| 100 | +{ |
| 101 | + struct Name_NS { |
| 102 | + sv name, ns; |
| 103 | + }; |
| 104 | + |
| 105 | + struct iterator: XMLElem<xmlNode> |
| 106 | + { |
| 107 | + using iterator_category = std::forward_iterator_tag; |
| 108 | + using difference_type = std::ptrdiff_t; |
| 109 | + |
| 110 | + constexpr XMLNode operator*() const noexcept { return {d}; } |
| 111 | + }; |
| 112 | + |
| 113 | + constexpr iterator begin() const noexcept |
| 114 | + { |
| 115 | + return {find(d ? d->children : nullptr, XML_ELEMENT_NODE)}; |
| 116 | + } |
| 117 | + |
| 118 | + constexpr iterator end() const noexcept |
| 119 | + { |
| 120 | + return {}; |
| 121 | + } |
| 122 | + |
| 123 | + XMLNode addChild(sv name, sv ns = {}) const noexcept |
| 124 | + { |
| 125 | + return {xmlNewChild(d, searchNS(ns), pcxmlChar(name.data()), nullptr)}; |
| 126 | + } |
| 127 | + |
| 128 | + xmlNsPtr addNS(sv href, sv prefix = {}) const noexcept |
| 129 | + { |
| 130 | + return xmlNewNs(d, pcxmlChar(href.data()), prefix.empty() ? nullptr : pcxmlChar(prefix.data())); |
| 131 | + } |
| 132 | + |
| 133 | + xmlNsPtr searchNS(sv ns) const noexcept |
| 134 | + { |
| 135 | + return xmlSearchNsByHref(nullptr, d, ns.empty() ? nullptr : pcxmlChar(ns.data())); |
| 136 | + } |
| 137 | + |
| 138 | + constexpr sv property(sv name, sv ns = {}) const noexcept |
| 139 | + { |
| 140 | + for(XMLElem<xmlAttr> a{d ? d->properties : nullptr}; a; ++a) |
| 141 | + { |
| 142 | + if(a.name() == name && a.ns() == ns) |
| 143 | + return a; |
| 144 | + } |
| 145 | + return {}; |
| 146 | + } |
| 147 | + |
| 148 | + void setProperty(sv name, sv value, xmlNsPtr ns = {}) const noexcept |
| 149 | + { |
| 150 | + xmlSetNsProp(d, ns, pcxmlChar(name.data()), pcxmlChar(value.data())); |
| 151 | + } |
| 152 | + |
| 153 | + static iterator erase(iterator pos) noexcept |
| 154 | + { |
| 155 | + iterator next = pos; |
| 156 | + ++next; |
| 157 | + xmlUnlinkNode(pos.d); |
| 158 | + xmlFreeNode(pos.d); |
| 159 | + return next; |
| 160 | + } |
| 161 | + |
| 162 | + XMLNode& operator=(sv text) noexcept |
| 163 | + { |
| 164 | + if(!d) |
| 165 | + return *this; |
| 166 | + xmlChar *content = xmlEncodeSpecialChars(d->doc, pcxmlChar(text.data())); |
| 167 | + xmlNodeSetContent(d, content); |
| 168 | + xmlFree(content); |
| 169 | + return *this; |
| 170 | + } |
| 171 | +}; |
| 172 | + |
| 173 | +struct XMLDocument: public unique_xml_t<decltype(xmlFreeDoc)>, public XMLNode |
| 174 | +{ |
| 175 | + using XMLNode::operator bool; |
| 176 | + |
| 177 | + XMLDocument(element_type *ptr, std::string_view _name = {}, std::string_view _ns = {}) noexcept |
| 178 | + : std::unique_ptr<element_type, deleter_type>(ptr, xmlFreeDoc) |
| 179 | + , XMLNode{xmlDocGetRootElement(get())} |
| 180 | + { |
| 181 | + if(d && !_name.empty() && _name != name() && !_ns.empty() && _ns != ns()) |
| 182 | + d = {}; |
| 183 | + } |
| 184 | + |
| 185 | + XMLDocument(std::string_view path, std::string_view name = {}) noexcept |
| 186 | + : XMLDocument(xmlParseFile(path.data()), name) |
| 187 | + {} |
| 188 | + |
| 189 | + static XMLDocument create(std::string_view name = {}, std::string_view href = {}, std::string_view prefix = {}) noexcept |
| 190 | + { |
| 191 | + XMLDocument doc(xmlNewDoc(nullptr)); |
| 192 | + if(!name.empty()) |
| 193 | + { |
| 194 | + doc.d = xmlNewNode(nullptr, pcxmlChar(name.data())); |
| 195 | + if(!href.empty()) |
| 196 | + xmlSetNs(doc.d, doc.addNS(href, prefix)); |
| 197 | + xmlDocSetRootElement(doc.get(), doc.d); |
| 198 | + } |
| 199 | + return doc; |
| 200 | + } |
| 201 | + |
| 202 | + bool save(std::string_view path) const noexcept |
| 203 | + { |
| 204 | + return xmlSaveFormatFileEnc(path.data(), get(), "UTF-8", 1) > 0; |
| 205 | + } |
| 206 | + |
| 207 | + bool validateSchema(const std::string &schemaPath) const noexcept |
| 208 | + { |
| 209 | + auto parser = make_unique_ptr(xmlSchemaNewParserCtxt(schemaPath.c_str()), xmlSchemaFreeParserCtxt); |
| 210 | + if(!parser) |
| 211 | + return false; |
| 212 | + xmlSchemaSetParserErrors(parser.get(), schemaValidationError, schemaValidationWarning, nullptr); |
| 213 | + auto schema = make_unique_ptr(xmlSchemaParse(parser.get()), xmlSchemaFree); |
| 214 | + if(!schema) |
| 215 | + return false; |
| 216 | + auto validate = make_unique_ptr(xmlSchemaNewValidCtxt(schema.get()), xmlSchemaFreeValidCtxt); |
| 217 | + if(!validate) |
| 218 | + return false; |
| 219 | + xmlSchemaSetValidErrors(validate.get(), schemaValidationError, schemaValidationWarning, nullptr); |
| 220 | + return xmlSchemaValidateDoc(validate.get(), get()) == 0; |
| 221 | + } |
| 222 | + |
| 223 | + static void schemaValidationError(void */*ctx*/, const char *msg, ...) noexcept |
| 224 | + { |
| 225 | + va_list args{}; |
| 226 | + va_start(args, msg); |
| 227 | + std::string m = Log::formatArgList(msg, args); |
| 228 | + va_end(args); |
| 229 | + ERR("Schema validation error: %s", m.c_str()); |
| 230 | + } |
| 231 | + |
| 232 | + static void schemaValidationWarning(void */*ctx*/, const char *msg, ...) noexcept |
| 233 | + { |
| 234 | + va_list args{}; |
| 235 | + va_start(args, msg); |
| 236 | + std::string m = Log::formatArgList(msg, args); |
| 237 | + va_end(args); |
| 238 | + WARN("Schema validation warning: %s", m.c_str()); |
| 239 | + } |
| 240 | +}; |
| 241 | + |
| 242 | +} |
0 commit comments