AttributesImplSerializerpublic final class AttributesImplSerializer extends AttributesImpl This class extends org.xml.sax.helpers.AttributesImpl which implements org.
xml.sax.Attributes. But for optimization this class adds a Hashtable for
faster lookup of an index by qName, which is commonly done in the stream
serializer. |
Fields Summary |
---|
private final Hashtable | m_indexFromQNameHash table of qName/index values to quickly lookup the index
of an attributes qName. qNames are in uppercase in the hash table
to make the search case insensitive.
The keys to the hashtable to find the index are either
"prefix:localName" or "{uri}localName". | private final StringBuffer | m_buff | private static final int | MAXThis is the number of attributes before switching to the hash table,
and can be tuned, but 12 seems good for now - Brian M. | private static final int | MAXMinus1One less than the number of attributes before switching to
the Hashtable. |
Methods Summary |
---|
public final void | addAttribute(java.lang.String uri, java.lang.String local, java.lang.String qname, java.lang.String type, java.lang.String val)This method adds the attribute, but also records its qName/index pair in
the hashtable for fast lookup by getIndex(qName).
int index = super.getLength();
super.addAttribute(uri, local, qname, type, val);
// (index + 1) is now the number of attributes
// so either compare (index+1) to MAX, or compare index to (MAX-1)
if (index < MAXMinus1)
{
return;
}
else if (index == MAXMinus1)
{
switchOverToHash(MAX);
}
else
{
/* add the key with the format of "prefix:localName" */
/* we have just added the attibute, its index is the old length */
Integer i = new Integer(index);
m_indexFromQName.put(qname, i);
/* now add with key of the format "{uri}localName" */
m_buff.setLength(0);
m_buff.append('{").append(uri).append('}").append(local);
String key = m_buff.toString();
m_indexFromQName.put(key, i);
}
return;
| public final void | clear()This method clears the accumulated attributes.
int len = super.getLength();
super.clear();
if (MAX <= len)
{
// if we have had enough attributes and are
// using the Hashtable, then clear the Hashtable too.
m_indexFromQName.clear();
}
| public final int | getIndex(java.lang.String qname)This method gets the index of an attribute given its qName.
int index;
if (super.getLength() < MAX)
{
// if we haven't got too many attributes let the
// super class look it up
index = super.getIndex(qname);
return index;
}
// we have too many attributes and the super class is slow
// so find it quickly using our Hashtable.
Integer i = (Integer)m_indexFromQName.get(qname);
if (i == null)
index = -1;
else
index = i.intValue();
return index;
| public final int | getIndex(java.lang.String uri, java.lang.String localName)This method gets the index of an attribute given its uri and locanName.
int index;
if (super.getLength() < MAX)
{
// if we haven't got too many attributes let the
// super class look it up
index = super.getIndex(uri,localName);
return index;
}
// we have too many attributes and the super class is slow
// so find it quickly using our Hashtable.
// Form the key of format "{uri}localName"
m_buff.setLength(0);
m_buff.append('{").append(uri).append('}").append(localName);
String key = m_buff.toString();
Integer i = (Integer)m_indexFromQName.get(key);
if (i == null)
index = -1;
else
index = i.intValue();
return index;
| public final void | setAttributes(org.xml.sax.Attributes atts)This method sets the attributes, previous attributes are cleared,
it also keeps the hashtable up to date for quick lookup via
getIndex(qName).
super.setAttributes(atts);
// we've let the super class add the attributes, but
// we need to keep the hash table up to date ourselves for the
// potentially new qName/index pairs for quick lookup.
int numAtts = atts.getLength();
if (MAX <= numAtts)
switchOverToHash(numAtts);
| private void | switchOverToHash(int numAtts)We are switching over to having a hash table for quick look
up of attributes, but up until now we haven't kept any
information in the Hashtable, so we now update the Hashtable.
Future additional attributes will update the Hashtable as
they are added.
for (int index = 0; index < numAtts; index++)
{
String qName = super.getQName(index);
Integer i = new Integer(index);
m_indexFromQName.put(qName, i);
// Add quick look-up to find with uri/local name pair
String uri = super.getURI(index);
String local = super.getLocalName(index);
m_buff.setLength(0);
m_buff.append('{").append(uri).append('}").append(local);
String key = m_buff.toString();
m_indexFromQName.put(key, i);
}
|
|