Attr.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // Attr.h
  3. //
  4. // Library: XML
  5. // Package: DOM
  6. // Module: DOM
  7. //
  8. // Definition of the DOM Attr 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_Attr_INCLUDED
  16. #define DOM_Attr_INCLUDED
  17. #include "Poco/XML/XML.h"
  18. #include "Poco/DOM/AbstractNode.h"
  19. #include "Poco/DOM/Element.h"
  20. #include "Poco/XML/Name.h"
  21. namespace Poco {
  22. namespace XML {
  23. class XML_API Attr: public AbstractNode
  24. /// The Attr interface represents an attribute in an Element object. Typically
  25. /// the allowable values for the attribute are defined in a document type definition.
  26. ///
  27. /// Attr objects inherit the Node interface, but since they are not actually
  28. /// child nodes of the element they describe, the DOM does not consider them
  29. /// part of the document tree. Thus, the Node attributes parentNode, previousSibling,
  30. /// and nextSibling have a null value for Attr objects. The DOM takes the view
  31. /// that attributes are properties of elements rather than having a separate
  32. /// identity from the elements they are associated with; this should make it
  33. /// more efficient to implement such features as default attributes associated
  34. /// with all elements of a given type. Furthermore, Attr nodes may not be immediate
  35. /// children of a DocumentFragment. However, they can be associated with Element
  36. /// nodes contained within a DocumentFragment. In short, users and implementors
  37. /// of the DOM need to be aware that Attr nodes have some things in common with
  38. /// other objects inheriting the Node interface, but they also are quite distinct.
  39. ///
  40. /// The attribute's effective value is determined as follows: if this attribute
  41. /// has been explicitly assigned any value, that value is the attribute's effective
  42. /// value; otherwise, if there is a declaration for this attribute, and that
  43. /// declaration includes a default value, then that default value is the attribute's
  44. /// effective value; otherwise, the attribute does not exist on this element
  45. /// in the structure model until it has been explicitly added. Note that the
  46. /// nodeValue attribute on the Attr instance can also be used to retrieve the
  47. /// string version of the attribute's value(s).
  48. ///
  49. /// In XML, where the value of an attribute can contain entity references, the
  50. /// child nodes of the Attr node provide a representation in which entity references
  51. /// are not expanded. These child nodes may be either Text or EntityReference
  52. /// nodes. Because the attribute type may be unknown, there are no tokenized
  53. /// attribute values.
  54. {
  55. public:
  56. const XMLString& name() const;
  57. /// Returns the name of this attribute.
  58. bool specified() const;
  59. /// If this attribute was explicitly given a value in the original document,
  60. /// this is true; otherwise, it is false. Note that the implementation is in
  61. /// charge of this attribute, not the user. If the user changes the value of
  62. /// the attribute (even if it ends up having the same value as the default value)
  63. /// then the specified flag is automatically flipped to true. To re-specify
  64. /// the attribute as the default value from the DTD, the user must delete the
  65. /// attribute. The implementation will then make a new attribute available with
  66. /// specified set to false and the default value (if one exists).
  67. /// In summary:
  68. ///
  69. /// * If the attribute has an assigned value in the document then specified
  70. /// is true, and the value is the assigned value.
  71. /// * If the attribute has no assigned value in the document and has a default
  72. /// value in the DTD, then specified is false, and the value is the default
  73. /// value in the DTD.
  74. /// * If the attribute has no assigned value in the document and has a value
  75. /// of #IMPLIED in the DTD, then the attribute does not appear in the structure
  76. /// model of the document.
  77. /// * If the attribute is not associated to any element (i.e. because it
  78. /// was just created or was obtained from some removal or cloning operation)
  79. /// specified is true.
  80. const XMLString& value() const;
  81. /// Returns the value of the attribute as a string. Character
  82. /// and general entity references are replaced with their values. See also the
  83. /// method getAttribute on the Element interface.
  84. const XMLString& getValue() const;
  85. /// Returns the value of the attribute as a string. Character
  86. /// and general entity references are replaced with their values. See also the
  87. /// method getAttribute on the Element interface.
  88. void setValue(const XMLString& value);
  89. /// Sets the value of the attribute as a string.
  90. /// This creates a Text node with the unparsed contents of the string.
  91. /// I.e. any characters that an XML processor would recognize as markup are
  92. /// instead treated as literal text. See also the method setAttribute on the
  93. /// Element interface.
  94. // DOM Level 2
  95. Element* ownerElement() const;
  96. /// The Element node this attribute is attached to or null
  97. /// if this attribute is not in use.
  98. // Node
  99. Node* parentNode() const;
  100. const XMLString& nodeName() const;
  101. const XMLString& getNodeValue() const;
  102. void setNodeValue(const XMLString& value);
  103. unsigned short nodeType() const;
  104. Node* previousSibling() const;
  105. const XMLString& namespaceURI() const;
  106. XMLString prefix() const;
  107. const XMLString& localName() const;
  108. // Non-standard extensions
  109. XMLString innerText() const;
  110. protected:
  111. Attr(Document* pOwnerDocument, Element* pOwnerElement, const XMLString& namespaceURI, const XMLString& localName, const XMLString& qname, const XMLString& value, bool specified = true);
  112. Attr(Document* pOwnerDocument, const Attr& attr);
  113. ~Attr();
  114. Node* copyNode(bool deep, Document* pOwnerDocument) const;
  115. private:
  116. const Name& _name;
  117. XMLString _value;
  118. bool _specified;
  119. friend class Document;
  120. friend class Element;
  121. friend class DOMBuilder;
  122. };
  123. //
  124. // inlines
  125. //
  126. inline const XMLString& Attr::name() const
  127. {
  128. return _name.qname();
  129. }
  130. inline const XMLString& Attr::value() const
  131. {
  132. return _value;
  133. }
  134. inline const XMLString& Attr::getValue() const
  135. {
  136. return _value;
  137. }
  138. inline bool Attr::specified() const
  139. {
  140. return _specified;
  141. }
  142. inline Element* Attr::ownerElement() const
  143. {
  144. return static_cast<Element*>(_pParent);
  145. }
  146. } } // namespace Poco::XML
  147. #endif // DOM_Attr_INCLUDED