KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > dom > ElementOverNodeInfo


1 package net.sf.saxon.dom;
2
3 import net.sf.saxon.om.*;
4 import net.sf.saxon.pattern.NameTest;
5 import net.sf.saxon.type.Type;
6 import org.w3c.dom.*;
7
8 /**
9  * This class is an implementation of the DOM Element class that wraps a Saxon NodeInfo
10  * representation of an element node.
11  */

12
13 public class ElementOverNodeInfo extends NodeOverNodeInfo implements Element {
14
15     /**
16      * The name of the element (DOM interface).
17      */

18
19     public String JavaDoc getTagName() {
20         return node.getDisplayName();
21     }
22
23     /**
24      * Returns a <code>NodeList</code> of all descendant <code>Elements</code>
25      * with a given tag name, in document order.
26      *
27      * @param name The name of the tag to match on. The special value "*"
28      * matches all tags.
29      * @return A list of matching <code>Element</code> nodes.
30      */

31     public NodeList getElementsByTagName(String JavaDoc name) {
32         return DocumentOverNodeInfo.getElementsByTagName(node, name);
33     }
34
35     /**
36      * Returns a <code>NodeList</code> of all the descendant
37      * <code>Elements</code> with a given local name and namespace URI in
38      * document order.
39      *
40      * @param namespaceURI The namespace URI of the elements to match on. The
41      * special value "*" matches all namespaces.
42      * @param localName The local name of the elements to match on. The
43      * special value "*" matches all local names.
44      * @return A new <code>NodeList</code> object containing all the matched
45      * <code>Elements</code>.
46      * @throws org.w3c.dom.DOMException NOT_SUPPORTED_ERR: May be raised if the implementation does not
47      * support the feature <code>"XML"</code> and the language exposed
48      * through the Document does not support XML Namespaces (such as [<a HREF='http://www.w3.org/TR/1999/REC-html401-19991224/'>HTML 4.01</a>]).
49      * @since DOM Level 2
50      */

51     public NodeList getElementsByTagNameNS(String JavaDoc namespaceURI, String JavaDoc localName) throws DOMException {
52         return DocumentOverNodeInfo.getElementsByTagNameNS(node, namespaceURI, localName);
53     }
54
55     /**
56      * Retrieves an attribute value by name. Namespace declarations will not
57      * be retrieved. DOM interface.
58      * @param name The QName of the attribute to retrieve.
59      * @return The <code>Attr</code> value as a string, or the empty string if
60      * that attribute does not have a specified or default value.
61      */

62
63     public String JavaDoc getAttribute(String JavaDoc name) {
64         AxisIterator atts = node.iterateAxis(Axis.ATTRIBUTE);
65         while (true) {
66             NodeInfo att = (NodeInfo)atts.next();
67             if (att == null) {
68                 return "";
69             }
70             if (att.getDisplayName().equals(name)) {
71                 String JavaDoc val = att.getStringValue();
72                 if (val==null) return "";
73                 return val;
74             }
75         }
76     }
77
78     /**
79      * Retrieves an attribute node by name.
80      * Namespace declarations will not be retrieved.
81      * <br> To retrieve an attribute node by qualified name and namespace URI,
82      * use the <code>getAttributeNodeNS</code> method.
83      * @param name The name (<code>nodeName</code> ) of the attribute to
84      * retrieve.
85      * @return The <code>Attr</code> node with the specified name (
86      * <code>nodeName</code> ) or <code>null</code> if there is no such
87      * attribute.
88      */

89
90     public Attr getAttributeNode(String JavaDoc name) {
91         AxisIterator atts = node.iterateAxis(Axis.ATTRIBUTE);
92         while (true) {
93             NodeInfo att = (NodeInfo)atts.next();
94             if (att == null) {
95                 return null;
96             }
97             if (att.getDisplayName().equals(name)) {
98                 return (Attr)att;
99             }
100         }
101     }
102
103     /**
104      * Adds a new attribute node. Always fails
105      * @exception org.w3c.dom.DOMException
106      * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
107      */

108
109     public Attr setAttributeNode(Attr newAttr) throws DOMException {
110         disallowUpdate();
111         return null;
112     }
113
114     /**
115      * Removes the specified attribute. Always fails
116      * @exception org.w3c.dom.DOMException
117      * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
118      */

119
120     public void removeAttribute(String JavaDoc oldAttr) throws DOMException {
121         disallowUpdate();
122     }
123
124     /**
125      * Removes the specified attribute node. Always fails
126      * @exception org.w3c.dom.DOMException
127      * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
128      */

129
130     public Attr removeAttributeNode(Attr oldAttr) throws DOMException {
131         disallowUpdate();
132         return null;
133     }
134
135
136     /**
137      * Retrieves an attribute value by local name and namespace URI.
138      * HTML-only DOM implementations do not need to implement this method.
139      * @param namespaceURI The namespace URI of the attribute to retrieve.
140      * @param localName The local name of the attribute to retrieve.
141      * @return The <code>Attr</code> value as a string, or the empty string if
142      * that attribute does not have a specified or default value.
143      * @since DOM Level 2
144      */

145
146     public String JavaDoc getAttributeNS(String JavaDoc namespaceURI, String JavaDoc localName) {
147         String JavaDoc val = Navigator.getAttributeValue(node, namespaceURI, localName);
148         if (val==null) return "";
149         return val;
150     }
151
152     /**
153      * Adds a new attribute. Always fails
154      *
155      * @param name The name of the attribute to create or alter.
156      * @param value Value to set in string form.
157      * @throws org.w3c.dom.DOMException INVALID_CHARACTER_ERR: Raised if the specified name is not an XML
158      * name according to the XML version in use specified in the
159      * <code>Document.xmlVersion</code> attribute.
160      * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
161      */

162     public void setAttribute(String JavaDoc name, String JavaDoc value) throws DOMException {
163         disallowUpdate();
164     }
165
166     /**
167      * Adds a new attribute. Always fails.
168      * @param namespaceURI The namespace URI of the attribute to create or
169      * alter.
170      * @param qualifiedName The qualified name of the attribute to create or
171      * alter.
172      * @param value The value to set in string form.
173      * @exception org.w3c.dom.DOMException
174      * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
175      */

176
177     public void setAttributeNS(String JavaDoc namespaceURI,
178                                String JavaDoc qualifiedName,
179                                String JavaDoc value)
180                                throws DOMException {
181         disallowUpdate();
182     }
183
184     /**
185      * Removes an attribute by local name and namespace URI. Always fails
186      * @exception org.w3c.dom.DOMException
187      * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
188      * @since DOM Level 2
189      */

190
191     public void removeAttributeNS(String JavaDoc namespaceURI,
192                                   String JavaDoc localName)
193                                   throws DOMException{
194         disallowUpdate();
195     }
196
197     /**
198      * Retrieves an <code>Attr</code> node by local name and namespace URI.
199      * DOM method, so namespace declarations count as attributes.
200      * @param namespaceURI The namespace URI of the attribute to retrieve.
201      * @param localName The local name of the attribute to retrieve.
202      * @return The <code>Attr</code> node with the specified attribute local
203      * name and namespace URI or <code>null</code> if there is no such
204      * attribute.
205      * @since DOM Level 2
206      */

207
208     public Attr getAttributeNodeNS(String JavaDoc namespaceURI, String JavaDoc localName) {
209         DocumentInfo doc = node.getDocumentRoot();
210         if (doc==null) {
211             throw new UnsupportedOperationException JavaDoc("getAttributeNodeNS is not supported on a tree with no document node");
212         }
213         int fingerprint = doc.getNamePool().getFingerprint(namespaceURI, localName);
214         if (fingerprint==-1) return null;
215         NameTest test = new NameTest(Type.ATTRIBUTE, fingerprint, node.getNamePool());
216         AxisIterator atts = node.iterateAxis(Axis.ATTRIBUTE, test);
217         return (Attr)wrap((NodeInfo)atts.next());
218     }
219
220     /**
221      * Add a new attribute. Always fails.
222      * @param newAttr The <code>Attr</code> node to add to the attribute list.
223      * @return If the <code>newAttr</code> attribute replaces an existing
224      * attribute with the same local name and namespace URI , the
225      * replaced <code>Attr</code> node is returned, otherwise
226      * <code>null</code> is returned.
227      * @exception org.w3c.dom.DOMException
228      * <br> NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
229      * @since DOM Level 2
230      */

231
232     public Attr setAttributeNodeNS(Attr newAttr)
233                                    throws DOMException{
234         disallowUpdate();
235         return null;
236     }
237
238     /**
239      * Returns <code>true</code> when an attribute with a given name is
240      * specified on this element or has a default value, <code>false</code>
241      * otherwise.
242      * Namespace declarations will not be retrieved.
243      * @param name The name of the attribute to look for.
244      * @return <code>true</code> if an attribute with the given name is
245      * specified on this element or has a default value, <code>false</code>
246      * otherwise.
247      * @since DOM Level 2
248      */

249
250     public boolean hasAttribute(String JavaDoc name) {
251         AxisIterator atts = node.iterateAxis(Axis.ATTRIBUTE);
252         while (true) {
253             NodeInfo att = (NodeInfo)atts.next();
254             if (att == null) {
255                 return false;
256             }
257             if (att.getDisplayName().equals(name)) {
258                 return true;
259             }
260         }
261     }
262
263     /**
264      * Returns <code>true</code> when an attribute with a given local name
265      * and namespace URI is specified on this element or has a default value,
266      * <code>false</code> otherwise.
267      * Namespace declarations will not be retrieved.
268      * @param namespaceURI The namespace URI of the attribute to look for.
269      * @param localName The local name of the attribute to look for.
270      * @return <code>true</code> if an attribute with the given local name and
271      * namespace URI is specified or has a default value on this element,
272      * <code>false</code> otherwise.
273      * @since DOM Level 2
274      */

275
276     public boolean hasAttributeNS(String JavaDoc namespaceURI, String JavaDoc localName) {
277         return (Navigator.getAttributeValue(node, namespaceURI, localName) != null);
278     }
279
280     // DOM Level 3 methods
281

282     public void setIdAttribute(String JavaDoc name,
283                                boolean isId)
284                                throws DOMException{
285         disallowUpdate();
286     }
287
288     public void setIdAttributeNS(String JavaDoc namespaceURI,
289                                  String JavaDoc localName,
290                                  boolean isId)
291                                  throws DOMException{
292         disallowUpdate();
293     }
294
295     public void setIdAttributeNode(Attr idAttr,
296                                    boolean isId)
297                                    throws DOMException{
298         disallowUpdate();
299     }
300
301
302     /**
303      * Get the schema type information for this node. Returns null for an untyped node.
304      */

305
306     public TypeInfo getSchemaTypeInfo() {
307         int annotation = node.getTypeAnnotation();
308         if (annotation == -1) {
309             return null;
310         }
311         return new TypeInfoImpl(node.getConfiguration(),
312                 node.getConfiguration().getSchemaType(annotation));
313     }
314
315 }
316
317 //
318
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
319
// you may not use this file except in compliance with the License. You may obtain a copy of the
320
// License at http://www.mozilla.org/MPL/
321
//
322
// Software distributed under the License is distributed on an "AS IS" basis,
323
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
324
// See the License for the specific language governing rights and limitations under the License.
325
//
326
// The Original Code is: all this file.
327
//
328
// The Initial Developer of the Original Code is Michael H. Kay.
329
//
330
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
331
//
332
// Contributor(s): none.
333
//
Popular Tags