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