001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: StringToIntTable.java 468654 2006-10-28 07:09:23Z minchau $
020     */
021    package org.apache.xml.serializer.utils;
022    
023    /**
024     * A very simple lookup table that stores a list of strings, the even
025     * number strings being keys, and the odd number strings being values.
026     * 
027     * This class is a copy of the one in org.apache.xml.utils. 
028     * It exists to cut the serializers dependancy on that package.
029     * 
030     * This class is not a public API, it is only public so it can be used
031     * in org.apache.xml.serializer.
032     *  
033     * @xsl.usage internal
034     */
035    public final class StringToIntTable
036    {
037    
038      public static final int INVALID_KEY = -10000;
039      
040      /** Block size to allocate          */
041      private int m_blocksize;
042    
043      /** Array of strings this table points to. Associated with ints
044       * in m_values         */
045      private String m_map[];
046    
047      /** Array of ints this table points. Associated with strings from
048       * m_map.         */
049      private int m_values[];
050    
051      /** Number of ints in the table          */
052      private int m_firstFree = 0;
053    
054      /** Size of this table         */
055      private int m_mapSize;
056    
057      /**
058       * Default constructor.  Note that the default
059       * block size is very small, for small lists.
060       */
061      public StringToIntTable()
062      {
063    
064        m_blocksize = 8;
065        m_mapSize = m_blocksize;
066        m_map = new String[m_blocksize];
067        m_values = new int[m_blocksize];
068      }
069    
070      /**
071       * Construct a StringToIntTable, using the given block size.
072       *
073       * @param blocksize Size of block to allocate
074       */
075      public StringToIntTable(int blocksize)
076      {
077    
078        m_blocksize = blocksize;
079        m_mapSize = blocksize;
080        m_map = new String[blocksize];
081        m_values = new int[m_blocksize];
082      }
083    
084      /**
085       * Get the length of the list.
086       *
087       * @return the length of the list 
088       */
089      public final int getLength()
090      {
091        return m_firstFree;
092      }
093    
094      /**
095       * Append a string onto the vector.
096       *
097       * @param key String to append
098       * @param value The int value of the string
099       */
100      public final void put(String key, int value)
101      {
102    
103        if ((m_firstFree + 1) >= m_mapSize)
104        {
105          m_mapSize += m_blocksize;
106    
107          String newMap[] = new String[m_mapSize];
108    
109          System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
110    
111          m_map = newMap;
112    
113          int newValues[] = new int[m_mapSize];
114    
115          System.arraycopy(m_values, 0, newValues, 0, m_firstFree + 1);
116    
117          m_values = newValues;
118        }
119    
120        m_map[m_firstFree] = key;
121        m_values[m_firstFree] = value;
122    
123        m_firstFree++;
124      }
125    
126      /**
127       * Tell if the table contains the given string.
128       *
129       * @param key String to look for
130       *
131       * @return The String's int value
132       * 
133       */
134      public final int get(String key)
135      {
136    
137        for (int i = 0; i < m_firstFree; i++)
138        {
139          if (m_map[i].equals(key))
140            return m_values[i];
141        }
142    
143        return INVALID_KEY;
144      }
145    
146      /**
147       * Tell if the table contains the given string. Ignore case.
148       *
149       * @param key String to look for
150       *
151       * @return The string's int value
152       */
153      public final int getIgnoreCase(String key)
154      {
155    
156        if (null == key)
157            return INVALID_KEY;
158    
159        for (int i = 0; i < m_firstFree; i++)
160        {
161          if (m_map[i].equalsIgnoreCase(key))
162            return m_values[i];
163        }
164    
165        return INVALID_KEY;
166      }
167    
168      /**
169       * Tell if the table contains the given string.
170       *
171       * @param key String to look for
172       *
173       * @return True if the string is in the table
174       */
175      public final boolean contains(String key)
176      {
177    
178        for (int i = 0; i < m_firstFree; i++)
179        {
180          if (m_map[i].equals(key))
181            return true;
182        }
183    
184        return false;
185      }
186      
187      /**
188       * Return array of keys in the table.
189       * 
190       * @return Array of strings
191       */
192      public final String[] keys()
193      {
194        String [] keysArr = new String[m_firstFree];
195    
196        for (int i = 0; i < m_firstFree; i++)
197        {
198          keysArr[i] = m_map[i];
199        }
200    
201        return keysArr;
202      }  
203    }