1 /*
2 * Copyright (C) 2002 Carsten Krebs (Team-Konzept GmbH & Co.KG)
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 package com.teamkonzept.dom4jb.dom;
19
20 import org.w3c.dom.Attr;
21
22 import com.teamkonzept.dom4jb.dom.filter.NamedElementFilter;
23 import com.teamkonzept.dom4jb.schema.ElementDescriptor;
24 import com.teamkonzept.dom4jb.beans.Property;
25
26 public class Element extends NamedNode implements org.w3c.dom.Element {
27
28 private final ElementDescriptor descriptor;
29 private final Object bean;
30 private NamedNodeMap attributes;
31 private boolean isInitialized;
32
33 /*** Creates new Element */
34 public Element(final Document document,
35 final ElementDescriptor descriptor, final Object bean) {
36 super(document, descriptor.getName());
37 this.descriptor = descriptor;
38 this.bean = bean;
39 this.isInitialized = false;
40 }
41
42 /*** Creates new Element */
43 public Element(final Document document, final String qualifiedName,
44 final Object bean) {
45
46 super(document, NodeName.getInstance(qualifiedName));
47 this.bean = bean;
48 this.descriptor =
49 new ElementDescriptor(qualifiedName, Property.IDENTITY);
50 this.isInitialized = false;
51 }
52
53 /***
54 * A code representing the type of the Element
55 * @return <code>Node.ELEMENT_NODE</code>
56 */
57 public final short getNodeType() {
58 return ELEMENT_NODE;
59 }
60
61 /***
62 * The name of the element. For example, in:
63 * <pre> <elementExample
64 * id="demo"> ... </elementExample> , </pre>
65 * <code>tagName</code> has
66 * the value <code>"elementExample"</code>. Note that this is
67 * case-preserving in XML, as are all of the operations of the DOM. The
68 * HTML DOM returns the <code>tagName</code> of an HTML element in the
69 * canonical uppercase form, regardless of the case in the source HTML
70 * document.
71 */
72 public String getTagName() {
73 return getNodeName();
74 }
75
76 /***
77 * Returns a <code>NodeList</code> of all descendant <code>Elements</code>
78 * with a given tag name, in the order in which they are encountered in
79 * a preorder traversal of this <code>Element</code> tree.
80 * @param nameThe name of the tag to match on. The special value "*"
81 * matches all tags.
82 * @return A list of matching <code>Element</code> nodes.
83 */
84 public org.w3c.dom.NodeList getElementsByTagName(final String qualName) {
85 return new FilteredNodeList(
86 _getChildNodes(),
87 new NamedElementFilter(qualName));
88 }
89
90 /***
91 * Returns a <code>NodeList</code> of all the descendant
92 * <code>Elements</code> with a given local name and namespace URI in
93 * the order in which they are encountered in a preorder traversal of
94 * this <code>Element</code> tree.
95 * <br>HTML-only DOM implementations do not need to implement this method.
96 * @param namespaceURI The namespace URI of the elements to match on. The
97 * special value "*" matches all namespaces.
98 * @param localNameThe local name of the elements to match on. The
99 * special value "*" matches all local names.
100 * @return A new <code>NodeList</code> object containing all the matched
101 * <code>Elements</code>.
102 * @since DOM Level 2
103 */
104 public org.w3c.dom.NodeList getElementsByTagNameNS(
105 final String namespaceURI, final String name) {
106
107 return new FilteredNodeList(
108 _getChildNodes(),
109 new NamedElementFilter(namespaceURI, name));
110 }
111
112 /***
113 * A <code>NamedNodeMap</code> containing the attributes of this node
114 */
115 public org.w3c.dom.NamedNodeMap getAttributes() {
116 if (attributes != null) {
117 return attributes;
118 }
119 attributes =
120 new NamedNodeMap(
121 this,
122 descriptor.getAttributeIterator(document, bean));
123 return attributes;
124 }
125
126 /***
127 * Retrieves an attribute value by local name and namespace URI. HTML-only
128 * DOM implementations do not need to implement this method.
129 * @param namespaceURI The namespace URI of the attribute to retrieve.
130 * @param name The local name of the attribute to retrieve.
131 * @return The <code>Attr</code> value as a string, or the empty string
132 * if that attribute does not have a specified or default value.
133 * @since DOM Level 2
134 */
135 public String getAttributeNS(final String namespaceURI,
136 final String name) {
137
138 final Attr attr = getAttributeNodeNS(namespaceURI, name);
139 return attr == null ? null : attr.getValue();
140 // REVIEW, aber was passiert mit Attributen die nicht in der Menge
141 // drin sind
142 }
143
144 /***
145 * Retrieves an attribute value by name.
146 * @param qualifiedName The name of the attribute to retrieve.
147 * @return The <code>Attr</code> value as a string, or the empty string
148 * if that attribute does not have a specified or default value.
149 */
150 public String getAttribute(final String qualifiedName) {
151 final Attr attr = getAttributeNode(qualifiedName);
152 return attr == null ? null : attr.getValue();
153 // REVIEW, aber was passiert mit Attributen die nicht in der Menge
154 // drin sind
155 }
156
157 /***
158 * Retrieves an <code>Attr</code> node by local name and namespace URI.
159 * HTML-only DOM implementations do not need to implement this method.
160 * @param namespaceURI The namespace URI of the attribute to retrieve.
161 * @param name The local name of the attribute to retrieve.
162 * @return The <code>Attr</code> node with the specified attribute local
163 * name and namespace URI or <code>null</code> if there is no such
164 * attribute.
165 * @since DOM Level 2
166 */
167 public Attr getAttributeNodeNS(final String namespaceURI,
168 final String name) {
169
170 return (Attr) getAttributes().getNamedItemNS(namespaceURI, name);
171 }
172
173 /***
174 * Retrieves an attribute node by name.
175 * <br>To retrieve an attribute node by qualified name and namespace URI,
176 * use the <code>getAttributeNodeNS</code> method.
177 * @param qualifiedName The name (<code>nodeName</code>) of the attribute
178 * to retrieve.
179 * @return The <code>Attr</code> node with the specified name (
180 * <code>nodeName</code>) or <code>null</code> if there is no such
181 * attribute.
182 */
183 public Attr getAttributeNode(final String qualifiedName) {
184 return (Attr) getAttributes().getNamedItem(qualifiedName);
185 }
186
187 /***
188 * Returns <code>true</code> when an attribute with a given name is
189 * specified on this element or has a default value, <code>false</code>
190 * otherwise.
191 * @param qualifiedName The name of the attribute to look for.
192 * @return <code>true</code> if an attribute with the given name is
193 * specified on this element or has a default value, <code>false</code>
194 * otherwise.
195 * @since DOM Level 2
196 */
197 public boolean hasAttribute(final String qualifiedName) {
198 return getAttributeNode(qualifiedName) != null;
199 }
200
201 /***
202 * Returns <code>true</code> when an attribute with a given local name and
203 * namespace URI is specified on this element or has a default value,
204 * <code>false</code> otherwise. HTML-only DOM implementations do not
205 * need to implement this method.
206 * @param namespaceURI The namespace URI of the attribute to look for.
207 * @param name The local name of the attribute to look for.
208 * @return <code>true</code> if an attribute with the given local name
209 * and namespace URI is specified or has a default value on this
210 * element, <code>false</code> otherwise.
211 * @since DOM Level 2
212 */
213 public boolean hasAttributeNS(final String namespaceURI,
214 final String name) {
215
216 return getAttributeNodeNS(namespaceURI, name) != null;
217 }
218
219 /***
220 * Removes the specified attribute node. If the removed <code>Attr</code>
221 * has a default value it is immediately replaced. The replacing
222 * attribute has the same namespace URI and local name, as well as the
223 * original prefix, when applicable.
224 * @param attr The <code>Attr</code> node to remove from the attribute
225 * list.
226 * @return The <code>Attr</code> node that was removed.
227 */
228 public org.w3c.dom.Attr removeAttributeNode(final org.w3c.dom.Attr attr) {
229 throw new UnsupportedOperationException(
230 "Method removeAttributeNode(org.w3c.dom.Attr)"
231 + " not yet implemented.");
232 }
233
234 /***
235 * Removes an attribute by local name and namespace URI. If the removed
236 * attribute has a default value it is immediately replaced. The
237 * replacing attribute has the same namespace URI and local name, as
238 * well as the original prefix.
239 * <br>HTML-only DOM implementations do not need to implement this method.
240 * @param namespaceURI The namespace URI of the attribute to remove.
241 * @param localName The local name of the attribute to remove.
242 * @since DOM Level 2
243 */
244 public void removeAttributeNS(final String namespaceURI,
245 final String localName) {
246 throw new UnsupportedOperationException(
247 "Method removeAttributeNS("
248 + "String, String) not yet implemented.");
249 }
250
251 /***
252 * Removes an attribute by name. If the removed attribute is known to have
253 * a default value, an attribute immediately appears containing the
254 * default value as well as the corresponding namespace URI, local name,
255 * and prefix when applicable.
256 * <br>To remove an attribute by local name and namespace URI, use the
257 * <code>removeAttributeNS</code> method.
258 * @param name The name of the attribute to remove.
259 */
260 public void removeAttribute(final String name) {
261 throw new UnsupportedOperationException(
262 "Method removeAttribute(String) not yet implemented.");
263 }
264
265 /***
266 * A <code>NodeList</code> that contains all children of this node. If
267 * there are no children, this is a <code>NodeList</code> containing no
268 * nodes.
269 */
270 public final NodeList _getChildNodes() {
271 if (!isInitialized) {
272 setIterator(descriptor.getChildNodeIterator(document, bean));
273 isInitialized = true;
274 }
275 return this;
276 }
277
278 /***
279 * A <code>NodeList</code> that contains all children of this node. If
280 * there are no children, this is a <code>NodeList</code> containing no
281 * nodes.
282 */
283 public org.w3c.dom.NodeList getChildNodes() {
284 return _getChildNodes();
285 }
286
287 /***
288 * Adds a new attribute. If an attribute with that name is already present
289 * in the element, its value is changed to be that of the value
290 * parameter. This value is a simple string; it is not parsed as it is
291 * being set. So any markup (such as syntax to be recognized as an
292 * entity reference) is treated as literal text, and needs to be
293 * appropriately escaped by the implementation when it is written out.
294 * In order to assign an attribute value that contains entity
295 * references, the user must create an <code>Attr</code> node plus any
296 * <code>Text</code> and <code>EntityReference</code> nodes, build the
297 * appropriate subtree, and use <code>setAttributeNode</code> to assign
298 * it as the value of an attribute.
299 * <br>To set an attribute with a qualified name and namespace URI, use
300 * the <code>setAttributeNS</code> method.
301 * @param name The name of the attribute to create or alter.
302 * @param value Value to set in string form.
303 */
304 public void setAttribute(final String name, final String value) {
305 throw new UnsupportedOperationException(
306 "Method setAttribute(" + "String, String) not yet implemented.");
307 }
308
309 /***
310 * Adds a new attribute node. If an attribute with that name (
311 * <code>nodeName</code>) is already present in the element, it is
312 * replaced by the new one.
313 * <br>To add a new attribute node with a qualified name and namespace
314 * URI, use the <code>setAttributeNodeNS</code> method.
315 * @param attr The <code>Attr</code> node to add to the attribute list.
316 * @return If the <code>newAttr</code> attribute replaces an existing
317 * attribute, the replaced <code>Attr</code> node is returned,
318 * otherwise <code>null</code> is returned.
319 */
320 public org.w3c.dom.Attr setAttributeNode(final org.w3c.dom.Attr attr) {
321 throw new UnsupportedOperationException(
322 "Method setAttributeNode("
323 + "org.w3c.dom.Attr) not yet implemented.");
324 }
325
326 /***
327 * Adds a new attribute. If an attribute with that local name and that
328 * namespace URI is already present in the element, it is replaced by
329 * the new one.
330 * <br>HTML-only DOM implementations do not need to implement this method.
331 * @param attr The <code>Attr</code> node to add to the attribute list.
332 * @return If the <code>newAttr</code> attribute replaces an existing
333 * attribute with the same local name and namespace URI, the replaced
334 * <code>Attr</code> node is returned, otherwise <code>null</code> is
335 * returned.
336 * @since DOM Level 2
337 */
338 public org.w3c.dom.Attr setAttributeNodeNS(final org.w3c.dom.Attr attr) {
339 throw new UnsupportedOperationException(
340 "Method setAttributeNodeNS("
341 + "org.w3c.dom.Attr) not yet implemented.");
342 }
343
344 /***
345 * Adds a new attribute. If an attribute with the same local name and
346 * namespace URI is already present on the element, its prefix is
347 * changed to be the prefix part of the <code>qualifiedName</code>, and
348 * its value is changed to be the <code>value</code> parameter. This
349 * value is a simple string; it is not parsed as it is being set. So any
350 * markup (such as syntax to be recognized as an entity reference) is
351 * treated as literal text, and needs to be appropriately escaped by the
352 * implementation when it is written out. In order to assign an
353 * attribute value that contains entity references, the user must create
354 * an <code>Attr</code> node plus any <code>Text</code> and
355 * <code>EntityReference</code> nodes, build the appropriate subtree,
356 * and use <code>setAttributeNodeNS</code> or
357 * <code>setAttributeNode</code> to assign it as the value of an
358 * attribute.
359 * <br>HTML-only DOM implementations do not need to implement this method.
360 * @param namespaceURI The namespace URI of the attribute to create or
361 * alter.
362 * @param qualifiedName The qualified name of the attribute to create or
363 * alter.
364 * @param value The value to set in string form.
365 * @since DOM Level 2
366 */
367 public void setAttributeNS(final String namespaceURI,
368 final String qualifiedName, final String value) {
369
370 throw new UnsupportedOperationException(
371 "Method setAttributeNS("
372 + "String, String, String) not yet implemented.");
373 }
374
375 /***
376 * @see com.teamkonzept.dom4jb.dom.Node#accept(Filter)
377 */
378 public boolean accept(final Filter filter) {
379 return filter.match((Element) this);
380 }
381 }
This page was automatically generated by Maven