Coverage Report

Created: 2026-08-11 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/poco/XML/include/Poco/DOM/Document.h
Line
Count
Source
1
//
2
// Document.h
3
//
4
// Library: XML
5
// Package: DOM
6
// Module:  DOM
7
//
8
// Definition of the DOM Document class.
9
//
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11
// and Contributors.
12
//
13
// SPDX-License-Identifier: BSL-1.0
14
//
15
16
17
#ifndef DOM_Document_INCLUDED
18
#define DOM_Document_INCLUDED
19
20
21
#include "Poco/XML/XML.h"
22
#include "Poco/DOM/AbstractContainerNode.h"
23
#include "Poco/DOM/DocumentEvent.h"
24
#include "Poco/DOM/Element.h"
25
#include "Poco/XML/XMLString.h"
26
#include "Poco/XML/NamePool.h"
27
#include "Poco/AutoReleasePool.h"
28
29
30
namespace Poco::XML {
31
32
33
class NamePool;
34
class DocumentType;
35
class DOMImplementation;
36
class DocumentFragment;
37
class Text;
38
class Comment;
39
class CDATASection;
40
class ProcessingInstruction;
41
class Attr;
42
class EntityReference;
43
class NodeList;
44
class Entity;
45
class Notation;
46
47
48
class XML_API Document: public AbstractContainerNode, public DocumentEvent
49
  /// The Document interface represents the entire HTML or XML document. Conceptually,
50
  /// it is the root of the document tree, and provides the primary access to the
51
  /// document's data.
52
  ///
53
  /// Since elements, text nodes, comments, processing instructions, etc. cannot exist
54
  /// outside the context of a Document, the Document interface also contains the
55
  /// factory methods needed to create these objects. The Node objects created have a
56
  /// ownerDocument attribute which associates them with the Document within whose
57
  /// context they were created.
58
{
59
public:
60
  using AutoReleasePool = Poco::AutoReleasePool<DOMObject>;
61
62
  explicit Document(NamePool* pNamePool = nullptr);
63
    /// Creates a new document. If pNamePool == 0, the document
64
    /// creates its own name pool, otherwise it uses the given name pool.
65
    /// Sharing a name pool makes sense for documents containing instances
66
    /// of the same schema, thus reducing memory usage.
67
68
  explicit Document(unsigned long namePoolSize);
69
    /// Creates a new document using a name pool with the given size, which
70
    /// should be a prime number (e.g., 251, 509, 1021, 4093).
71
72
  Document(DocumentType* pDocumentType, NamePool* pNamePool = nullptr);
73
    /// Creates a new document. If pNamePool == 0, the document
74
    /// creates its own name pool, otherwise it uses the given name pool.
75
    /// Sharing a name pool makes sense for documents containing instances
76
    /// of the same schema, thus reducing memory usage.
77
78
  Document(DocumentType* pDocumentType, unsigned long namePoolSize);
79
    /// Creates a new document using a name pool with the given size, which
80
    /// should be a prime number (e.g., 251, 509, 1021, 4093).
81
82
  NamePool& namePool();
83
    /// Returns a pointer to the documents Name Pool.
84
85
  AutoReleasePool& autoReleasePool();
86
    /// Returns a pointer to the documents Auto Release Pool.
87
88
  void collectGarbage();
89
    /// Releases all objects in the Auto Release Pool.
90
91
  void suspendEvents();
92
    /// Suspends all events until resumeEvents() is called.
93
94
  void resumeEvents();
95
    /// Resumes all events suspended with suspendEvent()
96
97
  bool eventsSuspended() const;
98
    /// Returns true if events are suspended.
99
100
  bool events() const;
101
    /// Returns true if events are not suspended.
102
103
  const DocumentType* doctype() const;
104
    /// The Document Type Declaration (see DocumentType) associated with this document.
105
    /// For HTML documents as well as XML documents without a document type declaration
106
    /// this returns null. The DOM Level 1 does not support editing the Document
107
    /// Type Declaration. docType cannot be altered in any way, including through
108
    /// the use of methods inherited from the Node interface, such as insertNode
109
    /// or removeNode.
110
111
  const DOMImplementation& implementation() const;
112
    /// The DOMImplementation object that handles this document. A DOM application
113
    /// may use objects from multiple implementations.
114
115
  Element* documentElement() const;
116
    /// This is a convenience attribute that allows direct access to the child node
117
    /// that is the root element of the document. For HTML documents, this is the
118
    /// element with the tagName "HTML".
119
120
  Element* createElement(const XMLString& tagName) const;
121
    /// Creates an element of the type specified. Note that the instance returned
122
    /// implements the Element interface, so attributes can be specified directly
123
    /// on the returned object.
124
    ///
125
    /// In addition, if there are known attributes with default values, Attr nodes
126
    /// representing them are automatically created and attached to the element.
127
128
  DocumentFragment* createDocumentFragment() const;
129
    /// Creates an empty DocumentFragment object.
130
131
  Text* createTextNode(const XMLString& data) const;
132
    /// Creates a text node given the specified string.
133
134
  Comment* createComment(const XMLString& data) const;
135
    /// Creates a comment node given the specified string.
136
137
  CDATASection* createCDATASection(const XMLString& data) const;
138
    /// Creates a CDATASection node whose value is the specified string.
139
140
  ProcessingInstruction* createProcessingInstruction(const XMLString& target, const XMLString& data) const;
141
    /// Creates a ProcessingInstruction node given the specified target and data strings.
142
143
  Attr* createAttribute(const XMLString& name) const;
144
    /// Creates an Attr of the given name. Note that the Attr instance can then
145
    /// be set on an Element using the setAttributeNode method.
146
147
  EntityReference* createEntityReference(const XMLString& name) const;
148
    /// Creates an EntityReference object. In addition, if the referenced entity
149
    /// is known, the child list of the EntityReference node is made the same as
150
    /// that of the corresponding Entity node.
151
152
  NodeList* getElementsByTagName(const XMLString& name) const;
153
    /// Returns a NodeList of all Elements with a given tag name in the order
154
    /// in which they would be encountered in a preorder traversal of the
155
    /// document tree.
156
    ///
157
    /// The returned NodeList must be released with a call to release()
158
    /// when no longer needed.
159
160
  // DOM Level 2
161
  Node* importNode(Node* importedNode, bool deep);
162
    /// Imports a node from another document to this document. The returned node
163
    /// has no parent; (parentNode is null). The source node is not altered or removed
164
    /// from the original document; this method creates a new copy of the source
165
    /// node.
166
    /// For all nodes, importing a node creates a node object owned by the importing
167
    /// document, with attribute values identical to the source node's nodeName
168
    /// and nodeType, plus the attributes related to namespaces (prefix, localName,
169
    /// and namespaceURI). As in the cloneNode operation on a Node, the source node
170
    /// is not altered.
171
    /// Additional information is copied as appropriate to the nodeType, attempting
172
    /// to mirror the behavior expected if a fragment of XML or HTML source was
173
    /// copied from one document to another, recognizing that the two documents
174
    /// may have different DTDs in the XML case.
175
176
  Element* createElementNS(const XMLString& namespaceURI, const XMLString& qualifiedName) const;
177
    /// Creates an element of the given qualified name and namespace URI.
178
179
  Attr* createAttributeNS(const XMLString& namespaceURI, const XMLString& qualifiedName) const;
180
    /// Creates an attribute of the given qualified name and namespace URI.
181
182
  NodeList* getElementsByTagNameNS(const XMLString& namespaceURI, const XMLString& localName) const;
183
    /// Returns a NodeList of all the Elements with a given local name and
184
    /// namespace URI in the order in which they are encountered in a
185
    /// preorder traversal of the Document tree.
186
187
  Element* getElementById(const XMLString& elementId) const;
188
    /// Returns the Element whose ID is given by elementId. If no such
189
    /// element exists, returns null. Behavior is not defined if more
190
    /// than one element has this ID.
191
    ///
192
    /// Note: The DOM implementation must have information that says
193
    /// which attributes are of type ID. Attributes with the name "ID"
194
    /// are not of type ID unless so defined. Implementations that do
195
    /// not know whether attributes are of type ID or not are expected to
196
    /// return null. This implementation therefore returns null.
197
    ///
198
    /// See also the non-standard two argument variant of getElementById()
199
    /// and getElementByIdNS().
200
201
  // DocumentEvent
202
  Event* createEvent(const XMLString& eventType) const;
203
204
  // Node
205
  const XMLString& nodeName() const;
206
  unsigned short nodeType() const;
207
208
  // EventTarget
209
  bool dispatchEvent(Event* evt);
210
211
  // Extensions
212
  Entity* createEntity(const XMLString& name, const XMLString& publicId, const XMLString& systemId, const XMLString& notationName) const;
213
    /// Creates an Entity with the given name, publicId, systemId and notationName.
214
    ///
215
    /// This method is not part of the W3C Document Object Model.
216
217
  Notation* createNotation(const XMLString& name, const XMLString& publicId, const XMLString& systemId) const;
218
    /// Creates a Notation with the given name, publicId and systemId.
219
    ///
220
    /// This method is not part of the W3C Document Object Model.
221
222
  Element* getElementById(const XMLString& elementId, const XMLString& idAttribute) const;
223
    /// Returns the first Element whose ID attribute (given in idAttribute)
224
    /// has the given elementId. If no such element exists, returns null.
225
    ///
226
    /// This method is an extension to the W3C Document Object Model.
227
228
  Element* getElementByIdNS(const XMLString& elementId, const XMLString& idAttributeURI, const XMLString& idAttributeLocalName) const;
229
    /// Returns the first Element whose ID attribute (given in idAttributeURI and idAttributeLocalName)
230
    /// has the given elementId. If no such element exists, returns null.
231
    ///
232
    /// This method is an extension to the W3C Document Object Model.
233
234
protected:
235
  ~Document();
236
237
  Node* copyNode(bool deep, Document* pOwnerDocument) const;
238
239
  DocumentType* getDoctype();
240
  void setDoctype(DocumentType* pDoctype);
241
242
private:
243
  DocumentType*   _pDocumentType;
244
  NamePool*       _pNamePool;
245
  AutoReleasePool _autoReleasePool;
246
  int             _eventSuspendLevel;
247
248
  static const XMLString NODE_NAME;
249
250
  friend class DOMBuilder;
251
};
252
253
254
//
255
// inlines
256
//
257
inline NamePool& Document::namePool()
258
1.14M
{
259
1.14M
  return *_pNamePool;
260
1.14M
}
261
262
263
inline Document::AutoReleasePool& Document::autoReleasePool()
264
0
{
265
0
  return _autoReleasePool;
266
0
}
267
268
269
inline const DocumentType* Document::doctype() const
270
0
{
271
0
  return _pDocumentType;
272
0
}
273
274
275
inline DocumentType* Document::getDoctype()
276
27.9k
{
277
27.9k
  return _pDocumentType;
278
27.9k
}
279
280
281
} // namespace Poco::XML
282
283
284
#endif // DOM_Document_INCLUDED