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