All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.saxon.dom.ElementOverNodeInfo Maven / Gradle / Ivy

There is a newer version: 10.5
Show newest version
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2013 Saxonica Limited.
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package net.sf.saxon.dom;

import net.sf.saxon.om.AxisInfo;
import net.sf.saxon.om.NamePool;
import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.pattern.NameTest;
import net.sf.saxon.tree.iter.AxisIterator;
import net.sf.saxon.tree.util.Navigator;
import net.sf.saxon.type.BuiltInAtomicType;
import net.sf.saxon.type.SchemaType;
import net.sf.saxon.type.Type;
import net.sf.saxon.type.Untyped;
import org.w3c.dom.*;

/**
 * This class is an implementation of the DOM Element class that wraps a Saxon NodeInfo
 * representation of an element node.
 * 

* The class provides read-only access to the tree; methods that request updates all fail * with an UnsupportedOperationException. *

* Note that contrary to the DOM specification, this implementation does not expose namespace * declarations as attributes. */ public class ElementOverNodeInfo extends NodeOverNodeInfo implements Element { /** * The name of the element (DOM interface). */ public String getTagName() { return node.getDisplayName(); } /** * Returns a NodeList of all descendant Elements * with a given tag name, in document order. * * @param name The name of the tag to match on. The special value "*" * matches all tags. * @return A list of matching Element nodes. */ public NodeList getElementsByTagName(String name) { return DocumentOverNodeInfo.getElementsByTagName(node, name); } /** * Returns a NodeList of all the descendant * Elements with a given local name and namespace URI in * document order. * * @param namespaceURI The namespace URI of the elements to match on. The * special value "*" matches all namespaces. * @param localName The local name of the elements to match on. The * special value "*" matches all local names. * @return A new NodeList object containing all the matched * Elements. * @throws org.w3c.dom.DOMException NOT_SUPPORTED_ERR: May be raised if the implementation does not * support the feature "XML" and the language exposed * through the Document does not support XML Namespaces (such as [HTML 4.01]). * @since DOM Level 2 */ public NodeList getElementsByTagNameNS(String namespaceURI, String localName) throws DOMException { return DocumentOverNodeInfo.getElementsByTagNameNS(node, namespaceURI, localName); } /** * Retrieves an attribute value by name. * This implementation does not expose namespace nodes as attributes. * @param name The QName of the attribute to retrieve. * @return The Attr value as a string, or the empty string if * that attribute does not have a specified or default value. */ public String getAttribute(String name) { AxisIterator atts = node.iterateAxis(AxisInfo.ATTRIBUTE); while (true) { NodeInfo att = atts.next(); if (att == null) { return ""; } if (att.getDisplayName().equals(name)) { String val = att.getStringValue(); if (val==null) return ""; return val; } } } /** * Retrieves an attribute node by name. * This implementation does not expose namespace nodes as attributes. *
To retrieve an attribute node by qualified name and namespace URI, * use the getAttributeNodeNS method. * @param name The name (nodeName ) of the attribute to * retrieve. * @return The Attr node with the specified name ( * nodeName ) or null if there is no such * attribute. */ public Attr getAttributeNode(String name) { AxisIterator atts = node.iterateAxis(AxisInfo.ATTRIBUTE); while (true) { NodeInfo att = atts.next(); if (att == null) { return null; } if (att.getDisplayName().equals(name)) { return (Attr)att; } } } /** * Adds a new attribute node. Always fails * @exception org.w3c.dom.DOMException * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. */ public Attr setAttributeNode(Attr newAttr) throws DOMException { disallowUpdate(); return null; } /** * Removes the specified attribute. Always fails * @exception org.w3c.dom.DOMException * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. */ public void removeAttribute(String oldAttr) throws DOMException { disallowUpdate(); } /** * Removes the specified attribute node. Always fails * @exception org.w3c.dom.DOMException * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. */ public Attr removeAttributeNode(Attr oldAttr) throws DOMException { disallowUpdate(); return null; } /** * Retrieves an attribute value by local name and namespace URI. * This implementation does not expose namespace nodes as attributes. * @param namespaceURI The namespace URI of the attribute to retrieve. * @param localName The local name of the attribute to retrieve. * @return The Attr value as a string, or the empty string if * that attribute does not have a specified or default value. * @since DOM Level 2 */ public String getAttributeNS(String namespaceURI, String localName) { String val = Navigator.getAttributeValue(node, (namespaceURI==null ? "" : namespaceURI), localName); if (val==null) return ""; return val; } /** * Adds a new attribute. Always fails * * @param name The name of the attribute to create or alter. * @param value Value to set in string form. * @throws org.w3c.dom.DOMException INVALID_CHARACTER_ERR: Raised if the specified name is not an XML * name according to the XML version in use specified in the * Document.xmlVersion attribute. *
NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. */ public void setAttribute(String name, String value) throws DOMException { disallowUpdate(); } /** * Adds a new attribute. Always fails. * @param namespaceURI The namespace URI of the attribute to create or * alter. * @param qualifiedName The qualified name of the attribute to create or * alter. * @param value The value to set in string form. * @exception org.w3c.dom.DOMException * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. */ public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException { disallowUpdate(); } /** * Removes an attribute by local name and namespace URI. Always fails * @exception org.w3c.dom.DOMException * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. * @since DOM Level 2 */ public void removeAttributeNS(String namespaceURI, String localName) throws DOMException{ disallowUpdate(); } /** * Retrieves an Attr node by local name and namespace URI. * This implementation does not expose namespace nodes as attributes. * @param namespaceURI The namespace URI of the attribute to retrieve. * @param localName The local name of the attribute to retrieve. * @return The Attr node with the specified attribute local * name and namespace URI or null if there is no such * attribute. * @since DOM Level 2 */ public Attr getAttributeNodeNS(String namespaceURI, String localName) { NamePool pool = node.getNamePool(); int fingerprint = pool.getFingerprint((namespaceURI==null ? "" : namespaceURI), localName); if (fingerprint==-1) return null; NameTest test = new NameTest(Type.ATTRIBUTE, fingerprint, pool); AxisIterator atts = node.iterateAxis(AxisInfo.ATTRIBUTE, test); return (Attr)wrap(atts.next()); } /** * Add a new attribute. Always fails. * @param newAttr The Attr node to add to the attribute list. * @return If the newAttr attribute replaces an existing * attribute with the same local name and namespace URI , the * replaced Attr node is returned, otherwise * null is returned. * @exception org.w3c.dom.DOMException *
NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. * @since DOM Level 2 */ public Attr setAttributeNodeNS(Attr newAttr) throws DOMException{ disallowUpdate(); return null; } /** * Returns true when an attribute with a given name is * specified on this element or has a default value, false * otherwise. * This implementation does not expose namespace nodes as attributes. * @param name The name of the attribute to look for. * @return true if an attribute with the given name is * specified on this element or has a default value, false * otherwise. * @since DOM Level 2 */ public boolean hasAttribute(String name) { AxisIterator atts = node.iterateAxis(AxisInfo.ATTRIBUTE); while (true) { NodeInfo att = atts.next(); if (att == null) { return false; } if (att.getDisplayName().equals(name)) { return true; } } } /** * Returns true when an attribute with a given local name * and namespace URI is specified on this element or has a default value, * false otherwise. * This implementation does not expose namespace nodes as attributes. * @param namespaceURI The namespace URI of the attribute to look for. * @param localName The local name of the attribute to look for. * @return true if an attribute with the given local name and * namespace URI is specified or has a default value on this element, * false otherwise. * @since DOM Level 2 */ public boolean hasAttributeNS(String namespaceURI, String localName) { return (Navigator.getAttributeValue(node, (namespaceURI==null ? "" : namespaceURI), localName) != null); } /** * Mark an attribute as an ID. Always fails. * @throws DOMException */ public void setIdAttribute(String name, boolean isId) throws DOMException{ disallowUpdate(); } /** * Mark an attribute as an ID. Always fails. * @throws DOMException */ public void setIdAttributeNS(String namespaceURI, String localName, boolean isId) throws DOMException{ disallowUpdate(); } /** * Mark an attribute as an ID. Always fails. * @throws DOMException */ public void setIdAttributeNode(Attr idAttr, boolean isId) throws DOMException{ disallowUpdate(); } /** * Get the schema type information for this node. * @return the type information. Returns null for an untyped node. */ /*@Nullable*/ public TypeInfo getSchemaTypeInfo() { SchemaType type = node.getSchemaType(); if (type == null || Untyped.getInstance().equals(type) || BuiltInAtomicType.UNTYPED_ATOMIC.equals(type)) { return null; } return new TypeInfoImpl(node.getConfiguration(), type); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy