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: StringToStringTableVector.java 468655 2006-10-28 07:12:06Z minchau $
020     */
021    package org.apache.xml.utils;
022    
023    /**
024     * A very simple table that stores a list of StringToStringTables, optimized
025     * for small lists.
026     * @xsl.usage internal
027     */
028    public class StringToStringTableVector
029    {
030    
031      /** Size of blocks to allocate         */
032      private int m_blocksize;
033    
034      /** Array of StringToStringTable objects          */
035      private StringToStringTable m_map[];
036    
037      /** Number of StringToStringTable objects in this array          */
038      private int m_firstFree = 0;
039    
040      /** Size of this array          */
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 StringToStringTableVector()
048      {
049    
050        m_blocksize = 8;
051        m_mapSize = m_blocksize;
052        m_map = new StringToStringTable[m_blocksize];
053      }
054    
055      /**
056       * Construct a StringToStringTableVector, using the given block size.
057       *
058       * @param blocksize Size of blocks to allocate 
059       */
060      public StringToStringTableVector(int blocksize)
061      {
062    
063        m_blocksize = blocksize;
064        m_mapSize = blocksize;
065        m_map = new StringToStringTable[blocksize];
066      }
067    
068      /**
069       * Get the length of the list.
070       *
071       * @return Number of StringToStringTable objects in the list
072       */
073      public final int getLength()
074      {
075        return m_firstFree;
076      }
077    
078      /**
079       * Get the length of the list.
080       *
081       * @return Number of StringToStringTable objects in the list
082       */
083      public final int size()
084      {
085        return m_firstFree;
086      }
087    
088      /**
089       * Append a StringToStringTable object onto the vector.
090       *
091       * @param value StringToStringTable object to add
092       */
093      public final void addElement(StringToStringTable value)
094      {
095    
096        if ((m_firstFree + 1) >= m_mapSize)
097        {
098          m_mapSize += m_blocksize;
099    
100          StringToStringTable newMap[] = new StringToStringTable[m_mapSize];
101    
102          System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
103    
104          m_map = newMap;
105        }
106    
107        m_map[m_firstFree] = value;
108    
109        m_firstFree++;
110      }
111    
112      /**
113       * Given a string, find the last added occurance value
114       * that matches the key.
115       *
116       * @param key String to look up
117       *
118       * @return the last added occurance value that matches the key
119       * or null if not found.
120       */
121      public final String get(String key)
122      {
123    
124        for (int i = m_firstFree - 1; i >= 0; --i)
125        {
126          String nsuri = m_map[i].get(key);
127    
128          if (nsuri != null)
129            return nsuri;
130        }
131    
132        return null;
133      }
134    
135      /**
136       * Given a string, find out if there is a value in this table
137       * that matches the key.
138       *
139       * @param key String to look for  
140       *
141       * @return True if the string was found in table, null if not
142       */
143      public final boolean containsKey(String key)
144      {
145    
146        for (int i = m_firstFree - 1; i >= 0; --i)
147        {
148          if (m_map[i].get(key) != null)
149            return true;
150        }
151    
152        return false;
153      }
154    
155      /**
156       * Remove the last element.
157       */
158      public final void removeLastElem()
159      {
160    
161        if (m_firstFree > 0)
162        {
163          m_map[m_firstFree] = null;
164    
165          m_firstFree--;
166        }
167      }
168    
169      /**
170       * Get the nth element.
171       *
172       * @param i Index of element to find
173       *
174       * @return The StringToStringTable object at the given index
175       */
176      public final StringToStringTable elementAt(int i)
177      {
178        return m_map[i];
179      }
180    
181      /**
182       * Tell if the table contains the given StringToStringTable.
183       *
184       * @param s The StringToStringTable to find
185       *
186       * @return True if the StringToStringTable is found
187       */
188      public final boolean contains(StringToStringTable s)
189      {
190    
191        for (int i = 0; i < m_firstFree; i++)
192        {
193          if (m_map[i].equals(s))
194            return true;
195        }
196    
197        return false;
198      }
199    }