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 / PatternEntry.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  10.0 KB  |  294 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)PatternEntry.java    1.19 98/04/22
  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-1998 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.19 04/22/98
  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.         if (obj == null) return false;
  65.         PatternEntry other = (PatternEntry) obj;
  66.         boolean result = chars.equals(other.chars);
  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.     final int getStrength() {
  83.         return strength;
  84.     }
  85.  
  86.     /**
  87.      * Gets the expanding characters of the entry.
  88.      */
  89.     final String getExtension() {
  90.         return extension;
  91.     }
  92.  
  93.     /**
  94.      * Gets the core characters of the entry.
  95.      */
  96.     final String getChars() {
  97.         return chars;
  98.     }
  99.  
  100.     // ===== privates =====
  101.  
  102.     void addToBuffer(StringBuffer toAddTo,
  103.                      boolean showExtension,
  104.                      boolean showWhiteSpace,
  105.                      PatternEntry lastEntry)
  106.     {
  107.         if (showWhiteSpace && toAddTo.length() > 0)
  108.             if (strength == Collator.PRIMARY || lastEntry != null)
  109.                 toAddTo.append('\n');
  110.             else
  111.                 toAddTo.append(' ');
  112.         if (lastEntry != null) {
  113.             toAddTo.append('&');
  114.             if (showWhiteSpace)
  115.                 toAddTo.append(' ');
  116.             lastEntry.appendQuotedChars(toAddTo);
  117.             appendQuotedExtension(toAddTo);
  118.             if (showWhiteSpace)
  119.                 toAddTo.append(' ');
  120.         }
  121.         switch (strength) {
  122.         case Collator.IDENTICAL: toAddTo.append('='); break;
  123.         case Collator.TERTIARY:  toAddTo.append(','); break;
  124.         case Collator.SECONDARY: toAddTo.append(';'); break;
  125.         case Collator.PRIMARY:   toAddTo.append('<'); break;
  126.         case RESET: toAddTo.append('&'); break;
  127.         case UNSET: toAddTo.append('?'); break;
  128.         }
  129.         if (showWhiteSpace)
  130.             toAddTo.append(' ');
  131.         appendQuoted(chars,toAddTo);
  132.         if (showExtension && extension.length() != 0) {
  133.             toAddTo.append('/');
  134.             appendQuoted(extension,toAddTo);
  135.         }
  136.     }
  137.  
  138.     static void appendQuoted(String chars, StringBuffer toAddTo) {
  139.         boolean inQuote = false;
  140.         char ch = chars.charAt(0);
  141.         if (Character.isSpaceChar(ch)) {
  142.             inQuote = true;
  143.             toAddTo.append('\'');
  144.         } else {
  145.           if (PatternEntry.isSpecialChar(ch)) {
  146.                 inQuote = true;
  147.                 toAddTo.append('\'');
  148.             } else {
  149.                 switch (ch) {
  150.                     case 0x0010: case '\f': case '\r':
  151.                     case '\t': case '\n':  case '@':
  152.                     inQuote = true;
  153.                     toAddTo.append('\'');
  154.                     break;
  155.                 case '\'':
  156.                     inQuote = true;
  157.                     toAddTo.append('\'');
  158.                     break;
  159.                 default:
  160.                     if (inQuote) {
  161.                         inQuote = false; toAddTo.append('\'');
  162.                     }
  163.                     break;
  164.                 }
  165.            }
  166.         }
  167.         toAddTo.append(chars);
  168.         if (inQuote)
  169.             toAddTo.append('\'');
  170.     }
  171.  
  172.     //========================================================================
  173.     // Parsing a pattern into a list of PatternEntries....
  174.     //========================================================================
  175.  
  176.     PatternEntry(int strength,
  177.                  StringBuffer chars,
  178.                  StringBuffer extension)
  179.     {
  180.         this.strength = strength;
  181.         this.chars = chars.toString();
  182.         this.extension = (extension.length() > 0) ? extension.toString()
  183.                                                   : "";
  184.     }
  185.  
  186.     static class Parser {
  187.         private String pattern;
  188.         private int i;
  189.         
  190.         public Parser(String pattern) {
  191.             this.pattern = pattern;
  192.             this.i = 0;
  193.         }
  194.         
  195.         public PatternEntry next() throws ParseException {
  196.             int newStrength = UNSET;
  197.             
  198.             newChars.setLength(0);
  199.             newExtension.setLength(0);
  200.             
  201.             boolean inChars = true;
  202.             boolean inQuote = false;
  203.         mainLoop:
  204.             while (i < pattern.length()) {
  205.                 char ch = pattern.charAt(i);
  206.                 if (inQuote) {
  207.                     if (ch == '\'') {
  208.                         inQuote = false;
  209.                     } else {
  210.                         if (newChars.length() == 0) newChars.append(ch);
  211.                         else if (inChars) newChars.append(ch);
  212.                         else newExtension.append(ch);
  213.                     }
  214.                 } else switch (ch) {
  215.                 case '=': if (newStrength != UNSET) break mainLoop;
  216.                     newStrength = Collator.IDENTICAL; break;
  217.                 case ',': if (newStrength != UNSET) break mainLoop;
  218.                     newStrength = Collator.TERTIARY; break;
  219.                 case ';': if (newStrength != UNSET) break mainLoop;
  220.                     newStrength = Collator.SECONDARY; break;
  221.                 case '<': if (newStrength != UNSET) break mainLoop;
  222.                     newStrength = Collator.PRIMARY; break;
  223.                 case '&': if (newStrength != UNSET) break mainLoop;
  224.                     newStrength = RESET; break;
  225.                 case '\t':
  226.                 case '\n':
  227.                 case '\f':
  228.                 case '\r':
  229.                 case ' ': break; // skip whitespace TODO use Character
  230.                 case '/': inChars = false; break;
  231.                 case '\'':
  232.                     inQuote = true;
  233.                     ch = pattern.charAt(++i);
  234.                     if (newChars.length() == 0) newChars.append(ch);
  235.                     else if (inChars) newChars.append(ch);
  236.                     else newExtension.append(ch);
  237.                     break;
  238.                 default:
  239.                     if (newStrength == UNSET) {
  240.                         throw new ParseException
  241.                             ("missing char (=,;<&) : " +
  242.                              pattern.substring(i,
  243.                                 (i+10 < pattern.length()) ?
  244.                                  i+10 : pattern.length()),
  245.                              i);
  246.                     }
  247.                     if (PatternEntry.isSpecialChar(ch) && (inQuote == false))
  248.                         throw new ParseException
  249.                             ("Unquoted punctuation character : " + Integer.toString(ch, 16), i);
  250.                     if (inChars) {
  251.                         newChars.append(ch);
  252.                     } else {
  253.                         newExtension.append(ch);
  254.                     }
  255.                     break;
  256.                 }
  257.                 i++;
  258.             }
  259.             if (newStrength == UNSET)
  260.                 return null;
  261.             if (newChars.length() == 0) {
  262.                 throw new ParseException
  263.                     ("missing chars (=,;<&): " +
  264.                       pattern.substring(i,
  265.                           (i+10 < pattern.length()) ?
  266.                            i+10 : pattern.length()),
  267.                      i);
  268.             }
  269.             
  270.             return new PatternEntry(newStrength, newChars, newExtension);
  271.         }
  272.  
  273.         // We re-use these objects in order to improve performance
  274.         private StringBuffer newChars = new StringBuffer();
  275.         private StringBuffer newExtension = new StringBuffer();
  276.         
  277.     }
  278.         
  279.     static boolean isSpecialChar(char ch) {
  280.         return (((ch <= '\u002F') && (ch >= '\u0020')) ||
  281.                 ((ch <= '\u003F') && (ch >= '\u003A')) ||
  282.                 ((ch <= '\u0060') && (ch >= '\u005B')) ||
  283.                 ((ch <= '\u007E') && (ch >= '\u007B')));
  284.     }
  285.     
  286.  
  287.     static final int RESET = -2;
  288.     static final int UNSET = -1;
  289.  
  290.     int strength = UNSET;
  291.     String chars = "";
  292.     String extension = "";
  293. }
  294.