Document.h 10 KB

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