1. /*
  2. * %W% %E%
  3. *
  4. * Copyright 1997-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-1998 - 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.Vector;
  24. /**
  25. * This class contains all the code to parse a RuleBasedCollator pattern
  26. * and build a RBCollationTables object from it. A particular instance
  27. * of tis class exists only during the actual build process-- once an
  28. * RBCollationTables object has been built, the RBTableBuilder object
  29. * goes away. This object carries all of the state which is only needed
  30. * during the build process, plus a "shadow" copy of all of the state
  31. * that will go into the tables object itself. This object communicates
  32. * with RBCollationTables through a separate class, RBCollationTables.BuildAPI,
  33. * this is an inner class of RBCollationTables and provides a separate
  34. * private API for communication with RBTableBuilder.
  35. * This class isn't just an inner class of RBCollationTables itself because
  36. * of its large size. For source-code readability, it seemed better for the
  37. * builder to have its own source file.
  38. */
  39. final class RBTableBuilder {
  40. public RBTableBuilder(RBCollationTables.BuildAPI tables) {
  41. this.tables = tables;
  42. }
  43. /**
  44. * Create a table-based collation object with the given rules.
  45. * This is the main function that actually builds the tables and
  46. * stores them back in the RBCollationTables object. It is called
  47. * ONLY by the RBCollationTables constructor.
  48. * @see java.util.RuleBasedCollator#RuleBasedCollator
  49. * @exception ParseException If the rules format is incorrect.
  50. */
  51. public void build(String pattern, int decmp) throws ParseException
  52. {
  53. boolean isSource = true;
  54. int i = 0;
  55. String expChars;
  56. String groupChars;
  57. if (pattern.length() == 0)
  58. throw new ParseException("Build rules empty.", 0);
  59. // This array maps Unicode characters to their collation ordering
  60. mapping = new CompactIntArray((int)RBCollationTables.UNMAPPED);
  61. // Normalize the build rules. Find occurances of all decomposed characters
  62. // and normalize the rules before feeding into the builder. By "normalize",
  63. // we mean that all precomposed Unicode characters must be converted into
  64. // a base character and one or more combining characters (such as accents).
  65. // When there are multiple combining characters attached to a base character,
  66. // the combining characters must be in their canonical order
  67. //
  68. pattern = Normalizer.decompose(pattern, decmp);
  69. // Build the merged collation entries
  70. // Since rules can be specified in any order in the string
  71. // (e.g. "c , C < d , D < e , E .... C < CH")
  72. // this splits all of the rules in the string out into separate
  73. // objects and then sorts them. In the above example, it merges the
  74. // "C < CH" rule in just before the "C < D" rule.
  75. //
  76. mPattern = new MergeCollation(pattern);
  77. int order = 0;
  78. // Now walk though each entry and add it to my own tables
  79. for (i = 0; i < mPattern.getCount(); ++i)
  80. {
  81. PatternEntry entry = mPattern.getItemAt(i);
  82. if (entry != null) {
  83. groupChars = entry.getChars();
  84. if ((groupChars.length() > 1) &&
  85. (groupChars.charAt(groupChars.length()-1) == '@')) {
  86. frenchSec = true;
  87. groupChars = groupChars.substring(0, groupChars.length()-1);
  88. }
  89. order = increment(entry.getStrength(), order);
  90. expChars = entry.getExtension();
  91. if (expChars.length() != 0) {
  92. addExpandOrder(groupChars, expChars, order);
  93. } else if (groupChars.length() > 1) {
  94. addContractOrder(groupChars, order);
  95. } else {
  96. char ch = groupChars.charAt(0);
  97. addOrder(ch, order);
  98. }
  99. }
  100. }
  101. addComposedChars();
  102. commit();
  103. mapping.compact();
  104. tables.fillInTables(frenchSec, mapping, contractTable, expandTable,
  105. contractFlags, maxSecOrder, maxTerOrder);
  106. }
  107. /** Add expanding entries for pre-composed unicode characters so that this
  108. * collator can be used reasonably well with decomposition turned off.
  109. */
  110. private void addComposedChars() throws ParseException {
  111. StringBuffer buf = new StringBuffer(1);
  112. // Iterate through all of the pre-composed characters in Unicode
  113. Normalizer.DecompIterator iter =
  114. Normalizer.getDecompositions(Collator.CANONICAL_DECOMPOSITION);
  115. while (iter.hasNext()) {
  116. char c = iter.next();
  117. if (getCharOrder(c) == RBCollationTables.UNMAPPED) {
  118. //
  119. // We don't already have an ordering for this pre-composed character.
  120. //
  121. // First, see if the decomposed string is already in our
  122. // tables as a single contracting-string ordering.
  123. // If so, just map the precomposed character to that order.
  124. //
  125. // TODO: What we should really be doing here is trying to find the
  126. // longest initial substring of the decomposition that is present
  127. // in the tables as a contracting character sequence, and find its
  128. // ordering. Then do this recursively with the remaining chars
  129. // so that we build a list of orderings, and add that list to
  130. // the expansion table.
  131. // That would be more correct but also significantly slower, so
  132. // I'm not totally sure it's worth doing.
  133. //
  134. String s = iter.decomposition();
  135. int contractOrder = getContractOrder(s);
  136. if (contractOrder != RBCollationTables.UNMAPPED) {
  137. addOrder(c, contractOrder);
  138. } else {
  139. //
  140. // We don't have a contracting ordering for the entire string
  141. // that results from the decomposition, but if we have orders
  142. // for each individual character, we can add an expanding
  143. // table entry for the pre-composed character
  144. //
  145. boolean allThere = true;
  146. for (int i = 0; i < s.length(); i++) {
  147. if (getCharOrder(s.charAt(i)) == RBCollationTables.UNMAPPED) {
  148. allThere = false;
  149. break;
  150. }
  151. }
  152. if (allThere) {
  153. buf.setLength(0);
  154. buf.append(c);
  155. addExpandOrder(buf.toString(), s, RBCollationTables.UNMAPPED);
  156. }
  157. }
  158. }
  159. }
  160. }
  161. /**
  162. * Look up for unmapped values in the expanded character table.
  163. *
  164. * When the expanding character tables are built by addExpandOrder,
  165. * it doesn't know what the final ordering of each character
  166. * in the expansion will be. Instead, it just puts the raw character
  167. * code into the table, adding CHARINDEX as a flag. Now that we've
  168. * finished building the mapping table, we can go back and look up
  169. * that character to see what its real collation order is and
  170. * stick that into the expansion table. That lets us avoid doing
  171. * a two-stage lookup later.
  172. */
  173. private final void commit()
  174. {
  175. if (expandTable != null) {
  176. for (int i = 0; i < expandTable.size(); i++) {
  177. int[] valueList = (int [])expandTable.elementAt(i);
  178. for (int j = 0; j < valueList.length; j++) {
  179. int order = valueList[j];
  180. if (order < RBCollationTables.EXPANDCHARINDEX && order > CHARINDEX) {
  181. // found a expanding character that isn't filled in yet
  182. char ch = (char)(order - CHARINDEX);
  183. // Get the real values for the non-filled entry
  184. int realValue = getCharOrder(ch);
  185. if (realValue == RBCollationTables.UNMAPPED) {
  186. // The real value is still unmapped, maybe it's ignorable
  187. valueList[j] = IGNORABLEMASK & ch;
  188. } else {
  189. // just fill in the value
  190. valueList[j] = realValue;
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. /**
  198. * Increment of the last order based on the comparison level.
  199. */
  200. private final int increment(int aStrength, int lastValue)
  201. {
  202. switch(aStrength)
  203. {
  204. case Collator.PRIMARY:
  205. // increment priamry order and mask off secondary and tertiary difference
  206. lastValue += PRIMARYORDERINCREMENT;
  207. lastValue &= RBCollationTables.PRIMARYORDERMASK;
  208. isOverIgnore = true;
  209. break;
  210. case Collator.SECONDARY:
  211. // increment secondary order and mask off tertiary difference
  212. lastValue += SECONDARYORDERINCREMENT;
  213. lastValue &= RBCollationTables.SECONDARYDIFFERENCEONLY;
  214. // record max # of ignorable chars with secondary difference
  215. if (!isOverIgnore)
  216. maxSecOrder++;
  217. break;
  218. case Collator.TERTIARY:
  219. // increment tertiary order
  220. lastValue += TERTIARYORDERINCREMENT;
  221. // record max # of ignorable chars with tertiary difference
  222. if (!isOverIgnore)
  223. maxTerOrder++;
  224. break;
  225. }
  226. return lastValue;
  227. }
  228. /**
  229. * Adds a character and its designated order into the collation table.
  230. */
  231. private final void addOrder(char ch,
  232. int anOrder)
  233. {
  234. // See if the char already has an order in the mapping table
  235. int order = mapping.elementAt(ch);
  236. if (order >= RBCollationTables.CONTRACTCHARINDEX) {
  237. // There's already an entry for this character that points to a contracting
  238. // character table. Instead of adding the character directly to the mapping
  239. // table, we must add it to the contract table instead.
  240. key.setLength(0);
  241. key.append(ch);
  242. addContractOrder(key.toString(), anOrder);
  243. } else {
  244. // add the entry to the mapping table,
  245. // the same later entry replaces the previous one
  246. mapping.setElementAt(ch, anOrder);
  247. }
  248. }
  249. private final void addContractOrder(String groupChars, int anOrder) {
  250. addContractOrder(groupChars, anOrder, true);
  251. }
  252. /**
  253. * Adds the contracting string into the collation table.
  254. */
  255. private final void addContractOrder(String groupChars, int anOrder,
  256. boolean fwd)
  257. {
  258. if (contractTable == null) {
  259. contractTable = new Vector(INITIALTABLESIZE);
  260. }
  261. // See if the initial character of the string already has a contract table.
  262. int entry = mapping.elementAt(groupChars.charAt(0));
  263. Vector entryTable = getContractValues(entry - RBCollationTables.CONTRACTCHARINDEX);
  264. if (entryTable == null) {
  265. // We need to create a new table of contract entries for this base char
  266. int tableIndex = RBCollationTables.CONTRACTCHARINDEX + contractTable.size();
  267. entryTable = new Vector(INITIALTABLESIZE);
  268. contractTable.addElement(entryTable);
  269. // Add the initial character's current ordering first. then
  270. // update its mapping to point to this contract table
  271. entryTable.addElement(new EntryPair(groupChars.substring(0,1), entry));
  272. mapping.setElementAt(groupChars.charAt(0), tableIndex);
  273. }
  274. // Now add (or replace) this string in the table
  275. int index = RBCollationTables.getEntry(entryTable, groupChars, fwd);
  276. if (index != RBCollationTables.UNMAPPED) {
  277. EntryPair pair = (EntryPair) entryTable.elementAt(index);
  278. pair.value = anOrder;
  279. } else {
  280. EntryPair pair = (EntryPair)entryTable.lastElement();
  281. // NOTE: This little bit of logic is here to speed CollationElementIterator
  282. // .nextContractChar(). This code ensures that the longest sequence in
  283. // this list is always the _last_ one in the list. This keeps
  284. // nextContractChar() from having to search the entire list for the longest
  285. // sequence.
  286. if (groupChars.length() > pair.entryName.length()) {
  287. entryTable.addElement(new EntryPair(groupChars, anOrder, fwd));
  288. } else {
  289. entryTable.insertElementAt(new EntryPair(groupChars, anOrder,
  290. fwd), entryTable.size() - 1);
  291. }
  292. }
  293. // If this was a forward mapping for a contracting string, also add a
  294. // reverse mapping for it, so that CollationElementIterator.previous
  295. // can work right
  296. if (fwd && groupChars.length() > 1) {
  297. addContractFlags(groupChars);
  298. addContractOrder(new StringBuffer(groupChars).reverse().toString(),
  299. anOrder, false);
  300. }
  301. }
  302. /**
  303. * If the given string has been specified as a contracting string
  304. * in this collation table, return its ordering.
  305. * Otherwise return UNMAPPED.
  306. */
  307. private int getContractOrder(String groupChars)
  308. {
  309. int result = RBCollationTables.UNMAPPED;
  310. if (contractTable != null) {
  311. Vector entryTable = getContractValues(groupChars.charAt(0));
  312. if (entryTable != null) {
  313. int index = RBCollationTables.getEntry(entryTable, groupChars, true);
  314. if (index != RBCollationTables.UNMAPPED) {
  315. EntryPair pair = (EntryPair) entryTable.elementAt(index);
  316. result = pair.value;
  317. }
  318. }
  319. }
  320. return result;
  321. }
  322. private final int getCharOrder(char ch) {
  323. int order = mapping.elementAt(ch);
  324. if (order >= RBCollationTables.CONTRACTCHARINDEX) {
  325. Vector groupList = getContractValues(order - RBCollationTables.CONTRACTCHARINDEX);
  326. EntryPair pair = (EntryPair)groupList.firstElement();
  327. order = pair.value;
  328. }
  329. return order;
  330. }
  331. /**
  332. * Get the entry of hash table of the contracting string in the collation
  333. * table.
  334. * @param ch the starting character of the contracting string
  335. */
  336. Vector getContractValues(char ch)
  337. {
  338. int index = mapping.elementAt(ch);
  339. return getContractValues(index - RBCollationTables.CONTRACTCHARINDEX);
  340. }
  341. Vector getContractValues(int index)
  342. {
  343. if (index >= 0)
  344. {
  345. return (Vector)contractTable.elementAt(index);
  346. }
  347. else // not found
  348. {
  349. return null;
  350. }
  351. }
  352. /**
  353. * Adds the expanding string into the collation table.
  354. */
  355. private final void addExpandOrder(String contractChars,
  356. String expandChars,
  357. int anOrder) throws ParseException
  358. {
  359. // Create an expansion table entry
  360. int tableIndex = addExpansion(anOrder, expandChars);
  361. // And add its index into the main mapping table
  362. if (contractChars.length() > 1) {
  363. addContractOrder(contractChars, tableIndex);
  364. } else {
  365. addOrder(contractChars.charAt(0), tableIndex);
  366. }
  367. }
  368. /**
  369. * Create a new entry in the expansion table that contains the orderings
  370. * for the given characers. If anOrder is valid, it is added to the
  371. * beginning of the expanded list of orders.
  372. */
  373. private int addExpansion(int anOrder, String expandChars) {
  374. if (expandTable == null) {
  375. expandTable = new Vector(INITIALTABLESIZE);
  376. }
  377. // If anOrder is valid, we want to add it at the beginning of the list
  378. int offset = (anOrder == RBCollationTables.UNMAPPED) ? 0 : 1;
  379. int[] valueList = new int[expandChars.length() + offset];
  380. if (offset == 1) {
  381. valueList[0] = anOrder;
  382. }
  383. for (int i = 0; i < expandChars.length(); i++) {
  384. char ch = expandChars.charAt(i);
  385. int mapValue = getCharOrder(ch);
  386. if (mapValue != RBCollationTables.UNMAPPED) {
  387. valueList[i+offset] = mapValue;
  388. } else {
  389. // can't find it in the table, will be filled in by commit().
  390. valueList[i+offset] = CHARINDEX + (int)ch;
  391. }
  392. }
  393. // Add the expanding char list into the expansion table.
  394. int tableIndex = RBCollationTables.EXPANDCHARINDEX + expandTable.size();
  395. expandTable.addElement(valueList);
  396. return tableIndex;
  397. }
  398. private void addContractFlags(String chars) {
  399. char c;
  400. int len = chars.length();
  401. for (int i = 0; i < len; i++) {
  402. c = chars.charAt(i);
  403. contractFlags.put(c, 1);
  404. }
  405. }
  406. // ==============================================================
  407. // constants
  408. // ==============================================================
  409. final static int CHARINDEX = 0x70000000; // need look up in .commit()
  410. private final static int IGNORABLEMASK = 0x0000ffff;
  411. private final static int PRIMARYORDERINCREMENT = 0x00010000;
  412. private final static int SECONDARYORDERINCREMENT = 0x00000100;
  413. private final static int TERTIARYORDERINCREMENT = 0x00000001;
  414. private final static int INITIALTABLESIZE = 20;
  415. private final static int MAXKEYSIZE = 5;
  416. // ==============================================================
  417. // instance variables
  418. // ==============================================================
  419. // variables used by the build process
  420. private RBCollationTables.BuildAPI tables = null;
  421. private MergeCollation mPattern = null;
  422. private boolean isOverIgnore = false;
  423. private StringBuffer key = new StringBuffer(MAXKEYSIZE);
  424. private IntHashtable contractFlags = new IntHashtable(100);
  425. // "shadow" copies of the instance variables in RBCollationTables
  426. // (the values in these variables are copied back into RBCollationTables
  427. // at the end of the build process)
  428. private boolean frenchSec = false;
  429. private CompactIntArray mapping = null;
  430. private Vector contractTable = null;
  431. private Vector expandTable = null;
  432. private short maxSecOrder = 0;
  433. private short maxTerOrder = 0;
  434. }