jaxp/src/com/sun/org/apache/xml/internal/serializer/utils/StringToIntTable.java
author joehw
Tue, 29 Jul 2014 20:52:36 -0700
changeset 25834 aba3efbf4ec5
parent 12457 c348e06f0e82
permissions -rw-r--r--
8035467: Xerces Update: Move to Xalan based DOM L3 serializer. Deprecate Xerces' native serializer. Reviewed-by: lancea
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 1999-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: StringToIntTable.java,v 1.1.4.1 2005/09/08 11:03:19 suresh_emailid Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
package com.sun.org.apache.xml.internal.serializer.utils;
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
 * A very simple lookup table that stores a list of strings, the even
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
 * number strings being keys, and the odd number strings being values.
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
 * This class is a copy of the one in com.sun.org.apache.xml.internal.utils.
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
 * It exists to cut the serializers dependancy on that package.
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
 * This class is not a public API, it is only public so it can be used
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
 * in com.sun.org.apache.xml.internal.serializer.
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
 * @xsl.usage internal
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
public final class StringToIntTable
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
  public static final int INVALID_KEY = -10000;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
  /** Block size to allocate          */
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
  private int m_blocksize;
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
  /** Array of strings this table points to. Associated with ints
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
   * in m_values         */
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
  private String m_map[];
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
  /** Array of ints this table points. Associated with strings from
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
   * m_map.         */
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
  private int m_values[];
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
  /** Number of ints in the table          */
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
  private int m_firstFree = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
  /** Size of this table         */
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
  private int m_mapSize;
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
   * Default constructor.  Note that the default
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
   * block size is very small, for small lists.
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
  public StringToIntTable()
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
    m_blocksize = 8;
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
    m_mapSize = m_blocksize;
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
    m_map = new String[m_blocksize];
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
    m_values = new int[m_blocksize];
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
   * Construct a StringToIntTable, using the given block size.
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
   * @param blocksize Size of block to allocate
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
  public StringToIntTable(int blocksize)
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
    m_blocksize = blocksize;
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
    m_mapSize = blocksize;
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
    m_map = new String[blocksize];
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    m_values = new int[m_blocksize];
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
   * Get the length of the list.
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
   * @return the length of the list
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
  public final int getLength()
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    return m_firstFree;
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
   * Append a string onto the vector.
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
   * @param key String to append
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
   * @param value The int value of the string
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
  public final void put(String key, int value)
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
    if ((m_firstFree + 1) >= m_mapSize)
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
      m_mapSize += m_blocksize;
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
      String newMap[] = new String[m_mapSize];
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
      System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
      m_map = newMap;
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
      int newValues[] = new int[m_mapSize];
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
      System.arraycopy(m_values, 0, newValues, 0, m_firstFree + 1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
      m_values = newValues;
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
    m_map[m_firstFree] = key;
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
    m_values[m_firstFree] = value;
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
    m_firstFree++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
   * Tell if the table contains the given string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
   * @param key String to look for
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
   * @return The String's int value
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
  public final int get(String key)
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
    for (int i = 0; i < m_firstFree; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
      if (m_map[i].equals(key))
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
        return m_values[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
    return INVALID_KEY;
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
   * Tell if the table contains the given string. Ignore case.
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
   * @param key String to look for
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
   * @return The string's int value
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
  public final int getIgnoreCase(String key)
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
    if (null == key)
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
        return INVALID_KEY;
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
    for (int i = 0; i < m_firstFree; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
      if (m_map[i].equalsIgnoreCase(key))
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
        return m_values[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
    return INVALID_KEY;
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
   * Tell if the table contains the given string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
   * @param key String to look for
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
   * @return True if the string is in the table
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
  public final boolean contains(String key)
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
    for (int i = 0; i < m_firstFree; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
      if (m_map[i].equals(key))
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
        return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
    return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
   * Return array of keys in the table.
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
   * @return Array of strings
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
  public final String[] keys()
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
    String [] keysArr = new String[m_firstFree];
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
    for (int i = 0; i < m_firstFree; i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
      keysArr[i] = m_map[i];
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
    return keysArr;
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
}