[2674] | 1 | /* |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
| 3 | * contributor license agreements. See the NOTICE file distributed with |
---|
| 4 | * this work for additional information regarding copyright ownership. |
---|
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
| 6 | * (the "License"); you may not use this file except in compliance with |
---|
| 7 | * the License. You may obtain a copy of the License at |
---|
| 8 | * |
---|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 10 | * |
---|
| 11 | * Unless required by applicable law or agreed to in writing, software |
---|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 14 | * See the License for the specific language governing permissions and |
---|
| 15 | * limitations under the License. |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | /* |
---|
| 19 | * $Id: DOM_Element.hpp 568078 2007-08-21 11:43:25Z amassari $ |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #ifndef DOM_Element_HEADER_GUARD_ |
---|
| 23 | #define DOM_Element_HEADER_GUARD_ |
---|
| 24 | |
---|
| 25 | #include <xercesc/util/XercesDefs.hpp> |
---|
| 26 | #include "DOM_Node.hpp" |
---|
| 27 | |
---|
| 28 | XERCES_CPP_NAMESPACE_BEGIN |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | class DOM_Attr; |
---|
| 32 | class DOM_NodeList; |
---|
| 33 | class ElementImpl; |
---|
| 34 | |
---|
| 35 | /** |
---|
| 36 | * By far the vast majority of objects (apart from text) that authors |
---|
| 37 | * encounter when traversing a document are <code>DOM_Element</code> nodes. |
---|
| 38 | * |
---|
| 39 | * Assume the following XML document:<elementExample id="demo"> |
---|
| 40 | * <subelement1/> |
---|
| 41 | * <subelement2><subsubelement/></subelement2> |
---|
| 42 | * </elementExample> |
---|
| 43 | * <p>When represented using DOM, the top node is an <code>DOM_Element</code> node |
---|
| 44 | * for "elementExample", which contains two child <code>DOM_Element</code> nodes, |
---|
| 45 | * one for "subelement1" and one for "subelement2". "subelement1" contains no |
---|
| 46 | * child nodes. |
---|
| 47 | * <p>Elements may have attributes associated with them; since the |
---|
| 48 | * <code>DOM_Element</code> interface inherits from <code>DOM_Node</code>, the generic |
---|
| 49 | * <code>DOM_Node</code> interface method <code>getAttributes</code> may be used |
---|
| 50 | * to retrieve the set of all attributes for an element. There are methods on |
---|
| 51 | * the <code>DOM_Element</code> interface to retrieve either an <code>DOM_Attr</code> |
---|
| 52 | * object by name or an attribute value by name. In XML, where an attribute |
---|
| 53 | * value may contain entity references, an <code>DOM_Attr</code> object should be |
---|
| 54 | * retrieved to examine the possibly fairly complex sub-tree representing the |
---|
| 55 | * attribute value. On the other hand, in HTML, where all attributes have |
---|
| 56 | * simple string values, methods to directly access an attribute value can |
---|
| 57 | * safely be used as a convenience. |
---|
| 58 | */ |
---|
| 59 | |
---|
| 60 | class DEPRECATED_DOM_EXPORT DOM_Element: public DOM_Node { |
---|
| 61 | private: |
---|
| 62 | |
---|
| 63 | public: |
---|
| 64 | /** @name Constructors and assignment operator */ |
---|
| 65 | //@{ |
---|
| 66 | /** |
---|
| 67 | * Default constructor for DOM_Element. The resulting object does not |
---|
| 68 | * refer to an actual Element node; it will compare == to 0, and is similar |
---|
| 69 | * to a null object reference variable in Java. It may subsequently be |
---|
| 70 | * assigned to refer to an actual Element node. |
---|
| 71 | * <p> |
---|
| 72 | * New comment nodes are created by DOM_Document::createElement(). |
---|
| 73 | * |
---|
| 74 | */ |
---|
| 75 | DOM_Element(); |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * Copy constructor. Creates a new <code>DOM_Element</code> that refers to the |
---|
| 79 | * same underlying actual element as the original. |
---|
| 80 | * |
---|
| 81 | * @param other The object to be copied |
---|
| 82 | */ |
---|
| 83 | DOM_Element(const DOM_Element &other); |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | * Assignment operator. |
---|
| 87 | * |
---|
| 88 | * @param other The object to be copied. |
---|
| 89 | */ |
---|
| 90 | DOM_Element & operator = (const DOM_Element &other); |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | * Assignment operator. This overloaded variant is provided for |
---|
| 94 | * the sole purpose of setting a DOM_Node reference variable to |
---|
| 95 | * zero. Nulling out a reference variable in this way will decrement |
---|
| 96 | * the reference count on the underlying Node object that the variable |
---|
| 97 | * formerly referenced. This effect is normally obtained when reference |
---|
| 98 | * variable goes out of scope, but zeroing them can be useful for |
---|
| 99 | * global instances, or for local instances that will remain in scope |
---|
| 100 | * for an extended time, when the storage belonging to the underlying |
---|
| 101 | * node needs to be reclaimed. |
---|
| 102 | * |
---|
| 103 | * @param val Only a value of 0, or null, is allowed. |
---|
| 104 | */ |
---|
| 105 | DOM_Element & operator = (const DOM_NullPtr *val); |
---|
| 106 | |
---|
| 107 | //@} |
---|
| 108 | /** @name Destructor. */ |
---|
| 109 | //@{ |
---|
| 110 | /** |
---|
| 111 | * Destructor. The object being destroyed is the reference |
---|
| 112 | * object, not the underlying Element itself. |
---|
| 113 | * |
---|
| 114 | */ |
---|
| 115 | ~DOM_Element(); |
---|
| 116 | //@} |
---|
| 117 | /** @name Getter functions. */ |
---|
| 118 | //@{ |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | * The name of the element. |
---|
| 122 | * |
---|
| 123 | * For example, in: <elementExample |
---|
| 124 | * id="demo"> ... </elementExample> , <code>tagName</code> has |
---|
| 125 | * the value <code>"elementExample"</code>. Note that this is |
---|
| 126 | * case-preserving in XML, as are all of the operations of the DOM. |
---|
| 127 | */ |
---|
| 128 | DOMString getTagName() const; |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | * Retrieves an attribute value by name. |
---|
| 132 | * |
---|
| 133 | * @param name The name of the attribute to retrieve. |
---|
| 134 | * @return The <code>DOM_Attr</code> value as a string, or the empty string if |
---|
| 135 | * that attribute does not have a specified or default value. |
---|
| 136 | */ |
---|
| 137 | DOMString getAttribute(const DOMString &name) const; |
---|
| 138 | |
---|
| 139 | /** |
---|
| 140 | * Retrieves an <code>DOM_Attr</code> node by name. |
---|
| 141 | * |
---|
| 142 | * @param name The name (<CODE>nodeName</CODE>) of the attribute to retrieve. |
---|
| 143 | * @return The <code>DOM_Attr</code> node with the specified name (<CODE>nodeName</CODE>) or |
---|
| 144 | * <code>null</code> if there is no such attribute. |
---|
| 145 | */ |
---|
| 146 | DOM_Attr getAttributeNode(const DOMString &name) const; |
---|
| 147 | |
---|
| 148 | /** |
---|
| 149 | * Returns a <code>NodeList</code> of all descendant elements with a given |
---|
| 150 | * tag name, in the order in which they would be encountered in a preorder |
---|
| 151 | * traversal of the <code>DOM_Element</code> tree. |
---|
| 152 | * |
---|
| 153 | * @param name The name of the tag to match on. The special value "*" |
---|
| 154 | * matches all tags. |
---|
| 155 | * @return A list of matching <code>DOM_Element</code> nodes. |
---|
| 156 | */ |
---|
| 157 | DOM_NodeList getElementsByTagName(const DOMString &name) const; |
---|
| 158 | |
---|
| 159 | //@} |
---|
| 160 | /** @name Set functions. */ |
---|
| 161 | //@{ |
---|
| 162 | |
---|
| 163 | /** |
---|
| 164 | * Adds a new attribute. |
---|
| 165 | * |
---|
| 166 | * If an attribute with that name is already present |
---|
| 167 | * in the element, its value is changed to be that of the value parameter. |
---|
| 168 | * This value is a simple string, it is not parsed as it is being set. So |
---|
| 169 | * any markup (such as syntax to be recognized as an entity reference) is |
---|
| 170 | * treated as literal text, and needs to be appropriately escaped by the |
---|
| 171 | * implementation when it is written out. In order to assign an attribute |
---|
| 172 | * value that contains entity references, the user must create an |
---|
| 173 | * <code>DOM_Attr</code> node plus any <code>Text</code> and |
---|
| 174 | * <code>EntityReference</code> nodes, build the appropriate subtree, and |
---|
| 175 | * use <code>setAttributeNode</code> to assign it as the value of an |
---|
| 176 | * attribute. |
---|
| 177 | * @param name The name of the attribute to create or alter. |
---|
| 178 | * @param value Value to set in string form. |
---|
| 179 | * @exception DOMException |
---|
| 180 | * INVALID_CHARACTER_ERR: Raised if the specified name contains an |
---|
| 181 | * illegal character. |
---|
| 182 | * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. |
---|
| 183 | */ |
---|
| 184 | void setAttribute(const DOMString &name, |
---|
| 185 | const DOMString &value); |
---|
| 186 | /** |
---|
| 187 | * Adds a new attribute. |
---|
| 188 | * |
---|
| 189 | * If an attribute with that name (<CODE>nodeName</CODE>) is already present |
---|
| 190 | * in the element, it is replaced by the new one. |
---|
| 191 | * @param newAttr The <code>DOM_Attr</code> node to add to the attribute list. |
---|
| 192 | * @return If the <code>newAttr</code> attribute replaces an existing |
---|
| 193 | * attribute, the replaced |
---|
| 194 | * <code>DOM_Attr</code> node is returned, otherwise <code>null</code> is |
---|
| 195 | * returned. |
---|
| 196 | * @exception DOMException |
---|
| 197 | * WRONG_DOCUMENT_ERR: Raised if <code>newAttr</code> was created from a |
---|
| 198 | * different document than the one that created the element. |
---|
| 199 | * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. |
---|
| 200 | * <br>INUSE_ATTRIBUTE_ERR: Raised if <code>newAttr</code> is already an |
---|
| 201 | * attribute of another <code>DOM_Element</code> object. The DOM user must |
---|
| 202 | * explicitly clone <code>DOM_Attr</code> nodes to re-use them in other |
---|
| 203 | * elements. |
---|
| 204 | */ |
---|
| 205 | DOM_Attr setAttributeNode(DOM_Attr newAttr); |
---|
| 206 | |
---|
| 207 | //@} |
---|
| 208 | /** @name Functions which modify the Element. */ |
---|
| 209 | //@{ |
---|
| 210 | /** |
---|
| 211 | * Removes the specified attribute node. |
---|
| 212 | * If the removed <CODE>DOM_Attr</CODE> |
---|
| 213 | * has a default value it is immediately replaced. The replacing attribute |
---|
| 214 | * has the same namespace URI and local name, as well as the original prefix, |
---|
| 215 | * when applicable. |
---|
| 216 | * |
---|
| 217 | * @param oldAttr The <code>DOM_Attr</code> node to remove from the attribute |
---|
| 218 | * list. |
---|
| 219 | * @return The <code>DOM_Attr</code> node that was removed. |
---|
| 220 | * @exception DOMException |
---|
| 221 | * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. |
---|
| 222 | * <br>NOT_FOUND_ERR: Raised if <code>oldAttr</code> is not an attribute |
---|
| 223 | * of the element. |
---|
| 224 | */ |
---|
| 225 | DOM_Attr removeAttributeNode(DOM_Attr oldAttr); |
---|
| 226 | |
---|
| 227 | /** |
---|
| 228 | * Removes an attribute by name. |
---|
| 229 | * |
---|
| 230 | * If the removed attribute |
---|
| 231 | * is known to have a default value, an attribute immediately appears |
---|
| 232 | * containing the default value as well as the corresponding namespace URI, |
---|
| 233 | * local name, and prefix when applicable.<BR>To remove an attribute by local |
---|
| 234 | * name and namespace URI, use the <CODE>removeAttributeNS</CODE> method. |
---|
| 235 | * @param name The name of the attribute to remove. |
---|
| 236 | * @exception DOMException |
---|
| 237 | * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. |
---|
| 238 | */ |
---|
| 239 | void removeAttribute(const DOMString &name); |
---|
| 240 | |
---|
| 241 | //@} |
---|
| 242 | /** @name Functions introduced in DOM Level 2. */ |
---|
| 243 | //@{ |
---|
| 244 | |
---|
| 245 | /** |
---|
| 246 | * Retrieves an attribute value by local name and namespace URI. |
---|
| 247 | * |
---|
| 248 | * @param namespaceURI The <em>namespace URI</em> of |
---|
| 249 | * the attribute to retrieve. |
---|
| 250 | * @param localName The <em>local name</em> of the |
---|
| 251 | * attribute to retrieve. |
---|
| 252 | * @return The <code>DOM_Attr</code> value as a string, or an <CODE>null</CODE> if |
---|
| 253 | * that attribute does not have a specified or default value. |
---|
| 254 | */ |
---|
| 255 | DOMString getAttributeNS(const DOMString &namespaceURI, |
---|
| 256 | const DOMString &localName) const; |
---|
| 257 | |
---|
| 258 | /** |
---|
| 259 | * Adds a new attribute. If an attribute with the same |
---|
| 260 | * local name and namespace URI is already present on the element, its prefix |
---|
| 261 | * is changed to be the prefix part of the <CODE>qualifiedName</CODE>, and |
---|
| 262 | * its value is changed to be the <CODE>value</CODE> parameter. This value is |
---|
| 263 | * a simple string, it is not parsed as it is being set. So any markup (such |
---|
| 264 | * as syntax to be recognized as an entity reference) is treated as literal |
---|
| 265 | * text, and needs to be appropriately escaped by the implementation when it |
---|
| 266 | * is written out. In order to assign an attribute value that contains entity |
---|
| 267 | * references, the user must create an <CODE>DOM_Attr</CODE> |
---|
| 268 | * node plus any <CODE>DOM_Text</CODE> and <CODE>DOM_EntityReference</CODE> |
---|
| 269 | * nodes, build the appropriate subtree, and use |
---|
| 270 | * <CODE>setAttributeNodeNS</CODE> or <CODE>setAttributeNode</CODE> to assign |
---|
| 271 | * it as the value of an attribute. |
---|
| 272 | * |
---|
| 273 | * @param namespaceURI The <em>namespace URI</em> of |
---|
| 274 | * the attribute to create or alter. |
---|
| 275 | * @param qualifiedName The <em>qualified name</em> of the |
---|
| 276 | * attribute to create or alter. |
---|
| 277 | * @param value The value to set in string form. |
---|
| 278 | * @exception DOMException |
---|
| 279 | * INVALID_CHARACTER_ERR: Raised if the specified qualified name contains an |
---|
| 280 | * illegal character. |
---|
| 281 | * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. |
---|
| 282 | * <br> |
---|
| 283 | * NAMESPACE_ERR: Raised if the <CODE>qualifiedName</CODE> is |
---|
| 284 | * malformed, if the <CODE>qualifiedName</CODE> has a prefix and the |
---|
| 285 | * <CODE>namespaceURI</CODE> is <CODE>null</CODE> or an empty string, |
---|
| 286 | * if the <CODE>qualifiedName</CODE> has a prefix that is "xml" and the |
---|
| 287 | * <CODE>namespaceURI</CODE> is different from |
---|
| 288 | * "http://www.w3.org/XML/1998/namespace", if the |
---|
| 289 | * <CODE>qualifiedName</CODE> has a prefix that is "xmlns" and the |
---|
| 290 | * <CODE>namespaceURI</CODE> is different from |
---|
| 291 | * "http://www.w3.org/2000/xmlns/", or if the |
---|
| 292 | * <CODE>qualifiedName</CODE> is "xmlns" and the |
---|
| 293 | * <CODE>namespaceURI</CODE> is different from |
---|
| 294 | * "http://www.w3.org/2000/xmlns/". |
---|
| 295 | */ |
---|
| 296 | void setAttributeNS(const DOMString &namespaceURI, |
---|
| 297 | const DOMString &qualifiedName, const DOMString &value); |
---|
| 298 | |
---|
| 299 | /** |
---|
| 300 | * Removes an attribute by local name and namespace URI. If the |
---|
| 301 | * removed attribute has a default value it is immediately replaced. |
---|
| 302 | * The replacing attribute has the same namespace URI and local name, as well as |
---|
| 303 | * the original prefix. |
---|
| 304 | * |
---|
| 305 | * @param namespaceURI The <em>namespace URI</em> of |
---|
| 306 | * the attribute to remove. |
---|
| 307 | * @param localName The <em>local name</em> of the |
---|
| 308 | * attribute to remove. |
---|
| 309 | * @exception DOMException |
---|
| 310 | * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. |
---|
| 311 | */ |
---|
| 312 | void removeAttributeNS(const DOMString &namespaceURI, |
---|
| 313 | const DOMString &localName); |
---|
| 314 | |
---|
| 315 | /** |
---|
| 316 | * Retrieves an <code>DOM_Attr</code> node by local name and namespace URI. |
---|
| 317 | * |
---|
| 318 | * @param namespaceURI The <em>namespace URI</em> of |
---|
| 319 | * the attribute to retrieve. |
---|
| 320 | * @param localName The <em>local name</em> of the |
---|
| 321 | * attribute to retrieve. |
---|
| 322 | * @return The <code>DOM_Attr</code> node with the specified attribute local |
---|
| 323 | * name and namespace URI or <code>null</code> if there is no such attribute. |
---|
| 324 | */ |
---|
| 325 | DOM_Attr getAttributeNodeNS(const DOMString &namespaceURI, |
---|
| 326 | const DOMString &localName) const; |
---|
| 327 | |
---|
| 328 | /** |
---|
| 329 | * Adds a new attribute. |
---|
| 330 | * |
---|
| 331 | * If an attribute with that local name and namespace URI is already present |
---|
| 332 | * in the element, it is replaced by the new one. |
---|
| 333 | * |
---|
| 334 | * @param newAttr The <code>DOM_Attr</code> node to add to the attribute list. |
---|
| 335 | * @return If the <code>newAttr</code> attribute replaces an existing |
---|
| 336 | * attribute with the same <em>local name</em> and <em>namespace URI</em>, |
---|
| 337 | * the replaced <code>DOM_Attr</code> node is |
---|
| 338 | * returned, otherwise <code>null</code> is returned. |
---|
| 339 | * @exception DOMException |
---|
| 340 | * WRONG_DOCUMENT_ERR: Raised if <code>newAttr</code> was created from a |
---|
| 341 | * different document than the one that created the element. |
---|
| 342 | * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. |
---|
| 343 | * <br>INUSE_ATTRIBUTE_ERR: Raised if <code>newAttr</code> is already an |
---|
| 344 | * attribute of another <code>DOM_Element</code> object. The DOM user must |
---|
| 345 | * explicitly clone <code>DOM_Attr</code> nodes to re-use them in other |
---|
| 346 | * elements. |
---|
| 347 | */ |
---|
| 348 | DOM_Attr setAttributeNodeNS(DOM_Attr newAttr); |
---|
| 349 | |
---|
| 350 | /** |
---|
| 351 | * Returns a <code>DOM_NodeList</code> of all the <code>DOM_Element</code>s |
---|
| 352 | * with a given local name and namespace URI in the order in which they |
---|
| 353 | * would be encountered in a preorder traversal of the |
---|
| 354 | * <code>DOM_Document</code> tree, starting from this node. |
---|
| 355 | * |
---|
| 356 | * @param namespaceURI The <em>namespace URI</em> of |
---|
| 357 | * the elements to match on. The special value "*" matches all |
---|
| 358 | * namespaces. |
---|
| 359 | * @param localName The <em>local name</em> of the |
---|
| 360 | * elements to match on. The special value "*" matches all local names. |
---|
| 361 | * @return A new <code>DOM_NodeList</code> object containing all the matched |
---|
| 362 | * <code>DOM_Element</code>s. |
---|
| 363 | */ |
---|
| 364 | DOM_NodeList getElementsByTagNameNS(const DOMString &namespaceURI, |
---|
| 365 | const DOMString &localName) const; |
---|
| 366 | |
---|
| 367 | /** |
---|
| 368 | * Returns whether this node (if it is an element) has any attributes. |
---|
| 369 | * @return <code>true</code> if this node has any attributes, |
---|
| 370 | * <code>false</code> otherwise. |
---|
| 371 | */ |
---|
| 372 | bool hasAttributes() const; |
---|
| 373 | |
---|
| 374 | /** |
---|
| 375 | * Returns <code>true</code> when an attribute with a given name is |
---|
| 376 | * specified on this element or has a default value, <code>false</code> |
---|
| 377 | * otherwise. |
---|
| 378 | * @param name The name of the attribute to look for. |
---|
| 379 | * @return <code>true</code> if an attribute with the given name is |
---|
| 380 | * specified on this element or has a default value, <code>false</code> |
---|
| 381 | * otherwise. |
---|
| 382 | */ |
---|
| 383 | bool hasAttribute(const DOMString &name) const; |
---|
| 384 | |
---|
| 385 | /** |
---|
| 386 | * Returns <code>true</code> when an attribute with a given local name and |
---|
| 387 | * namespace URI is specified on this element or has a default value, |
---|
| 388 | * <code>false</code> otherwise. HTML-only DOM implementations do not |
---|
| 389 | * need to implement this method. |
---|
| 390 | * @param namespaceURI The namespace URI of the attribute to look for. |
---|
| 391 | * @param localName The local name of the attribute to look for. |
---|
| 392 | * @return <code>true</code> if an attribute with the given local name |
---|
| 393 | * and namespace URI is specified or has a default value on this |
---|
| 394 | * element, <code>false</code> otherwise. |
---|
| 395 | * @since DOM Level 2 |
---|
| 396 | */ |
---|
| 397 | bool hasAttributeNS(const DOMString &namespaceURI, |
---|
| 398 | const DOMString &localName) const; |
---|
| 399 | |
---|
| 400 | //@} |
---|
| 401 | |
---|
| 402 | protected: |
---|
| 403 | DOM_Element(ElementImpl *impl); |
---|
| 404 | |
---|
| 405 | friend class DOM_Document; |
---|
| 406 | friend class DOM_Attr; |
---|
| 407 | }; |
---|
| 408 | |
---|
| 409 | XERCES_CPP_NAMESPACE_END |
---|
| 410 | |
---|
| 411 | #endif |
---|
| 412 | |
---|
| 413 | |
---|