Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
XmlNode.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* XmlNode.h (C) 2000-2023 */
9/* */
10/* Any node in a DOM tree. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CORE_XMLNODE_H
13#define ARCANE_CORE_XMLNODE_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/String.h"
18#include "arcane/core/Dom.h"
19
20#include <iterator>
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31class IRessourceMng;
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
36class XmlNodeIterator;
38class XmlNodeList;
39
40/*!
41 * \ingroup Xml
42 * \brief Node of a DOM tree.
43 *
44 * This class is used for all types of DOM nodes and allows
45 * them to be manipulated more simply than with the DOM without having
46 * to perform type conversions.
47 *
48 * Each node can be considered a container in the sense of the STL.
49 */
50class ARCANE_CORE_EXPORT XmlNode
51{
52 public:
53
54 //! Type of the elements in the array
55 typedef XmlNode value_type;
56 //! Type of the iterator over an element in the array
58 //! Type of the constant iterator over an element in the array
60 //! Type pointer of an element in the array
62 //! Type constant pointer of an element in the array
63 typedef const value_type* const_pointer;
64 //! Type reference of an element in the array
66 //! Type constant reference of an element in the array
68 //! Type indexing the array
70 //! Type of a distance between iterator elements in the array
71 typedef int difference_type;
72
73 //! Type of an iterator over the entire array
75 //! Type of a constant iterator over the entire array
77
78 public:
79
80 /*! \brief NodeType
81 An integer indicating which type of node this is.
82 \note Numeric codes up to 200 are reserved to W3C for possible future use.
83 */
84 enum eType
85 {
86 //! The node is an Element
88 //! The node is an Attr
90 //! The node is a Text node
91 TEXT = 3,
92 //! The node is a CDATASection
94 //! The node is an EntityReference
96 //! The node is an Entity
97 ENTITY = 6,
98 //! The node is a ProcessingInstruction
100 //! The node is a Comment
102 //! The node is a Document
104 //! The node is a DocumentType
106 //! The node is a DocumentFragment
108 //! The node is a Notation
110 };
111
112 public:
113
114 XmlNode(IRessourceMng* m, const dom::Node& node)
115 : m_rm(m)
116 , m_node(node)
117 {}
118 //TODO: to be removed
119 explicit XmlNode(IRessourceMng* m)
120 : m_rm(m)
121 , m_node()
122 {}
123 XmlNode()
124 : m_rm(nullptr)
125 , m_node()
126 {}
127
128 public:
129
130 //! Returns an iterator over the first element of the array
131 inline iterator begin();
132 //! Returns an iterator over the first element after the end of the array
133 inline iterator end();
134 //! Returns a constant iterator over the first element of the array
135 inline const_iterator begin() const;
136 //! Returns a constant iterator over the first element after the end of the array
137 inline const_iterator end() const;
138
139 public:
140
141 //! Node type
142 eType type() const;
143
144 //! Node name
145 String name() const;
146
147 /*! \brief XPath name of the node with its ancestors.
148 * \warning Only works for elements.
149 */
150 String xpathFullName() const;
151
152 //! True if the element name is \a name
153 bool isNamed(const String& name) const;
154
155 /*! \brief Node value.
156 *
157 * For an element, it is the concatenation of the values of each
158 * of its child nodes of type TEXT, CDATA_SECTION or ENTITY_REFERENCE.
159 * For nodes other than elements, it is the value of
160 * the Node::nodeValue() method of the DOM.
161 */
162 String value() const;
163
164 /*! \brief Node value converted to integer.
165 *
166 * If conversion fails, if \a throw_exception
167 * is \a false returns 0, otherwise throws an exception.
168 */
169 Integer valueAsInteger(bool throw_exception = false) const;
170
171 /*! \brief Node value converted to 64-bit integer. 0 if conversion fails.
172 *
173 * If conversion fails, if \a throw_exception
174 * is \a false returns 0, otherwise throws an exception.
175 */
176 Int64 valueAsInt64(bool throw_exception = false) const;
177
178 /*! \brief Node value converted to boolean.
179 *
180 * A value of \c false or \c 0 corresponds to \a false. A value
181 * of \c true or \c 1 corresponds to \a true.
182 * If conversion fails, if \a throw_exception
183 * is \a false returns \a false, otherwise throws an exception.
184 */
185 bool valueAsBoolean(bool throw_exception = false) const;
186
187 /*! \brief Node value converted to real number.
188 * If conversion fails, if \a throw_exception
189 * is \a false returns 0.0, otherwise throws an exception.
190 */
191 Real valueAsReal(bool throw_exception = false) const;
192
193 /*! \brief Sets the node value.
194 *
195 * This method is only valid for ELEMENT_NODE or
196 * ATTRIBUTE_NODE. For elements, it deletes all children
197 * and adds a single TEXT_NODE child containing the value \a value
198 */
199 void setValue(const String& value);
200
201 /*! \brief Value of attribute \a name.
202 *
203 * If the attribute does not exist, if \a throw_exception is \a false returns
204 * the null string, otherwise throws an exception.
205 */
206 String attrValue(const String& name, bool throw_exception = false) const;
207
208 //! Sets the attribute \a name to the value \a value
209 void setAttrValue(const String& name, const String& value);
210
211 /*!
212 * \brief Returns the attribute of name \a name.
213 *
214 * If the attribute does not exist, if \a throw_exception is \a false returns
215 * a null node, otherwise throws an exception.
216 */
217 XmlNode attr(const String& name, bool throw_exception = false) const;
218
219 /*!
220 * \brief Returns the attribute of name \a name.
221 * If no attribute with this name exists, an attribute with
222 * the null string as value is created and returned.
223 */
224 XmlNode forceAttr(const String& name);
225
226 /*!
227 * \brief Removes the attribute of name \a name from this node.
228 * If this node is not an element, nothing is done.
229 */
230 void removeAttr(const String& name) const;
231
232 /*!
233 * \brief Returns the document element.
234 * \pre type()==DOCUMENT_NODE
235 */
236 XmlNode documentElement() const;
237
238 /*!
239 * \brief Returns the owning element of this attribute.
240 * \pre type()==ATTRIBUTE_NODE
241 */
242 XmlNode ownerElement() const;
243
244 //! Deletes all child nodes
245 void clear();
246
247 /*!
248 * \brief Child node of this node with name \a name
249 *
250 * If multiple nodes with this name exist, returns the first one.
251 * If the node is not found, returns a null node
252 */
253 XmlNode child(const String& name) const;
254
255 /*!
256 * \brief Child node of this node with name \a name
257 *
258 * If multiple nodes with this name exist, returns the first one.
259 * If the node is not found, throws an exception.
260 */
261 XmlNode expectedChild(const String& name) const;
262
263 //! Set of child nodes of this node having the name \a name
264 XmlNodeList children(const String& name) const;
265
266 //! Set of child nodes of this node
267 XmlNodeList children() const;
268
269 //! Parent of this node (null if none)
270 XmlNode parent() const { return XmlNode(m_rm, m_node.parentNode()); }
271
272 /*! \brief Adds \a child_node as a child of this node.
273 *
274 * The node is added after all children.
275 */
276 void append(const XmlNode& child_node) { m_node.appendChild(child_node.domNode()); }
277 //! Removes the child node \a child_node
278 void remove(const XmlNode& child_node);
279 //! Replaces the child node \a ref_node with the node \a new_node
280 void replace(const XmlNode& new_node, XmlNode& ref_node);
281 //! Removes this node from the document
282 void remove();
283 //! First child
284 XmlNode front() const { return XmlNode(m_rm, m_node.firstChild()); }
285 //! Last child
286 XmlNode last() const { return XmlNode(m_rm, m_node.lastChild()); }
287 //! Next node (nextSibling())
288 XmlNode next() const { return XmlNode(m_rm, m_node.nextSibling()); }
289 //! Previous node (previousSibling())
290 XmlNode prev() const { return XmlNode(m_rm, m_node.previousSibling()); }
291 //! Returns the next node after this node having the name \a name.
292 XmlNode nextWithName(const String& name) const;
293 //! Returns the previous node before this node having the name \a name.
294 XmlNode prevWithName(const String& name) const;
295 //! Returns the next node of the same type.
296 XmlNode nextSameType() const;
297 //! Returns the previous node of the same type.
298 XmlNode prevSameType() const;
299 void operator++() { m_node = m_node.nextSibling(); }
300 void operator--() { m_node = m_node.previousSibling(); }
301
302 //! True if the node is null
303 bool null() const { return m_node._null(); }
304 bool operator!() const { return null(); }
305
306 //! \internal
307 dom::Node domNode() const { return m_node; }
308 //! \internal
309 void assignDomNode(const dom::Node& node);
310
311 /*!
312 * \brief Inserts a node.
313 * Inserts the node \a new_child after the node \a ref_node.
314 * If \a new_child is \c null, does nothing.
315 * If \a ref_node is \c null, \a new_child is added to the end (like append()). Otherwise,
316 * \a ref_node must be a child of this node and \a new_child is inserted after
317 * \a ref_node.
318 * On success, returns the added node (\a new_child), otherwise the null node.
319 */
320 XmlNode insertAfter(const XmlNode& new_child, const XmlNode& ref_node);
321
322 /*!
323 * \brief Returns the child of this node having the name \a elem_name and
324 * an attribute of name \a attr_name with value \a attr_value.
325 */
326 XmlNode childWithAttr(const String& elem_name, const String& attr_name,
327 const String& attr_value) const;
328 /*!
329 * \brief Returns the child of this node having the name \a elem_name and
330 * an attribute of name \c "name" with value \a attr_value.
331 */
332 XmlNode childWithNameAttr(const String& elem_name,
333 const String& attr_value) const;
334
335 /*!
336 * \brief Returns a node from an XPath expression.
337 * \param xpath_expr XPath expression.
338 */
339 XmlNode xpathNode(const String& xpath_expr) const;
340
341 /*!
342 * \brief Creates a node of a given type.
343 *
344 * If type() is not DOCUMENT_NODE, it uses ownerDocument() as
345 * factory.
346 *
347 * \param type type of the node.
348 * \param name of the node.
349 * \param value of the node.
350 * \return the created node.
351 * \pre type()==DOCUMENT_NODE
352 */
353 XmlNode createNode(eType type, const String& name, const String& value);
354
355 /*!
356 * \brief Creates a node of a given type.
357 *
358 * If type() is not DOCUMENT_NODE, it uses ownerDocument() as
359 * factory.
360 *
361 * \param type type of the node.
362 * \param name or value of the node in the case where the node has no name.
363 * \return the created node.
364 */
365 XmlNode createNode(eType type, const String& name_or_value);
366
367 /*!
368 * \brief Creates a text node.
369 * \param value value of the text node.
370 * \return the created node.
371 */
372 XmlNode createText(const String& value);
373
374 XmlNode createElement(const String& name);
375
376 XmlNode createAndAppendElement(const String& name);
377
378 XmlNode createAndAppendElement(const String& name, const String& value);
379
380 XmlNode ownerDocument() const { return XmlNode(m_rm, m_node.ownerDocument()); }
381
382 IRessourceMng* rm() const { return m_rm; }
383
384 private:
385
386 IRessourceMng* m_rm;
387 dom::Node m_node;
388
389 protected:
390
391 String _value() const;
392 XmlNode _build(const dom::Node& node) const;
393 XmlNode _nullNode() const;
394 void _setNode(const dom::Node& n) { m_node = n; }
395 inline void _throwBadConvert(const char* type_name, const String& value) const;
396};
397
398/*---------------------------------------------------------------------------*/
399/*---------------------------------------------------------------------------*/
400
401/*!
402 * \brief Element of a DOM tree.
403 */
404class ARCANE_CORE_EXPORT XmlElement
405: public XmlNode
406{
407 public:
408
409 /*! \brief Creates a child element of \a parent.
410 * The created element has the name \a name and the value \a value.
411 * It is added to the end of the list of children of \a parent.
412 */
413 XmlElement(XmlNode& parent, const String& name, const String& value);
414 /*! \brief Creates a child element of \a parent.
415 * The created element has the name \a name and the value \a value.
416 * It is added to the end of the list of children of \a parent.
417 */
418 XmlElement(XmlNode& parent, const String& name);
419};
420
421/*---------------------------------------------------------------------------*/
422/*---------------------------------------------------------------------------*/
423
424inline bool
425operator==(const XmlNode& n1, const XmlNode& n2)
426{
427 return n1.domNode() == n2.domNode();
428}
429
430inline bool
431operator!=(const XmlNode& n1, const XmlNode& n2)
432{
433 return n1.domNode() != n2.domNode();
434}
435
436/*---------------------------------------------------------------------------*/
437/*---------------------------------------------------------------------------*/
438
439} // namespace Arcane
440
441/*---------------------------------------------------------------------------*/
442/*---------------------------------------------------------------------------*/
443
444#endif
Interface of a resource manager.
XmlElement(XmlNode &parent, const String &name, const String &value)
Creates a child element of parent. The created element has the name name and the value value....
Definition XmlNode.cc:691
List of nodes of a DOM tree.
Definition XmlNodeList.h:36
Node of a DOM tree.
Definition XmlNode.h:51
int difference_type
Type of a distance between iterator elements in the array.
Definition XmlNode.h:71
Integer size_type
Type indexing the array.
Definition XmlNode.h:69
XmlNode value_type
Type of the elements in the array.
Definition XmlNode.h:55
value_type & reference
Type reference of an element in the array.
Definition XmlNode.h:65
const value_type & const_reference
Type constant reference of an element in the array.
Definition XmlNode.h:67
value_type * pointer
Type pointer of an element in the array.
Definition XmlNode.h:61
const value_type * const_pointer
Type constant pointer of an element in the array.
Definition XmlNode.h:63
XmlNodeIterator iterator
Type of the iterator over an element in the array.
Definition XmlNode.h:57
XmlNode prev() const
Previous node (previousSibling()).
Definition XmlNode.h:290
XmlNodeConstIterator const_iterator
Type of the constant iterator over an element in the array.
Definition XmlNode.h:59
void append(const XmlNode &child_node)
Adds child_node as a child of this node.
Definition XmlNode.h:276
String value() const
Node value.
Definition XmlNode.cc:208
IterT< XmlNode > iter
Type of an iterator over the entire array.
Definition XmlNode.h:74
XmlNode front() const
First child.
Definition XmlNode.h:284
bool null() const
True if the node is null.
Definition XmlNode.h:303
XmlNode next() const
Next node (nextSibling()).
Definition XmlNode.h:288
eType
NodeType An integer indicating which type of node this is.
Definition XmlNode.h:85
@ TEXT
The node is a Text node.
Definition XmlNode.h:91
@ ATTRIBUTE
The node is an Attr.
Definition XmlNode.h:89
@ ENTITY
The node is an Entity.
Definition XmlNode.h:97
@ ELEMENT
The node is an Element.
Definition XmlNode.h:87
@ DOCUMENT_TYPE
The node is a DocumentType.
Definition XmlNode.h:105
@ ENTITY_REFERENCE
The node is an EntityReference.
Definition XmlNode.h:95
@ COMMENT
The node is a Comment.
Definition XmlNode.h:101
@ NOTATION
The node is a Notation.
Definition XmlNode.h:109
@ CDATA_SECTION
The node is a CDATASection.
Definition XmlNode.h:93
@ PROCESSING_INSTRUCTION
The node is a ProcessingInstruction.
Definition XmlNode.h:99
@ DOCUMENT
The node is a Document.
Definition XmlNode.h:103
@ DOCUMENT_FRAGMENT
The node is a DocumentFragment.
Definition XmlNode.h:107
ConstIterT< XmlNode > const_iter
Type of a constant iterator over the entire array.
Definition XmlNode.h:76
XmlNode parent() const
Parent of this node (null if none).
Definition XmlNode.h:270
String name() const
Node name.
Definition XmlNode.cc:141
XmlNode last() const
Last child.
Definition XmlNode.h:286
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.
double Real
Type representing a real number.