aGrUM 3.2.0
a C++ library for (probabilistic) graphical models
XmlDocument.cpp
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41// The only TU in aGrUM (besides ticpp's own sources) allowed to include the
42// vendored parser -- see XmlDocument.h's file comment.
43#include <agrum/base/external/tinyxml/ticpp/ticpp.h>
45
46namespace gum {
47
48 XmlException::XmlException(std::string message) : _message_(std::move(message)) {}
49
50 const char* XmlException::what() const noexcept { return _message_.c_str(); }
51
52 XmlElement XmlElement::firstChildElement(const std::string& tag) const {
53 auto* self = static_cast< ticpp::Element* >(_handle_);
54 try {
55 return XmlElement(self->FirstChildElement(tag));
56 } catch (ticpp::Exception& e) { throw XmlException(e.what()); }
57 }
58
59 XmlElement XmlElement::firstChildElement(const std::string& tag, bool throwIfNotFound) const {
60 auto* self = static_cast< ticpp::Element* >(_handle_);
61 try {
62 return XmlElement(self->FirstChildElement(tag, throwIfNotFound));
63 } catch (ticpp::Exception& e) { throw XmlException(e.what()); }
64 }
65
66 std::vector< XmlElement > XmlElement::children(const std::string& tag) const {
67 std::vector< XmlElement > result;
68 if (_handle_ == nullptr) { return result; }
69 auto* parent = static_cast< ticpp::Element* >(_handle_);
70 ticpp::Iterator< ticpp::Element > it(tag);
71 for (it = it.begin(parent); it != it.end(); ++it) {
72 result.push_back(XmlElement(it.Get()));
73 }
74 return result;
75 }
76
77 std::string XmlElement::textOrDefault(const std::string& defaultValue) const {
78 auto* self = static_cast< ticpp::Element* >(_handle_);
79 return self->GetTextOrDefault(defaultValue);
80 }
81
82 std::string XmlElement::attribute(const std::string& name) const {
83 auto* self = static_cast< ticpp::Element* >(_handle_);
84 try {
85 return self->GetAttribute(name);
86 } catch (ticpp::Exception& e) { throw XmlException(e.what()); }
87 }
88
89 bool
90 XmlElement::attribute(const std::string& name, std::string* out, bool throwIfNotFound) const {
91 auto* self = static_cast< ticpp::Element* >(_handle_);
92 // ticpp's GetAttribute(name, out, false) returns silently (no exception,
93 // *out untouched) when the attribute is missing, so the try/catch below
94 // never fires on that path -- HasAttribute is the only reliable signal.
95 const bool found = self->HasAttribute(name);
96 try {
97 self->GetAttribute(name, out, throwIfNotFound);
98 } catch (ticpp::Exception& e) { throw XmlException(e.what()); }
99 return found;
100 }
101
103 ticpp::Document doc;
104
105 Impl() = default;
106
107 explicit Impl(const std::string& filePath) : doc(filePath) {}
108 };
109
110 XmlDocument::XmlDocument() : _pimpl_(std::make_unique< Impl >()) {}
111
112 XmlDocument::XmlDocument(const std::string& filePath) :
113 _pimpl_(std::make_unique< Impl >(filePath)) {}
114
115 XmlDocument::~XmlDocument() = default;
116 XmlDocument::XmlDocument(XmlDocument&&) noexcept = default;
117 XmlDocument& XmlDocument::operator=(XmlDocument&&) noexcept = default;
118
119 void XmlDocument::parse(const std::string& content) {
120 try {
121 _pimpl_->doc.Parse(content);
122 } catch (ticpp::Exception& e) { throw XmlException(e.what()); }
123 }
124
126 try {
127 _pimpl_->doc.LoadFile();
128 } catch (ticpp::Exception& e) { throw XmlException(e.what()); }
129 }
130
131 bool XmlDocument::noChildren() const { return _pimpl_->doc.NoChildren(); }
132
133 XmlElement XmlDocument::firstChildElement(const std::string& tag) const {
134 try {
135 return XmlElement(_pimpl_->doc.FirstChildElement(tag));
136 } catch (ticpp::Exception& e) { throw XmlException(e.what()); }
137 }
138
139} /* namespace gum */
Opaque facade over the vendored ticpp/tinyxml XML parser.
An XML document, opaque over ticpp::Document.
XmlElement firstChildElement(const std::string &tag) const
std::unique_ptr< Impl > _pimpl_
void loadFile()
Loads the file passed to the constructor.
void parse(const std::string &content)
bool noChildren() const
A non-owning handle to an XML element, opaque over ticpp::Element.
Definition XmlDocument.h:89
std::vector< XmlElement > children(const std::string &tag) const
All direct children with this tag, in document order.
void * _handle_
opaque ticpp::Element*, never exposed
std::string textOrDefault(const std::string &defaultValue) const
XmlElement()=default
std::string attribute(const std::string &name) const
XmlElement firstChildElement(const std::string &tag) const
std::string _message_
Definition XmlDocument.h:79
XmlException(std::string message)
const char * what() const noexcept override
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
const float MultiDimFunctionGraph< float >::defaultValue
STL namespace.
Impl(const std::string &filePath)