org.apache.axiom.om
Interface OMElement

All Superinterfaces:
OMContainer, OMInformationItem, OMNamedInformationItem, OMNode, OMSerializable
All Known Subinterfaces:
OMElementEx, OMSourcedElement, SOAPBody, SOAPEnvelope, SOAPFault, SOAPFaultClassifier, SOAPFaultCode, SOAPFaultDetail, SOAPFaultNode, SOAPFaultReason, SOAPFaultRole, SOAPFaultSubCode, SOAPFaultText, SOAPFaultValue, SOAPHeader, SOAPHeaderBlock

public interface OMElement
extends OMNode, OMContainer, OMNamedInformationItem

A particular kind of node that represents an element infoset information item.

An element has a collection of children, attributes, and namespace declarations. In contrast with DOM, this interface exposes namespace declarations separately from the attributes.

Namespace declarations are either added explicitly using declareNamespace(String, String), declareDefaultNamespace(String) or declareNamespace(OMNamespace), or are created implicitly as side effect of other method calls:

Thus, creating a new element or adding an attribute preserves the consistency of the object model with respect to namespaces. However, Axiom does not enforce namespace well-formedness for all possible operations on the object model. E.g. moving an element from one location in the tree to another one may cause the object model to loose its namespace well-formedness. In that case it is possible that the object model contains elements or attributes with namespaces for which no corresponding namespace declarations are in scope.

Fortunately, loosing namespace well-formedness has only very limited impact:


Field Summary
 
Fields inherited from interface org.apache.axiom.om.OMNode
CDATA_SECTION_NODE, COMMENT_NODE, DTD_NODE, ELEMENT_NODE, ENTITY_REFERENCE_NODE, PI_NODE, SPACE_NODE, TEXT_NODE
 
Method Summary
 OMAttribute addAttribute(OMAttribute attr)
          Adds an attribute to this element.
 OMAttribute addAttribute(String localName, String value, OMNamespace ns)
          Adds an attribute to this element.
 OMElement cloneOMElement()
          Clones this element and its descendants using default options.
 OMNamespace declareDefaultNamespace(String uri)
          Add a namespace declaration for the default namespace to this element.
 OMNamespace declareNamespace(OMNamespace namespace)
          Declares a namespace with the element as its scope.
 OMNamespace declareNamespace(String uri, String prefix)
          Add a namespace declaration for the given namespace URI to this element, optionally generating a prefix for that namespace.
 OMNamespace findNamespace(String uri, String prefix)
          Finds a namespace with the given uri and prefix, in the scope of the hierarchy.
 OMNamespace findNamespaceURI(String prefix)
          Checks for a namespace in the context of this element with the given prefix and returns the relevant namespace object, if available.
 Iterator getAllAttributes()
          Returns a list of OMAttributes.
 Iterator getAllDeclaredNamespaces()
          Returns an iterator for all of the namespaces declared on this element.
 OMAttribute getAttribute(QName qname)
          Returns a named attribute if present.
 String getAttributeValue(QName qname)
          Returns a named attribute's value, if present.
 Iterator getChildElements()
          Returns a filtered list of children - just the elements.
 OMNamespace getDefaultNamespace()
          Get the default namespace in scope on this element.
 OMElement getFirstElement()
          Returns the first child element of the element.
 int getLineNumber()
           
 NamespaceContext getNamespaceContext(boolean detached)
          Get the namespace context of this element, as determined by the namespace declarations present on this element and its ancestors.
 Iterator getNamespacesInScope()
          Get an iterator that returns all namespaces in scope for this element.
 String getText()
          Returns the non-empty text children as a string.
 QName getTextAsQName()
          Resolve the content of this element to a QName.
 Reader getTextAsStream(boolean cache)
          Returns a stream representing the concatenation of the text nodes that are children of a this element.
 void removeAttribute(OMAttribute attr)
          Removes the given attribute from this element.
 QName resolveQName(String qname)
          Resolves a QName literal in the namespace context defined by this element and produces a corresponding QName object.
 void serialize(OutputStream output)
          Serialize the node with caching enabled.
 void serialize(OutputStream output, OMOutputFormat format)
          Serialize the node with caching enabled.
 void serialize(Writer writer)
          Serialize the node with caching enabled.
 void serialize(Writer writer, OMOutputFormat format)
          Serialize the node with caching enabled.
 void serializeAndConsume(OutputStream output)
          Serialize the node without caching.
 void serializeAndConsume(OutputStream output, OMOutputFormat format)
          Serialize the node without caching.
 void serializeAndConsume(Writer writer)
          Serialize the node without caching.
 void serializeAndConsume(Writer writer, OMOutputFormat format)
          Serialize the node without caching.
 void setBuilder(OMXMLParserWrapper wrapper)
          Method setBuilder.
 void setLineNumber(int lineNumber)
           
 void setNamespace(OMNamespace namespace)
          Set the namespace for this element.
 void setNamespaceWithNoFindInCurrentScope(OMNamespace namespace)
          This will not search the namespace in the scope nor will declare in the current element, as in setNamespace(OMNamespace).
 void setText(QName qname)
          Set the content of this element to the given QName.
 void setText(String text)
          Set the content of this element to the given text.
 String toString()
          Convenience method to serialize the element to a string with caching enabled.
 String toStringWithConsume()
          Convenience method to serialize the element to a string without caching.
 void undeclarePrefix(String prefix)
          Add a namespace declaration that undeclares a given prefix.
 void writeTextTo(Writer out, boolean cache)
          Write the content of the text nodes that are children of a given element to a Writer.
 
Methods inherited from interface org.apache.axiom.om.OMNode
buildWithAttachments, detach, discard, getNextOMSibling, getParent, getPreviousOMSibling, getType, insertSiblingAfter, insertSiblingBefore
 
Methods inherited from interface org.apache.axiom.om.OMContainer
addChild, getBuilder, getChildren, getChildrenWithLocalName, getChildrenWithName, getChildrenWithNamespaceURI, getDescendants, getFirstChildWithName, getFirstOMChild, getSAXSource, getXMLStreamReader, getXMLStreamReader, getXMLStreamReader, getXMLStreamReaderWithoutCaching, removeChildren
 
Methods inherited from interface org.apache.axiom.om.OMSerializable
build, close, isComplete, serialize, serialize, serializeAndConsume
 
Methods inherited from interface org.apache.axiom.om.OMInformationItem
clone, getOMFactory
 
Methods inherited from interface org.apache.axiom.om.OMNamedInformationItem
getLocalName, getNamespace, getNamespaceURI, getPrefix, getQName, setLocalName
 

Method Detail

getChildElements

Iterator getChildElements()
Returns a filtered list of children - just the elements.

Returns:
Returns an iterator over the child elements.
See Also:
OMContainer.getChildren(), OMContainer.getChildrenWithName(javax.xml.namespace.QName)

declareNamespace

OMNamespace declareNamespace(String uri,
                             String prefix)
Add a namespace declaration for the given namespace URI to this element, optionally generating a prefix for that namespace.

Note that this method can't be used to declare a default namespace. For that purpose use declareDefaultNamespace(String) or declareNamespace(OMNamespace).

Parameters:
uri - The namespace to declare in the current scope. The caller is expected to ensure that the URI is a valid namespace name.
prefix - The prefix to associate with the given namespace. The caller is expected to ensure that this is a valid XML prefix. If null or the empty string is given, a prefix will be auto-generated. Please note that using the empty string for this purpose is deprecated and will no longer be supported in Axiom 1.3.
Returns:
the created namespace information item
Throws:
IllegalArgumentException - if an attempt is made to bind a prefix to the empty namespace name
See Also:
declareNamespace(OMNamespace), findNamespace(String, String), getAllDeclaredNamespaces()

declareDefaultNamespace

OMNamespace declareDefaultNamespace(String uri)
Add a namespace declaration for the default namespace to this element.

Note that this method will never change the namespace of the element itself. If an attempt is made to add a namespace declaration that conflicts with the namespace information of the element, an exception is thrown.

Parameters:
uri - The default namespace to declare in the current scope. The caller is expected to ensure that the URI is a valid namespace name.
Returns:
the created namespace information item
Throws:
OMException - if an attempt is made to add a conflicting namespace declaration

getDefaultNamespace

OMNamespace getDefaultNamespace()
Get the default namespace in scope on this element.

Returns:
The default namespace or null if no default namespace is in scope. This method never returns an OMNamespace object with an empty namespace URI; if the element or one of its ancestors has a xmlns="" declaration, then null is returned. Note that if the method returns an OMNamespace object, then its prefix will obviously be the empty string.

declareNamespace

OMNamespace declareNamespace(OMNamespace namespace)
Declares a namespace with the element as its scope.

Parameters:
namespace - The namespace to declare. If the prefix specified by the OMNamespace object is null, then a prefix will be generated.
Returns:
The declared namespace, which will be equal to the OMNamespace object passed as parameter, except if the prefix was null, in which case the return value contains the generated prefix.
Throws:
IllegalArgumentException - if an attempt is made to bind a prefix to the empty namespace name
See Also:
declareNamespace(String, String), findNamespace(String, String), getAllDeclaredNamespaces()

undeclarePrefix

void undeclarePrefix(String prefix)
Add a namespace declaration that undeclares a given prefix. Prefix undeclaring is supported in XML 1.1, but forbidden in XML 1.0. If an object model on which this method has been used is later serialized to an XML 1.0 document, an error will occur. When serialized to an XML 1.1 document, a namespace declaration in the form xmlns:p="" will be produced.

A namespace declaration with empty namespace name will be added even if no existing namespace declaration for the given prefix is in scope in the context of the current element. If a namespace declaration for the given prefix is already defined on this element, it will be replaced.

The namespace declaration created by this method will be returned by getAllDeclaredNamespaces(). It is represented as an OMNamespace object for which OMNamespace.getNamespaceURI() returns an empty string.

Parameters:
prefix - the prefix to undeclare

findNamespace

OMNamespace findNamespace(String uri,
                          String prefix)
Finds a namespace with the given uri and prefix, in the scope of the hierarchy.

Searches from the current element and goes up the hiararchy until a match is found. If no match is found, returns null.

Either prefix or uri should be null. Results are undefined if both are specified.

Parameters:
uri - The namespace to look for. If this is specified, prefix should be null.
prefix - The prefix to look for. If this is specified, uri should be null.
Returns:
Returns the matching namespace declaration, or null if none was found.
See Also:
declareNamespace(String, String), declareNamespace(OMNamespace), getAllDeclaredNamespaces()

findNamespaceURI

OMNamespace findNamespaceURI(String prefix)
Checks for a namespace in the context of this element with the given prefix and returns the relevant namespace object, if available. If not available, returns null.

Parameters:
prefix -

getAllDeclaredNamespaces

Iterator getAllDeclaredNamespaces()
                                  throws OMException
Returns an iterator for all of the namespaces declared on this element. Note that this is not the same as the set of namespace declarations in scope. Since building a namespace context and resolving namespace prefixes has some subtleties associated with it (such as masked namespace declarations and prefix undeclaring), it is generally recommended to use one of the following specialized methods for this purpose:

It is expected that applications only rarely use getAllDeclaredNamespaces() directly.

The iterator returned by this method supports Iterator.remove() and that method can be used to remove a namespace declaration from this element.

Returns:
An iterator over the OMNamespace items declared on this element. Note that the iterator may be invalidated by a call to declareNamespace(OMNamespace), declareNamespace(String, String), declareDefaultNamespace(String) or any other method that modifies the namespace declarations of this element.
Throws:
OMException

getNamespacesInScope

Iterator getNamespacesInScope()
Get an iterator that returns all namespaces in scope for this element. This method may be used to determine the namespace context for this element. For any given prefix, the iterator returns at most one OMNamespace object with that prefix, and this object specifies the namespace URI bound to the prefix. The iterator returns an OMNamespace object with an empty prefix if and only if there is a default namespace. It will never return an OMNamespace object with both the prefix and the namespace URI set to the empty string, even if the element or one of its ancestors has a namespace declaration of the form xmlns="".

The order in which the iterator returns the namespaces is undefined, and invoking the Iterator.remove() method on the returned iterator is not supported. The iterator may be a "live" object, which means that results are undefined if the document is modified (in a way that would modify the namespace context for the element) while the iterator is in use.

Returns:
an iterator over all namespaces in scope for this element

getNamespaceContext

NamespaceContext getNamespaceContext(boolean detached)
Get the namespace context of this element, as determined by the namespace declarations present on this element and its ancestors.

The method supports two different NamespaceContext implementation variants:

Typically, creating a live NamespaceContext is cheaper, but the lookup performance of a detached NamespaceContext is better. The detached variant should always be used if the reference to the NamespaceContext is kept longer than the object model itself, because in this case a live NamespaceContext would prevent the object model from being garbage collected.

Parameters:
detached - true if the method should return a detached implementation, false if the method should return a live object
Returns:
The namespace context for this element. Note that the caller must not make any assumption about the actual implementation class returned by this method.

getAllAttributes

Iterator getAllAttributes()
Returns a list of OMAttributes.

Note that the iterator returned by this function will be invalidated by any addAttribute call.

Returns:
Returns an Iterator of OMAttribute items associated with the element.
See Also:
getAttribute(javax.xml.namespace.QName), addAttribute(OMAttribute), addAttribute(String, String, OMNamespace)

getAttribute

OMAttribute getAttribute(QName qname)
Returns a named attribute if present.

Parameters:
qname - the qualified name to search for
Returns:
Returns an OMAttribute with the given name if found, or null

getAttributeValue

String getAttributeValue(QName qname)
Returns a named attribute's value, if present.

Parameters:
qname - the qualified name to search for
Returns:
The attribute value, or null if no matching attribute is found.

addAttribute

OMAttribute addAttribute(OMAttribute attr)
Adds an attribute to this element.

If the attribute already has an owner, the attribute is cloned (i.e. its name, value and namespace are copied to a new attribute) and the new attribute is added to the element. Otherwise the existing instance specified by the attr parameter is added to the element. In both cases the owner of the added attribute is set to be the particular OMElement.

If there is already an attribute with the same name and namespace URI, it will be replaced and its owner set to null.

In the particular case where the attribute specified by attr is already owned by the element, calling this method has no effect.

Attributes are not stored in any particular order. In particular, there is no guarantee that the added attribute will be returned as the last item by the iterator produced by a subsequent call to getAllAttributes().

If the attribute being added has a namespace, but no corresponding namespace declaration is in scope for the element (i.e. declared on the element or one of its ancestors), a new namespace declaration is added to the element. Note that both the namespace prefix and URI are taken into account when looking for an existing namespace declaration.

Parameters:
attr - The attribute to add.
Returns:
The attribute that was added to the element. As described above this may or may not be the same as attr, depending on whether the attribute specified by this parameter already has an owner or not.

addAttribute

OMAttribute addAttribute(String localName,
                         String value,
                         OMNamespace ns)
Adds an attribute to this element.

If the element already has an attribute with the same local name and namespace URI, then this existing attribute will be removed from the element, i.e. this method will always create a new OMAttribute instance and never update an existing one.

Parameters:
localName - The local name for the attribute.
value - The string value of the attribute. This function does not check to make sure that the given attribute value can be serialized directly as an XML value. The caller may, for example, pass a string with the character 0x01.
ns - The namespace has to be one of the in scope namespace. i.e. the passed namespace must be declared in the parent element of this attribute or ancestors of the parent element of the attribute.
Returns:
Returns the added attribute.
Throws:
IllegalArgumentException - if an attempt is made to create a prefixed attribute with an empty namespace name

removeAttribute

void removeAttribute(OMAttribute attr)
Removes the given attribute from this element.

Parameters:
attr - the attribute to remove
Throws:
OMException - if the attribute is not owned by this element

setBuilder

void setBuilder(OMXMLParserWrapper wrapper)
Method setBuilder.

Parameters:
wrapper -

getFirstElement

OMElement getFirstElement()
Returns the first child element of the element.

Returns:
Returns the first child element of the element, or null if none was found.

setText

void setText(String text)
Set the content of this element to the given text. If the element has children, then all these children are detached before the content is set. If the parameter is a non empty string, then the element will have a single child of type OMText after the method returns. If the parameter is null or an empty string, then the element will have no children.

Parameters:
text - the new text content for the element

setText

void setText(QName qname)
Set the content of this element to the given QName. If no matching namespace declaration for the QName is in scope, then this method will add one. If the QName specifies a namespace URI but no prefix, then a prefix will be generated. If the element has children, then all these children are detached before the content is set. If the parameter is not null, then the element will have a single child of type OMText after the method returns. If the parameter is null, then the element will have no children.

Parameters:
qname - the QName value

getText

String getText()
Returns the non-empty text children as a string.

This method iterates over all the text children of the element and concatenates them to a single string. Only direct children will be considered, i.e. the text is not extracted recursively. For example the return value for <element>A<child>B</child>C</element> will be AC.

All whitespace will be preserved.

Returns:
A string representing the concatenation of the child text nodes. If there are no child text nodes, an empty string is returned.

getTextAsStream

Reader getTextAsStream(boolean cache)
Returns a stream representing the concatenation of the text nodes that are children of a this element. The stream returned by this method produces exactly the same character sequence as the the stream created by the following expression:
new StringReader(element.getText())

The difference is that the stream implementation returned by this method is optimized for performance and is guaranteed to have constant memory usage, provided that:

  1. The method is not required to cache the content of the OMElement, i.e. cache is false or the element is an OMSourcedElement that is backed by a non destructive OMDataSource.
  2. The underlying parser (or the implementation of the underlying XMLStreamReader in the case of an OMSourcedElement) is non coalescing. Note that this is not the default in Axiom and it may be necessary to configure the parser with StAXParserConfiguration.NON_COALESCING.

Parameters:
cache - whether to enable caching when accessing the element
Returns:
a stream representing the concatenation of the text nodes
See Also:
getText()

writeTextTo

void writeTextTo(Writer out,
                 boolean cache)
                 throws IOException
Write the content of the text nodes that are children of a given element to a Writer. If cache is true, this method has the same effect as the following instruction:
out.write(element.getText())

The difference is that this method is guaranteed to have constant memory usage and is optimized for performance (with the same restrictions that apply to getTextAsStream(boolean)).

The method does not call Writer.close().

Parameters:
out - the stream to write the content to
cache - whether to enable caching when accessing the element
Throws:
OMException - if an error occurs when reading from the element
IOException - if an error occurs when writing to the stream
See Also:
getText()

getTextAsQName

QName getTextAsQName()
Resolve the content of this element to a QName. The QName is interpreted in a way that is compatible with the XML schema specification. In particular, surrounding whitespace is ignored.

Returns:
the resolved QName, or null if the element is empty or the QName could not be resolved

setNamespace

void setNamespace(OMNamespace namespace)
Set the namespace for this element. In addition to changing the namespace URI and prefix of the element information item, this method ensures that a corresponding namespace declaration exists. If no corresponding namespace declaration is already in scope, then a new one will be added to this element.

Parameters:
namespace - The new namespace for this element, or null to remove the namespace from this element. If an OMNamespace instance with a null prefix is given, then a prefix will be generated automatically. In this case, the generated prefix can be determined using OMNamedInformationItem.getNamespace().
Throws:
IllegalArgumentException - if an attempt is made to bind a prefix to the empty namespace name

setNamespaceWithNoFindInCurrentScope

void setNamespaceWithNoFindInCurrentScope(OMNamespace namespace)
This will not search the namespace in the scope nor will declare in the current element, as in setNamespace(OMNamespace). This will just assign the given namespace to the element.

Parameters:
namespace -

toString

String toString()
Convenience method to serialize the element to a string with caching enabled. Caching means that the object model tree for the element will be fully built in memory and can be accessed after invoking this method.

This method produces the same result as OMContainer.serialize(Writer). In particular, the element is always serialized as plain XML and OMText nodes containing optimized binary data are always inlined using base64 encoding. Since the output is accumulated into a single string object, this may result in high memory consumption. Therefore this method should be used with care.

Overrides:
toString in class Object
Returns:
the serialized object model

toStringWithConsume

String toStringWithConsume()
                           throws XMLStreamException
Convenience method to serialize the element to a string without caching. This method will not built the object model tree in memory. This means that an attempt to access the object model after invoking this method may result in an error (unless the object model was already fully built before, e.g. because it was created programmatically).

As for toString(), this method may cause high memory consumption for object model trees containing optimized binary data and should therefore be used with care.

Returns:
the serialized object model
Throws:
XMLStreamException

resolveQName

QName resolveQName(String qname)
Resolves a QName literal in the namespace context defined by this element and produces a corresponding QName object. The implementation uses the algorithm defined by the XML Schema specification. In particular, the namespace for an unprefixed QName is the default namespace (not the null namespace), i.e. QNames are resolved in the same way as element names.

Parameters:
qname - the QName literal to resolve
Returns:
the QName object, or null if the QName can't be resolved, i.e. if the prefix is not bound in the namespace context of this element

cloneOMElement

OMElement cloneOMElement()
Clones this element and its descendants using default options. This method has the same effect as OMInformationItem.clone(OMCloneOptions) with default options.

Returns:
the cloned element

setLineNumber

void setLineNumber(int lineNumber)

getLineNumber

int getLineNumber()

serialize

void serialize(OutputStream output)
               throws XMLStreamException
Serialize the node with caching enabled.

This method will always serialize the infoset as plain XML. In particular, any OMText containing optimized binary will be inlined using base64 encoding.

Specified by:
serialize in interface OMContainer
Specified by:
serialize in interface OMNode
Parameters:
output - the byte stream to write the serialized infoset to
Throws:
XMLStreamException

serialize

void serialize(Writer writer)
               throws XMLStreamException
Serialize the node with caching enabled.

This method will always serialize the infoset as plain XML. In particular, any OMText containing optimized binary will be inlined using base64 encoding.

Specified by:
serialize in interface OMContainer
Specified by:
serialize in interface OMNode
Parameters:
writer - the character stream to write the serialized infoset to
Throws:
XMLStreamException

serialize

void serialize(OutputStream output,
               OMOutputFormat format)
               throws XMLStreamException
Serialize the node with caching enabled.

The format of the output is controlled by the provided OMOutputFormat object. In particular, OMOutputFormat.setDoOptimize(boolean) can be used to instruct this method to produce an XOP/MTOM encoded MIME message.

Specified by:
serialize in interface OMContainer
Specified by:
serialize in interface OMNode
Parameters:
output - the byte stream to write the serialized infoset to
format - the output format to use
Throws:
XMLStreamException

serialize

void serialize(Writer writer,
               OMOutputFormat format)
               throws XMLStreamException
Serialize the node with caching enabled.

Specified by:
serialize in interface OMContainer
Specified by:
serialize in interface OMNode
Parameters:
writer - the character stream to write the serialized infoset to
format - the output format to use
Throws:
XMLStreamException

serializeAndConsume

void serializeAndConsume(OutputStream output)
                         throws XMLStreamException
Serialize the node without caching.

This method will always serialize the infoset as plain XML. In particular, any OMText containing optimized binary will be inlined using base64 encoding.

Specified by:
serializeAndConsume in interface OMContainer
Specified by:
serializeAndConsume in interface OMNode
Parameters:
output - the byte stream to write the serialized infoset to
Throws:
XMLStreamException

serializeAndConsume

void serializeAndConsume(Writer writer)
                         throws XMLStreamException
Serialize the node without caching.

This method will always serialize the infoset as plain XML. In particular, any OMText containing optimized binary will be inlined using base64 encoding.

Specified by:
serializeAndConsume in interface OMContainer
Specified by:
serializeAndConsume in interface OMNode
Parameters:
writer - the character stream to write the serialized infoset to
Throws:
XMLStreamException

serializeAndConsume

void serializeAndConsume(OutputStream output,
                         OMOutputFormat format)
                         throws XMLStreamException
Serialize the node without caching.

The format of the output is controlled by the provided OMOutputFormat object. In particular, OMOutputFormat.setDoOptimize(boolean) can be used to instruct this method to produce an XOP/MTOM encoded MIME message.

Specified by:
serializeAndConsume in interface OMContainer
Specified by:
serializeAndConsume in interface OMNode
Parameters:
output - the byte stream to write the serialized infoset to
format - the output format to use
Throws:
XMLStreamException

serializeAndConsume

void serializeAndConsume(Writer writer,
                         OMOutputFormat format)
                         throws XMLStreamException
Serialize the node without caching.

Specified by:
serializeAndConsume in interface OMContainer
Specified by:
serializeAndConsume in interface OMNode
Parameters:
writer - the character stream to write the serialized infoset to
format - the output format to use
Throws:
XMLStreamException


Copyright © 2004-2012 The Apache Software Foundation. All Rights Reserved.