home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / PatternEntry.java < prev    next >
Text File  |  1997-05-20  |  10KB  |  281 lines

  1. /*
  2.  * @(#)PatternEntry.java    1.14 97/03/10
  3.  *
  4.  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  5.  * (C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved
  6.  *
  7.  * Portions copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  8.  *
  9.  *   The original version of this source code and documentation is copyrighted
  10.  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  11.  * materials are provided under terms of a License Agreement between Taligent
  12.  * and Sun. This technology is protected by multiple US and International
  13.  * patents. This notice and attribution to Taligent may not be removed.
  14.  *   Taligent is a registered trademark of Taligent, Inc.
  15.  *
  16.  * Permission to use, copy, modify, and distribute this software
  17.  * and its documentation for NON-COMMERCIAL purposes and without
  18.  * fee is hereby granted provided that this copyright notice
  19.  * appears in all copies. Please refer to the file "copyright.html"
  20.  * for further important copyright and licensing information.
  21.  *
  22.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  23.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  24.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  25.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  26.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  27.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  28.  *
  29.  */
  30. package java.text;
  31.  
  32. import java.lang.Character;
  33.  
  34. /**
  35.  * Utility class for normalizing and merging patterns for collation.
  36.  * This is to be used with MergeCollation for adding patterns to an
  37.  * existing rule table.
  38.  * @see        MergeCollation
  39.  * @version    1.14 03/10/97
  40.  * @author     Mark Davis, Helena Shih
  41.  */
  42.  
  43. class PatternEntry {
  44.     /**
  45.      * Gets the current extension, quoted
  46.      */
  47.     public void appendQuotedExtension(StringBuffer toAddTo) {
  48.         appendQuoted(extension,toAddTo);
  49.     }
  50.  
  51.     /**
  52.      * Gets the current chars, quoted
  53.      */
  54.     public void appendQuotedChars(StringBuffer toAddTo) {
  55.         appendQuoted(chars,toAddTo);
  56.     }
  57.  
  58.     /**
  59.      * WARNING this is used for searching in a Vector.
  60.      * Because Vector.indexOf doesn't take a comparator,
  61.      * this method is ill-defined and ignores strength.
  62.      */
  63.     public boolean equals(Object obj) {
  64.         PatternEntry other = (PatternEntry) obj;
  65.         boolean result = (chars.equals(other.chars) &&
  66.                           extension.equals(other.extension));
  67.         return result;
  68.     }
  69.  
  70.     /**
  71.      * For debugging.
  72.      */
  73.     public String toString() {
  74.         StringBuffer result = new StringBuffer();
  75.         addToBuffer(result, true, false, null);
  76.         return result.toString();
  77.     }
  78.  
  79.     /**
  80.      * Gets the strength of the entry.
  81.      */
  82.     int getStrength()
  83.     {
  84.     return strength;
  85.     }
  86.  
  87.     /**
  88.      * Gets the expanding characters of the entry.
  89.      */
  90.     String getExtension()
  91.     {
  92.         return extension;
  93.     }
  94.  
  95.     /**
  96.      * Gets the core characters of the entry.
  97.      */
  98.     String getChars()
  99.     {
  100.         return chars;
  101.     }
  102.  
  103.     // ===== privates =====
  104.  
  105.     PatternEntry() {
  106.     }
  107.  
  108.     PatternEntry(int strength,
  109.                  StringBuffer chars,
  110.                  StringBuffer extension)
  111.     {
  112.         this.strength = strength;
  113.         this.chars = chars.toString();
  114.         this.extension = extension.toString();
  115.     }
  116.  
  117.     void addToBuffer(StringBuffer toAddTo,
  118.                      boolean showExtension,
  119.                      boolean showWhiteSpace,
  120.                      PatternEntry lastEntry)
  121.     {
  122.         if (showWhiteSpace && toAddTo.length() > 0)
  123.             if (strength == Collator.PRIMARY || lastEntry != null)
  124.                 toAddTo.append('\n');
  125.             else
  126.                 toAddTo.append(' ');
  127.         if (lastEntry != null) {
  128.             toAddTo.append('&');
  129.             if (showWhiteSpace)
  130.                 toAddTo.append(' ');
  131.             lastEntry.appendQuotedChars(toAddTo);
  132.             appendQuotedExtension(toAddTo);
  133.             if (showWhiteSpace)
  134.                 toAddTo.append(' ');
  135.         }
  136.         switch (strength) {
  137.         case Collator.IDENTICAL: toAddTo.append('='); break;
  138.         case Collator.TERTIARY:  toAddTo.append(','); break;
  139.         case Collator.SECONDARY: toAddTo.append(';'); break;
  140.         case Collator.PRIMARY:   toAddTo.append('<'); break;
  141.         case RESET: toAddTo.append('&'); break;
  142.         case UNSET: toAddTo.append('?'); break;
  143.         }
  144.         if (showWhiteSpace)
  145.             toAddTo.append(' ');
  146.         appendQuoted(chars,toAddTo);
  147.         if (showExtension && extension.length() != 0) {
  148.             toAddTo.append('/');
  149.             appendQuoted(extension,toAddTo);
  150.         }
  151.     }
  152.  
  153.     int getNextEntry(String pattern,
  154.                      int i) throws ParseException
  155.     {
  156.         int newStrength = UNSET;
  157.         StringBuffer newChars = new StringBuffer();
  158.         StringBuffer newExtension = new StringBuffer();
  159.         boolean inChars = true;
  160.         boolean inQuote = false;
  161.  mainLoop:
  162.         while (i < pattern.length()) {
  163.             char ch = pattern.charAt(i);
  164.             if (inQuote) {
  165.                 if (ch == '\'') {
  166.                     inQuote = false;
  167.                 } else {
  168.                     if (newChars.length() == 0) newChars.append(ch);
  169.                     else if (inChars) newChars.append(ch);
  170.                     else newExtension.append(ch);
  171.                 }
  172.             } else switch (ch) {
  173.             case '=': if (newStrength != UNSET) break mainLoop;
  174.                 newStrength = Collator.IDENTICAL; break;
  175.             case ',': if (newStrength != UNSET) break mainLoop;
  176.                 newStrength = Collator.TERTIARY; break;
  177.             case ';': if (newStrength != UNSET) break mainLoop;
  178.                 newStrength = Collator.SECONDARY; break;
  179.             case '<': if (newStrength != UNSET) break mainLoop;
  180.                 newStrength = Collator.PRIMARY; break;
  181.             case '&': if (newStrength != UNSET) break mainLoop;
  182.                 newStrength = RESET; break;
  183.             case '\t':
  184.             case '\n':
  185.             case '\f': 
  186.             case '\r':
  187.             case ' ': break; // skip whitespace TODO use Character
  188.             case '/': inChars = false; break;
  189.             case '\'':
  190.                 inQuote = true;
  191.                 ch = pattern.charAt(++i);
  192.                 if (newChars.length() == 0) newChars.append(ch);
  193.                 else if (inChars) newChars.append(ch);
  194.                 else newExtension.append(ch);
  195.                 break;
  196.             default:
  197.                 if (newStrength == UNSET) {
  198.                     throw new ParseException
  199.                         ("missing char (=,;<&) : " +
  200.                          pattern.substring(i,
  201.                             (i+10 < pattern.length()) ?
  202.                              i+10 : pattern.length()),
  203.                          i);
  204.                 }
  205.                 if (PatternEntry.isSpecialChar(ch) && (inQuote == false))
  206.                     throw new ParseException
  207.                         ("Unquoted punctuation character", i);
  208.                 if (inChars) {
  209.                     newChars.append(ch);
  210.                 } else {
  211.                     newExtension.append(ch);
  212.                 }
  213.                 break;
  214.             }
  215.             i++;
  216.         }
  217.         if (newStrength == UNSET)
  218.             return -1;
  219.         if (newChars.length() == 0) {
  220.             throw new ParseException
  221.                 ("missing chars (=,;<&): " +
  222.                   pattern.substring(i,
  223.                       (i+10 < pattern.length()) ?
  224.                        i+10 : pattern.length()),
  225.                  i);
  226.         }
  227.         strength = newStrength;
  228.         chars = newChars.toString();
  229.         extension = newExtension.toString();
  230.         return i;
  231.     }
  232.  
  233.     static boolean isSpecialChar(char ch) {
  234.         return (((ch <= '\u002F') && (ch >= '\u0020')) ||
  235.                 ((ch <= '\u003F') && (ch >= '\u003A')) ||
  236.                 ((ch <= '\u0060') && (ch >= '\u005B')) ||
  237.                 ((ch <= '\u007E') && (ch >= '\u007B')));
  238.     }
  239.  
  240.     static void appendQuoted(String chars, StringBuffer toAddTo) {
  241.         boolean inQuote = false;
  242.         char ch = chars.charAt(0);
  243.         if (Character.isSpaceChar(ch)) {
  244.             inQuote = true;
  245.             toAddTo.append('\'');
  246.         } else {
  247.           if (PatternEntry.isSpecialChar(ch)) {
  248.                 inQuote = true;
  249.                 toAddTo.append('\'');
  250.             } else {
  251.                 switch (ch) {
  252.                     case 0x0010: case '\f': case '\r': 
  253.                     case '\t': case '\n':  case '@':
  254.                     inQuote = true;
  255.                     toAddTo.append('\'');
  256.                     break;
  257.                 case '\'':
  258.                     inQuote = true;
  259.                     toAddTo.append('\'');
  260.                     break;
  261.                 default:
  262.                     if (inQuote) {
  263.                         inQuote = false; toAddTo.append('\'');
  264.                     }
  265.                     break;
  266.                 }
  267.            }
  268.         }
  269.         toAddTo.append(chars);
  270.         if (inQuote)
  271.             toAddTo.append('\'');
  272.     }
  273.  
  274.     static final int RESET = -2;
  275.     static final int UNSET = -1;
  276.  
  277.     int strength = UNSET;
  278.     String chars = "";
  279.     String extension = "";
  280. }
  281.