1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 /*
19  * $Id: AttributesImplSerializer.java 468654 2006-10-28 07:09:23Z minchau $
20  */
21 
22 package org.apache.xml.serializer;
23 
24 import java.util.Hashtable;
25 
26 import org.xml.sax.Attributes;
27 import org.xml.sax.helpers.AttributesImpl;
28 
29 /**
30  * This class extends org.xml.sax.helpers.AttributesImpl which implements org.
31  * xml.sax.Attributes. But for optimization this class adds a Hashtable for
32  * faster lookup of an index by qName, which is commonly done in the stream
33  * serializer.
34  *
35  * @see org.xml.sax.Attributes
36  *
37  * @xsl.usage internal
38  */
39 public final class AttributesImplSerializer extends AttributesImpl
40 {
41     /**
42      * Hash table of qName/index values to quickly lookup the index
43      * of an attributes qName.  qNames are in uppercase in the hash table
44      * to make the search case insensitive.
45      *
46      * The keys to the hashtable to find the index are either
47      * "prefix:localName"  or "{uri}localName".
48      */
49     private final Hashtable m_indexFromQName = new Hashtable();
50 
51     private final StringBuffer m_buff = new StringBuffer();
52 
53     /**
54      * This is the number of attributes before switching to the hash table,
55      * and can be tuned, but 12 seems good for now - Brian M.
56      */
57     private static final int MAX = 12;
58 
59     /**
60      * One less than the number of attributes before switching to
61      * the Hashtable.
62      */
63     private static final int MAXMinus1 = MAX - 1;
64 
65     /**
66      * This method gets the index of an attribute given its qName.
67      * @param qname the qualified name of the attribute, e.g. "prefix1:locName1"
68      * @return the integer index of the attribute.
69      * @see org.xml.sax.Attributes#getIndex(String)
70      */
getIndex(String qname)71     public final int getIndex(String qname)
72     {
73         int index;
74 
75         if (super.getLength() < MAX)
76         {
77             // if we haven't got too many attributes let the
78             // super class look it up
79             index = super.getIndex(qname);
80             return index;
81         }
82         // we have too many attributes and the super class is slow
83         // so find it quickly using our Hashtable.
84         Integer i = (Integer)m_indexFromQName.get(qname);
85         if (i == null)
86             index = -1;
87         else
88             index = i.intValue();
89         return index;
90     }
91     /**
92      * This method adds the attribute, but also records its qName/index pair in
93      * the hashtable for fast lookup by getIndex(qName).
94      * @param uri the URI of the attribute
95      * @param local the local name of the attribute
96      * @param qname the qualified name of the attribute
97      * @param type the type of the attribute
98      * @param val the value of the attribute
99      *
100      * @see org.xml.sax.helpers.AttributesImpl#addAttribute(String, String, String, String, String)
101      * @see #getIndex(String)
102      */
addAttribute( String uri, String local, String qname, String type, String val)103     public final void addAttribute(
104         String uri,
105         String local,
106         String qname,
107         String type,
108         String val)
109     {
110         int index = super.getLength();
111         super.addAttribute(uri, local, qname, type, val);
112         // (index + 1) is now the number of attributes
113         // so either compare (index+1) to MAX, or compare index to (MAX-1)
114 
115         if (index < MAXMinus1)
116         {
117             return;
118         }
119         else if (index == MAXMinus1)
120         {
121             switchOverToHash(MAX);
122         }
123         else
124         {
125             /* add the key with the format of "prefix:localName" */
126             /* we have just added the attibute, its index is the old length */
127             Integer i = new Integer(index);
128             m_indexFromQName.put(qname, i);
129 
130             /* now add with key of the format "{uri}localName" */
131             m_buff.setLength(0);
132             m_buff.append('{').append(uri).append('}').append(local);
133             String key = m_buff.toString();
134             m_indexFromQName.put(key, i);
135         }
136         return;
137     }
138 
139     /**
140      * We are switching over to having a hash table for quick look
141      * up of attributes, but up until now we haven't kept any
142      * information in the Hashtable, so we now update the Hashtable.
143      * Future additional attributes will update the Hashtable as
144      * they are added.
145      * @param numAtts
146      */
switchOverToHash(int numAtts)147     private void switchOverToHash(int numAtts)
148     {
149         for (int index = 0; index < numAtts; index++)
150         {
151             String qName = super.getQName(index);
152             Integer i = new Integer(index);
153             m_indexFromQName.put(qName, i);
154 
155             // Add quick look-up to find with uri/local name pair
156             String uri = super.getURI(index);
157             String local = super.getLocalName(index);
158             m_buff.setLength(0);
159             m_buff.append('{').append(uri).append('}').append(local);
160             String key = m_buff.toString();
161             m_indexFromQName.put(key, i);
162         }
163     }
164 
165     /**
166      * This method clears the accumulated attributes.
167      *
168      * @see org.xml.sax.helpers.AttributesImpl#clear()
169      */
clear()170     public final void clear()
171     {
172 
173         int len = super.getLength();
174         super.clear();
175         if (MAX <= len)
176         {
177             // if we have had enough attributes and are
178             // using the Hashtable, then clear the Hashtable too.
179             m_indexFromQName.clear();
180         }
181 
182     }
183 
184     /**
185      * This method sets the attributes, previous attributes are cleared,
186      * it also keeps the hashtable up to date for quick lookup via
187      * getIndex(qName).
188      * @param atts the attributes to copy into these attributes.
189      * @see org.xml.sax.helpers.AttributesImpl#setAttributes(Attributes)
190      * @see #getIndex(String)
191      */
setAttributes(Attributes atts)192     public final void setAttributes(Attributes atts)
193     {
194 
195         super.setAttributes(atts);
196 
197         // we've let the super class add the attributes, but
198         // we need to keep the hash table up to date ourselves for the
199         // potentially new qName/index pairs for quick lookup.
200         int numAtts = atts.getLength();
201         if (MAX <= numAtts)
202             switchOverToHash(numAtts);
203 
204     }
205 
206     /**
207      * This method gets the index of an attribute given its uri and locanName.
208      * @param uri the URI of the attribute name.
209      * @param localName the local namer (after the ':' ) of the attribute name.
210      * @return the integer index of the attribute.
211      * @see org.xml.sax.Attributes#getIndex(String)
212      */
getIndex(String uri, String localName)213     public final int getIndex(String uri, String localName)
214     {
215         int index;
216 
217         if (super.getLength() < MAX)
218         {
219             // if we haven't got too many attributes let the
220             // super class look it up
221             index = super.getIndex(uri,localName);
222             return index;
223         }
224         // we have too many attributes and the super class is slow
225         // so find it quickly using our Hashtable.
226         // Form the key of format "{uri}localName"
227         m_buff.setLength(0);
228         m_buff.append('{').append(uri).append('}').append(localName);
229         String key = m_buff.toString();
230         Integer i = (Integer)m_indexFromQName.get(key);
231         if (i == null)
232             index = -1;
233         else
234             index = i.intValue();
235         return index;
236     }
237 }
238