Top Description Fields Constructors Methods
com.ctc.wstx.sr

public final Class AttributeCollector

extends Object
Class Inheritance
Imports
java.io.IOException, java.util.Arrays, javax.xml.XMLConstants, javax.xml.stream.Location, .XMLStreamException, javax.xml.namespace.QName, org.codehaus.stax2.ri.typed.CharArrayBase64Decoder, .ValueDecoderFactory, org.codehaus.stax2.typed.Base64Variant, .TypedArrayDecoder, .TypedValueDecoder, .TypedXMLStreamException, org.codehaus.stax2.validation.XMLValidator, com.ctc.wstx.api.ReaderConfig, com.ctc.wstx.cfg.ErrorConsts, com.ctc.wstx.sw.XmlWriter, com.ctc.wstx.util.*

Shared base class that defines API stream reader uses to communicate with the attribute collector implementation, independent of whether it's operating in namespace-aware or non-namespace modes. Collector class is used to build up attribute lists; for the most part will just hold references to few specialized TextBuilders that are used to create efficient semi-shared value Strings.

Field Summary

Modifier and TypeField and Description
protected static final int
EXP_ATTR_COUNT

Expected typical maximum number of attributes for any element; chosen to minimize need to resize, while trying not to waste space.

protected static final int
protected static final int
protected static final int
LONG_ATTR_LIST_LEN

Threshold value that indicates minimum length for lists instances that need a Map structure, for fast attribute access by fully-qualified name.

protected int
mAttrCount

Actual number of attributes collected, including attributes added via default values.

protected int
mAttrHashSize

Size of hash area in mAttrMap; generally at least 20% more than number of attributes (mAttrCount).

protected Attribute[]
mAttributes

Array of attributes collected for this element.

protected int[]
mAttrMap

Encoding of a data structure that contains mapping from attribute names to attribute index in main attribute name arrays.

protected int
mAttrSpillEnd

Pointer to int slot right after last spill entr, in mAttrMap array.

protected boolean
mDefaultNsDeclared

Flag to indicate whether the default namespace has already been declared for the current element.

protected int
private final TextBuilder
mNamespaceBuilder

TextBuilder into which values of namespace URIs are added (including URI for the default namespace, if one defined).

protected Attribute[]
mNamespaces

Array of namespace declaration attributes collected for this element; not used in non-namespace-aware mode

protected int
mNonDefCount

Number of attribute values actually parsed, not including ones created via default value expansion.

protected int
mNsCount

Number of valid namespace declarations in mNamespaces.

protected TextBuilder
mValueBuilder

TextBuilder into which values of all attributes are appended to, including default valued ones (defaults are added after explicit ones).

protected int
mXmlIdAttrIndex

Index of "xml:id" attribute, if one exists for the current element; XMLID_IX_NONE if none.

protected final String
protected final String
protected static final InternCache
protected static final int
XMLID_IX_DISABLED

This value is used to indicate that we shouldn't keep track of index of xml:id attribute -- generally done when Xml:id support is disabled

protected static final int

Constructor Summary

AccessConstructor and Description
protected
AttributeCollector(ReaderConfig cfg, boolean nsAware)

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

Index of the newly added attribute, if added; -1 to indicate this was a duplicate
addDefaultAttribute
(String localName, String uri, String prefix, String value)

Method called by validator to insert an attribute that has a default value and wasn't yet included in collector's attribute set.

protected final void
allocBuffers()

Method called to initialize buffers that need not be immediately initialized

public ElemAttrs
buildAttrOb()

Method needed by event creating code, to build a non-transient attribute container, to use with XMLEvent objects (specifically implementation of StartElement event).

private static final boolean
checkExpand(TypedArrayDecoder tad)

Internal method used to see if we can expand the buffer that the array decoder has.

public final byte[]
public final void
decodeValue(int index, TypedValueDecoder tvd)

Method called to decode the whole attribute value as a single typed value.

public final int

Returns:

Number of tokens decoded
decodeValues
(int index, TypedArrayDecoder tad, InputProblemReporter rep)

Method called to decode the attribute value that consists of zero or more space-separated tokens.

private static final int
decodeValues(TypedArrayDecoder tad, InputProblemReporter rep, final char[] buf, int ptr, final int end)

public int
findIndex(String localName)

public int
findIndex(String nsURI, String localName)

public final TextBuilder
getAttrBuilder(String attrPrefix, String attrLocalName)

Low-level accessor method that attribute validation code may call for certain types of attributes; generally only for id and idref/idrefs attributes.

public final int
public TextBuilder

Returns:

null if the default namespace URI has been already declared for the current element; TextBuilder to add URI to if not.
getDefaultNsBuilder
()

public String
getLocalName(int index)

public int
public TextBuilder

Returns:

null if prefix has been already declared; TextBuilder to add value to if not.
getNsBuilder
(String prefix)

protected int

Returns:

Number of namespace declarations collected, including possible default namespace declaration
getNsCount
()

public String
getNsPrefix(int index)

public String
getNsURI(int index)

public String
getPrefix(int index)

public QName
getQName(int index)

protected char[]
public int

Returns:

Number of attributes that were explicitly specified; may be less than the total count due to attributes created using attribute default values
getSpecifiedCount
()

public String
getURI(int index)

public final String
getValue(int index)

Note

the main reason this method is defined at this level, and made final, is performance.

public String
getValue(String nsURI, String localName)

public String
getValueByLocalName(String localName)

Specialized version in which namespace information is completely ignored.

protected int
getValueStartOffset(int index)

Method for getting start pointer within shared value buffer for given attribute.

public final int
public boolean
private void
initHashArea()

Method called to ensure hash area will be properly set up in cases where initially no room was needed, but default attribute(s) is being added.

public final boolean
isSpecified(int index)

public void
normalizeSpacesInValue(int index)

Method that can be called to force space normalization (remove leading/trailing spaces, replace non-spaces white space with spaces, collapse spaces to one) on specified attribute.

public void
reset()

Method called to allow reusing of collector, usually right before starting collecting attributes for a new start tag.

protected Attribute

Returns:

Attribute that contains specified namespace declaration
resolveNamespaceDecl
(int index, boolean internURI)

Method called to resolve and initialize specified collected namespace declaration

public int

Returns:

Index of xml:id attribute, if any, -1 if not
resolveNamespaces
(InputProblemReporter
Reporter to use for reporting well-formedness problems
rep
,
StringVector
Namespace prefix/URI mappings active for this element
ns
)

Method called to resolve namespace URIs from attribute prefixes.

public void
setMaxAttributesPerElement(int maxAttributesPerElement)

public final void
setNormalizedValue(int index, String value)

Low-level mutator method that attribute validation code may call for certain types of attributes, when it wants to handle the whole validation and normalization process by itself.

private int[]

Returns:

Null, if attribute is a duplicate (to indicate error); map itself, or resized version, otherwise.
spillAttr
(String uri, String name, int[] map, int currIndex, int spillIndex, int hash, int hashCount)

protected void
throwDupAttr(InputProblemReporter rep, int index)

Method that can be used to get the specified attribute value, by getting it written using Writer passed in. Can potentially save one String allocation, since no (temporary) Strings need to be created.

protected void
throwIndex(int index)

protected void
public void
writeAttribute(int index, XmlWriter xw)
Deprecated Since 5.0.3

public void
writeAttribute(int index, XmlWriter xw, XMLValidator validator)

Method that basically serializes the specified (read-in) attribute using Writers provided.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EXP_ATTR_COUNTback to summary
protected static final int EXP_ATTR_COUNT

Expected typical maximum number of attributes for any element; chosen to minimize need to resize, while trying not to waste space. Dynamically grown; better not to set too high to avoid excessive overhead for small attribute-less documents.

EXP_NS_COUNTback to summary
protected static final int EXP_NS_COUNT
INT_SPACEback to summary
protected static final int INT_SPACE
LONG_ATTR_LIST_LENback to summary
protected static final int LONG_ATTR_LIST_LEN

Threshold value that indicates minimum length for lists instances that need a Map structure, for fast attribute access by fully-qualified name.

mAttrCountback to summary
protected int mAttrCount

Actual number of attributes collected, including attributes added via default values.

mAttrHashSizeback to summary
protected int mAttrHashSize

Size of hash area in mAttrMap; generally at least 20% more than number of attributes (mAttrCount).

mAttributesback to summary
protected Attribute[] mAttributes

Array of attributes collected for this element.

mAttrMapback to summary
protected int[] mAttrMap

Encoding of a data structure that contains mapping from attribute names to attribute index in main attribute name arrays.

Data structure contains two separate areas; main hash area (with size mAttrHashSize), and remaining spillover area that follows hash area up until (but not including) mAttrSpillEnd index. Main hash area only contains indexes (index+1; 0 signifying empty slot) to actual attributes; spillover area has both hash and index for any spilled entry. Spilled entries are simply stored in order added, and need to be searched using linear search. In case of both primary hash hits and spills, eventual comparison with the local name needs to be done with actual name array.

mAttrSpillEndback to summary
protected int mAttrSpillEnd

Pointer to int slot right after last spill entr, in mAttrMap array.

mDefaultNsDeclaredback to summary
protected boolean mDefaultNsDeclared

Flag to indicate whether the default namespace has already been declared for the current element.

mMaxAttributesPerElementback to summary
protected int mMaxAttributesPerElement
mNamespaceBuilderback to summary
private final TextBuilder mNamespaceBuilder

TextBuilder into which values of namespace URIs are added (including URI for the default namespace, if one defined).

mNamespacesback to summary
protected Attribute[] mNamespaces

Array of namespace declaration attributes collected for this element; not used in non-namespace-aware mode

mNonDefCountback to summary
protected int mNonDefCount

Number of attribute values actually parsed, not including ones created via default value expansion. Equal to or less than mAttrCount.

mNsCountback to summary
protected int mNsCount

Number of valid namespace declarations in mNamespaces.

mValueBuilderback to summary
protected TextBuilder mValueBuilder

TextBuilder into which values of all attributes are appended to, including default valued ones (defaults are added after explicit ones). Constructed lazily, if and when needed (not needed for short attribute-less docs)

mXmlIdAttrIndexback to summary
protected int mXmlIdAttrIndex

Index of "xml:id" attribute, if one exists for the current element; XMLID_IX_NONE if none.

mXmlIdLocalNameback to summary
protected final String mXmlIdLocalName
mXmlIdPrefixback to summary
protected final String mXmlIdPrefix
sInternCacheback to summary
protected static final InternCache sInternCache
XMLID_IX_DISABLEDback to summary
protected static final int XMLID_IX_DISABLED

This value is used to indicate that we shouldn't keep track of index of xml:id attribute -- generally done when Xml:id support is disabled

XMLID_IX_NONEback to summary
protected static final int XMLID_IX_NONE

Constructor Detail

AttributeCollectorback to summary
protected AttributeCollector(ReaderConfig cfg, boolean nsAware)

Method Detail

addDefaultAttributeback to summary
public int addDefaultAttribute(String localName, String uri, String prefix, String value) throws XMLStreamException

Method called by validator to insert an attribute that has a default value and wasn't yet included in collector's attribute set.

Returns:int

Index of the newly added attribute, if added; -1 to indicate this was a duplicate

allocBuffersback to summary
protected final void allocBuffers()

Method called to initialize buffers that need not be immediately initialized

buildAttrObback to summary
public ElemAttrs buildAttrOb()

Method needed by event creating code, to build a non-transient attribute container, to use with XMLEvent objects (specifically implementation of StartElement event).

checkExpandback to summary
private static final boolean checkExpand(TypedArrayDecoder tad)

Internal method used to see if we can expand the buffer that the array decoder has. Bit messy, but simpler than having separately typed instances; and called rarely so that performance downside of instanceof is irrelevant.

decodeBinaryback to summary
public final byte[] decodeBinary(int index, Base64Variant v, CharArrayBase64Decoder dec, InputProblemReporter rep) throws XMLStreamException
decodeValueback to summary
public final void decodeValue(int index, TypedValueDecoder tvd) throws IllegalArgumentException

Method called to decode the whole attribute value as a single typed value. Decoding is done using the decoder provided.

decodeValuesback to summary
public final int decodeValues(int index, TypedArrayDecoder tad, InputProblemReporter rep) throws XMLStreamException

Method called to decode the attribute value that consists of zero or more space-separated tokens. Decoding is done using the decoder provided.

Returns:int

Number of tokens decoded

decodeValuesback to summary
private static final int decodeValues(TypedArrayDecoder tad, InputProblemReporter rep, final char[] buf, int ptr, final int end) throws XMLStreamException
findIndexback to summary
public int findIndex(String localName)
findIndexback to summary
public int findIndex(String nsURI, String localName)
getAttrBuilderback to summary
public final TextBuilder getAttrBuilder(String attrPrefix, String attrLocalName) throws XMLStreamException

Low-level accessor method that attribute validation code may call for certain types of attributes; generally only for id and idref/idrefs attributes. It returns the underlying 'raw' attribute value buffer for direct access.

getCountback to summary
public final int getCount()
getDefaultNsBuilderback to summary
public TextBuilder getDefaultNsBuilder() throws XMLStreamException
Returns:TextBuilder

null if the default namespace URI has been already declared for the current element; TextBuilder to add URI to if not.

getLocalNameback to summary
public String getLocalName(int index)
getMaxAttributesPerElementback to summary
public int getMaxAttributesPerElement()
getNsBuilderback to summary
public TextBuilder getNsBuilder(String prefix) throws XMLStreamException
Returns:TextBuilder

null if prefix has been already declared; TextBuilder to add value to if not.

getNsCountback to summary
protected int getNsCount()
Returns:int

Number of namespace declarations collected, including possible default namespace declaration

getNsPrefixback to summary
public String getNsPrefix(int index)
getNsURIback to summary
public String getNsURI(int index)
getPrefixback to summary
public String getPrefix(int index)
getQNameback to summary
public QName getQName(int index)
getSharedValueBufferback to summary
protected char[] getSharedValueBuffer()
getSpecifiedCountback to summary
public int getSpecifiedCount()
Returns:int

Number of attributes that were explicitly specified; may be less than the total count due to attributes created using attribute default values

getURIback to summary
public String getURI(int index)
getValueback to summary
public final String getValue(int index)

Note

the main reason this method is defined at this level, and made final, is performance. JIT may be able to fully inline this method, even when reference is via this base class. This is important since this is likely to be the most often called method of the collector instances.

getValueback to summary
public String getValue(String nsURI, String localName)
getValueByLocalNameback to summary
public String getValueByLocalName(String localName)

Specialized version in which namespace information is completely ignored.

Since
5.2
getValueStartOffsetback to summary
protected int getValueStartOffset(int index)

Method for getting start pointer within shared value buffer for given attribute. It is also the same as end pointer for preceding attribute, if any.

getXmlIdAttrIndexback to summary
public final int getXmlIdAttrIndex()
hasDefaultNsback to summary
public boolean hasDefaultNs()
initHashAreaback to summary
private void initHashArea()

Method called to ensure hash area will be properly set up in cases where initially no room was needed, but default attribute(s) is being added.

isSpecifiedback to summary
public final boolean isSpecified(int index)
normalizeSpacesInValueback to summary
public void normalizeSpacesInValue(int index)

Method that can be called to force space normalization (remove leading/trailing spaces, replace non-spaces white space with spaces, collapse spaces to one) on specified attribute. Currently called by InputElementStack to force normalization of Xml:id attribute

resetback to summary
public void reset()

Method called to allow reusing of collector, usually right before starting collecting attributes for a new start tag.

Note

public only so that it can be called by unit tests.

resolveNamespaceDeclback to summary
protected Attribute resolveNamespaceDecl(int index, boolean internURI)

Method called to resolve and initialize specified collected namespace declaration

Returns:Attribute

Attribute that contains specified namespace declaration

resolveNamespacesback to summary
public int resolveNamespaces(InputProblemReporter rep, StringVector ns) throws XMLStreamException

Method called to resolve namespace URIs from attribute prefixes.

Note

public only so that it can be called by unit tests.

Parameters
rep:InputProblemReporter

Reporter to use for reporting well-formedness problems

ns:StringVector

Namespace prefix/URI mappings active for this element

Returns:int

Index of xml:id attribute, if any, -1 if not

setMaxAttributesPerElementback to summary
public void setMaxAttributesPerElement(int maxAttributesPerElement)
setNormalizedValueback to summary
public final void setNormalizedValue(int index, String value)

Low-level mutator method that attribute validation code may call for certain types of attributes, when it wants to handle the whole validation and normalization process by itself. It is generally only called for id and idref/idrefs attributes, as those values are usually normalized.

spillAttrback to summary
private int[] spillAttr(String uri, String name, int[] map, int currIndex, int spillIndex, int hash, int hashCount)
Returns:int[]

Null, if attribute is a duplicate (to indicate error); map itself, or resized version, otherwise.

throwDupAttrback to summary
protected void throwDupAttr(InputProblemReporter rep, int index) throws XMLStreamException

Method that can be used to get the specified attribute value, by getting it written using Writer passed in. Can potentially save one String allocation, since no (temporary) Strings need to be created.

throwIndexback to summary
protected void throwIndex(int index)
validateAttributeback to summary
protected void validateAttribute(int index, XMLValidator vld) throws XMLStreamException
writeAttributeback to summary
public void writeAttribute(int index, XmlWriter xw) throws IOException, XMLStreamException

Deprecated

Since 5.0.3

Annotations
@Deprecated
writeAttributeback to summary
public void writeAttribute(int index, XmlWriter xw, XMLValidator validator) throws IOException, XMLStreamException

Method that basically serializes the specified (read-in) attribute using Writers provided. Serialization is done by writing out (fully-qualified) name of the attribute, followed by the equals sign and quoted value.