home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / text / UnicodeClassMapping.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  3.8 KB  |  105 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)UnicodeClassMapping.java    1.12 98/07/24
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996-1998 Sun Microsystems, Inc.
  8.  * All Rights Reserved.
  9.  *
  10.  * The original version of this source code and documentation
  11.  * is copyrighted and owned by Taligent, Inc., a wholly-owned
  12.  * subsidiary of IBM. These materials are provided under terms
  13.  * of a License Agreement between Taligent and Sun. This technology
  14.  * is protected by multiple US and International patents.
  15.  *
  16.  * This notice and attribution to Taligent may not be removed.
  17.  * Taligent is a registered trademark of Taligent, Inc.
  18.  *
  19.  * Permission to use, copy, modify, and distribute this software
  20.  * and its documentation for NON-COMMERCIAL purposes and without
  21.  * fee is hereby granted provided that this copyright notice
  22.  * appears in all copies. Please refer to the file "copyright.html"
  23.  * for further important copyright and licensing information.
  24.  *
  25.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  26.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  27.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  28.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  29.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  30.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  31.  *
  32.  */
  33.  
  34. package java.text;
  35.  
  36. import java.lang.Character;
  37. /**
  38.  * This class maps categories to state change inputs for the
  39.  * WordBreakTable.  An entire category is mapped to the same
  40.  * value unless the character in question appears in the exception list.
  41.  */
  42. final class UnicodeClassMapping
  43. {
  44.     private int mappedValue[];
  45.     private SpecialMapping exceptionChars[];
  46.     private boolean hasException[];
  47.     private int asciiValues[];
  48.  
  49.     /**
  50.      * Create a mapping given a mapping from categories and a list
  51.      * of exceptions.  Both the mapping list and exceptionChars list must
  52.      * be sorted in ascending order.
  53.      */
  54.     public UnicodeClassMapping(int mappedValue[],
  55.                                SpecialMapping exceptionChars[],
  56.                                boolean hasException[],
  57.                                int asciiValues[])
  58.     {
  59.         this.mappedValue = mappedValue;
  60.         this.exceptionChars = exceptionChars;
  61.         this.hasException = hasException;
  62.         this.asciiValues = asciiValues;
  63.     }
  64.  
  65.     /**
  66.      * Map a character to a stage change input for WordBreakTable
  67.      * @param ch The character to map.
  68.      * @return The mapped value.
  69.      */
  70.     public int mappedChar(char ch)
  71.     {
  72.         if (ch <= 255)
  73.             return asciiValues[ch];
  74.  
  75.         // get an appropriate category based on the character's Unicode class
  76.         // if there's no entry in the exception table for that Unicode class,
  77.         // we're done; otherwise we have to look in the exception table for
  78.         // the character's category (\uffff is treated here as a sentinel
  79.         // value meaning "end of the string"-- we always look in the exception
  80.         // table for its category)
  81.         int    charType = Character.getType(ch);
  82.         if ((exceptionChars.length == 0) //|| (ch > '\u003f' && ch < '\u00a0')
  83.                 || (!hasException[charType] && ch != '\uffff')) {
  84.             return mappedValue[charType];
  85.         }
  86.  
  87.         //do binary search of exceptionChars table
  88.         int min = 0;
  89.         int max = exceptionChars.length - 1;
  90.         while (max > min) {
  91.             int pos = (max + min) >> 1;
  92.             if (ch > exceptionChars[pos].endChar)
  93.                 min = pos + 1;
  94.             else
  95.                 max = pos;
  96.         }
  97.         SpecialMapping sm = exceptionChars[min];
  98.         if (sm.startChar <= ch && ch <= sm.endChar)
  99.             return sm.newValue;
  100.         else
  101.             return mappedValue[charType];
  102.     }
  103. }
  104.  
  105.