286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: StringToStringTable.java,v 1.2.4.1 2005/09/15 08:15:56 suresh_emailid Exp $
286N/A */
286N/Apackage com.sun.org.apache.xml.internal.utils;
286N/A
286N/A/**
286N/A * A very simple lookup table that stores a list of strings, the even
286N/A * number strings being keys, and the odd number strings being values.
286N/A * @xsl.usage internal
286N/A */
286N/Apublic class StringToStringTable
286N/A{
286N/A
286N/A /** Size of blocks to allocate */
286N/A private int m_blocksize;
286N/A
286N/A /** Array of strings this contains */
286N/A private String m_map[];
286N/A
286N/A /** Number of strings this contains */
286N/A private int m_firstFree = 0;
286N/A
286N/A /** Size of this table */
286N/A private int m_mapSize;
286N/A
286N/A /**
286N/A * Default constructor. Note that the default
286N/A * block size is very small, for small lists.
286N/A */
286N/A public StringToStringTable()
286N/A {
286N/A
286N/A m_blocksize = 16;
286N/A m_mapSize = m_blocksize;
286N/A m_map = new String[m_blocksize];
286N/A }
286N/A
286N/A /**
286N/A * Construct a StringToStringTable, using the given block size.
286N/A *
286N/A * @param blocksize Size of blocks to allocate
286N/A */
286N/A public StringToStringTable(int blocksize)
286N/A {
286N/A
286N/A m_blocksize = blocksize;
286N/A m_mapSize = blocksize;
286N/A m_map = new String[blocksize];
286N/A }
286N/A
286N/A /**
286N/A * Get the length of the list.
286N/A *
286N/A * @return Number of strings in the list
286N/A */
286N/A public final int getLength()
286N/A {
286N/A return m_firstFree;
286N/A }
286N/A
286N/A /**
286N/A * Append a string onto the vector.
286N/A * The strings go to the even locations in the array
286N/A * and the values in the odd.
286N/A *
286N/A * @param key String to add to the list
286N/A * @param value Value of the string
286N/A */
286N/A public final void put(String key, String value)
286N/A {
286N/A
286N/A if ((m_firstFree + 2) >= m_mapSize)
286N/A {
286N/A m_mapSize += m_blocksize;
286N/A
286N/A String newMap[] = new String[m_mapSize];
286N/A
286N/A System.arraycopy(m_map, 0, newMap, 0, m_firstFree + 1);
286N/A
286N/A m_map = newMap;
286N/A }
286N/A
286N/A m_map[m_firstFree] = key;
286N/A
286N/A m_firstFree++;
286N/A
286N/A m_map[m_firstFree] = value;
286N/A
286N/A m_firstFree++;
286N/A }
286N/A
286N/A /**
286N/A * Tell if the table contains the given string.
286N/A *
286N/A * @param key String to look up
286N/A *
286N/A * @return return the value of the string or null if not found.
286N/A */
286N/A public final String get(String key)
286N/A {
286N/A
286N/A for (int i = 0; i < m_firstFree; i += 2)
286N/A {
286N/A if (m_map[i].equals(key))
286N/A return m_map[i + 1];
286N/A }
286N/A
286N/A return null;
286N/A }
286N/A
286N/A /**
286N/A * Remove the given string and its value from this table.
286N/A *
286N/A * @param key String to remove from the table
286N/A */
286N/A public final void remove(String key)
286N/A {
286N/A
286N/A for (int i = 0; i < m_firstFree; i += 2)
286N/A {
286N/A if (m_map[i].equals(key))
286N/A {
286N/A if ((i + 2) < m_firstFree)
286N/A System.arraycopy(m_map, i + 2, m_map, i, m_firstFree - (i + 2));
286N/A
286N/A m_firstFree -= 2;
286N/A m_map[m_firstFree] = null;
286N/A m_map[m_firstFree + 1] = null;
286N/A
286N/A break;
286N/A }
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Tell if the table contains the given string. Ignore case
286N/A *
286N/A * @param key String to look up
286N/A *
286N/A * @return The value of the string or null if not found
286N/A */
286N/A public final String getIgnoreCase(String key)
286N/A {
286N/A
286N/A if (null == key)
286N/A return null;
286N/A
286N/A for (int i = 0; i < m_firstFree; i += 2)
286N/A {
286N/A if (m_map[i].equalsIgnoreCase(key))
286N/A return m_map[i + 1];
286N/A }
286N/A
286N/A return null;
286N/A }
286N/A
286N/A /**
286N/A * Tell if the table contains the given string in the value.
286N/A *
286N/A * @param val Value of the string to look up
286N/A *
286N/A * @return the string associated with the given value or null if not found
286N/A */
286N/A public final String getByValue(String val)
286N/A {
286N/A
286N/A for (int i = 1; i < m_firstFree; i += 2)
286N/A {
286N/A if (m_map[i].equals(val))
286N/A return m_map[i - 1];
286N/A }
286N/A
286N/A return null;
286N/A }
286N/A
286N/A /**
286N/A * Get the nth element.
286N/A *
286N/A * @param i index of the string to look up.
286N/A *
286N/A * @return The string at the given index.
286N/A */
286N/A public final String elementAt(int i)
286N/A {
286N/A return m_map[i];
286N/A }
286N/A
286N/A /**
286N/A * Tell if the table contains the given string.
286N/A *
286N/A * @param key String to look up
286N/A *
286N/A * @return True if the given string is in this table
286N/A */
286N/A public final boolean contains(String key)
286N/A {
286N/A
286N/A for (int i = 0; i < m_firstFree; i += 2)
286N/A {
286N/A if (m_map[i].equals(key))
286N/A return true;
286N/A }
286N/A
286N/A return false;
286N/A }
286N/A
286N/A /**
286N/A * Tell if the table contains the given string.
286N/A *
286N/A * @param val value to look up
286N/A *
286N/A * @return True if the given value is in the table.
286N/A */
286N/A public final boolean containsValue(String val)
286N/A {
286N/A
286N/A for (int i = 1; i < m_firstFree; i += 2)
286N/A {
286N/A if (m_map[i].equals(val))
286N/A return true;
286N/A }
286N/A
286N/A return false;
286N/A }
286N/A}