/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2000-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 1999, International
* Business Machines, Inc., http://www.apache.org. For more
* information on the Apache Software Foundation, please see
*/
/**
* 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.
* <p>
* 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.
*
* @see com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler#startElement
*
* @author Andy Clark, IBM
* @author Elena Litani, IBM
* @author Michael Glavassevich, IBM
*
* @version $Id: XMLAttributesImpl.java,v 1.7 2010/05/07 20:13:09 joehw Exp $
*/
public class XMLAttributesImpl
implements XMLAttributes, XMLBufferListener {
//
// Constants
//
/** Default table size. */
/**
* Threshold at which an instance is treated
* as a large attribute list.
*/
//
// Data
//
// features
/** Namespaces. */
protected boolean fNamespaces = true;
// data
/**
* Usage count for the attribute table view.
* Incremented each time all attributes are removed
* when the attribute table view is in use.
*/
/** Attribute count. */
protected int fLength;
/** Attribute information. */
/**
* Hashtable of attribute information.
* Provides an alternate view of the attribute specification.
*/
/**
* 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.
*/
protected int[] fAttributeTableViewChainState;
/**
* Actual number of buckets in the table view.
*/
protected int fTableViewBuckets;
/**
* Indicates whether the table view contains consistent data.
*/
protected boolean fIsTableViewConsistent;
//
// Constructors
//
/** Default constructor. */
public XMLAttributesImpl() {
this(TABLE_SIZE);
}
/**
* @param tableSize initial size of table view
*/
fAttributes[i] = new Attribute();
}
} // <init>()
//
// Public methods
//
/**
* Sets whether namespace processing is being performed. This state
* is needed to return the correct value from the getLocalName method.
*
* @param namespaces True if namespace processing is turned on.
*
* @see #getLocalName
*/
} // setNamespaces(boolean)
//
// XMLAttributes methods
//
/**
* 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 <code>setNonNormalizedValue</code> method. Also,
* the added attribute will be marked as specified in the XML instance
* document unless set otherwise using the <code>setSpecified</code>
* method.
* <p>
* <strong>Note:</strong> If an attribute of the same name already
* exists, the old values for the attribute are replaced by the new
* values.
*
* @param name The attribute name.
* @param 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)".
* @param value The attribute value.
*
* @return Returns the attribute index.
*
* @see #setNonNormalizedValue
* @see #setSpecified
*/
}
int index;
if (fLength < SIZE_LIMIT) {
if (index == -1) {
attributes[i] = new Attribute();
}
}
}
}
/**
* If attributes were removed from the list after the table
* becomes in use this isn't reflected in the table view. It's
* assumed that once a user starts removing attributes they're
* not likely to add more. We only make the view consistent if
* the user of this class adds attributes, removes them, and
* then adds more.
*/
fIsTableViewConsistent = true;
}
// The chain is stale.
// This must be a unique attribute.
attributes[i] = new Attribute();
}
}
// Update table view.
}
// This chain is active.
// We need to check if any of the attributes has the same rawname.
else {
// Search the table.
break;
}
}
// This attribute is unique.
attributes[i] = new Attribute();
}
}
// Update table view
}
// Duplicate. We still need to find the index.
else {
}
}
}
// set values
// clear augmentations
return index;
} // addAttribute(QName,String,XMLString)
/**
* Removes all of the attributes. This method will also remove all
* entities associated to the attributes.
*/
public void removeAllAttributes() {
fLength = 0;
} // removeAllAttributes()
/**
* Removes the attribute at the specified index.
* <p>
* <strong>Note:</strong> This operation changes the indexes of all
* attributes following the attribute at the specified index.
*
* @param attrIndex The attribute index.
*/
fIsTableViewConsistent = false;
// Make the discarded Attribute object available for re-use
// by tucking it after the Attributes that are still in use
}
fLength--;
} // removeAttributeAt(int)
/**
* Sets the name of the attribute at the specified index.
*
* @param attrIndex The attribute index.
* @param attrName The new attribute name.
*/
} // setName(int,QName)
/**
* Sets the fields in the given QName structure with the values
* of the attribute name at the specified index.
*
* @param attrIndex The attribute index.
* @param attrName The attribute name structure to fill in.
*/
} // getName(int,QName)
/**
* Sets the type of the attribute at the specified index.
*
* @param attrIndex The attribute index.
* @param 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)".
*/
} // setType(int,String)
/**
* Sets the value of the attribute at the specified index. This
* method will overwrite the non-normalized value of the attribute.
*
* @param attrIndex The attribute index.
* @param attrValue The new attribute value.
*
* @see #setNonNormalizedValue
*/
}
} // setValue(int,String)
/**
* Sets the non-normalized value of the attribute at the specified
* index.
*
* @param attrIndex The attribute index.
* @param attrValue The new non-normalized attribute value.
*/
}
} // setNonNormalizedValue(int,String)
/**
* 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 <code>getValue(int)</code> method.
*
* @param attrIndex The attribute index.
*/
return value;
} // getNonNormalizedValue(int):String
/**
* Sets whether an attribute is specified in the instance document
* or not.
*
* @param attrIndex The attribute index.
* @param specified True if the attribute is specified in the instance
* document.
*/
} // setSpecified(int,boolean)
/**
* Returns true if the attribute is specified in the instance document.
*
* @param attrIndex The attribute index.
*/
} // isSpecified(int):boolean
//
// AttributeList and Attributes methods
//
/**
* Return the number of attributes in the list.
*
* <p>Once you know the number of attributes, you can iterate
* through the list.</p>
*
* @return The number of attributes in the list.
*/
public int getLength() {
return fLength;
} // getLength():int
/**
* Look up an attribute's type by index.
*
* <p>The attribute type is one of the strings "CDATA", "ID",
* "IDREF", "IDREFS", "NMTOKEN", "NMTOKENS", "ENTITY", "ENTITIES",
* or "NOTATION" (always in upper case).</p>
*
* <p>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").</p>
*
* <p>For an enumerated attribute that is not a notation, the
* parser will report the type as "NMTOKEN".</p>
*
* @param index The attribute index (zero-based).
* @return The attribute's type as a string, or null if the
* index is out of range.
* @see #getLength
*/
return null;
}
} // getType(int):String
/**
* Look up an attribute's type by XML 1.0 qualified name.
*
* <p>See {@link #getType(int) getType(int)} for a description
* of the possible types.</p>
*
* @param qname The XML 1.0 qualified name.
* @return The attribute type as a string, or null if the
* attribute is not in the list or if qualified names
* are not available.
*/
} // getType(String):String
/**
* Look up an attribute's value by index.
*
* <p>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.</p>
*
* @param index The attribute index (zero-based).
* @return The attribute's value as a string, or null if the
* index is out of range.
* @see #getLength
*/
return null;
}
} // getValue(int):String
/**
* Look up an attribute's value by XML 1.0 qualified name.
*
* <p>See {@link #getValue(int) getValue(int)} for a description
* of the possible values.</p>
*
* @param qname The XML 1.0 qualified name.
* @return The attribute value as a string, or null if the
* attribute is not in the list or if qualified names
* are not available.
*/
if(index == -1 )
return null;
} // getValue(String):String
//
// AttributeList methods
//
/**
* Return the name of an attribute in this list (by position).
*
* <p>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.</p>
*
* <p>If the attribute name has a namespace prefix, the prefix
* will still be attached.</p>
*
* @param i The index of the attribute in the list (starting at 0).
* @return The name of the indexed attribute, or null
* if the index is out of range.
* @see #getLength
*/
return null;
}
} // getName(int):String
//
// Attributes methods
//
/**
* Look up the index of an attribute by XML 1.0 qualified name.
*
* @param qName The qualified (prefixed) name.
* @return The index of the attribute, or -1 if it does not
* appear in the list.
*/
for (int i = 0; i < fLength; i++) {
return i;
}
}
return -1;
} // getIndex(String):int
/**
* Look up the index of an attribute by Namespace name.
*
* @param uri The Namespace URI, or null if
* the name has no Namespace URI.
* @param localName The attribute's local name.
* @return The index of the attribute, or -1 if it does not
* appear in the list.
*/
for (int i = 0; i < fLength; i++) {
return i;
}
}
return -1;
} // getIndex(String,String):int
/**
* Look up the index of an attribute by local name only,
* ignoring its namespace.
*
* @param localName The attribute's local name.
* @return The index of the attribute, or -1 if it does not
* appear in the list.
*/
for (int i = 0; i < fLength; i++) {
return i;
}
}
return -1;
} // getIndex(String):int
/**
* Look up an attribute's local name by index.
*
* @param index The attribute index (zero-based).
* @return The local name, or the empty string if Namespace
* processing is not being performed, or null
* if the index is out of range.
* @see #getLength
*/
if (!fNamespaces) {
return "";
}
return null;
}
} // getLocalName(int):String
/**
* Look up an attribute's XML 1.0 qualified name by index.
*
* @param index The attribute index (zero-based).
* @return The XML 1.0 qualified name, or the empty string
* if none is available, or null if the index
* is out of range.
* @see #getLength
*/
return null;
}
} // getQName(int):String
return null;
}
}
/**
* Look up an attribute's type by Namespace name.
*
* <p>See {@link #getType(int) getType(int)} for a description
* of the possible types.</p>
*
* @param uri The Namespace URI, or null if the
* name has no Namespace URI.
* @param localName The local name of the attribute.
* @return The attribute type as a string, or null if the
* attribute is not in the list or if Namespace
* processing is not being performed.
*/
if (!fNamespaces) {
return null;
}
} // getType(String,String):String
/**
* Look up the index of an attribute by XML 1.0 qualified name.
* <p>
* <strong>Note:</strong>
* 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.
*
* @param qName The qualified (prefixed) name.
* @return The index of the attribute, or -1 if it does not
* appear in the list.
*/
for (int i = 0; i < fLength; ++i) {
return i;
}
}
return -1;
} // getIndexFast(String):int
/**
* 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 <code>setNonNormalizedValue</code> method. Also,
* the added attribute will be marked as specified in the XML instance
* document unless set otherwise using the <code>setSpecified</code>
* method.
* <p>
* This method differs from <code>addAttribute</code> 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.
* <p>
* <strong>Caution:</strong> If this method is called it should
* not be mixed with calls to <code>addAttribute</code> unless
* it has been determined that all the attribute names are unique.
*
* @param name the attribute name
* @param type the attribute type
* @param value the attribute value
*
* @see #setNonNormalizedValue
* @see #setSpecified
* @see #checkDuplicatesNS
*/
if (fLength < SIZE_LIMIT) {
}
else {
}
attributes[i] = new Attribute();
}
}
// set values
// clear augmentations
}
/**
* Checks for duplicate expanded names (local part and namespace name
* pairs) in the attribute specification. If a duplicate is found its
* name is returned.
* <p>
* 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.
*
* @return the name of a duplicate attribute found in the search,
* otherwise null.
*/
// If the list is small check for duplicates using pairwise comparison.
if (fLength <= SIZE_LIMIT) {
for (int j = i + 1; j < fLength; ++j) {
}
}
}
}
// If the list is large check duplicates using a hash table.
else {
// We don't want this table view to be read if someone calls
// addAttribute so we invalidate it up front.
fIsTableViewConsistent = false;
int bucket;
attr = fAttributes[i];
// The chain is stale.
// This must be a unique attribute.
}
// This chain is active.
// We need to check if any of the attributes has the same name.
else {
// Search the table.
}
}
// Update table view
}
}
}
return null;
}
/**
* Look up the index of an attribute by Namespace name.
* <p>
* <strong>Note:</strong>
* 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.
*
* @param uri The Namespace URI, or null if
* the name has no Namespace URI.
* @param localName The attribute's local name.
* @return The index of the attribute, or -1 if it does not
* appear in the list.
*/
for (int i = 0; i < fLength; ++i) {
return i;
}
}
return -1;
} // getIndexFast(String,String):int
/**
* Returns the value passed in or NMTOKEN if it's an enumerated type.
*
* @param type attribute type
* @return the value passed in or NMTOKEN if it's an enumerated type.
*/
return "NMTOKEN";
}
return type;
}
/**
* Returns the position in the table view
* where the given attribute name would be hashed.
*
* @param qname the attribute name
* @return the position in the table view where the given attribute
* would be hashed
*/
}
/**
* Returns the position in the table view
* where the given attribute name would be hashed.
*
* @param localpart the local part of the attribute
* @param uri the namespace name of the attribute
* @return the position in the table view where the given attribute
* would be hashed
*/
}
else {
& 0x7FFFFFFF) % fTableViewBuckets;
}
}
/**
* Purges all elements from the table view.
*/
protected void cleanTableView() {
if (++fLargeCount < 0) {
// Overflow. We actually need to visit the chain state array.
if (fAttributeTableViewChainState != null) {
fAttributeTableViewChainState[i] = 0;
}
}
fLargeCount = 1;
}
}
/**
* Prepares the table view of the attributes list for use.
*/
protected void prepareTableView() {
if (fAttributeTableView == null) {
fAttributeTableViewChainState = new int[fTableViewBuckets];
}
else {
}
}
/**
* Prepares the table view of the attributes list for use,
* and populates it with the attributes which have been
* previously read.
*/
protected void prepareAndPopulateTableView() {
// Need to populate the hash table with the attributes we've scanned so far.
int bucket;
for (int i = 0; i < fLength; ++i) {
attr = fAttributes[i];
}
else {
// Update table view
}
}
}
/**
* Returns the prefix of the attribute at the specified index.
*
* @param index The index of the attribute.
*/
return null;
}
// REVISIT: The empty string is not entered in the symbol table!
} // getPrefix(int):String
/**
* Look up an attribute's Namespace URI by index.
*
* @param index The attribute index (zero-based).
* @return The Namespace URI
* @see #getLength
*/
return null;
}
return uri;
} // getURI(int):String
/**
* Look up an attribute's value by Namespace name and
* Local name. If Namespace is null, ignore namespace
* comparison. If Namespace is "", treat the name as
* having no Namespace URI.
*
* <p>See {@link #getValue(int) getValue(int)} for a description
* of the possible values.</p>
*
* @param uri The Namespace URI, or null namespaces are ignored.
* @param localName The local name of the attribute.
* @return The attribute value as a string, or null if the
* attribute is not in the list.
*/
} // getValue(String,String):String
/**
* Look up an augmentations by Namespace name.
*
* @param uri The Namespace URI, or null if the
* @param localName The local name of the attribute.
* @return Augmentations
*/
}
/**
* Look up an augmentation by XML 1.0 qualified name.
* <p>
*
* @param qName The XML 1.0 qualified name.
*
* @return Augmentations
*
*/
}
/**
* Look up an augmentations by attributes index.
*
* @param attributeIndex The attribute index.
* @return Augmentations
*/
return null;
}
}
/**
* Sets the augmentations of the attribute at the specified index.
*
* @param attrIndex The attribute index.
* @param augs The augmentations.
*/
}
/**
* Sets the uri of the attribute at the specified index.
*
* @param attrIndex The attribute index.
* @param uri Namespace uri
*/
} // getURI(int,QName)
// Implementation methods
}
return false;
}
}
} // getType(String):String
if (!fNamespaces) {
return false;
}
} // getType(String,String):String
//XMLBufferListener methods
/**
* This method will be invoked by XMLEntityReader before ScannedEntities buffer
* is reloaded.
*/
public void refresh() {
if(fLength > 0){
for(int i = 0 ; i < fLength ; i++){
getValue(i);
}
}
}
}
//
// Classes
//
/**
* Attribute information.
*
* @author Andy Clark, IBM
*/
static class Attribute {
//
// Data
//
// basic info
/** Name. */
/** Type. */
/** Value. */
/** This will point to the ScannedEntities buffer.*/
/** Non-normalized value. */
/** Specified. */
public boolean specified;
/** Schema ID type. */
public boolean schemaId;
/**
* Augmentations information for this attribute.
* XMLAttributes has no knowledge if any augmentations
* were attached to Augmentations.
*/
// Additional data for attribute table view
/** Pointer to the next attribute in the chain. **/
} // class Attribute
} // class XMLAttributesImpl