aGrUM
3.2.0
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::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
102
struct
XmlDocument::Impl
{
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
125
void
XmlDocument::loadFile
() {
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 */
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:133
gum::XmlDocument::_pimpl_
std::unique_ptr< Impl > _pimpl_
Definition
XmlDocument.h:149
gum::XmlDocument::~XmlDocument
~XmlDocument()
gum::XmlDocument::XmlDocument
XmlDocument()
Definition
XmlDocument.cpp:110
gum::XmlDocument::loadFile
void loadFile()
Loads the file passed to the constructor.
Definition
XmlDocument.cpp:125
gum::XmlDocument::parse
void parse(const std::string &content)
Definition
XmlDocument.cpp:119
gum::XmlDocument::noChildren
bool noChildren() const
Definition
XmlDocument.cpp:131
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:66
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:77
gum::XmlElement::XmlElement
XmlElement()=default
gum::XmlElement::attribute
std::string attribute(const std::string &name) const
Definition
XmlDocument.cpp:82
gum::XmlElement::firstChildElement
XmlElement firstChildElement(const std::string &tag) const
Definition
XmlDocument.cpp:52
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:67
std
STL namespace.
gum::XmlDocument::Impl
Definition
XmlDocument.cpp:102
gum::XmlDocument::Impl::doc
ticpp::Document doc
Definition
XmlDocument.cpp:103
gum::XmlDocument::Impl::Impl
Impl()=default
gum::XmlDocument::Impl::Impl
Impl(const std::string &filePath)
Definition
XmlDocument.cpp:107
aGrUM
3.2.0
© PHW&CG&others - 2022
DoXyGeN 1.18.0