Element.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // Element.h
  3. //
  4. // Library: XML
  5. // Package: DOM
  6. // Module: DOM
  7. //
  8. // Definition of the DOM Element 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_Element_INCLUDED
  16. #define DOM_Element_INCLUDED
  17. #include "Poco/XML/XML.h"
  18. #include "Poco/DOM/AbstractContainerNode.h"
  19. #include "Poco/XML/Name.h"
  20. namespace Poco {
  21. namespace XML {
  22. class Attr;
  23. class NodeList;
  24. class Document;
  25. class XML_API Element: public AbstractContainerNode
  26. /// The Element interface represents an element in an XML document.
  27. /// Elements may have attributes associated with them; since the Element interface
  28. /// inherits from Node, the generic Node interface attribute attributes may
  29. /// be used to retrieve the set of all attributes for an element. There are
  30. /// methods on the Element interface to retrieve either an Attr object by name
  31. /// or an attribute value by name. In XML, where an attribute value may contain
  32. /// entity references, an Attr object should be retrieved to examine the possibly
  33. /// fairly complex sub-tree representing the attribute value.
  34. {
  35. public:
  36. const XMLString& tagName() const;
  37. /// Returns the name of the element.
  38. ///
  39. /// For example, in
  40. ///
  41. /// <elementExample id="demo">
  42. /// ...
  43. /// </elementExample>
  44. ///
  45. /// tagName has the value "elementExample". Note that this is case-preserving in XML,
  46. /// as are all of the operations of the DOM.
  47. const XMLString& getAttribute(const XMLString& name) const;
  48. /// Retrieves an attribute value by name.
  49. ///
  50. /// Returns the attribute's value, if the attribute
  51. /// exists, or an empty string otherwise.
  52. void setAttribute(const XMLString& name, const XMLString& value);
  53. /// Adds a new attribute. If an attribute with that name is already present
  54. /// in the element, its value is changed to be that of the value parameter.
  55. /// This value is a simple string; it is not parsed as it is being set. So any
  56. /// markup (such as syntax to be recognized as an entity reference) is treated
  57. /// as literal text, and needs to be appropriately escaped by the implementation
  58. /// when it is written out.
  59. void removeAttribute(const XMLString& name);
  60. /// Removes an attribute by name.
  61. Attr* getAttributeNode(const XMLString& name) const;
  62. /// Retrieves an Attr node by name.
  63. Attr* setAttributeNode(Attr* newAttr);
  64. /// Adds a new attribute. If an attribute with that name is already
  65. /// present in the element, it is replaced by the new one.
  66. Attr* addAttributeNodeNP(Attr* oldAttr, Attr* newAttr);
  67. /// For internal use only.
  68. /// Adds a new attribute after oldAttr.
  69. /// If oldAttr is 0, newAttr is set as first attribute.
  70. /// Returns newAttr.
  71. /// Does not fire any events.
  72. Attr* removeAttributeNode(Attr* oldAttr);
  73. /// Removes the specified attribute.
  74. NodeList* getElementsByTagName(const XMLString& name) const;
  75. /// Returns a NodeList of all descendant elements with a given tag
  76. /// name, in the order in which they would be encountered in a
  77. /// preorder traversal of the Element tree.
  78. ///
  79. /// The special name "*" matches all tags.
  80. ///
  81. /// The returned NodeList must be released with a call
  82. /// to release() when no longer needed.
  83. void normalize();
  84. /// Puts all Text nodes in the full depth of the sub-tree underneath this Element,
  85. /// including attribute nodes, into a "normal" form where only markup (e.g.,
  86. /// tags, comments, processing instructions, CDATA sections, and entity references)
  87. /// separates Text nodes, i.e., there are no adjacent Text nodes. This can be
  88. /// used to ensure that the DOM view of a document is the same as if it were
  89. /// saved and re-loaded, and is useful when operations (such as XPointer
  90. /// lookups) that depend on a particular document tree structure are to be used.
  91. ///
  92. /// Note: In cases where the document contains CDATASections, the normalize
  93. /// operation alone may not be sufficient, since XPointers do not differentiate
  94. /// between Text nodes and CDATASection nodes.
  95. // DOM Level 2
  96. const XMLString& getAttributeNS(const XMLString& namespaceURI, const XMLString& localName) const;
  97. /// Retrieves an attribute value by name.
  98. ///
  99. /// Returns the attribute's value, if the attribute
  100. /// exists, or an empty string otherwise.
  101. void setAttributeNS(const XMLString& namespaceURI, const XMLString& qualifiedName, const XMLString& value);
  102. /// Adds a new attribute. If an attribute with that name
  103. /// is already present in the element, its value is changed
  104. /// to be that of the value parameter.
  105. void removeAttributeNS(const XMLString& namespaceURI, const XMLString& localName);
  106. /// Removes an attribute by name.
  107. Attr* getAttributeNodeNS(const XMLString& namespaceURI, const XMLString& localName) const;
  108. /// Retrieves an Attr node by name.
  109. Attr* setAttributeNodeNS(Attr* newAttr);
  110. /// Adds a new attribute. If an attribute with that name is already
  111. /// present in the element, it is replaced by the new one.
  112. bool hasAttribute(const XMLString& name) const;
  113. /// Returns true if and only if the element has the specified attribute.
  114. bool hasAttributeNS(const XMLString& namespaceURI, const XMLString& localName) const;
  115. /// Returns true if and only if the element has the specified attribute.
  116. NodeList* getElementsByTagNameNS(const XMLString& namespaceURI, const XMLString& localName) const;
  117. /// Returns a NodeList of all the descendant Elements with a given local name and namespace URI
  118. /// in the order in which they are encountered in a preorder traversal of this Element tree.
  119. ///
  120. /// The special value "*" matches all namespaces, or local names respectively.
  121. ///
  122. /// The returned NodeList must be released with a call
  123. /// to release() when no longer needed.
  124. const XMLString& namespaceURI() const;
  125. XMLString prefix() const;
  126. const XMLString& localName() const;
  127. bool hasAttributes() const;
  128. XMLString innerText() const;
  129. Element* getChildElement(const XMLString& name) const;
  130. /// Returns the first child element with the given name, or null
  131. /// if such an element does not exist.
  132. ///
  133. /// This method is an extension to the W3C Document Object Model.
  134. Element* getChildElementNS(const XMLString& namespaceURI, const XMLString& localName) const;
  135. /// Returns the first child element with the given namespaceURI and localName,
  136. /// or null if such an element does not exist.
  137. ///
  138. /// This method is an extension to the W3C Document Object Model.
  139. Element* getElementById(const XMLString& elementId, const XMLString& idAttribute) const;
  140. /// Returns the first Element whose ID attribute (given in idAttribute)
  141. /// has the given elementId. If no such element exists, returns null.
  142. ///
  143. /// This method is an extension to the W3C Document Object Model.
  144. Element* getElementByIdNS(const XMLString& elementId, const XMLString& idAttributeURI, const XMLString& idAttributeLocalName) const;
  145. /// Returns the first Element whose ID attribute (given in idAttributeURI and idAttributeLocalName)
  146. /// has the given elementId. If no such element exists, returns null.
  147. ///
  148. /// This method is an extension to the W3C Document Object Model.
  149. // Node
  150. const XMLString& nodeName() const;
  151. NamedNodeMap* attributes() const;
  152. unsigned short nodeType() const;
  153. protected:
  154. Element(Document* pOwnerDocument, const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname);
  155. Element(Document* pOwnerDocument, const Element& elem);
  156. ~Element();
  157. Node* copyNode(bool deep, Document* pOwnerDocument) const;
  158. void dispatchNodeRemovedFromDocument();
  159. void dispatchNodeInsertedIntoDocument();
  160. private:
  161. const Name& _name;
  162. Attr* _pFirstAttr;
  163. friend class Attr;
  164. friend class Document;
  165. friend class AttrMap;
  166. };
  167. //
  168. // inlines
  169. //
  170. inline const XMLString& Element::tagName() const
  171. {
  172. return _name.qname();
  173. }
  174. } } // namespace Poco::XML
  175. #endif // DOM_Element_INCLUDED