Line | Branch | Exec | Source |
---|---|---|---|
1 | /* GATE PROJECT LICENSE: | ||
2 | +----------------------------------------------------------------------------+ | ||
3 | | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | | ||
4 | | All rights reserved. | | ||
5 | | | | ||
6 | | Redistribution and use in source and binary forms, with or without | | ||
7 | | modification, are permitted provided that the following conditions are met:| | ||
8 | | | | ||
9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
10 | | this list of conditions and the following disclaimer. | | ||
11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
12 | | notice, this list of conditions and the following disclaimer in the | | ||
13 | | documentation and/or other materials provided with the distribution. | | ||
14 | | | | ||
15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
26 | +----------------------------------------------------------------------------+ | ||
27 | */ | ||
28 | #include "gate/encode/xml.hpp" | ||
29 | #include "gate/exceptions.hpp" | ||
30 | |||
31 | namespace gate | ||
32 | { | ||
33 | namespace enc | ||
34 | { | ||
35 | |||
36 | ✗ | XmlNode::XmlNode() | |
37 | { | ||
38 | ✗ | gate_mem_clear(&this->impl, sizeof(this->impl)); | |
39 | ✗ | } | |
40 | |||
41 | 3 | XmlNode::XmlNode(enumint_t nodeType, String const& tag) | |
42 | { | ||
43 | 3 | result_t result = gate_xml_node_create(&this->impl, nodeType, tag.c_impl(), NULL, NULL, NULL); | |
44 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | GATEXX_CHECK_ERROR(result); |
45 | 3 | } | |
46 | |||
47 | |||
48 | ✗ | XmlNode::XmlNode(gate_xml_node_t const* node) | |
49 | { | ||
50 | ✗ | result_t result = gate_xml_node_create(&this->impl, node->node_type, &node->tag, | |
51 | &node->attributes, &node->child_nodes, &node->content); | ||
52 | ✗ | GATEXX_CHECK_ERROR(result); | |
53 | ✗ | } | |
54 | |||
55 | ✗ | XmlNode::XmlNode(XmlNode const& src) | |
56 | { | ||
57 | ✗ | if (NULL == gate_xml_node_duplicate(&this->impl, &src.impl)) | |
58 | { | ||
59 | ✗ | GATEXX_RAISE_ERROR(results::OutOfMemory); | |
60 | } | ||
61 | ✗ | } | |
62 | |||
63 | ✗ | XmlNode& XmlNode::operator=(XmlNode const& src) | |
64 | { | ||
65 | ✗ | if (this != &src) | |
66 | { | ||
67 | gate_xml_node_t new_node; | ||
68 | ✗ | if (NULL == gate_xml_node_duplicate(&new_node, &src.impl)) | |
69 | { | ||
70 | ✗ | GATEXX_RAISE_ERROR(results::OutOfMemory); | |
71 | } | ||
72 | } | ||
73 | ✗ | return *this; | |
74 | } | ||
75 | |||
76 | |||
77 | 6 | XmlNode::~XmlNode() noexcept | |
78 | { | ||
79 | 3 | gate_xml_node_destroy(&this->impl); | |
80 | 3 | } | |
81 | |||
82 | ✗ | enumint_t XmlNode::getNodeType() const | |
83 | { | ||
84 | ✗ | return this->impl.node_type; | |
85 | } | ||
86 | |||
87 | ✗ | String XmlNode::getTag() const | |
88 | { | ||
89 | ✗ | return String::duplicate(this->impl.tag); | |
90 | } | ||
91 | |||
92 | ✗ | size_t XmlNode::getAttributeCount() const | |
93 | { | ||
94 | ✗ | return gate_xml_node_attribute_count(&this->impl); | |
95 | } | ||
96 | |||
97 | ✗ | String XmlNode::getAttribute(String const& name) const | |
98 | { | ||
99 | ✗ | gate_string_t value = GATE_STRING_INIT_EMPTY; | |
100 | ✗ | result_t result = gate_xml_node_attribute_by_name(&this->impl, name.c_impl(), &value); | |
101 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
102 | ✗ | return String::createFrom(value); | |
103 | } | ||
104 | ✗ | String XmlNode::getAttribute(String const& name, String const& altValue) const | |
105 | { | ||
106 | ✗ | gate_string_t value = GATE_STRING_INIT_EMPTY; | |
107 | ✗ | result_t result = gate_xml_node_attribute_by_name(&this->impl, name.c_impl(), &value); | |
108 | ✗ | if (GATE_FAILED(result)) | |
109 | { | ||
110 | ✗ | return altValue; | |
111 | } | ||
112 | else | ||
113 | { | ||
114 | ✗ | return String::createFrom(value); | |
115 | } | ||
116 | } | ||
117 | |||
118 | |||
119 | ✗ | void XmlNode::getAttribute(size_t index, String& name, String& value) const | |
120 | { | ||
121 | ✗ | gate_string_t str_name = GATE_STRING_INIT_EMPTY; | |
122 | ✗ | gate_string_t str_value = GATE_STRING_INIT_EMPTY; | |
123 | ✗ | result_t result = gate_xml_node_attribute_by_index(&this->impl, index, &str_name, &str_value); | |
124 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
125 | ✗ | name = String::createFrom(str_name); | |
126 | ✗ | value = String::createFrom(str_value); | |
127 | ✗ | } | |
128 | |||
129 | ✗ | size_t XmlNode::getChildrenCount() const | |
130 | { | ||
131 | ✗ | return gate_xml_node_children_count(&this->impl); | |
132 | } | ||
133 | |||
134 | ✗ | XmlNode XmlNode::getChild(size_t index) const | |
135 | { | ||
136 | ✗ | XmlNode node; | |
137 | ✗ | result_t result = gate_xml_node_child(&this->impl, index, &node.impl); | |
138 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
139 | ✗ | return node; | |
140 | } | ||
141 | |||
142 | ✗ | String XmlNode::getContent() const | |
143 | { | ||
144 | ✗ | return String::duplicate(this->impl.content); | |
145 | } | ||
146 | |||
147 | 1 | void XmlNode::store(Stream const& storeStream) const | |
148 | { | ||
149 | 1 | result_t result = gate_xml_node_print(&this->impl, storeStream.c_impl(), 0); | |
150 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
151 | 1 | } | |
152 | |||
153 | 1 | String XmlNode::toString() const | |
154 | { | ||
155 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | StringStream ss; |
156 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | this->store(ss); |
157 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | return ss.toString(); |
158 | } | ||
159 | |||
160 | ✗ | gate_xml_node_t const* XmlNode::c_impl() const noexcept | |
161 | { | ||
162 | ✗ | return &this->impl; | |
163 | } | ||
164 | |||
165 | ✗ | gate_xml_node_t* XmlNode::c_impl() noexcept | |
166 | { | ||
167 | ✗ | return &this->impl; | |
168 | } | ||
169 | |||
170 | |||
171 | |||
172 | |||
173 | 1 | XmlDoc::XmlDoc() | |
174 | { | ||
175 | 1 | result_t result = gate_xml_doc_create(&this->impl, NULL); | |
176 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_ERROR(result); |
177 | 1 | } | |
178 | |||
179 | ✗ | XmlDoc::XmlDoc(XmlNode const& documentElement) | |
180 | { | ||
181 | ✗ | result_t result = gate_xml_doc_create(&this->impl, documentElement.c_impl()); | |
182 | ✗ | GATEXX_CHECK_ERROR(result); | |
183 | ✗ | } | |
184 | |||
185 | ✗ | XmlDoc::XmlDoc(Stream& loadStream) | |
186 | { | ||
187 | ✗ | result_t result = gate_xml_doc_load(&this->impl, loadStream.c_impl()); | |
188 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
189 | ✗ | } | |
190 | |||
191 | 1 | XmlDoc::XmlDoc(String const& loadText) | |
192 | { | ||
193 | 1 | result_t result = gate_xml_doc_load_text(&this->impl, loadText.c_impl()); | |
194 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
195 | 1 | } | |
196 | |||
197 | 4 | XmlDoc::~XmlDoc() noexcept | |
198 | { | ||
199 | 2 | gate_xml_doc_destroy(&this->impl); | |
200 | 2 | } | |
201 | |||
202 | ✗ | gate_xml_doc_t* XmlDoc::c_impl() noexcept | |
203 | { | ||
204 | ✗ | return &this->impl; | |
205 | } | ||
206 | ✗ | gate_xml_doc_t const* XmlDoc::c_impl() const noexcept | |
207 | { | ||
208 | ✗ | return &this->impl; | |
209 | } | ||
210 | |||
211 | ✗ | void XmlDoc::store(Stream& saveStream, size_t indentSpaces) | |
212 | { | ||
213 | ✗ | result_t result = gate_xml_doc_store(&this->impl, saveStream.c_impl(), indentSpaces); | |
214 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
215 | ✗ | } | |
216 | |||
217 | ✗ | String XmlDoc::toString(size_t indentSpaces) | |
218 | { | ||
219 | ✗ | gate_string_t text = GATE_STRING_INIT_EMPTY; | |
220 | ✗ | result_t result = gate_xml_doc_store_text(&this->impl, &text, indentSpaces); | |
221 | ✗ | GATEXX_CHECK_ERROR(result); | |
222 | ✗ | return String::createFrom(text); | |
223 | } | ||
224 | |||
225 | ✗ | XmlNode XmlDoc::rootElement() | |
226 | { | ||
227 | ✗ | gate_xml_node_t const* ptr_node = NULL; | |
228 | ✗ | result_t result = gate_xml_doc_root_element(&this->impl, &ptr_node); | |
229 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
230 | ✗ | return XmlNode(ptr_node); | |
231 | } | ||
232 | |||
233 | ✗ | void XmlDoc::addNode(XmlNode& parentNode, XmlNode const& childNode) | |
234 | { | ||
235 | ✗ | result_t result = gate_xml_doc_add_node(&this->impl, parentNode.c_impl(), childNode.c_impl()); | |
236 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
237 | ✗ | } | |
238 | |||
239 | ✗ | void XmlDoc::removeNode(XmlNode& node, size_t index) | |
240 | { | ||
241 | ✗ | result_t result = gate_xml_doc_remove_node(&this->impl, node.c_impl(), index); | |
242 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
243 | ✗ | } | |
244 | |||
245 | ✗ | void XmlDoc::addAttribute(XmlNode& node, String const& name, String const& value) | |
246 | { | ||
247 | ✗ | result_t result = gate_xml_doc_add_attribute(&this->impl, node.c_impl(), name.c_impl(), value.c_impl()); | |
248 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
249 | ✗ | } | |
250 | |||
251 | ✗ | void XmlDoc::removeAttribute(XmlNode& node, String const& name) | |
252 | { | ||
253 | ✗ | result_t result = gate_xml_doc_remove_attribute(&this->impl, node.c_impl(), name.c_impl()); | |
254 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
255 | ✗ | } | |
256 | ✗ | void XmlDoc::setContent(XmlNode& node, String const& content) | |
257 | { | ||
258 | ✗ | result_t result = gate_xml_doc_set_node_content(&this->impl, node.c_impl(), content.c_impl()); | |
259 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
260 | ✗ | } | |
261 | |||
262 | |||
263 | } // end of namespace enc | ||
264 | } // end of namespace gate | ||
265 |