aGrUM
3.3.2
a C++ library for (probabilistic) graphical models
Toggle main menu visibility
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>
44
#include <
agrum/BN/io/xml/XmlDocument.h
>
45
46
namespace
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
(
void
* ticppElement) noexcept :
_handle_
(ticppElement) {}
53
54
bool
XmlElement::isNull
() const noexcept {
return
_handle_
==
nullptr
; }
55
56
XmlElement
XmlElement::firstChildElement
(
const
std::string& tag)
const
{
57
auto
* self =
static_cast<
ticpp::Element*
>
(
_handle_
);
58
try
{
59
return
XmlElement
(self->FirstChildElement(tag));
60
}
catch
(ticpp::Exception& e) {
throw
XmlException
(e.what()); }
61
}
62
63
XmlElement
XmlElement::firstChildElement
(
const
std::string& tag,
bool
throwIfNotFound)
const
{
64
auto
* self =
static_cast<
ticpp::Element*
>
(
_handle_
);
65
try
{
66
return
XmlElement
(self->FirstChildElement(tag, throwIfNotFound));
67
}
catch
(ticpp::Exception& e) {
throw
XmlException
(e.what()); }
68
}
69
70
std::vector< XmlElement >
XmlElement::children
(
const
std::string& tag)
const
{
71
std::vector< XmlElement > result;
72
if
(
_handle_
==
nullptr
) {
return
result; }
73
auto
* parent =
static_cast<
ticpp::Element*
>
(
_handle_
);
74
ticpp::Iterator< ticpp::Element > it(tag);
75
for
(it = it.begin(parent); it != it.end(); ++it) {
76
result.push_back(
XmlElement
(it.Get()));
77
}
78
return
result;
79
}
80
81
std::string
XmlElement::textOrDefault
(
const
std::string&
defaultValue
)
const
{
82
auto
* self =
static_cast<
ticpp::Element*
>
(
_handle_
);
83
return
self->GetTextOrDefault(
defaultValue
);
84
}
85
86
std::string
XmlElement::attribute
(
const
std::string& name)
const
{
87
auto
* self =
static_cast<
ticpp::Element*
>
(
_handle_
);
88
try
{
89
return
self->GetAttribute(name);
90
}
catch
(ticpp::Exception& e) {
throw
XmlException
(e.what()); }
91
}
92
93
bool
94
XmlElement::attribute
(
const
std::string& name, std::string* out,
bool
throwIfNotFound)
const
{
95
auto
* self =
static_cast<
ticpp::Element*
>
(
_handle_
);
96
// ticpp's GetAttribute(name, out, false) returns silently (no exception,
97
// *out untouched) when the attribute is missing, so the try/catch below
98
// never fires on that path -- HasAttribute is the only reliable signal.
99
const
bool
found = self->HasAttribute(name);
100
try
{
101
self->GetAttribute(name, out, throwIfNotFound);
102
}
catch
(ticpp::Exception& e) {
throw
XmlException
(e.what()); }
103
return
found;
104
}
105
106
struct
XmlDocument::Impl
{
107
ticpp::Document
doc
;
108
109
Impl
() =
default
;
110
111
explicit
Impl
(
const
std::string& filePath) :
doc
(filePath) {}
112
};
113
114
XmlDocument::XmlDocument
() :
_pimpl_
(
std
::make_unique<
Impl
>()) {}
115
116
XmlDocument::XmlDocument
(
const
std::string& filePath) :
117
_pimpl_
(
std
::make_unique<
Impl
>(filePath)) {}
118
119
XmlDocument::~XmlDocument
() =
default
;
120
XmlDocument::XmlDocument
(
XmlDocument
&&) noexcept = default;
121
XmlDocument
&
XmlDocument
::operator=(
XmlDocument
&&) noexcept = default;
122
123
void
XmlDocument
::
parse
(const
std
::
string
& content) {
124
try
{
125
_pimpl_
->doc.Parse(content);
126
}
catch
(ticpp::Exception& e) {
throw
XmlException
(e.what()); }
127
}
128
129
void
XmlDocument::loadFile
() {
130
try
{
131
_pimpl_
->doc.LoadFile();
132
}
catch
(ticpp::Exception& e) {
throw
XmlException
(e.what()); }
133
}
134
135
bool
XmlDocument::noChildren
()
const
{
return
_pimpl_
->doc.NoChildren(); }
136
137
XmlElement
XmlDocument::firstChildElement
(
const
std::string& tag)
const
{
138
try
{
139
return
XmlElement
(
_pimpl_
->doc.FirstChildElement(tag));
140
}
catch
(ticpp::Exception& e) {
throw
XmlException
(e.what()); }
141
}
142
143
}
/* namespace gum */
XmlDocument.h
Opaque facade over the vendored ticpp/tinyxml XML parser.
gum::XmlDocument
An XML document, opaque over ticpp::Document.
Definition
XmlDocument.h:125
gum::XmlDocument::firstChildElement
XmlElement firstChildElement(const std::string &tag) const
Definition
XmlDocument.cpp:137
gum::XmlDocument::_pimpl_
std::unique_ptr< Impl > _pimpl_
Definition
XmlDocument.h:149
gum::XmlDocument::~XmlDocument
~XmlDocument()
gum::XmlDocument::XmlDocument
XmlDocument()
Definition
XmlDocument.cpp:114
gum::XmlDocument::loadFile
void loadFile()
Loads the file passed to the constructor.
Definition
XmlDocument.cpp:129
gum::XmlDocument::parse
void parse(const std::string &content)
Definition
XmlDocument.cpp:123
gum::XmlDocument::noChildren
bool noChildren() const
Definition
XmlDocument.cpp:135
gum::XmlElement
A non-owning handle to an XML element, opaque over ticpp::Element.
Definition
XmlDocument.h:89
gum::XmlElement::children
std::vector< XmlElement > children(const std::string &tag) const
All direct children with this tag, in document order.
Definition
XmlDocument.cpp:70
gum::XmlElement::_handle_
void * _handle_
opaque ticpp::Element*, never exposed
Definition
XmlDocument.h:118
gum::XmlElement::textOrDefault
std::string textOrDefault(const std::string &defaultValue) const
Definition
XmlDocument.cpp:81
gum::XmlElement::XmlElement
XmlElement()=default
gum::XmlElement::isNull
bool isNull() const noexcept
Definition
XmlDocument.cpp:54
gum::XmlElement::attribute
std::string attribute(const std::string &name) const
Definition
XmlDocument.cpp:86
gum::XmlElement::firstChildElement
XmlElement firstChildElement(const std::string &tag) const
Definition
XmlDocument.cpp:56
gum::XmlException::_message_
std::string _message_
Definition
XmlDocument.h:79
gum::XmlException::XmlException
XmlException(std::string message)
Definition
XmlDocument.cpp:48
gum::XmlException::what
const char * what() const noexcept override
Definition
XmlDocument.cpp:50
gum
gum is the global namespace for all aGrUM entities
Definition
agrum.h:46
gum::defaultValue
const float MultiDimFunctionGraph< float >::defaultValue
Definition
multiDimFunctionGraph.cpp:56
std
STL namespace.
gum::XmlDocument::Impl
Definition
XmlDocument.cpp:106
gum::XmlDocument::Impl::doc
ticpp::Document doc
Definition
XmlDocument.cpp:107
gum::XmlDocument::Impl::Impl
Impl()=default
gum::XmlDocument::Impl::Impl
Impl(const std::string &filePath)
Definition
XmlDocument.cpp:111
aGrUM
3.3.2
© PHW&CG&others - 2022
DoXyGeN 1.18.0