KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > serializer > AttributesImplSerializer


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: AttributesImplSerializer.java,v 1.4 2004/02/17 04:18:19 minchau Exp $
18  */

19
20 package com.sun.org.apache.xml.internal.serializer;
21
22 import java.util.Hashtable JavaDoc;
23
24 import org.xml.sax.Attributes JavaDoc;
25 import org.xml.sax.helpers.AttributesImpl JavaDoc;
26
27 /**
28  * This class extends org.xml.sax.helpers.AttributesImpl which implements org.
29  * xml.sax.Attributes. But for optimization this class adds a Hashtable for
30  * faster lookup of an index by qName, which is commonly done in the stream
31  * serializer.
32  *
33  * @see org.xml.sax.Attributes
34  */

35 public class AttributesImplSerializer extends AttributesImpl JavaDoc
36 {
37     /**
38      * Hash table of qName/index values to quickly lookup the index
39      * of an attributes qName. qNames are in uppercase in the hash table
40      * to make the search case insensitive.
41      */

42     private Hashtable JavaDoc m_indexFromQName = new Hashtable JavaDoc();
43     
44     /**
45      * This is the number of attributes before switching to the hash table,
46      * and can be tuned, but 12 seems good for now - bjm
47      */

48     public static final int MAX = 12;
49     
50     /**
51      * One less than the number of attributes before switching to
52      * the Hashtable.
53      */

54     private static final int MAXMinus1 = MAX - 1;
55
56     /**
57      * This method gets the index of an attribute given its qName.
58      * @param qname the qualified name of the attribute, e.g. "prefix1:locName1"
59      * @return the integer index of the attribute.
60      * @see org.xml.sax.Attributes#getIndex(String)
61      */

62     public int getIndex(String JavaDoc qname)
63     {
64         int index;
65
66         if (super.getLength() < MAX)
67         {
68             // if we haven't got too many attributes let the
69
// super class look it up
70
index = super.getIndex(qname);
71             return index;
72         }
73         // we have too many attributes and the super class is slow
74
// so find it quickly using our Hashtable.
75
Integer JavaDoc i = (Integer JavaDoc)m_indexFromQName.get(qname);
76         if (i == null)
77             index = -1;
78         else
79             index = i.intValue();
80         return index;
81     }
82     /**
83      * This method adds the attribute, but also records its qName/index pair in
84      * the hashtable for fast lookup by getIndex(qName).
85      * @param uri the URI of the attribute
86      * @param local the local name of the attribute
87      * @param qname the qualified name of the attribute
88      * @param type the type of the attribute
89      * @param val the value of the attribute
90      *
91      * @see org.xml.sax.helpers.AttributesImpl#addAttribute(String, String, String, String, String)
92      * @see #getIndex(String)
93      */

94     public void addAttribute(
95         String JavaDoc uri,
96         String JavaDoc local,
97         String JavaDoc qname,
98         String JavaDoc type,
99         String JavaDoc val)
100     {
101         int index = super.getLength();
102         super.addAttribute(uri, local, qname, type, val);
103         // (index + 1) is now the number of attributes
104
// so either compare (index+1) to MAX, or compare index to (MAX-1)
105

106         if (index < MAXMinus1)
107         {
108             return;
109         }
110         else if (index == MAXMinus1)
111         {
112             switchOverToHash(MAX);
113         }
114         else
115         {
116             /* we have just added the attibute, its index is the old length */
117             Integer JavaDoc i = new Integer JavaDoc(index);
118             m_indexFromQName.put(qname, i);
119         }
120         return;
121     }
122
123     /**
124      * We are switching over to having a hash table for quick look
125      * up of attributes, but up until now we haven't kept any
126      * information in the Hashtable, so we now update the Hashtable.
127      * Future additional attributes will update the Hashtable as
128      * they are added.
129      * @param numAtts
130      */

131     private void switchOverToHash(int numAtts)
132     {
133         for (int index = 0; index < numAtts; index++)
134         {
135             String JavaDoc qName = super.getQName(index);
136             Integer JavaDoc i = new Integer JavaDoc(index);
137             m_indexFromQName.put(qName, i);
138         }
139     }
140
141     /**
142      * This method clears the accumulated attributes.
143      *
144      * @see org.xml.sax.helpers.AttributesImpl#clear()
145      */

146     public void clear()
147     {
148
149         int len = super.getLength();
150         super.clear();
151         if (MAX <= len)
152         {
153             // if we have had enough attributes and are
154
// using the Hashtable, then clear the Hashtable too.
155
m_indexFromQName.clear();
156         }
157
158     }
159
160     /**
161      * This method sets the attributes, previous attributes are cleared,
162      * it also keeps the hashtable up to date for quick lookup via
163      * getIndex(qName).
164      * @param atts the attributes to copy into these attributes.
165      * @see org.xml.sax.helpers.AttributesImpl#setAttributes(Attributes)
166      * @see #getIndex(String)
167      */

168     public void setAttributes(Attributes JavaDoc atts)
169     {
170
171         super.setAttributes(atts);
172
173         // we've let the super class add the attributes, but
174
// we need to keep the hash table up to date ourselves for the
175
// potentially new qName/index pairs for quick lookup.
176
int numAtts = atts.getLength();
177         if (MAX <= numAtts)
178             switchOverToHash(numAtts);
179
180     }
181 }
182
Popular Tags