Package javanet.staxutils.helpers
Class ElementContext
- java.lang.Object
-
- javanet.staxutils.SimpleNamespaceContext
-
- javanet.staxutils.helpers.ElementContext
-
- All Implemented Interfaces:
ExtendedNamespaceContext,StaticNamespaceContext,NamespaceContext
public class ElementContext extends SimpleNamespaceContext
Encapsulates access to contextual element information, such as the element name, attributes, and namespaces. This class is useful for recording element information in a stack to keep track of the current element c[position in a document.- Version:
- $Revision: 1.1 $
- Author:
- Christian Niles
-
-
Field Summary
-
Fields inherited from class javanet.staxutils.SimpleNamespaceContext
namespaces
-
-
Constructor Summary
Constructors Constructor Description ElementContext(QName name)Constructs a newElementContextwith the provided name and no enclosing context.ElementContext(QName name, boolean isEmpty)Constructs a newElementContextwith the provided name and empty value, and no enclosing context.ElementContext(QName name, ElementContext parent)Constructs a newElementContextwith the provided name and enclosing context.ElementContext(QName name, ElementContext parent, boolean isEmpty)Constructs a newElementContextwith the provided name and enclosing context.ElementContext(QName name, NamespaceContext context)Constructs a newElementContextwith the provided name and namespace context.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description StringBufferappendPath(StringBuffer buffer)Appends the current context path to aStringBuffer.intattributeCount()Returns the number of attributes defined in this context.booleanattributeExists(QName name)Determines if an attribute with the specified name exists in this context.IteratorattributeNames()Returns anIteratorover the names of all attributes defined in this context.StringgetAttribute(int idx)Returns the value of theidxth attribute defined on the context.StringgetAttribute(QName name)Returns the value of a named attribute.QNamegetAttributeName(int idx)Returns the name of theidxth attribute defined on the context.intgetDepth()Determines the number of enclosing contexts.QNamegetName()Returns the qualified name associated with the context.StringgetNamespacePrefix(int idx)Returns the prefix of theidxth namespace declaration defined in this context.StringgetNamespaceURI(int idx)Returns the URI of theidxth namespace declaration defined in this context.ElementContextgetParentContext()Returns a reference to the enclosingElementContext.StringgetPath()Returns the current context path.booleanisEmpty()Whether this context represents an emtpy element.booleanisReadOnly()Whether this context may be edited or not.booleanisRoot()Determines if this context has an enclosing context or not.intnamespaceCount()Determines the number of namespaces declared in this context.ElementContextnewSubContext(QName name)Constructs a new childElementContextwith the specified name.ElementContextnewSubContext(QName name, boolean isEmpty)Constructs a new childElementContextwith the specified name and empty value.voidputAttribute(QName name, String value)Adds an attribute to the context with the specified name and value.voidputNamespace(String prefix, String nsURI)Adds a namespace declaration to this context with the specified prefix and namespace uri.voidsetReadOnly()Prevents any further additions to this context.StringtoString()-
Methods inherited from class javanet.staxutils.SimpleNamespaceContext
getDeclaredPrefixCount, getDeclaredPrefixes, getNamespaceURI, getParent, getPrefix, getPrefixes, getPrefixes, isPrefixDeclared, isRootContext, setDefaultNamespace, setParent, setPrefix
-
-
-
-
Constructor Detail
-
ElementContext
public ElementContext(QName name)
Constructs a newElementContextwith the provided name and no enclosing context.- Parameters:
name- The element name.
-
ElementContext
public ElementContext(QName name, boolean isEmpty)
Constructs a newElementContextwith the provided name and empty value, and no enclosing context.- Parameters:
name- The element name.isEmpty- Whether the element is an empty element or not.
-
ElementContext
public ElementContext(QName name, NamespaceContext context)
Constructs a newElementContextwith the provided name and namespace context.- Parameters:
name- The element name.context- The enclosing namespace context.
-
ElementContext
public ElementContext(QName name, ElementContext parent)
Constructs a newElementContextwith the provided name and enclosing context.- Parameters:
name- The element name.parent- The enclosing element context.
-
ElementContext
public ElementContext(QName name, ElementContext parent, boolean isEmpty)
Constructs a newElementContextwith the provided name and enclosing context.- Parameters:
name- The element name.parent- The enclosing element context.isEmpty- Whether the element is an empty element or not.
-
-
Method Detail
-
getParentContext
public ElementContext getParentContext()
Returns a reference to the enclosingElementContext.- Returns:
- The enclosing context, or
null.
-
isRoot
public boolean isRoot()
Determines if this context has an enclosing context or not.- Returns:
trueif this context is the root context and has no enclosing context,falseotherwise.
-
getName
public QName getName()
Returns the qualified name associated with the context.- Returns:
- The qualified name of the context.
-
getPath
public String getPath()
Returns the current context path.- Returns:
- A string representing the context path.
-
appendPath
public StringBuffer appendPath(StringBuffer buffer)
Appends the current context path to aStringBuffer.- Parameters:
buffer- The buffer to which to append the context path.- Returns:
- The provided buffer.
-
getDepth
public int getDepth()
Determines the number of enclosing contexts.- Returns:
- The number of enclosing contexts.
-
newSubContext
public ElementContext newSubContext(QName name)
Constructs a new childElementContextwith the specified name.- Parameters:
name- The name associated with the child context.- Returns:
- The newly constructed child context.
- Throws:
IllegalStateException- If this context is empty.
-
newSubContext
public ElementContext newSubContext(QName name, boolean isEmpty)
Constructs a new childElementContextwith the specified name and empty value.- Parameters:
name- The name associated with the child context.isEmpty- Whether the child context represents an empty element.- Returns:
- The newly constructed child context.
- Throws:
IllegalStateException- If this context is empty.
-
putAttribute
public void putAttribute(QName name, String value)
Adds an attribute to the context with the specified name and value.- Parameters:
name- The attribute name.value- The attribute value.- Throws:
IllegalStateException- If the context is read-only.
-
putNamespace
public void putNamespace(String prefix, String nsURI)
Adds a namespace declaration to this context with the specified prefix and namespace uri.- Parameters:
prefix- The namespace prefix.nsURI- The namespace uri.
-
attributeCount
public int attributeCount()
Returns the number of attributes defined in this context.- Returns:
- The number of attributes defined in the context.
-
getAttribute
public String getAttribute(int idx)
Returns the value of theidxth attribute defined on the context.- Parameters:
idx- The zero-based index of the attribute value to retrieve.- Returns:
- The value of the
idxth attribute defined on the context. - Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
getAttributeName
public QName getAttributeName(int idx)
Returns the name of theidxth attribute defined on the context.- Parameters:
idx- The zero-based index of the attribute name to retrieve.- Returns:
- The name of the
idxth attribute defined on the context. - Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
getAttribute
public String getAttribute(QName name)
Returns the value of a named attribute.- Parameters:
name- The name of the attribute value to retrieve.- Returns:
- The value of the named attribute, or
null.
-
attributeExists
public boolean attributeExists(QName name)
Determines if an attribute with the specified name exists in this context.- Parameters:
name- The name of the attribute.- Returns:
trueif an attribute with the specified name has been defined in this context,falseotherwise.
-
attributeNames
public Iterator attributeNames()
Returns anIteratorover the names of all attributes defined in this context. The returned iterator will not support theIterator.remove()operation.- Returns:
- An
Iteratorover the names of all attributes defined in this context.
-
namespaceCount
public int namespaceCount()
Determines the number of namespaces declared in this context.- Returns:
- The number of namespaces declared in this context.
-
getNamespaceURI
public String getNamespaceURI(int idx)
Returns the URI of theidxth namespace declaration defined in this context.- Parameters:
idx- The index of the namespace URI to return.- Returns:
- The URI of the
idxth namespace declaration defined in this context. - Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
getNamespacePrefix
public String getNamespacePrefix(int idx)
Returns the prefix of theidxth namespace declaration defined in this context.- Parameters:
idx- The index of the namespace prefix to return.- Returns:
- The prefix of the
idxth namespace declaration defined in this context. - Throws:
IndexOutOfBoundsException- If the index is out of bounds.
-
isReadOnly
public boolean isReadOnly()
Whether this context may be edited or not.- Returns:
trueif no additional modifications may be made to this context,falseotherwise.
-
setReadOnly
public void setReadOnly()
Prevents any further additions to this context.
-
isEmpty
public boolean isEmpty()
Whether this context represents an emtpy element. Empty contexts may not enclose any other contexts.- Returns:
trueif this context represents an emtpy element,falseotherwise.
-
-