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: StringToStringTable.java 468655 2006-10-28 07:12:06Z minchau $
020     */
021    package org.apache.xml.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     * @xsl.usage internal
027     */
028    public class StringToStringTable
029    {
030    
031      /** Size of blocks to allocate          */
032      private int m_blocksize;
033    
034      /** Array of strings this contains          */
035      private String m_map[];
036    
037      /** Number of strings this contains           */
038      private int m_firstFree = 0;
039    
040      /** Size of this table           */
041      private int m_mapSize;
042    
043      /**
044       * Default constructor.  Note that the default
045       * block size is very small, for small lists.
046       */
047      public StringToStringTable()
048      {
049    
050        m_blocksize = 16;
051        m_mapSize = m_blocksize;
052        m_map = new String[m_blocksize];
053      }
054    
055      /**
056       * Construct a StringToStringTable, using the given block size.
057       *
058       * @param blocksize Size of blocks to allocate 
059       */
060      public StringToStringTable(int blocksize)
061      {
062    
063        m_blocksize = blocksize;
064        m_mapSize = blocksize;
065        m_map = new String[blocksize];
066      }
067    
068      /**
069       * Get the length of the list.
070       *
071       * @return Number of strings in the list
072       */
073      public final int getLength()
074      {
075        return m_firstFree;
076      }
077    
078      /**
079       * Append a string onto the vector.
080       * The strings go to the even locations in the array 
081       * and the values in the odd. 
082       *
083       * @param key String to add to the list 
084       * @param value Value of the string
085       */
086      public final void put(String key, String value)
087      {
088    
089        if ((m_firstFree + 2) >= m_mapSize)
090        {
091          m_mapSize += m_blocksize;
092    
093          String newMap[] = new String[m_mapSize];
094    
095          System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
096    
097          m_map = newMap;
098        }
099    
100        m_map[m_firstFree] = key;
101    
102        m_firstFree++;
103    
104        m_map[m_firstFree] = value;
105    
106        m_firstFree++;
107      }
108    
109      /**
110       * Tell if the table contains the given string.
111       *
112       * @param key String to look up
113       *
114       * @return return the value of the string or null if not found. 
115       */
116      public final String get(String key)
117      {
118    
119        for (int i = 0; i < m_firstFree; i += 2)
120        {
121          if (m_map[i].equals(key))
122            return m_map[i + 1];
123        }
124    
125        return null;
126      }
127    
128      /**
129       * Remove the given string and its value from this table.
130       *
131       * @param key String to remove from the table
132       */
133      public final void remove(String key)
134      {
135    
136        for (int i = 0; i < m_firstFree; i += 2)
137        {
138          if (m_map[i].equals(key))
139          {
140            if ((i + 2) < m_firstFree)
141              System.arraycopy(m_map, i + 2, m_map, i, m_firstFree - (i + 2));
142    
143            m_firstFree -= 2;
144            m_map[m_firstFree] = null;
145            m_map[m_firstFree + 1] = null;
146    
147            break;
148          }
149        }
150      }
151    
152      /**
153       * Tell if the table contains the given string. Ignore case
154       *
155       * @param key String to look up
156       *
157       * @return The value of the string or null if not found
158       */
159      public final String getIgnoreCase(String key)
160      {
161    
162        if (null == key)
163          return null;
164    
165        for (int i = 0; i < m_firstFree; i += 2)
166        {
167          if (m_map[i].equalsIgnoreCase(key))
168            return m_map[i + 1];
169        }
170    
171        return null;
172      }
173    
174      /**
175       * Tell if the table contains the given string in the value.
176       *
177       * @param val Value of the string to look up
178       *
179       * @return the string associated with the given value or null if not found
180       */
181      public final String getByValue(String val)
182      {
183    
184        for (int i = 1; i < m_firstFree; i += 2)
185        {
186          if (m_map[i].equals(val))
187            return m_map[i - 1];
188        }
189    
190        return null;
191      }
192    
193      /**
194       * Get the nth element.
195       *
196       * @param i index of the string to look up.
197       *
198       * @return The string at the given index.
199       */
200      public final String elementAt(int i)
201      {
202        return m_map[i];
203      }
204    
205      /**
206       * Tell if the table contains the given string.
207       *
208       * @param key String to look up
209       *
210       * @return True if the given string is in this table 
211       */
212      public final boolean contains(String key)
213      {
214    
215        for (int i = 0; i < m_firstFree; i += 2)
216        {
217          if (m_map[i].equals(key))
218            return true;
219        }
220    
221        return false;
222      }
223    
224      /**
225       * Tell if the table contains the given string.
226       *
227       * @param val value to look up
228       *
229       * @return True if the given value is in the table.
230       */
231      public final boolean containsValue(String val)
232      {
233    
234        for (int i = 1; i < m_firstFree; i += 2)
235        {
236          if (m_map[i].equals(val))
237            return true;
238        }
239    
240        return false;
241      }
242    }