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