org.apache.xerces.util

Class XMLAttributesImpl

Implemented Interfaces:
org.apache.xerces.xni.XMLAttributes

public class XMLAttributesImpl
extends java.lang.Object
implements org.apache.xerces.xni.XMLAttributes

The XMLAttributesImpl class is an implementation of the XMLAttributes interface which defines a collection of attributes for an element. In the parser, the document source would scan the entire start element and collect the attributes. The attributes are communicated to the document handler in the startElement method.

The attributes are read-write so that subsequent stages in the document pipeline can modify the values or change the attributes that are propogated to the next stage.

Version:
$Id: XMLAttributesImpl.java,v 1.26 2004/03/25 04:03:22 mrglavas Exp $
Authors:
Andy Clark, IBM
Elena Litani, IBM
Michael Glavassevich, IBM
See Also:
org.apache.xerces.xni.XMLDocumentHandler.startElement

Field Summary

protected static int
SIZE_LIMIT
Threshold at which an instance is treated as a large attribute list.
protected static int
TABLE_SIZE
Default table size.
protected org.apache.xerces.util.XMLAttributesImpl.Attribute[]
fAttributeTableView
Hashtable of attribute information.
protected int[]
fAttributeTableViewChainState
Tracks whether each chain in the hash table is stale with respect to the current state of this object.
protected org.apache.xerces.util.XMLAttributesImpl.Attribute[]
fAttributes
Attribute information.
protected boolean
fIsTableViewConsistent
Indicates whether the table view contains consistent data.
protected int
fLargeCount
Usage count for the attribute table view.
protected int
fLength
Attribute count.
protected boolean
fNamespaces
Namespaces.
protected int
fTableViewBuckets
Actual number of buckets in the table view.

Constructor Summary

XMLAttributesImpl()
Default constructor.
XMLAttributesImpl(int tableSize)

Method Summary

int
addAttribute(org.apache.xerces.xni.QName name, String type, String value)
Adds an attribute.
void
addAttributeNS(org.apache.xerces.xni.QName name, String type, String value)
Adds an attribute.
org.apache.xerces.xni.QName
checkDuplicatesNS()
Checks for duplicate expanded names (local part and namespace name pairs) in the attribute specification.
protected void
cleanTableView()
Purges all elements from the table view.
org.apache.xerces.xni.Augmentations
getAugmentations(String qName)
Look up an augmentation by XML 1.0 qualified name.
org.apache.xerces.xni.Augmentations
getAugmentations(String uri, String localName)
Look up an augmentations by Namespace name.
org.apache.xerces.xni.Augmentations
getAugmentations(int attributeIndex)
Look up an augmentations by attributes index.
int
getIndex(String qName)
Look up the index of an attribute by XML 1.0 qualified name.
int
getIndex(String uri, String localPart)
Look up the index of an attribute by Namespace name.
int
getIndexFast(String qName)
Look up the index of an attribute by XML 1.0 qualified name.
int
getIndexFast(String uri, String localPart)
Look up the index of an attribute by Namespace name.
int
getLength()
Return the number of attributes in the list.
String
getLocalName(int index)
Look up an attribute's local name by index.
String
getName(int index)
Return the name of an attribute in this list (by position).
void
getName(int attrIndex, org.apache.xerces.xni.QName attrName)
Sets the fields in the given QName structure with the values of the attribute name at the specified index.
String
getNonNormalizedValue(int attrIndex)
Returns the non-normalized value of the attribute at the specified index.
String
getPrefix(int index)
Returns the prefix of the attribute at the specified index.
String
getQName(int index)
Look up an attribute's XML 1.0 qualified name by index.
boolean
getSchemaId(String qname)
boolean
getSchemaId(String uri, String localName)
boolean
getSchemaId(int index)
protected int
getTableViewBucket(String qname)
Returns the position in the table view where the given attribute name would be hashed.
protected int
getTableViewBucket(String localpart, String uri)
Returns the position in the table view where the given attribute name would be hashed.
String
getType(String qname)
Look up an attribute's type by XML 1.0 qualified name.
String
getType(String uri, String localName)
Look up an attribute's type by Namespace name.
String
getType(int index)
Look up an attribute's type by index.
String
getURI(int index)
Look up an attribute's Namespace URI by index.
String
getValue(String qname)
Look up an attribute's value by XML 1.0 qualified name.
String
getValue(String uri, String localName)
Look up an attribute's value by Namespace name.
String
getValue(int index)
Look up an attribute's value by index.
boolean
isSpecified(int attrIndex)
Returns true if the attribute is specified in the instance document.
protected void
prepareAndPopulateTableView()
Prepares the table view of the attributes list for use, and populates it with the attributes which have been previously read.
protected void
prepareTableView()
Prepares the table view of the attributes list for use.
void
removeAllAttributes()
Removes all of the attributes.
void
removeAttributeAt(int attrIndex)
Removes the attribute at the specified index.
void
setAugmentations(int attrIndex, org.apache.xerces.xni.Augmentations augs)
Sets the augmentations of the attribute at the specified index.
void
setName(int attrIndex, org.apache.xerces.xni.QName attrName)
Sets the name of the attribute at the specified index.
void
setNamespaces(boolean namespaces)
Sets whether namespace processing is being performed.
void
setNonNormalizedValue(int attrIndex, String attrValue)
Sets the non-normalized value of the attribute at the specified index.
void
setSchemaId(int attrIndex, boolean schemaId)
void
setSpecified(int attrIndex, boolean specified)
Sets whether an attribute is specified in the instance document or not.
void
setType(int attrIndex, String attrType)
Sets the type of the attribute at the specified index.
void
setURI(int attrIndex, String uri)
Sets the uri of the attribute at the specified index.
void
setValue(int attrIndex, String attrValue)
Sets the value of the attribute at the specified index.

Field Details

SIZE_LIMIT

protected static final int SIZE_LIMIT
Threshold at which an instance is treated as a large attribute list.
Field Value:
20

TABLE_SIZE

protected static final int TABLE_SIZE
Default table size.
Field Value:
101

fAttributeTableView

protected org.apache.xerces.util.XMLAttributesImpl.Attribute[] fAttributeTableView
Hashtable of attribute information. Provides an alternate view of the attribute specification.

fAttributeTableViewChainState

protected int[] fAttributeTableViewChainState
Tracks whether each chain in the hash table is stale with respect to the current state of this object. A chain is stale if its state is not the same as the number of times the attribute table view has been used.

fAttributes

protected org.apache.xerces.util.XMLAttributesImpl.Attribute[] fAttributes
Attribute information.

fIsTableViewConsistent

protected boolean fIsTableViewConsistent
Indicates whether the table view contains consistent data.

fLargeCount

protected int fLargeCount
Usage count for the attribute table view. Incremented each time all attributes are removed when the attribute table view is in use.

fLength

protected int fLength
Attribute count.

fNamespaces

protected boolean fNamespaces
Namespaces.

fTableViewBuckets

protected int fTableViewBuckets
Actual number of buckets in the table view.

Constructor Details

XMLAttributesImpl

public XMLAttributesImpl()
Default constructor.

XMLAttributesImpl

public XMLAttributesImpl(int tableSize)
Parameters:
tableSize - initial size of table view

Method Details

addAttribute

public int addAttribute(org.apache.xerces.xni.QName name,
                        String type,
                        String value)
Adds an attribute. The attribute's non-normalized value of the attribute will have the same value as the attribute value until set using the setNonNormalizedValue method. Also, the added attribute will be marked as specified in the XML instance document unless set otherwise using the setSpecified method.

Note: If an attribute of the same name already exists, the old values for the attribute are replaced by the new values.

Specified by:
addAttribute in interface org.apache.xerces.xni.XMLAttributes
Parameters:
name - The attribute name.
type - The attribute type. The type name is determined by the type specified for this attribute in the DTD. For example: "CDATA", "ID", "NMTOKEN", etc. However, attributes of type enumeration will have the type value specified as the pipe ('|') separated list of the enumeration values prefixed by an open parenthesis and suffixed by a close parenthesis. For example: "(true|false)".
value - The attribute value.
Returns:
Returns the attribute index.

addAttributeNS

public void addAttributeNS(org.apache.xerces.xni.QName name,
                           String type,
                           String value)
Adds an attribute. The attribute's non-normalized value of the attribute will have the same value as the attribute value until set using the setNonNormalizedValue method. Also, the added attribute will be marked as specified in the XML instance document unless set otherwise using the setSpecified method.

This method differs from addAttribute in that it does not check if an attribute of the same name already exists in the list before adding it. In order to improve performance of namespace processing, this method allows uniqueness checks to be deferred until all the namespace information is available after the entire attribute specification has been read.

Caution: If this method is called it should not be mixed with calls to addAttribute unless it has been determined that all the attribute names are unique.

Parameters:
name - the attribute name
type - the attribute type
value - the attribute value

checkDuplicatesNS

public org.apache.xerces.xni.QName checkDuplicatesNS()
Checks for duplicate expanded names (local part and namespace name pairs) in the attribute specification. If a duplicate is found its name is returned.

This should be called once all the in-scope namespaces for the element enclosing these attributes is known, and after all the attributes have gone through namespace binding.

Returns:
the name of a duplicate attribute found in the search, otherwise null.

cleanTableView

protected void cleanTableView()
Purges all elements from the table view.

getAugmentations

public org.apache.xerces.xni.Augmentations getAugmentations(String qName)
Look up an augmentation by XML 1.0 qualified name.

Specified by:
getAugmentations in interface org.apache.xerces.xni.XMLAttributes
Parameters:
qName - The XML 1.0 qualified name.
Returns:
Augmentations

getAugmentations

public org.apache.xerces.xni.Augmentations getAugmentations(String uri,
                                                            String localName)
Look up an augmentations by Namespace name.
Specified by:
getAugmentations in interface org.apache.xerces.xni.XMLAttributes
Parameters:
uri - The Namespace URI, or null if the
localName - The local name of the attribute.
Returns:
Augmentations

getAugmentations

public org.apache.xerces.xni.Augmentations getAugmentations(int attributeIndex)
Look up an augmentations by attributes index.
Specified by:
getAugmentations in interface org.apache.xerces.xni.XMLAttributes
Parameters:
attributeIndex - The attribute index.
Returns:
Augmentations

getIndex

public int getIndex(String qName)
Look up the index of an attribute by XML 1.0 qualified name.
Specified by:
getIndex in interface org.apache.xerces.xni.XMLAttributes
Parameters:
qName - The qualified (prefixed) name.
Returns:
The index of the attribute, or -1 if it does not appear in the list.

getIndex

public int getIndex(String uri,
                    String localPart)
Look up the index of an attribute by Namespace name.
Specified by:
getIndex in interface org.apache.xerces.xni.XMLAttributes
Parameters:
uri - The Namespace URI, or null if the name has no Namespace URI.
localPart - The attribute's local name.
Returns:
The index of the attribute, or -1 if it does not appear in the list.

getIndexFast

public int getIndexFast(String qName)
Look up the index of an attribute by XML 1.0 qualified name.

Note: This method uses reference comparison, and thus should only be used internally. We cannot use this method in any code exposed to users as they may not pass in unique strings.

Parameters:
qName - The qualified (prefixed) name.
Returns:
The index of the attribute, or -1 if it does not appear in the list.

getIndexFast

public int getIndexFast(String uri,
                        String localPart)
Look up the index of an attribute by Namespace name.

Note: This method uses reference comparison, and thus should only be used internally. We cannot use this method in any code exposed to users as they may not pass in unique strings.

Parameters:
uri - The Namespace URI, or null if the name has no Namespace URI.
localPart - The attribute's local name.
Returns:
The index of the attribute, or -1 if it does not appear in the list.

getLength

public int getLength()
Return the number of attributes in the list.

Once you know the number of attributes, you can iterate through the list.

Specified by:
getLength in interface org.apache.xerces.xni.XMLAttributes
Returns:
The number of attributes in the list.

getLocalName

public String getLocalName(int index)
Look up an attribute's local name by index.
Specified by:
getLocalName in interface org.apache.xerces.xni.XMLAttributes
Parameters:
index - The attribute index (zero-based).
Returns:
The local name, or the empty string if Namespace processing is not being performed, or null if the index is out of range.
See Also:
getLength()

getName

public String getName(int index)
Return the name of an attribute in this list (by position).

The names must be unique: the SAX parser shall not include the same attribute twice. Attributes without values (those declared #IMPLIED without a value specified in the start tag) will be omitted from the list.

If the attribute name has a namespace prefix, the prefix will still be attached.

Parameters:
index - The index of the attribute in the list (starting at 0).
Returns:
The name of the indexed attribute, or null if the index is out of range.

getName

public void getName(int attrIndex,
                    org.apache.xerces.xni.QName attrName)
Sets the fields in the given QName structure with the values of the attribute name at the specified index.
Specified by:
getName in interface org.apache.xerces.xni.XMLAttributes
Parameters:
attrIndex - The attribute index.
attrName - The attribute name structure to fill in.

getNonNormalizedValue

public String getNonNormalizedValue(int attrIndex)
Returns the non-normalized value of the attribute at the specified index. If no non-normalized value is set, this method will return the same value as the getValue(int) method.
Specified by:
getNonNormalizedValue in interface org.apache.xerces.xni.XMLAttributes
Parameters:
attrIndex - The attribute index.

getPrefix

public String getPrefix(int index)
Returns the prefix of the attribute at the specified index.
Specified by:
getPrefix in interface org.apache.xerces.xni.XMLAttributes
Parameters:
index - The index of the attribute.

getQName

public String getQName(int index)
Look up an attribute's XML 1.0 qualified name by index.
Specified by:
getQName in interface org.apache.xerces.xni.XMLAttributes
Parameters:
index - The attribute index (zero-based).
Returns:
The XML 1.0 qualified name, or the empty string if none is available, or null if the index is out of range.
See Also:
getLength()

getSchemaId

public boolean getSchemaId(String qname)

getSchemaId

public boolean getSchemaId(String uri,
                           String localName)

getSchemaId

public boolean getSchemaId(int index)

getTableViewBucket

protected int getTableViewBucket(String qname)
Returns the position in the table view where the given attribute name would be hashed.
Parameters:
qname - the attribute name
Returns:
the position in the table view where the given attribute would be hashed

getTableViewBucket

protected int getTableViewBucket(String localpart,
                                 String uri)
Returns the position in the table view where the given attribute name would be hashed.
Parameters:
localpart - the local part of the attribute
uri - the namespace name of the attribute
Returns:
the position in the table view where the given attribute would be hashed

getType

public String getType(String qname)
Specified by:
getType in interface org.apache.xerces.xni.XMLAttributes
Parameters:
qname - The XML 1.0 qualified name.
Returns:
The attribute type as a string, or null if the attribute is not in the list or if qualified names are not available.

getType

public String getType(String uri,
                      String localName)
Specified by:
getType in interface org.apache.xerces.xni.XMLAttributes
Parameters:
uri - The Namespace URI, or null if the name has no Namespace URI.
localName - The local name of the attribute.
Returns:
The attribute type as a string, or null if the attribute is not in the list or if Namespace processing is not being performed.

getType

public String getType(int index)
Look up an attribute's type by index.

The attribute type is one of the strings "CDATA", "ID", "IDREF", "IDREFS", "NMTOKEN", "NMTOKENS", "ENTITY", "ENTITIES", or "NOTATION" (always in upper case).

If the parser has not read a declaration for the attribute, or if the parser does not report attribute types, then it must return the value "CDATA" as stated in the XML 1.0 Recommentation (clause 3.3.3, "Attribute-Value Normalization").

For an enumerated attribute that is not a notation, the parser will report the type as "NMTOKEN".

Specified by:
getType in interface org.apache.xerces.xni.XMLAttributes
Parameters:
index - The attribute index (zero-based).
Returns:
The attribute's type as a string, or null if the index is out of range.
See Also:
getLength()

getURI

public String getURI(int index)
Look up an attribute's Namespace URI by index.
Specified by:
getURI in interface org.apache.xerces.xni.XMLAttributes
Parameters:
index - The attribute index (zero-based).
Returns:
The Namespace URI
See Also:
getLength()

getValue

public String getValue(String qname)
Specified by:
getValue in interface org.apache.xerces.xni.XMLAttributes
Parameters:
qname - The XML 1.0 qualified name.
Returns:
The attribute value as a string, or null if the attribute is not in the list or if qualified names are not available.

getValue

public String getValue(String uri,
                       String localName)
Specified by:
getValue in interface org.apache.xerces.xni.XMLAttributes
Parameters:
uri - The Namespace URI, or null if the
localName - The local name of the attribute.
Returns:
The attribute value as a string, or null if the attribute is not in the list.

getValue

public String getValue(int index)
Look up an attribute's value by index.

If the attribute value is a list of tokens (IDREFS, ENTITIES, or NMTOKENS), the tokens will be concatenated into a single string with each token separated by a single space.

Specified by:
getValue in interface org.apache.xerces.xni.XMLAttributes
Parameters:
index - The attribute index (zero-based).
Returns:
The attribute's value as a string, or null if the index is out of range.
See Also:
getLength()

isSpecified

public boolean isSpecified(int attrIndex)
Returns true if the attribute is specified in the instance document.
Specified by:
isSpecified in interface org.apache.xerces.xni.XMLAttributes
Parameters:
attrIndex - The attribute index.

prepareAndPopulateTableView

protected void prepareAndPopulateTableView()
Prepares the table view of the attributes list for use, and populates it with the attributes which have been previously read.

prepareTableView

protected void prepareTableView()
Prepares the table view of the attributes list for use.

removeAllAttributes

public void removeAllAttributes()
Removes all of the attributes. This method will also remove all entities associated to the attributes.
Specified by:
removeAllAttributes in interface org.apache.xerces.xni.XMLAttributes

removeAttributeAt

public void removeAttributeAt(int attrIndex)
Removes the attribute at the specified index.

Note: This operation changes the indexes of all attributes following the attribute at the specified index.

Specified by:
removeAttributeAt in interface org.apache.xerces.xni.XMLAttributes
Parameters:
attrIndex - The attribute index.

setAugmentations

public void setAugmentations(int attrIndex,
                             org.apache.xerces.xni.Augmentations augs)
Sets the augmentations of the attribute at the specified index.
Specified by:
setAugmentations in interface org.apache.xerces.xni.XMLAttributes
Parameters:
attrIndex - The attribute index.
augs - The augmentations.

setName

public void setName(int attrIndex,
                    org.apache.xerces.xni.QName attrName)
Sets the name of the attribute at the specified index.
Specified by:
setName in interface org.apache.xerces.xni.XMLAttributes
Parameters:
attrIndex - The attribute index.
attrName - The new attribute name.

setNamespaces

public void setNamespaces(boolean namespaces)
Sets whether namespace processing is being performed. This state is needed to return the correct value from the getLocalName method.
Parameters:
namespaces - True if namespace processing is turned on.

setNonNormalizedValue

public void setNonNormalizedValue(int attrIndex,
                                  String attrValue)
Sets the non-normalized value of the attribute at the specified index.
Specified by:
setNonNormalizedValue in interface org.apache.xerces.xni.XMLAttributes
Parameters:
attrIndex - The attribute index.
attrValue - The new non-normalized attribute value.

setSchemaId

public void setSchemaId(int attrIndex,
                        boolean schemaId)

setSpecified

public void setSpecified(int attrIndex,
                         boolean specified)
Sets whether an attribute is specified in the instance document or not.
Specified by:
setSpecified in interface org.apache.xerces.xni.XMLAttributes
Parameters:
attrIndex - The attribute index.
specified - True if the attribute is specified in the instance document.

setType

public void setType(int attrIndex,
                    String attrType)
Sets the type of the attribute at the specified index.
Specified by:
setType in interface org.apache.xerces.xni.XMLAttributes
Parameters:
attrIndex - The attribute index.
attrType - The attribute type. The type name is determined by the type specified for this attribute in the DTD. For example: "CDATA", "ID", "NMTOKEN", etc. However, attributes of type enumeration will have the type value specified as the pipe ('|') separated list of the enumeration values prefixed by an open parenthesis and suffixed by a close parenthesis. For example: "(true|false)".

setURI

public void setURI(int attrIndex,
                   String uri)
Sets the uri of the attribute at the specified index.
Parameters:
attrIndex - The attribute index.
uri - Namespace uri

setValue

public void setValue(int attrIndex,
                     String attrValue)
Sets the value of the attribute at the specified index. This method will overwrite the non-normalized value of the attribute.
Specified by:
setValue in interface org.apache.xerces.xni.XMLAttributes
Parameters:
attrIndex - The attribute index.
attrValue - The new attribute value.

Copyright B) 1999-2005 Apache XML Project. All Rights Reserved.