com.ctc.wstx.sr
Class NsInputElementStack

java.lang.Object
  extended by com.ctc.wstx.sr.InputElementStack
      extended by com.ctc.wstx.sr.NsInputElementStack
All Implemented Interfaces:
javax.xml.namespace.NamespaceContext, AttributeInfo, ValidationContext

public final class NsInputElementStack
extends InputElementStack

Sub-class of InputElementStack used when operating in namespace-aware mode.

Implementation note: this class reuses NamespaceContext instances, so that consequtive accesses just return the same instance, as long as nothing in bindings change. As a result, only those instances that explicitly add new bindings create distinct non-shareable context instances. Although it would also be possible to share underlying String array to further improve object sharing, it seems like marginal gain with more complexity: as such the current simple scheme should work just fine (and is measure to be very close to optimal for most common namespace-heavey document types like Soap messages).


Field Summary
protected  NsAttributeCollector mAttrCollector
           
protected  java.lang.String[] mElements
          Array that contains path of open elements from root; for each there are 4 Strings; prefix, localname, URI, and default name space URI.
protected  java.lang.String mLastLocalName
           
protected  javax.xml.namespace.QName mLastName
           
protected  BaseNsContext mLastNsContext
          Last potentially shareable NamespaceContext created by this stack.
protected  java.lang.String mLastNsURI
           
protected  java.lang.String mLastPrefix
           
protected  boolean mMayHaveNsDefaults
           
protected  StringVector mNamespaces
          Vector that contains all currently active namespaces; one String for prefix, another for matching URI.
protected  int[] mNsCounts
          Array that contains namespace offsets for each element; that is, index of first 'local' name space entry, entry declared for current element.
protected  NsDefaultProvider mNsDefaultProvider
          Object that will need to be consulted about namespace bindings, since it has some knowledge about default namespace declarations (has default attribute value expansion).
protected  int mSize
          Number of Strings in mElements that are valid (ie depth multiplied by 4)
protected static InternCache sInternCache
           
 
Fields inherited from class com.ctc.wstx.sr.InputElementStack
mConfig, mIdAttrIndex, mReporter, mValidator
 
Constructor Summary
NsInputElementStack(int initialSize, ReaderConfig cfg)
           
 
Method Summary
 int addDefaultAttribute(java.lang.String localName, java.lang.String uri, java.lang.String prefix, java.lang.String value)
          Method called by actual validator instances when attributes with default values have no explicit values for the element; if so, default value needs to be added as if it was parsed from the element.
 void addNsBinding(java.lang.String prefix, java.lang.String uri)
          Callback method called by the namespace default provider.
 BaseNsContext createNonTransientNsContext(javax.xml.stream.Location loc)
          Method called to construct a non-transient NamespaceContext instance; generally needed when creating events to return from event-based iterators.
 int findAttributeIndex(java.lang.String nsURI, java.lang.String localName)
           
 AttributeCollector getAttrCollector()
          Method called by BasicStreamReader, to retrieve the attribute collector it needs for some direct access.
 int getAttributeCount()
          This method returns number of attributes accessible from within currently active start element.
 javax.xml.namespace.QName getCurrentElementName()
          Method that can be used to access name information of the innermost (top) element in the element stack.
 int getCurrentNsCount()
           
 java.lang.String getDefaultNsURI()
           
 int getDepth()
           
 java.lang.String getLocalName()
           
 java.lang.String getLocalNsPrefix(int index)
           
 java.lang.String getLocalNsURI(int index)
           
 java.lang.String getNamespaceURI(java.lang.String prefix)
          Method that can be called by the validator to resolve a namespace prefix of the currently active top-level element.
 java.lang.String getNsURI()
           
 java.lang.String getPrefix()
           
 java.lang.String getPrefix(java.lang.String nsURI)
           
 java.util.Iterator getPrefixes(java.lang.String nsURI)
           
 java.lang.String getTopElementDesc()
           
 int getTotalNsCount()
           
 boolean isEmpty()
           
 boolean isNamespaceAware()
           
 boolean isPrefixLocallyDeclared(java.lang.String internedPrefix)
           
 boolean matches(java.lang.String prefix, java.lang.String localName)
           
 int pop()
          Method called by the stream reader when encountering an end tag.
 void push(java.lang.String fullName)
          Method called by the stream reader to add new (start) element into the stack in non-namespace mode; called when a start element is encountered during parsing, but only in non-namespace mode.
 void push(java.lang.String prefix, java.lang.String localName)
          Method called by the stream reader to add new (start) element into the stack in namespace-aware mode; called when a start element is encountered during parsing, but only in ns-aware mode.
 int resolveAndValidateElement()
          Method called to update information about top of the stack, with attribute information passed in.
protected  void setAutomaticDTDValidator(XMLValidator validator, NsDefaultProvider nsDefs)
          Method called to connect the automatically handled DTD validator (one detected from DOCTYPE, loaded and completely handled by the stream reader without application calling validation methods).
 
Methods inherited from class com.ctc.wstx.sr.InputElementStack
addValidator, connectReporter, getAttributeLocalName, getAttributeNamespace, getAttributePrefix, getAttributeType, getAttributeValue, getAttributeValue, getBaseUri, getIdAttributeIndex, getNotationAttributeIndex, getValidationLocation, getXmlVersion, isNotationDeclared, isUnparsedEntityDeclared, normalizeXmlIdAttr, reallyValidating, reportProblem, stopValidatingAgainst, stopValidatingAgainst, validateAgainst, validateText
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sInternCache

protected static final InternCache sInternCache

mAttrCollector

protected final NsAttributeCollector mAttrCollector

mNsDefaultProvider

protected NsDefaultProvider mNsDefaultProvider
Object that will need to be consulted about namespace bindings, since it has some knowledge about default namespace declarations (has default attribute value expansion).


mNamespaces

protected final StringVector mNamespaces
Vector that contains all currently active namespaces; one String for prefix, another for matching URI. Does also include default name spaces (at most one per level).


mElements

protected java.lang.String[] mElements
Array that contains path of open elements from root; for each there are 4 Strings; prefix, localname, URI, and default name space URI.


mSize

protected int mSize
Number of Strings in mElements that are valid (ie depth multiplied by 4)


mNsCounts

protected int[] mNsCounts
Array that contains namespace offsets for each element; that is, index of first 'local' name space entry, entry declared for current element. Number of such local entries is mCurrNsCount - mNsCounts[mSize-1]


mMayHaveNsDefaults

protected boolean mMayHaveNsDefaults

mLastLocalName

protected java.lang.String mLastLocalName

mLastPrefix

protected java.lang.String mLastPrefix

mLastNsURI

protected java.lang.String mLastNsURI

mLastName

protected javax.xml.namespace.QName mLastName

mLastNsContext

protected BaseNsContext mLastNsContext
Last potentially shareable NamespaceContext created by this stack. This reference is cleared each time bindings change (either due to a start element with new bindings, or due to the matching end element that closes scope of such binding(s)).

Constructor Detail

NsInputElementStack

public NsInputElementStack(int initialSize,
                           ReaderConfig cfg)
Method Detail

setAutomaticDTDValidator

protected void setAutomaticDTDValidator(XMLValidator validator,
                                        NsDefaultProvider nsDefs)
Description copied from class: InputElementStack
Method called to connect the automatically handled DTD validator (one detected from DOCTYPE, loaded and completely handled by the stream reader without application calling validation methods). Handled separately, since its behaviour is potentially different from that of explicitly added validators.

Specified by:
setAutomaticDTDValidator in class InputElementStack

push

public final void push(java.lang.String prefix,
                       java.lang.String localName)
Description copied from class: InputElementStack
Method called by the stream reader to add new (start) element into the stack in namespace-aware mode; called when a start element is encountered during parsing, but only in ns-aware mode.

Specified by:
push in class InputElementStack

push

public final void push(java.lang.String fullName)
Description copied from class: InputElementStack
Method called by the stream reader to add new (start) element into the stack in non-namespace mode; called when a start element is encountered during parsing, but only in non-namespace mode.

Specified by:
push in class InputElementStack

pop

public int pop()
        throws javax.xml.stream.XMLStreamException
Method called by the stream reader when encountering an end tag.

Specified by:
pop in class InputElementStack
Returns:
Validation state that should be effective for the parent element state
Throws:
javax.xml.stream.XMLStreamException

resolveAndValidateElement

public int resolveAndValidateElement()
                              throws javax.xml.stream.XMLStreamException
Method called to update information about top of the stack, with attribute information passed in. Will resolve namespace references, and update namespace stack with information.

Specified by:
resolveAndValidateElement in class InputElementStack
Returns:
Validation state that should be effective for the fully resolved element context
Throws:
javax.xml.stream.XMLStreamException

isNamespaceAware

public final boolean isNamespaceAware()
Specified by:
isNamespaceAware in class InputElementStack

getDepth

public final int getDepth()
Specified by:
getDepth in class InputElementStack
Returns:
Number of open elements in the stack; 0 when parser is in prolog/epilog, 1 inside root element and so on.

getAttrCollector

public final AttributeCollector getAttrCollector()
Description copied from class: InputElementStack
Method called by BasicStreamReader, to retrieve the attribute collector it needs for some direct access.

Specified by:
getAttrCollector in class InputElementStack

createNonTransientNsContext

public final BaseNsContext createNonTransientNsContext(javax.xml.stream.Location loc)
Method called to construct a non-transient NamespaceContext instance; generally needed when creating events to return from event-based iterators.

Specified by:
createNonTransientNsContext in class InputElementStack

getNamespaceURI

public final java.lang.String getNamespaceURI(java.lang.String prefix)
Description copied from interface: ValidationContext
Method that can be called by the validator to resolve a namespace prefix of the currently active top-level element. This may be necessary for things like DTD validators (which may need to heuristically guess proper namespace URI of attributes, esp. ones with default values).

Specified by:
getNamespaceURI in interface javax.xml.namespace.NamespaceContext
Specified by:
getNamespaceURI in interface ValidationContext
Specified by:
getNamespaceURI in class InputElementStack

getPrefix

public final java.lang.String getPrefix(java.lang.String nsURI)
Specified by:
getPrefix in interface javax.xml.namespace.NamespaceContext
Specified by:
getPrefix in class InputElementStack

getPrefixes

public final java.util.Iterator getPrefixes(java.lang.String nsURI)
Specified by:
getPrefixes in interface javax.xml.namespace.NamespaceContext
Specified by:
getPrefixes in class InputElementStack

getAttributeCount

public final int getAttributeCount()
Description copied from interface: ValidationContext
This method returns number of attributes accessible from within currently active start element.

Note: this method is only guaranteed to be callable during execution of XMLValidator methods XMLValidator.validateElementStart(java.lang.String, java.lang.String, java.lang.String), XMLValidator.validateAttribute(java.lang.String, java.lang.String, java.lang.String, java.lang.String) and XMLValidator.validateElementAndAttributes(). At other times implementations may choose to allow it to be called (for example, with information regarding last start element processed), to throw a IllegalArgumentException, or to return 0 to indicate no attribute information is available.

Also note that whether defaulted attributes (attributes for which values are only available via attribute defaulting) are accessible depends on exact time when this method is called, and in general can not be assumed to function reliably.

Specified by:
getAttributeCount in interface AttributeInfo
Specified by:
getAttributeCount in interface ValidationContext
Specified by:
getAttributeCount in class InputElementStack
Returns:
Number of all attributes accessible (including ones created from the default values, if any) using this Object.

findAttributeIndex

public final int findAttributeIndex(java.lang.String nsURI,
                                    java.lang.String localName)
Specified by:
findAttributeIndex in interface AttributeInfo
Specified by:
findAttributeIndex in interface ValidationContext
Specified by:
findAttributeIndex in class InputElementStack
Returns:
Index of the specified attribute, if the current element has such an attribute (explicit, or one created via default value expansion); -1 if not.

getCurrentElementName

public final javax.xml.namespace.QName getCurrentElementName()
Description copied from interface: ValidationContext
Method that can be used to access name information of the innermost (top) element in the element stack.

Specified by:
getCurrentElementName in interface ValidationContext
Specified by:
getCurrentElementName in class InputElementStack
Returns:
Name of the element at the top of the current element stack, if any. During validation calls it refers to the element being processed (start or end tag), or its parent (when processing text nodes), or null (in document prolog and epilog).

addDefaultAttribute

public int addDefaultAttribute(java.lang.String localName,
                               java.lang.String uri,
                               java.lang.String prefix,
                               java.lang.String value)
Description copied from class: InputElementStack
Method called by actual validator instances when attributes with default values have no explicit values for the element; if so, default value needs to be added as if it was parsed from the element.

Specified by:
addDefaultAttribute in interface ValidationContext
Specified by:
addDefaultAttribute in class InputElementStack
Returns:
Index of the newly added attribute, if operation was succesful; -1 if not.

isPrefixLocallyDeclared

public boolean isPrefixLocallyDeclared(java.lang.String internedPrefix)
Specified by:
isPrefixLocallyDeclared in class InputElementStack
Returns:
True, if the given prefix ("" for default namespace) was bound/unbound in the current element (one at the top of the stack); false if not.

addNsBinding

public void addNsBinding(java.lang.String prefix,
                         java.lang.String uri)
Callback method called by the namespace default provider. At this point we can trust it to only call this method with somewhat valid arguments (no dups etc).

Specified by:
addNsBinding in class InputElementStack

isEmpty

public final boolean isEmpty()
Specified by:
isEmpty in class InputElementStack

getDefaultNsURI

public final java.lang.String getDefaultNsURI()
Specified by:
getDefaultNsURI in class InputElementStack

getNsURI

public final java.lang.String getNsURI()
Specified by:
getNsURI in class InputElementStack

getPrefix

public final java.lang.String getPrefix()
Specified by:
getPrefix in class InputElementStack

getLocalName

public final java.lang.String getLocalName()
Specified by:
getLocalName in class InputElementStack

matches

public final boolean matches(java.lang.String prefix,
                             java.lang.String localName)
Specified by:
matches in class InputElementStack

getTopElementDesc

public final java.lang.String getTopElementDesc()
Specified by:
getTopElementDesc in class InputElementStack

getTotalNsCount

public final int getTotalNsCount()
Specified by:
getTotalNsCount in class InputElementStack
Returns:
Number of active prefix/namespace mappings for current scope, including mappings from enclosing elements.

getCurrentNsCount

public final int getCurrentNsCount()
Specified by:
getCurrentNsCount in class InputElementStack
Returns:
Number of active prefix/namespace mappings for current scope, NOT including mappings from enclosing elements.

getLocalNsPrefix

public final java.lang.String getLocalNsPrefix(int index)
Specified by:
getLocalNsPrefix in class InputElementStack

getLocalNsURI

public final java.lang.String getLocalNsURI(int index)
Specified by:
getLocalNsURI in class InputElementStack