home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / MergeCollation.java < prev    next >
Text File  |  1998-09-22  |  11KB  |  318 lines

  1. /*
  2.  * @(#)MergeCollation.java    1.10 97/12/05
  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.util.Vector;
  33.  
  34. /**
  35.  * Utility class for normalizing and merging patterns for collation.
  36.  * Patterns are strings of the form <entry>*, where <entry> has the
  37.  * form:
  38.  * <pattern> := <entry>*
  39.  * <entry> := <separator><chars>{"/"<extension>}
  40.  * <separator> := "=", ",", ";", "<", "&"
  41.  * <chars>, and <extension> are both arbitrary strings.
  42.  * unquoted whitespaces are ignored.
  43.  * 'xxx' can be used to quote characters
  44.  * One difference from Collator is that & is used to reset to a current
  45.  * point. Or, in other words, it introduces a new sequence which is to
  46.  * be added to the old.
  47.  * That is: "a < b < c < d" is the same as "a < b & b < c & c < d" OR
  48.  * "a < b < d & b < c"
  49.  * XXX: make '' be a single quote.
  50.  * @see PatternEntry
  51.  * @version    1.10 12/05/97
  52.  * @author             Mark Davis, Helena Shih
  53.  */
  54.  
  55. final class MergeCollation {
  56.  
  57.     /**
  58.      * Creates from a pattern
  59.      * @exception ParseException If the input pattern is incorrect.
  60.      */
  61.     public MergeCollation(String pattern) throws ParseException
  62.     {
  63.         for (int i = 0; i < statusArray.length; i++)
  64.             statusArray[i] = 0;
  65.         setPattern(pattern);
  66.     }
  67.  
  68.     /**
  69.      * recovers current pattern
  70.      */
  71.     public String getPattern() {
  72.         return getPattern(true);
  73.     }
  74.  
  75.     /**
  76.      * recovers current pattern.
  77.      * @param withWhiteSpace puts spacing around the entries, and \n
  78.      * before & and <
  79.      */
  80.     public String getPattern(boolean withWhiteSpace) {
  81.         StringBuffer result = new StringBuffer();
  82.         PatternEntry tmp = null;
  83.         int i;
  84.         for (i = 0; i < patterns.size(); ++i) {
  85.             PatternEntry entry = (PatternEntry) patterns.elementAt(i);
  86.             if (entry.extension.length() != 0) {
  87.                 if (extList == null)
  88.                     extList = new Vector();
  89.                 extList.insertElementAt(entry, extList.size());
  90.             } else {
  91.                 if (extList != null) {
  92.                     PatternEntry last = findLastWithNoExtension(i-1);
  93.                     for (int j = extList.size() - 1; j >= 0 ; j--) {
  94.                         tmp = (PatternEntry)(extList.elementAt(j));
  95.                         tmp.addToBuffer(result, false, withWhiteSpace, last);
  96.                     }
  97.                     extList = null;
  98.                 }
  99.                 entry.addToBuffer(result, false, withWhiteSpace, null);
  100.             }
  101.         }
  102.         if (extList != null) {
  103.             PatternEntry last = findLastWithNoExtension(i-1);
  104.             for (int j = extList.size() - 1; j >= 0 ; j--) {
  105.                 tmp = (PatternEntry)(extList.elementAt(j));
  106.                 tmp.addToBuffer(result, false, withWhiteSpace, last);
  107.             }
  108.             extList = null;
  109.         }
  110.         return result.toString();
  111.     }
  112.  
  113.     /**
  114.      * emits the pattern for collation builder.
  115.      * @return emits the string in the format understable to the collation
  116.      * builder.
  117.      */
  118.     public String emitPattern() {
  119.         return emitPattern(true);
  120.     }
  121.  
  122.     /**
  123.      * emits the pattern for collation builder.
  124.      * @param withWhiteSpace puts spacing around the entries, and \n
  125.      * before & and <
  126.      * @return emits the string in the format understable to the collation
  127.      * builder.
  128.      */
  129.     public String emitPattern(boolean withWhiteSpace) {
  130.         StringBuffer result = new StringBuffer();
  131.         for (int i = 0; i < patterns.size(); ++i)
  132.         {
  133.             PatternEntry entry = (PatternEntry) patterns.elementAt(i);
  134.             if (entry != null) {
  135.                 entry.addToBuffer(result, true, withWhiteSpace, null);
  136.             }
  137.         }
  138.         return result.toString();
  139.     }
  140.  
  141.     /**
  142.      * sets the pattern.
  143.      */
  144.     public void setPattern(String pattern) throws ParseException
  145.     {
  146.         patterns.removeAllElements();
  147.         addPattern(pattern);
  148.     }
  149.  
  150.     /**
  151.      * adds a pattern to the current one.
  152.      * @param pattern the new pattern to be added
  153.      */
  154.     public void addPattern(String pattern) throws ParseException
  155.     {
  156.         if (pattern == null)
  157.             return;
  158.         int i = 0;
  159.         while (true) {
  160.             PatternEntry entry = new PatternEntry();
  161.             i = entry.getNextEntry(pattern, i);
  162.             if (i < 0)
  163.                 break;
  164.             fixEntry(entry);
  165.         }
  166.     }
  167.  
  168.     /**
  169.      * gets count of separate entries
  170.      * @return the size of pattern entries
  171.      */
  172.     public int getCount() {
  173.         return patterns.size();
  174.     }
  175.  
  176.     /**
  177.      * gets count of separate entries
  178.      * @param index the offset of the desired pattern entry
  179.      * @return the requested pattern entry
  180.      */
  181.     public PatternEntry getItemAt(int index) {
  182.         return (PatternEntry) patterns.elementAt(index);
  183.     }
  184.  
  185.     //============================================================
  186.     // privates
  187.     //============================================================
  188.     Vector patterns = new Vector(); // a vector of PatternEntries
  189.  
  190.     PatternEntry lastEntry = null;
  191.  
  192.     private final PatternEntry findLastWithNoExtension(int i) {
  193.         for (--i;i >= 0; --i) {
  194.             PatternEntry entry = (PatternEntry) patterns.elementAt(i);
  195.             if (entry.extension.length() == 0) {
  196.                 return entry;
  197.             }
  198.         }
  199.         return null;
  200.     }
  201.  
  202.     private byte[] statusArray = new byte[8192];
  203.     private PatternEntry saveEntry = null;
  204.     private int lastIndex = -1;
  205.     private Vector extList = null;
  206.     private final byte BITARRAYMASK = (byte)0x1;
  207.     private    final int  BYTEPOWER = 3;
  208.     private final int  BYTEMASK = (1 << BYTEPOWER) - 1;
  209.  
  210.     /*
  211.       If the strength is RESET, then just change the lastEntry to
  212.       be the current. (If the current is not in patterns, signal an error).
  213.       If not, then remove the current entry, and add it after lastEntry
  214.       (which is usually at the end).
  215.       */
  216.     private final void fixEntry(PatternEntry newEntry) throws ParseException
  217.     {
  218.         boolean changeLastEntry = true;
  219.         if (newEntry.strength != PatternEntry.RESET) {
  220.             int oldIndex = -1;
  221.             boolean setArray = false;
  222.             byte bitClump = 0;
  223.             byte setBit = 0;
  224.             if ((newEntry.chars.length() == 1)) {
  225.                 oldIndex = newEntry.chars.charAt(0) >> BYTEPOWER;
  226.                 bitClump = statusArray[oldIndex];
  227.                 setBit = (byte)(BITARRAYMASK << (newEntry.chars.charAt(0) & BYTEMASK));
  228.                 if (bitClump != 0 && (bitClump & setBit) != 0)
  229.                 {
  230.                     oldIndex = -1;
  231.                     int i = 0;
  232.                     for (i = patterns.size() - 1; i >= 0; --i) {
  233.                         PatternEntry entry = (PatternEntry)patterns.elementAt(i);
  234.                         if ((entry != null) &&
  235.                             (entry.chars.equals(newEntry.chars))) {
  236.                             oldIndex = i;
  237.                             break;
  238.                         }
  239.                     }
  240.                     if (oldIndex != -1) {
  241.                         patterns.removeElementAt(oldIndex);
  242.                     } else {
  243.                         System.out.println("FAILED SEARCH FOR value " + newEntry.chars);
  244.                     }
  245.                     lastIndex = patterns.indexOf(lastEntry);;
  246.                 } else {
  247.                     setArray = true;
  248.                 }
  249.             } else {
  250.                 oldIndex = patterns.indexOf(newEntry);
  251.                 if (oldIndex != -1)
  252.                     patterns.removeElementAt(oldIndex);
  253.             }
  254.             StringBuffer excess = new StringBuffer();
  255.             lastIndex = findLastEntry(lastEntry, excess);
  256.             if (setArray) statusArray[oldIndex] = (byte)(bitClump | setBit);
  257.             if (excess.length() != 0) {
  258.                 newEntry.extension = excess + newEntry.extension;
  259.                 if (lastIndex != patterns.size()) {
  260.                     lastEntry = saveEntry;
  261.                     changeLastEntry = false;
  262.                 }
  263.             }
  264.              if (lastIndex == patterns.size()) {
  265.                 patterns.addElement(newEntry);
  266.                 saveEntry = newEntry;
  267.             } else {
  268.                 patterns.insertElementAt(newEntry, lastIndex);
  269.                 lastEntry = saveEntry;
  270.             }
  271.         }
  272.         if (changeLastEntry)
  273.             lastEntry = newEntry;
  274.         else
  275.             lastIndex = patterns.indexOf(lastEntry);
  276.     }
  277.  
  278.     private final int findLastEntry(PatternEntry lastEntry,
  279.                               StringBuffer excess) throws ParseException
  280.     {
  281.         if (lastEntry == null)
  282.             return 0;
  283.         if (lastEntry.strength != PatternEntry.RESET) {
  284.             // Search backwards for string that contains this one;
  285.             // most likely entry is last one
  286.             int oldIndex = -1;
  287.             if ((lastEntry.chars.length() == 1)) {
  288.                 int index = lastEntry.chars.charAt(0) >> BYTEPOWER;
  289.                 if ((statusArray[index] &
  290.                     (BITARRAYMASK << (lastEntry.chars.charAt(0) & BYTEMASK))) != 0) {
  291.                     oldIndex = lastIndex;
  292.                 }
  293.             } else {
  294.                 oldIndex = patterns.indexOf(lastEntry);
  295.             }
  296.             if ((oldIndex == -1))
  297.                 throw new ParseException("couldn't find last entry: "
  298.                                           + lastEntry, oldIndex);
  299.             return oldIndex + 1;
  300.         } else {
  301.             int i;
  302.             for (i = patterns.size() - 1; i >= 0; --i) {
  303.                 PatternEntry entry = (PatternEntry) patterns.elementAt(i);
  304.                 if (entry.chars.regionMatches(0,lastEntry.chars,0,
  305.                                               entry.chars.length())) {
  306.                     excess.append(lastEntry.chars.substring(entry.chars.length(),
  307.                                                             lastEntry.chars.length()));
  308.                     break;
  309.                 }
  310.             }
  311.             if (i == -1)
  312.                 throw new ParseException("couldn't find: " + lastEntry, i);
  313.             return i + 1;
  314.         }
  315.     }
  316. }
  317.  
  318.