Package net.sf.saxon.tree
Class ElementImpl
- java.lang.Object
-
- net.sf.saxon.tree.NodeImpl
-
- net.sf.saxon.tree.ElementImpl
-
- All Implemented Interfaces:
javax.xml.transform.Source
,javax.xml.transform.SourceLocator
,PullEvent
,FingerprintedNode
,Item
,MutableNodeInfo
,NamespaceResolver
,NodeInfo
,SiblingCountingNode
,ValueRepresentation
- Direct Known Subclasses:
DataElement
,StyleElement
public class ElementImpl extends NodeImpl implements NamespaceResolver
ElementImpl implements an element with no attributes or namespace declarations.This class is an implementation of NodeInfo. For elements with attributes or namespace declarations, class ElementWithAttributes is used.
- Author:
- Michael H. Kay
-
-
Field Summary
Fields Modifier and Type Field Description protected AttributeCollection
attributeList
protected java.lang.Object
children
protected int
nameCode
protected int[]
namespaceList
protected int
sequence
protected int
typeCode
-
Fields inherited from class net.sf.saxon.tree.NodeImpl
index, NODE_LETTER, parent
-
Fields inherited from interface net.sf.saxon.om.NodeInfo
ALL_NAMESPACES, EMPTY_NAMESPACE_LIST, IS_DTD_TYPE, IS_NILLED, LOCAL_NAMESPACES, NO_NAMESPACES
-
Fields inherited from interface net.sf.saxon.om.ValueRepresentation
EMPTY_VALUE_ARRAY
-
-
Constructor Summary
Constructors Constructor Description ElementImpl()
Construct an empty ElementImpl
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addChild(NodeImpl node, int index)
Add a child node to this node.void
addNamespace(int nscode, boolean inherit)
Add a namespace binding (that is, a namespace node) to this element.void
compact(int size)
Compact the space used by this nodevoid
copy(Receiver out, int whichNamespaces, boolean copyAnnotations, int locationId)
Copy this node to a given outputter (supporting xsl:copy-of)void
delete()
Delete this node (that is, detach it from its parent)protected AxisIterator
enumerateChildren(NodeTest test)
Get an enumeration of the children of this nodeprotected void
fixupInsertedNamespaces(boolean inherit)
Ensure that a child element being inserted into a tree has the right namespace declarations.void
generateId(FastStringBuffer buffer)
Get a character string that uniquely identifies this nodeAttributeCollection
getAttributeList()
Get the attribute list for this element.java.lang.String
getAttributeValue(int fingerprint)
Get the value of a given attribute of this nodejava.lang.String
getAttributeValue(java.lang.String uri, java.lang.String localName)
Get the value of a given attribute of this nodejava.lang.String
getBaseURI()
Get the base URI of this element node.int
getColumnNumber()
Get the line number of the node within its source document entityint[]
getDeclaredNamespaces(int[] buffer)
Get all namespace undeclarations and undeclarations defined on this element.DocumentInfo
getDocumentRoot()
Get the root node, if it is a document node.NodeInfo
getFirstChild()
Get the first child node of the elementint[]
getInScopeNamespaceCodes()
Get the list of in-scope namespaces for this element as an array of namespace codes.NodeInfo
getLastChild()
Get the last child node of the elementint
getLineNumber()
Get the line number of the node within its source document entityint
getNameCode()
Get the nameCode of the node.int
getNodeKind()
Return the kind of node.protected NodeImpl
getNthChild(int n)
Get the nth child node of the element (numbering from 0)int
getNumberOfChildren()
Determine how many children the node hasjava.lang.String
getPrefixForURI(java.lang.String uri)
Search the NamespaceList for a given URI, returning the corresponding prefix.NodeInfo
getRoot()
Get the root nodeprotected long
getSequenceNumber()
Get the node sequence number (in document order).java.lang.String
getStringValue()
Return the string-value of the node, that is, the concatenation of the character content of all descendent elements and text nodes.java.lang.CharSequence
getStringValueCS()
Get the value of the item as a CharSequence.java.lang.String
getSystemId()
Get the system ID of the entity containing this element node.int
getTypeAnnotation()
Get the type annotation of this node, if anyshort
getURICodeForPrefix(java.lang.String prefix)
Search the NamespaceList for a given prefix, returning the corresponding URI.protected short
getURICodeForPrefixCode(int prefixCode)
Get the URI bound to a given prefix in the in-scope namespaces of this elementjava.lang.String
getURIForPrefix(java.lang.String prefix, boolean useDefault)
Get the namespace URI corresponding to a given prefix.boolean
hasChildNodes()
Determine if the node has any children.void
initialise(int nameCode, int typeCode, AttributeCollectionImpl atts, NodeInfo parent, int sequenceNumber)
Initialise a new ElementImpl with an element namevoid
insertChildren(NodeInfo[] source, boolean atStart, boolean inherit)
Insert copies of a sequence of nodes as children of this node.protected void
insertChildrenAt(NodeInfo[] source, int index, boolean inherit)
Insert children before or after a given existing childboolean
isNilled()
Determine whether the node has the is-nilled propertyjava.util.Iterator
iteratePrefixes()
Get an iterator over all the prefixes declared in this namespace context.void
putAttribute(int nameCode, int typeCode, java.lang.CharSequence value, int properties)
Add an attribute to this element node.void
removeAttribute(int nameCode)
Remove an attribute from this element nodeprotected void
removeChild(NodeImpl child)
Remove a given childvoid
removeTypeAnnotation()
Remove type information from this node (and its ancestors, recursively).void
rename(int newNameCode)
Rename this nodeprotected void
replaceChildrenAt(NodeInfo[] source, int index, boolean inherit)
Replace child at a given index by new childrenvoid
replaceStringValue(java.lang.CharSequence stringValue)
Replace the string-value of this nodevoid
setLineAndColumn(int line, int column)
Set the line number of the element within its source document entityvoid
setLocation(java.lang.String systemId, int line, int column)
Set location information for this nodevoid
setNameCode(int nameCode)
Set the name code.void
setNamespaceDeclarations(int[] namespaces, int namespacesUsed)
Set the namespace declarations for the elementvoid
setSystemId(java.lang.String uri)
Set the system ID of this node.void
setTypeAnnotation(int typeCode)
Set the type annotation on a node.protected void
useChildrenArray(NodeImpl[] array)
Supply an array to be used for the array of children.-
Methods inherited from class net.sf.saxon.tree.NodeImpl
atomize, compareOrder, equals, getConfiguration, getDisplayName, getDocumentNumber, getFingerprint, getLocalPart, getNamePool, getNextInDocument, getNextSibling, getParent, getPhysicalRoot, getPrefix, getPreviousInDocument, getPreviousSibling, getPublicId, getSiblingPosition, getTypedValue, getURI, insertSiblings, isId, isIdref, isSameNodeInfo, iterateAxis, iterateAxis, newBuilder, replace
-
-
-
-
Field Detail
-
nameCode
protected int nameCode
-
typeCode
protected int typeCode
-
attributeList
protected AttributeCollection attributeList
-
namespaceList
protected int[] namespaceList
-
children
protected java.lang.Object children
-
sequence
protected int sequence
-
-
Method Detail
-
setNameCode
public void setNameCode(int nameCode)
Set the name code. Used when creating a dummy element in the Stripper- Parameters:
nameCode
- the integer name code representing the element name
-
initialise
public void initialise(int nameCode, int typeCode, AttributeCollectionImpl atts, NodeInfo parent, int sequenceNumber)
Initialise a new ElementImpl with an element name- Parameters:
nameCode
- Integer representing the element name, with namespaces resolvedtypeCode
- Integer representing the schema type of the element nodeatts
- The attribute list: always nullparent
- The parent nodesequenceNumber
- Integer identifying this element within the document
-
setLocation
public void setLocation(java.lang.String systemId, int line, int column)
Set location information for this node- Parameters:
systemId
- the base URIline
- the line number if knowncolumn
- the column number if known
-
setSystemId
public void setSystemId(java.lang.String uri)
Set the system ID of this node. This method is provided so that a NodeInfo implements the javax.xml.transform.Source interface, allowing a node to be used directly as the Source of a transformation- Specified by:
setSystemId
in interfacejavax.xml.transform.Source
- Overrides:
setSystemId
in classNodeImpl
-
getRoot
public NodeInfo getRoot()
Get the root node
-
getDocumentRoot
public DocumentInfo getDocumentRoot()
Get the root node, if it is a document node.- Specified by:
getDocumentRoot
in interfaceNodeInfo
- Overrides:
getDocumentRoot
in classNodeImpl
- Returns:
- the DocumentInfo representing the containing document. If this node is part of a tree that does not have a document node as its root, returns null.
- Since:
- 8.4
-
getSystemId
public final java.lang.String getSystemId()
Get the system ID of the entity containing this element node.- Specified by:
getSystemId
in interfaceNodeInfo
- Specified by:
getSystemId
in interfacejavax.xml.transform.Source
- Specified by:
getSystemId
in interfacejavax.xml.transform.SourceLocator
- Overrides:
getSystemId
in classNodeImpl
- Returns:
- the System Identifier of the entity in the source document containing the node, or null if not known or not applicable.
-
getBaseURI
public java.lang.String getBaseURI()
Get the base URI of this element node. This will be the same as the System ID unless xml:base has been used.- Specified by:
getBaseURI
in interfaceNodeInfo
- Overrides:
getBaseURI
in classNodeImpl
- Returns:
- the base URI of the node. This may be null if the base URI is unknown.
-
isNilled
public boolean isNilled()
Determine whether the node has the is-nilled property
-
setTypeAnnotation
public void setTypeAnnotation(int typeCode)
Set the type annotation on a node. This must only be called when the caller has verified (by validation) that the node is a valid instance of the specified type. The call is ignored if the node is not an element or attribute node.- Specified by:
setTypeAnnotation
in interfaceMutableNodeInfo
- Overrides:
setTypeAnnotation
in classNodeImpl
- Parameters:
typeCode
- the type annotation (possibly including high bits set to indicate the isID, isIDREF, and isNilled properties)
-
getTypeAnnotation
public int getTypeAnnotation()
Get the type annotation of this node, if any- Specified by:
getTypeAnnotation
in interfaceNodeInfo
- Overrides:
getTypeAnnotation
in classNodeImpl
- Returns:
- the type annotation, as the integer name code of the type name
-
setLineAndColumn
public void setLineAndColumn(int line, int column)
Set the line number of the element within its source document entity- Parameters:
line
- the line numbercolumn
- the column number
-
getLineNumber
public int getLineNumber()
Get the line number of the node within its source document entity- Specified by:
getLineNumber
in interfaceNodeInfo
- Specified by:
getLineNumber
in interfacejavax.xml.transform.SourceLocator
- Overrides:
getLineNumber
in classNodeImpl
- Returns:
- the line number of the node in its original source document; or -1 if not available
-
getColumnNumber
public int getColumnNumber()
Get the line number of the node within its source document entity- Specified by:
getColumnNumber
in interfaceNodeInfo
- Specified by:
getColumnNumber
in interfacejavax.xml.transform.SourceLocator
- Overrides:
getColumnNumber
in classNodeImpl
- Returns:
- the column number of the node in its original source document; or -1 if not available
-
getNameCode
public int getNameCode()
Get the nameCode of the node. This is used to locate the name in the NamePool- Specified by:
getNameCode
in interfaceNodeInfo
- Overrides:
getNameCode
in classNodeImpl
- Returns:
- an integer name code, which may be used to obtain the actual node name from the name pool. For unnamed nodes (text nodes, comments, document nodes, and namespace nodes for the default namespace), returns -1.
- See Also:
allocate
,getFingerprint
-
generateId
public void generateId(FastStringBuffer buffer)
Get a character string that uniquely identifies this node- Specified by:
generateId
in interfaceNodeInfo
- Overrides:
generateId
in classNodeImpl
- Parameters:
buffer
- to contain the generated ID
-
getNodeKind
public final int getNodeKind()
Return the kind of node.- Specified by:
getNodeKind
in interfaceNodeInfo
- Returns:
- Type.ELEMENT
- See Also:
Type
-
copy
public void copy(Receiver out, int whichNamespaces, boolean copyAnnotations, int locationId) throws XPathException
Copy this node to a given outputter (supporting xsl:copy-of)- Specified by:
copy
in interfaceNodeInfo
- Parameters:
out
- The outputterwhichNamespaces
- indicates which namespaces should be output: all, none, or local namespaces only (those not declared on the parent element)copyAnnotations
- indicates whether the type annotations of element and attribute nodes should be copiedlocationId
- If non-zero, identifies the location of the instruction that requested this copy. If zero, indicates that the location information for the original node is to be copied; in this case the Receiver must be a LocationCopier- Throws:
XPathException
-
delete
public void delete()
Delete this node (that is, detach it from its parent)- Specified by:
delete
in interfaceMutableNodeInfo
- Overrides:
delete
in classNodeImpl
-
rename
public void rename(int newNameCode)
Rename this node- Specified by:
rename
in interfaceMutableNodeInfo
- Overrides:
rename
in classNodeImpl
- Parameters:
newNameCode
- the NamePool code of the new name
-
addNamespace
public void addNamespace(int nscode, boolean inherit)
Add a namespace binding (that is, a namespace node) to this element. This call has no effect if applied to a node other than an element.- Specified by:
addNamespace
in interfaceMutableNodeInfo
- Overrides:
addNamespace
in classNodeImpl
- Parameters:
nscode
- The namespace code representing the (prefix, uri) pair of the namespace binding to be added. If the target element already has a namespace binding with this (prefix, uri) pair, the call has no effect. If the target element currently has a namespace binding with this prefix and a different URI, an exception is raised.inherit
- If true, the new namespace binding will be inherited by any children of the target element that do not already have a namespace binding for the specified prefix, recursively. If false, the new namespace binding will not be inherited.- Throws:
java.lang.IllegalArgumentException
- if the target element already has a namespace binding for this prefix, or if the namespace code represents a namespace undeclaration
-
replaceStringValue
public void replaceStringValue(java.lang.CharSequence stringValue)
Replace the string-value of this node- Specified by:
replaceStringValue
in interfaceMutableNodeInfo
- Parameters:
stringValue
- the new string value
-
putAttribute
public void putAttribute(int nameCode, int typeCode, java.lang.CharSequence value, int properties)
Add an attribute to this element node.If this node is not an element, or if the supplied node is not an attribute, the method takes no action. If the element already has an attribute with this name, the existing attribute is replaced.
- Specified by:
putAttribute
in interfaceMutableNodeInfo
- Overrides:
putAttribute
in classNodeImpl
- Parameters:
nameCode
- the name of the new attributetypeCode
- the type annotation of the new attributevalue
- the string value of the new attributeproperties
- properties including IS_ID and IS_IDREF properties
-
removeAttribute
public void removeAttribute(int nameCode)
Remove an attribute from this element node- Specified by:
removeAttribute
in interfaceMutableNodeInfo
- Overrides:
removeAttribute
in classNodeImpl
- Parameters:
nameCode
- the name of the attribute to be removed
-
removeTypeAnnotation
public void removeTypeAnnotation()
Remove type information from this node (and its ancestors, recursively). This method implements the upd:removeType() primitive defined in the XQuery Update specification- Specified by:
removeTypeAnnotation
in interfaceMutableNodeInfo
- Overrides:
removeTypeAnnotation
in classNodeImpl
-
setNamespaceDeclarations
public void setNamespaceDeclarations(int[] namespaces, int namespacesUsed)
Set the namespace declarations for the element- Parameters:
namespaces
- the list of namespace codesnamespacesUsed
- the number of entries in the list that are used
-
getURIForPrefix
public java.lang.String getURIForPrefix(java.lang.String prefix, boolean useDefault)
Get the namespace URI corresponding to a given prefix. Return null if the prefix is not in scope.- Specified by:
getURIForPrefix
in interfaceNamespaceResolver
- Parameters:
prefix
- the namespace prefix. May be the zero-length string, indicating that there is no prefix. This indicates either the default namespace or the null namespace, depending on the value of useDefault.useDefault
- true if the default namespace is to be used when the prefix is "". If false, the method returns "" when the prefix is "".- Returns:
- the uri for the namespace, or null if the prefix is not in scope. The "null namespace" is represented by the pseudo-URI "".
-
iteratePrefixes
public java.util.Iterator iteratePrefixes()
Get an iterator over all the prefixes declared in this namespace context. This will include the default namespace (prefix="") and the XML namespace where appropriate- Specified by:
iteratePrefixes
in interfaceNamespaceResolver
-
getURICodeForPrefix
public short getURICodeForPrefix(java.lang.String prefix) throws NamespaceException
Search the NamespaceList for a given prefix, returning the corresponding URI.- Parameters:
prefix
- The prefix to be matched. To find the default namespace, supply ""- Returns:
- The URI code corresponding to this namespace. If it is an unnamed default namespace, return Namespace.NULL_CODE.
- Throws:
NamespaceException
- if the prefix has not been declared on this NamespaceList.
-
getURICodeForPrefixCode
protected short getURICodeForPrefixCode(int prefixCode)
Get the URI bound to a given prefix in the in-scope namespaces of this element- Parameters:
prefixCode
- the prefix code as a 16-bit integer- Returns:
- the uri code as a 16-bit integer, or -1 if there is no in-scope binding for this prefix
-
getPrefixForURI
public java.lang.String getPrefixForURI(java.lang.String uri)
Search the NamespaceList for a given URI, returning the corresponding prefix.- Parameters:
uri
- The URI to be matched.- Returns:
- The prefix corresponding to this URI. If not found, return null. If there is more than one prefix matching the URI, the first one found is returned. If the URI matches the default namespace, return an empty string.
-
getDeclaredNamespaces
public int[] getDeclaredNamespaces(int[] buffer)
Get all namespace undeclarations and undeclarations defined on this element.- Specified by:
getDeclaredNamespaces
in interfaceNodeInfo
- Overrides:
getDeclaredNamespaces
in classNodeImpl
- Parameters:
buffer
- If this is non-null, and the result array fits in this buffer, then the result may overwrite the contents of this array, to avoid the cost of allocating a new array on the heap.- Returns:
- An array of integers representing the namespace declarations and undeclarations present on
this element. For a node other than an element, return null. Otherwise, the returned array is a
sequence of namespace codes, whose meaning may be interpreted by reference to the name pool. The
top half word of each namespace code represents the prefix, the bottom half represents the URI.
If the bottom half is zero, then this is a namespace undeclaration rather than a declaration.
The XML namespace is never included in the list. If the supplied array is larger than required,
then the first unused entry will be set to -1.
For a node other than an element, the method returns null.
-
getInScopeNamespaceCodes
public int[] getInScopeNamespaceCodes()
Get the list of in-scope namespaces for this element as an array of namespace codes. (Used by LiteralResultElement)- Returns:
- the list of namespaces
-
fixupInsertedNamespaces
protected void fixupInsertedNamespaces(boolean inherit)
Ensure that a child element being inserted into a tree has the right namespace declarations. Redundant declarations should be removed. If the child is in the null namespace but the parent has a default namespace, xmlns="" should be added. If inherit is false, namespace undeclarations should be added for all namespaces that are declared on the parent but not on the child.- Parameters:
inherit
- true if the child is to inherit the inscope namespaces of its new parent
-
getAttributeList
public AttributeCollection getAttributeList()
Get the attribute list for this element.- Returns:
- The attribute list. This will not include any namespace attributes. The attribute names will be in expanded form, with prefixes replaced by URIs
-
getAttributeValue
public java.lang.String getAttributeValue(int fingerprint)
Get the value of a given attribute of this node- Specified by:
getAttributeValue
in interfaceNodeInfo
- Overrides:
getAttributeValue
in classNodeImpl
- Parameters:
fingerprint
- The fingerprint of the attribute name- Returns:
- the attribute value if it exists or null if not
-
getAttributeValue
public java.lang.String getAttributeValue(java.lang.String uri, java.lang.String localName)
Get the value of a given attribute of this node- Parameters:
uri
- the namespace URI of the attribute name, or "" if the attribute is not in a namepsacelocalName
- the local part of the attribute name- Returns:
- the attribute value if it exists or null if not
-
getSequenceNumber
protected final long getSequenceNumber()
Get the node sequence number (in document order). Sequence numbers are monotonic but not consecutive. In the current implementation, parent nodes (elements and document nodes) have a zero least-significant word, while namespaces, attributes, text nodes, comments, and PIs have the top word the same as their owner and the bottom half reflecting their relative position. For nodes added by XQUery Update, the sequence number is -1L- Overrides:
getSequenceNumber
in classNodeImpl
- Returns:
- the sequence number if there is one, or -1L otherwise.
-
hasChildNodes
public final boolean hasChildNodes()
Determine if the node has any children.- Specified by:
hasChildNodes
in interfaceNodeInfo
- Overrides:
hasChildNodes
in classNodeImpl
- Returns:
true
if the node has any children,false
if the node has no children.
-
getNumberOfChildren
public int getNumberOfChildren()
Determine how many children the node has- Returns:
- the number of children of this parent node
-
enumerateChildren
protected final AxisIterator enumerateChildren(NodeTest test)
Get an enumeration of the children of this node- Parameters:
test
- A NodeTest to be satisfied by the child nodes, or null if all child node are to be returned- Returns:
- an iterator over the children of this node
-
getFirstChild
public final NodeInfo getFirstChild()
Get the first child node of the element- Overrides:
getFirstChild
in classNodeImpl
- Returns:
- the first child node of the required type, or null if there are no children
-
getLastChild
public final NodeInfo getLastChild()
Get the last child node of the element- Overrides:
getLastChild
in classNodeImpl
- Returns:
- the last child of the element, or null if there are no children
-
getNthChild
protected final NodeImpl getNthChild(int n)
Get the nth child node of the element (numbering from 0)- Parameters:
n
- identifies the required child- Returns:
- the last child of the element, or null if there is no n'th child
-
removeChild
protected void removeChild(NodeImpl child)
Remove a given child- Parameters:
child
- the child to be removed
-
getStringValue
public java.lang.String getStringValue()
Return the string-value of the node, that is, the concatenation of the character content of all descendent elements and text nodes.- Returns:
- the accumulated character content of the element, including descendant elements.
- See Also:
Item.getStringValueCS()
-
getStringValueCS
public java.lang.CharSequence getStringValueCS()
Description copied from class:NodeImpl
Get the value of the item as a CharSequence. This is in some cases more efficient than the version of the method that returns a String.- Specified by:
getStringValueCS
in interfaceItem
- Specified by:
getStringValueCS
in interfaceValueRepresentation
- Overrides:
getStringValueCS
in classNodeImpl
- Returns:
- the string value of the item
- See Also:
Item.getStringValue()
-
useChildrenArray
protected void useChildrenArray(NodeImpl[] array)
Supply an array to be used for the array of children. For system use only.- Parameters:
array
- the array to be used
-
addChild
public void addChild(NodeImpl node, int index)
Add a child node to this node. For system use only. Note: normalizing adjacent text nodes is the responsibility of the caller.- Parameters:
node
- the node to be added as a child of this nodeindex
- the position where the child is to be added
-
insertChildren
public void insertChildren(NodeInfo[] source, boolean atStart, boolean inherit)
Insert copies of a sequence of nodes as children of this node.This method takes no action unless the target node is a document node or element node. It also takes no action in respect of any supplied nodes that are not elements, text nodes, comments, or processing instructions.
The supplied nodes will be copied to form the new children. Adjacent text nodes will be merged, and zero-length text nodes removed.
- Specified by:
insertChildren
in interfaceMutableNodeInfo
- Overrides:
insertChildren
in classNodeImpl
- Parameters:
source
- the nodes to be insertedatStart
- true if the new nodes are to be inserted before existing children; false if they areinherit
- true if the inserted nodes are to inherit the namespaces that are in-scope for their new parent; false if such namespaces should be undeclared on the children
-
insertChildrenAt
protected void insertChildrenAt(NodeInfo[] source, int index, boolean inherit)
Insert children before or after a given existing child- Parameters:
source
- the children to be insertedindex
- the position before which they are to be inserted: 0 indicates insertion before the first child, 1 insertion before the second child, and so on.inherit
- true if the inserted nodes are to inherit the namespaces that are in-scope for their new parent; false if such namespaces should be undeclared on the children
-
replaceChildrenAt
protected void replaceChildrenAt(NodeInfo[] source, int index, boolean inherit)
Replace child at a given index by new children- Parameters:
source
- the children to be insertedindex
- the position at which they are to be inserted: 0 indicates replacement of the first child, replacement of the second child, and so on. The effect is undefined if index is out of rangeinherit
- set to true if the new child elements are to inherit the in-scope namespaces of their new parent- Throws:
java.lang.IllegalArgumentException
- if any of the replacement nodes is not an element, text, comment, or processing instruction node
-
compact
public void compact(int size)
Compact the space used by this node- Parameters:
size
- the number of actual children
-
-