1. /*
  2. * @(#)MergeCollation.java 1.13 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /*
  8. * @(#)MergeCollation.java 1.13 01/11/29
  9. *
  10. * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
  11. * (C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved
  12. *
  13. * Portions copyright (c) 1996-1998 Sun Microsystems, Inc. All Rights Reserved.
  14. *
  15. * The original version of this source code and documentation is copyrighted
  16. * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
  17. * materials are provided under terms of a License Agreement between Taligent
  18. * and Sun. This technology is protected by multiple US and International
  19. * patents. This notice and attribution to Taligent may not be removed.
  20. * Taligent is a registered trademark of Taligent, Inc.
  21. *
  22. * Permission to use, copy, modify, and distribute this software
  23. * and its documentation for NON-COMMERCIAL purposes and without
  24. * fee is hereby granted provided that this copyright notice
  25. * appears in all copies. Please refer to the file "copyright.html"
  26. * for further important copyright and licensing information.
  27. *
  28. * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  29. * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  30. * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  31. * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  32. * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  33. * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  34. *
  35. */
  36. package java.text;
  37. import java.util.ArrayList;
  38. /**
  39. * Utility class for normalizing and merging patterns for collation.
  40. * Patterns are strings of the form <entry>*, where <entry> has the
  41. * form:
  42. * <pattern> := <entry>*
  43. * <entry> := <separator><chars>{"/"<extension>}
  44. * <separator> := "=", ",", ";", "<", "&"
  45. * <chars>, and <extension> are both arbitrary strings.
  46. * unquoted whitespaces are ignored.
  47. * 'xxx' can be used to quote characters
  48. * One difference from Collator is that & is used to reset to a current
  49. * point. Or, in other words, it introduces a new sequence which is to
  50. * be added to the old.
  51. * That is: "a < b < c < d" is the same as "a < b & b < c & c < d" OR
  52. * "a < b < d & b < c"
  53. * XXX: make '' be a single quote.
  54. * @see PatternEntry
  55. * @version 1.13 11/29/01
  56. * @author Mark Davis, Helena Shih
  57. */
  58. final class MergeCollation {
  59. /**
  60. * Creates from a pattern
  61. * @exception ParseException If the input pattern is incorrect.
  62. */
  63. public MergeCollation(String pattern) throws ParseException
  64. {
  65. for (int i = 0; i < statusArray.length; i++)
  66. statusArray[i] = 0;
  67. setPattern(pattern);
  68. }
  69. /**
  70. * recovers current pattern
  71. */
  72. public String getPattern() {
  73. return getPattern(true);
  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. ArrayList extList = null;
  84. int i;
  85. for (i = 0; i < patterns.size(); ++i) {
  86. PatternEntry entry = (PatternEntry) patterns.get(i);
  87. if (entry.extension.length() != 0) {
  88. if (extList == null)
  89. extList = new ArrayList();
  90. extList.add(entry);
  91. } else {
  92. if (extList != null) {
  93. PatternEntry last = findLastWithNoExtension(i-1);
  94. for (int j = extList.size() - 1; j >= 0 ; j--) {
  95. tmp = (PatternEntry)(extList.get(j));
  96. tmp.addToBuffer(result, false, withWhiteSpace, last);
  97. }
  98. extList = null;
  99. }
  100. entry.addToBuffer(result, false, withWhiteSpace, null);
  101. }
  102. }
  103. if (extList != null) {
  104. PatternEntry last = findLastWithNoExtension(i-1);
  105. for (int j = extList.size() - 1; j >= 0 ; j--) {
  106. tmp = (PatternEntry)(extList.get(j));
  107. tmp.addToBuffer(result, false, withWhiteSpace, last);
  108. }
  109. extList = null;
  110. }
  111. return result.toString();
  112. }
  113. private final PatternEntry findLastWithNoExtension(int i) {
  114. for (--i;i >= 0; --i) {
  115. PatternEntry entry = (PatternEntry) patterns.get(i);
  116. if (entry.extension.length() == 0) {
  117. return entry;
  118. }
  119. }
  120. return null;
  121. }
  122. /**
  123. * emits the pattern for collation builder.
  124. * @return emits the string in the format understable to the collation
  125. * builder.
  126. */
  127. public String emitPattern() {
  128. return emitPattern(true);
  129. }
  130. /**
  131. * emits the pattern for collation builder.
  132. * @param withWhiteSpace puts spacing around the entries, and \n
  133. * before & and <
  134. * @return emits the string in the format understable to the collation
  135. * builder.
  136. */
  137. public String emitPattern(boolean withWhiteSpace) {
  138. StringBuffer result = new StringBuffer();
  139. for (int i = 0; i < patterns.size(); ++i)
  140. {
  141. PatternEntry entry = (PatternEntry) patterns.get(i);
  142. if (entry != null) {
  143. entry.addToBuffer(result, true, withWhiteSpace, null);
  144. }
  145. }
  146. return result.toString();
  147. }
  148. /**
  149. * sets the pattern.
  150. */
  151. public void setPattern(String pattern) throws ParseException
  152. {
  153. patterns.clear();
  154. addPattern(pattern);
  155. }
  156. /**
  157. * adds a pattern to the current one.
  158. * @param pattern the new pattern to be added
  159. */
  160. public void addPattern(String pattern) throws ParseException
  161. {
  162. if (pattern == null)
  163. return;
  164. PatternEntry.Parser parser = new PatternEntry.Parser(pattern);
  165. PatternEntry entry = parser.next();
  166. while (entry != null) {
  167. fixEntry(entry);
  168. entry = parser.next();
  169. }
  170. }
  171. /**
  172. * gets count of separate entries
  173. * @return the size of pattern entries
  174. */
  175. public int getCount() {
  176. return patterns.size();
  177. }
  178. /**
  179. * gets count of separate entries
  180. * @param index the offset of the desired pattern entry
  181. * @return the requested pattern entry
  182. */
  183. public PatternEntry getItemAt(int index) {
  184. return (PatternEntry) patterns.get(index);
  185. }
  186. //============================================================
  187. // privates
  188. //============================================================
  189. ArrayList patterns = new ArrayList(); // a list of PatternEntries
  190. private transient PatternEntry saveEntry = null;
  191. private transient PatternEntry lastEntry = null;
  192. // This is really used as a local variable inside fixEntry, but we cache
  193. // it here to avoid newing it up every time the method is called.
  194. private transient StringBuffer excess = new StringBuffer();
  195. //
  196. // When building a MergeCollation, we need to do lots of searches to see
  197. // whether a given entry is already in the table. Since we're using an
  198. // array, this would make the algorithm O(N*N). To speed things up, we
  199. // use this bit array to remember whether the array contains any entries
  200. // starting with each Unicode character. If not, we can avoid the search.
  201. // Using BitSet would make this easier, but it's significantly slower.
  202. //
  203. private transient byte[] statusArray = new byte[8192];
  204. private final byte BITARRAYMASK = (byte)0x1;
  205. private final int BYTEPOWER = 3;
  206. private final int BYTEMASK = (1 << BYTEPOWER) - 1;
  207. /*
  208. If the strength is RESET, then just change the lastEntry to
  209. be the current. (If the current is not in patterns, signal an error).
  210. If not, then remove the current entry, and add it after lastEntry
  211. (which is usually at the end).
  212. */
  213. private final void fixEntry(PatternEntry newEntry) throws ParseException
  214. {
  215. boolean changeLastEntry = true;
  216. if (newEntry.strength != PatternEntry.RESET) {
  217. int oldIndex = -1;
  218. if ((newEntry.chars.length() == 1)) {
  219. char c = newEntry.chars.charAt(0);
  220. int statusIndex = c >> BYTEPOWER;
  221. byte bitClump = statusArray[statusIndex];
  222. byte setBit = (byte)(BITARRAYMASK << (c & BYTEMASK));
  223. if (bitClump != 0 && (bitClump & setBit) != 0) {
  224. oldIndex = patterns.lastIndexOf(newEntry);
  225. } else {
  226. // We're going to add an element that starts with this
  227. // character, so go ahead and set its bit.
  228. statusArray[statusIndex] = (byte)(bitClump | setBit);
  229. }
  230. } else {
  231. oldIndex = patterns.lastIndexOf(newEntry);
  232. }
  233. if (oldIndex != -1) {
  234. patterns.remove(oldIndex);
  235. }
  236. excess.setLength(0);
  237. int lastIndex = findLastEntry(lastEntry, excess);
  238. if (excess.length() != 0) {
  239. newEntry.extension = excess + newEntry.extension;
  240. if (lastIndex != patterns.size()) {
  241. lastEntry = saveEntry;
  242. changeLastEntry = false;
  243. }
  244. }
  245. if (lastIndex == patterns.size()) {
  246. patterns.add(newEntry);
  247. saveEntry = newEntry;
  248. } else {
  249. patterns.add(lastIndex, newEntry);
  250. }
  251. }
  252. if (changeLastEntry) {
  253. lastEntry = newEntry;
  254. }
  255. }
  256. private final int findLastEntry(PatternEntry entry,
  257. StringBuffer excessChars) throws ParseException
  258. {
  259. if (entry == null)
  260. return 0;
  261. if (entry.strength != PatternEntry.RESET) {
  262. // Search backwards for string that contains this one;
  263. // most likely entry is last one
  264. int oldIndex = -1;
  265. if ((entry.chars.length() == 1)) {
  266. int index = entry.chars.charAt(0) >> BYTEPOWER;
  267. if ((statusArray[index] &
  268. (BITARRAYMASK << (entry.chars.charAt(0) & BYTEMASK))) != 0) {
  269. oldIndex = patterns.lastIndexOf(entry);
  270. }
  271. } else {
  272. oldIndex = patterns.lastIndexOf(entry);
  273. }
  274. if ((oldIndex == -1))
  275. throw new ParseException("couldn't find last entry: "
  276. + entry, oldIndex);
  277. return oldIndex + 1;
  278. } else {
  279. int i;
  280. for (i = patterns.size() - 1; i >= 0; --i) {
  281. PatternEntry e = (PatternEntry) patterns.get(i);
  282. if (e.chars.regionMatches(0,entry.chars,0,
  283. e.chars.length())) {
  284. excessChars.append(entry.chars.substring(e.chars.length(),
  285. entry.chars.length()));
  286. break;
  287. }
  288. }
  289. if (i == -1)
  290. throw new ParseException("couldn't find: " + entry, i);
  291. return i + 1;
  292. }
  293. }
  294. }