source: NonGTP/Xerces/xerces/include/xercesc/dom/deprecated/DOM_Element.hpp @ 358

Revision 358, 17.0 KB checked in by bittner, 19 years ago (diff)

xerces added

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