001: package net.sf.saxon.dom;
002:
003: import net.sf.saxon.om.*;
004: import net.sf.saxon.pattern.NameTest;
005: import net.sf.saxon.type.Type;
006: import org.w3c.dom.*;
007:
008: /**
009: * This class is an implementation of the DOM Element class that wraps a Saxon NodeInfo
010: * representation of an element node.
011: */
012:
013: public class ElementOverNodeInfo extends NodeOverNodeInfo implements
014: Element {
015:
016: /**
017: * The name of the element (DOM interface).
018: */
019:
020: public String getTagName() {
021: return node.getDisplayName();
022: }
023:
024: /**
025: * Returns a <code>NodeList</code> of all descendant <code>Elements</code>
026: * with a given tag name, in document order.
027: *
028: * @param name The name of the tag to match on. The special value "*"
029: * matches all tags.
030: * @return A list of matching <code>Element</code> nodes.
031: */
032: public NodeList getElementsByTagName(String name) {
033: return DocumentOverNodeInfo.getElementsByTagName(node, name);
034: }
035:
036: /**
037: * Returns a <code>NodeList</code> of all the descendant
038: * <code>Elements</code> with a given local name and namespace URI in
039: * document order.
040: *
041: * @param namespaceURI The namespace URI of the elements to match on. The
042: * special value "*" matches all namespaces.
043: * @param localName The local name of the elements to match on. The
044: * special value "*" matches all local names.
045: * @return A new <code>NodeList</code> object containing all the matched
046: * <code>Elements</code>.
047: * @throws org.w3c.dom.DOMException NOT_SUPPORTED_ERR: May be raised if the implementation does not
048: * support the feature <code>"XML"</code> and the language exposed
049: * 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>]).
050: * @since DOM Level 2
051: */
052: public NodeList getElementsByTagNameNS(String namespaceURI,
053: String localName) throws DOMException {
054: return DocumentOverNodeInfo.getElementsByTagNameNS(node,
055: namespaceURI, localName);
056: }
057:
058: /**
059: * Retrieves an attribute value by name. Namespace declarations will not
060: * be retrieved. DOM interface.
061: * @param name The QName of the attribute to retrieve.
062: * @return The <code>Attr</code> value as a string, or the empty string if
063: * that attribute does not have a specified or default value.
064: */
065:
066: public String getAttribute(String name) {
067: AxisIterator atts = node.iterateAxis(Axis.ATTRIBUTE);
068: while (true) {
069: NodeInfo att = (NodeInfo) atts.next();
070: if (att == null) {
071: return "";
072: }
073: if (att.getDisplayName().equals(name)) {
074: String val = att.getStringValue();
075: if (val == null)
076: return "";
077: return val;
078: }
079: }
080: }
081:
082: /**
083: * Retrieves an attribute node by name.
084: * Namespace declarations will not be retrieved.
085: * <br> To retrieve an attribute node by qualified name and namespace URI,
086: * use the <code>getAttributeNodeNS</code> method.
087: * @param name The name (<code>nodeName</code> ) of the attribute to
088: * retrieve.
089: * @return The <code>Attr</code> node with the specified name (
090: * <code>nodeName</code> ) or <code>null</code> if there is no such
091: * attribute.
092: */
093:
094: public Attr getAttributeNode(String name) {
095: AxisIterator atts = node.iterateAxis(Axis.ATTRIBUTE);
096: while (true) {
097: NodeInfo att = (NodeInfo) atts.next();
098: if (att == null) {
099: return null;
100: }
101: if (att.getDisplayName().equals(name)) {
102: return (Attr) att;
103: }
104: }
105: }
106:
107: /**
108: * Adds a new attribute node. Always fails
109: * @exception org.w3c.dom.DOMException
110: * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
111: */
112:
113: public Attr setAttributeNode(Attr newAttr) throws DOMException {
114: disallowUpdate();
115: return null;
116: }
117:
118: /**
119: * Removes the specified attribute. Always fails
120: * @exception org.w3c.dom.DOMException
121: * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
122: */
123:
124: public void removeAttribute(String oldAttr) throws DOMException {
125: disallowUpdate();
126: }
127:
128: /**
129: * Removes the specified attribute node. Always fails
130: * @exception org.w3c.dom.DOMException
131: * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
132: */
133:
134: public Attr removeAttributeNode(Attr oldAttr) throws DOMException {
135: disallowUpdate();
136: return null;
137: }
138:
139: /**
140: * Retrieves an attribute value by local name and namespace URI.
141: * HTML-only DOM implementations do not need to implement this method.
142: * @param namespaceURI The namespace URI of the attribute to retrieve.
143: * @param localName The local name of the attribute to retrieve.
144: * @return The <code>Attr</code> value as a string, or the empty string if
145: * that attribute does not have a specified or default value.
146: * @since DOM Level 2
147: */
148:
149: public String getAttributeNS(String namespaceURI, String localName) {
150: String val = Navigator.getAttributeValue(node, namespaceURI,
151: localName);
152: if (val == null)
153: return "";
154: return val;
155: }
156:
157: /**
158: * Adds a new attribute. Always fails
159: *
160: * @param name The name of the attribute to create or alter.
161: * @param value Value to set in string form.
162: * @throws org.w3c.dom.DOMException INVALID_CHARACTER_ERR: Raised if the specified name is not an XML
163: * name according to the XML version in use specified in the
164: * <code>Document.xmlVersion</code> attribute.
165: * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
166: */
167: public void setAttribute(String name, String value)
168: throws DOMException {
169: disallowUpdate();
170: }
171:
172: /**
173: * Adds a new attribute. Always fails.
174: * @param namespaceURI The namespace URI of the attribute to create or
175: * alter.
176: * @param qualifiedName The qualified name of the attribute to create or
177: * alter.
178: * @param value The value to set in string form.
179: * @exception org.w3c.dom.DOMException
180: * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
181: */
182:
183: public void setAttributeNS(String namespaceURI,
184: String qualifiedName, String value) throws DOMException {
185: disallowUpdate();
186: }
187:
188: /**
189: * Removes an attribute by local name and namespace URI. Always fails
190: * @exception org.w3c.dom.DOMException
191: * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
192: * @since DOM Level 2
193: */
194:
195: public void removeAttributeNS(String namespaceURI, String localName)
196: throws DOMException {
197: disallowUpdate();
198: }
199:
200: /**
201: * Retrieves an <code>Attr</code> node by local name and namespace URI.
202: * DOM method, so namespace declarations count as attributes.
203: * @param namespaceURI The namespace URI of the attribute to retrieve.
204: * @param localName The local name of the attribute to retrieve.
205: * @return The <code>Attr</code> node with the specified attribute local
206: * name and namespace URI or <code>null</code> if there is no such
207: * attribute.
208: * @since DOM Level 2
209: */
210:
211: public Attr getAttributeNodeNS(String namespaceURI, String localName) {
212: DocumentInfo doc = node.getDocumentRoot();
213: if (doc == null) {
214: throw new UnsupportedOperationException(
215: "getAttributeNodeNS is not supported on a tree with no document node");
216: }
217: int fingerprint = doc.getNamePool().getFingerprint(
218: namespaceURI, localName);
219: if (fingerprint == -1)
220: return null;
221: NameTest test = new NameTest(Type.ATTRIBUTE, fingerprint, node
222: .getNamePool());
223: AxisIterator atts = node.iterateAxis(Axis.ATTRIBUTE, test);
224: return (Attr) wrap((NodeInfo) atts.next());
225: }
226:
227: /**
228: * Add a new attribute. Always fails.
229: * @param newAttr The <code>Attr</code> node to add to the attribute list.
230: * @return If the <code>newAttr</code> attribute replaces an existing
231: * attribute with the same local name and namespace URI , the
232: * replaced <code>Attr</code> node is returned, otherwise
233: * <code>null</code> is returned.
234: * @exception org.w3c.dom.DOMException
235: * <br> NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
236: * @since DOM Level 2
237: */
238:
239: public Attr setAttributeNodeNS(Attr newAttr) throws DOMException {
240: disallowUpdate();
241: return null;
242: }
243:
244: /**
245: * Returns <code>true</code> when an attribute with a given name is
246: * specified on this element or has a default value, <code>false</code>
247: * otherwise.
248: * Namespace declarations will not be retrieved.
249: * @param name The name of the attribute to look for.
250: * @return <code>true</code> if an attribute with the given name is
251: * specified on this element or has a default value, <code>false</code>
252: * otherwise.
253: * @since DOM Level 2
254: */
255:
256: public boolean hasAttribute(String name) {
257: AxisIterator atts = node.iterateAxis(Axis.ATTRIBUTE);
258: while (true) {
259: NodeInfo att = (NodeInfo) atts.next();
260: if (att == null) {
261: return false;
262: }
263: if (att.getDisplayName().equals(name)) {
264: return true;
265: }
266: }
267: }
268:
269: /**
270: * Returns <code>true</code> when an attribute with a given local name
271: * and namespace URI is specified on this element or has a default value,
272: * <code>false</code> otherwise.
273: * Namespace declarations will not be retrieved.
274: * @param namespaceURI The namespace URI of the attribute to look for.
275: * @param localName The local name of the attribute to look for.
276: * @return <code>true</code> if an attribute with the given local name and
277: * namespace URI is specified or has a default value on this element,
278: * <code>false</code> otherwise.
279: * @since DOM Level 2
280: */
281:
282: public boolean hasAttributeNS(String namespaceURI, String localName) {
283: return (Navigator.getAttributeValue(node, namespaceURI,
284: localName) != null);
285: }
286:
287: // DOM Level 3 methods
288:
289: public void setIdAttribute(String name, boolean isId)
290: throws DOMException {
291: disallowUpdate();
292: }
293:
294: public void setIdAttributeNS(String namespaceURI, String localName,
295: boolean isId) throws DOMException {
296: disallowUpdate();
297: }
298:
299: public void setIdAttributeNode(Attr idAttr, boolean isId)
300: throws DOMException {
301: disallowUpdate();
302: }
303:
304: /**
305: * Get the schema type information for this node. Returns null for an untyped node.
306: */
307:
308: public TypeInfo getSchemaTypeInfo() {
309: int annotation = node.getTypeAnnotation();
310: if (annotation == -1) {
311: return null;
312: }
313: return new TypeInfoImpl(node.getConfiguration(), node
314: .getConfiguration().getSchemaType(annotation));
315: }
316:
317: }
318:
319: //
320: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
321: // you may not use this file except in compliance with the License. You may obtain a copy of the
322: // License at http://www.mozilla.org/MPL/
323: //
324: // Software distributed under the License is distributed on an "AS IS" basis,
325: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
326: // See the License for the specific language governing rights and limitations under the License.
327: //
328: // The Original Code is: all this file.
329: //
330: // The Initial Developer of the Original Code is Michael H. Kay.
331: //
332: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
333: //
334: // Contributor(s): none.
335: //
|