Node.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // Node.h
  3. //
  4. // Library: XML
  5. // Package: DOM
  6. // Module: DOM
  7. //
  8. // Definition of the DOM Node interface.
  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_Node_INCLUDED
  16. #define DOM_Node_INCLUDED
  17. #include "Poco/XML/XML.h"
  18. #include "Poco/DOM/EventTarget.h"
  19. #include "Poco/XML/XMLString.h"
  20. #include "Poco/SAX/NamespaceSupport.h"
  21. namespace Poco {
  22. namespace XML {
  23. class NamedNodeMap;
  24. class Document;
  25. class NodeList;
  26. class XML_API Node: public EventTarget
  27. /// The Node interface is the primary datatype for the entire Document Object
  28. /// Model. It represents a single node in the document tree. While all objects
  29. /// implementing the Node interface expose methods for dealing with children,
  30. /// not all objects implementing the Node interface may have children. For
  31. /// example, Text nodes may not have children, and adding children to such
  32. /// nodes results in a DOMException being raised.
  33. ///
  34. /// The attributes nodeName, nodeValue and attributes are included as a mechanism
  35. /// to get at node information without casting down to the specific derived
  36. /// interface. In cases where there is no obvious mapping of these attributes
  37. /// for a specific nodeType (e.g., nodeValue for an Element or attributes for
  38. /// a Comment), this returns null. Note that the specialized interfaces may
  39. /// contain additional and more convenient mechanisms to get and set the relevant
  40. /// information.
  41. ///
  42. /// This implementation differs in some ways from the W3C DOM recommendations.
  43. /// For example, the DOM specifies that some methods can return null strings.
  44. /// Instead of null strings, this implementation always returns empty strings.
  45. {
  46. public:
  47. enum
  48. {
  49. ELEMENT_NODE = 1, /// The node is an Element.
  50. ATTRIBUTE_NODE, /// The node is an Attr.
  51. TEXT_NODE, /// The node is a Text node.
  52. CDATA_SECTION_NODE, /// The node is a CDATASection.
  53. ENTITY_REFERENCE_NODE, /// The node is an EntityReference.
  54. ENTITY_NODE, /// The node is an Entity.
  55. PROCESSING_INSTRUCTION_NODE, /// The node is a ProcessingInstruction.
  56. COMMENT_NODE, /// The node is a Comment.
  57. DOCUMENT_NODE, /// The node is a Document.
  58. DOCUMENT_TYPE_NODE, /// The node is a DocumentType.
  59. DOCUMENT_FRAGMENT_NODE, /// The node is a DocumentFragment.
  60. NOTATION_NODE /// The node is a Notation.
  61. };
  62. virtual const XMLString& nodeName() const = 0;
  63. /// Returns the name of this node, depending on its type.
  64. const XMLString& nodeValue() const;
  65. /// Returns the value of this node, depending on its type.
  66. virtual const XMLString& getNodeValue() const = 0;
  67. /// Returns the value of this node, depending on its type.
  68. virtual void setNodeValue(const XMLString& value) = 0;
  69. /// Sets the value of this node. Throws an exception
  70. /// if the node is read-only.
  71. virtual unsigned short nodeType() const = 0;
  72. /// Returns a code representing the type of the underlying object.
  73. virtual Node* parentNode() const = 0;
  74. /// The parent of this node. All nodes, except Attr, Document, DocumentFragment,
  75. /// Entity, and Notation may have a parent. However, if a node has just been
  76. /// created and not yet added to the tree, or if it has been removed from the
  77. /// tree, this is null.
  78. virtual NodeList* childNodes() const = 0;
  79. /// Returns a NodeList containing all children of this node.
  80. ///
  81. /// The returned NodeList must be released with a call
  82. /// to release() when no longer needed.
  83. virtual Node* firstChild() const = 0;
  84. /// Returns the first child of this node. If there is no such
  85. /// node, this returns null.
  86. virtual Node* lastChild() const = 0;
  87. /// Returns the last child of this node. If there is no such
  88. /// node, this returns null.
  89. virtual Node* previousSibling() const = 0;
  90. /// Returns the node immediately preceding this node. If there
  91. /// is no such node, this returns null.
  92. virtual Node* nextSibling() const = 0;
  93. /// Returns the node immediately following this node. If there
  94. /// is no such node, this returns null.
  95. virtual NamedNodeMap* attributes() const = 0;
  96. /// Returns a NamedNodeMap containing the attributes of this
  97. /// node (if it is an Element) or null otherwise.
  98. ///
  99. /// The returned NamedNodeMap must be released with a call
  100. /// to release() when no longer needed.
  101. virtual Document* ownerDocument() const = 0;
  102. /// Returns the Document object associated with this node.
  103. /// This is also the Document object used to create new nodes.
  104. /// When this node is a Document, this is null.
  105. virtual Node* insertBefore(Node* newChild, Node* refChild) = 0;
  106. /// Inserts the node newChild before the existing child node refChild.
  107. ///
  108. /// If refChild is null, insert newChild at the end of the list of children.
  109. /// If newChild is a DocumentFragment object, all of its children are
  110. /// inserted in the same order, before refChild. If the newChild is already
  111. /// in the tree, it is first removed.
  112. virtual Node* replaceChild(Node* newChild, Node* oldChild) = 0;
  113. /// Replaces the child node oldChild with newChild in the list of children,
  114. /// and returns the oldChild node.
  115. /// If newChild is a DocumentFragment object, oldChild is replaced by all of
  116. /// the DocumentFragment children, which are inserted in the same order. If
  117. /// the newChild is already in the tree, it is first removed.
  118. virtual Node* removeChild(Node* oldChild) = 0;
  119. /// Removes the child node indicated by oldChild from the list of children
  120. /// and returns it.
  121. virtual Node* appendChild(Node* newChild) = 0;
  122. /// Appends the node newChild to the end of the list of children of this node.
  123. /// If newChild is already in the tree, it is first removed.
  124. virtual bool hasChildNodes() const = 0;
  125. /// This is a convenience method to allow easy determination of whether a
  126. /// node has any children.
  127. /// Returns true if the node has any children, false otherwise.
  128. virtual Node* cloneNode(bool deep) const = 0;
  129. /// Returns a duplicate of this node, i.e., serves as a generic copy constructor
  130. /// for nodes. The duplicate node has no parent; (parentNode is null.).
  131. /// Cloning an Element copies all attributes and their values, including those
  132. /// generated by the XML processor to represent defaulted attributes, but this
  133. /// method does not copy any text it contains unless it is a deep clone, since
  134. /// the text is contained in a child Text node. Cloning an Attribute directly,
  135. /// as opposed to be cloned as part of an Element cloning operation, returns
  136. /// a specified attribute (specified is true). Cloning any other type of node
  137. /// simply returns a copy of this node.
  138. /// Note that cloning an immutable subtree results in a mutable copy, but the
  139. /// children of an EntityReference clone are readonly. In addition, clones of
  140. /// unspecified Attr nodes are specified. And, cloning Document, DocumentType,
  141. /// Entity, and Notation nodes is implementation dependent.
  142. // DOM Level 2
  143. virtual void normalize() = 0;
  144. /// Puts all Text nodes in the full depth of the sub-tree underneath this Node,
  145. /// including attribute nodes, into a "normal" form where only structure (e.g.,
  146. /// elements, comments, processing instructions, CDATA sections, and entity
  147. /// references) separates Text nodes, i.e., there are neither adjacent Text
  148. /// nodes nor empty Text nodes. This can be used to ensure that the DOM view
  149. /// of a document is the same as if it were saved and re-loaded, and is useful
  150. /// when operations (such as XPointer lookups) that depend on a particular
  151. /// document tree structure are to be used.
  152. ///
  153. /// Note: In cases where the document contains CDATASections, the normalize
  154. /// operation alone may not be sufficient, since XPointers do not differentiate
  155. /// between Text nodes and CDATASection nodes.
  156. virtual bool isSupported(const XMLString& feature, const XMLString& version) const = 0;
  157. /// Tests whether the DOM implementation implements a specific
  158. /// feature and that feature is supported by this node.
  159. virtual const XMLString& namespaceURI() const = 0;
  160. /// Returns the namespace URI of the node.
  161. /// This is not a computed value that is the result of a namespace lookup based on an
  162. /// examination of the namespace declarations in scope. It is merely the namespace URI
  163. /// given at creation time.
  164. ///
  165. /// For nodes of any type other than ELEMENT_NODE and ATTRIBUTE_NODE and nodes created with a
  166. /// DOM Level 1 method, such as createElement from the Document interface, this is always the
  167. /// empty string.
  168. virtual XMLString prefix() const = 0;
  169. /// Returns the namespace prefix from the qualified name of the node.
  170. virtual const XMLString& localName() const = 0;
  171. /// Returns the local name of the node.
  172. virtual bool hasAttributes() const = 0;
  173. /// Returns whether this node (if it is an element) has any attributes.
  174. // Extensions
  175. typedef Poco::XML::NamespaceSupport NSMap;
  176. virtual XMLString innerText() const = 0;
  177. /// Returns a string containing the concatenated values of the node
  178. /// and all its child nodes.
  179. ///
  180. /// This method is not part of the W3C Document Object Model.
  181. virtual Node* getNodeByPath(const XMLString& path) const = 0;
  182. /// Searches a node (element or attribute) based on a simplified XPath
  183. /// expression.
  184. ///
  185. /// Only simple XPath expressions are supported. These are the slash
  186. /// notation for specifying paths to elements, and the square bracket
  187. /// expression for finding elements by their index, by attribute value,
  188. /// or finding attributes by names.
  189. ///
  190. /// The slash at the beginning is optional, the evaluation always starts
  191. /// at this element. A double-slash at the beginning recursively searches
  192. /// the entire subtree for the first element.
  193. ///
  194. /// Examples:
  195. /// elem1/elem2/elem3
  196. /// /elem1/elem2/elem3
  197. /// /elem1/elem2[1]
  198. /// /elem1/elem2[@attr1]
  199. /// /elem1/elem2[@attr1='value']
  200. /// //elem2[@attr1='value']
  201. /// //[@attr1='value']
  202. ///
  203. /// This method is an extension to the W3C Document Object Model.
  204. virtual Node* getNodeByPathNS(const XMLString& path, const NSMap& nsMap) const = 0;
  205. /// Searches a node (element or attribute) based on a simplified XPath
  206. /// expression. The given NSMap must contain mappings from namespace
  207. /// prefixes to namespace URIs for all namespace prefixes used in
  208. /// the path expression.
  209. ///
  210. /// Only simple XPath expressions are supported. These are the slash
  211. /// notation for specifying paths to elements, and the square bracket
  212. /// expression for finding elements by their index, by attribute value,
  213. /// or finding attributes by names.
  214. ///
  215. /// The slash at the beginning is optional, the evaluation always starts
  216. /// at this element. A double-slash at the beginning recursively searches
  217. /// the entire subtree for the first element.
  218. ///
  219. /// Examples:
  220. /// /ns1:elem1/ns2:elem2/ns2:elem3
  221. /// /ns1:elem1/ns2:elem2[1]
  222. /// /ns1:elem1/ns2:elem2[@attr1]
  223. /// /ns1:elem1/ns2:elem2[@attr1='value']
  224. /// //ns2:elem2[@ns1:attr1='value']
  225. /// //[@ns1:attr1='value']
  226. ///
  227. /// This method is an extension to the W3C Document Object Model.
  228. protected:
  229. virtual ~Node();
  230. };
  231. //
  232. // inlines
  233. //
  234. inline const XMLString& Node::nodeValue() const
  235. {
  236. return getNodeValue();
  237. }
  238. } } // namespace Poco::XML
  239. #endif // DOM_Node_INCLUDED