CollationElementIterator.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /* CollationElementIterator.java -- Walks through collation elements
  2. Copyright (C) 1998, 1999, 2001, 2002, 2003, 2004, 2012 Free Software Foundation
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.text;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.util.ArrayList;
  34. /* Written using "Java Class Libraries", 2nd edition, plus online
  35. * API docs for JDK 1.2 from http://www.javasoft.com.
  36. * Status: Believed complete and correct to JDK 1.1.
  37. */
  38. /**
  39. * This class walks through the character collation elements of a
  40. * <code>String</code> as defined by the collation rules in an instance of
  41. * <code>RuleBasedCollator</code>. There is no public constructor for
  42. * this class. An instance is created by calling the
  43. * <code>getCollationElementIterator</code> method on
  44. * <code>RuleBasedCollator</code>.
  45. *
  46. * @author Aaron M. Renn (arenn@urbanophile.com)
  47. * @author Tom Tromey (tromey@cygnus.com)
  48. * @author Guilhem Lavaux (guilhem.lavaux@free.fr)
  49. */
  50. public final class CollationElementIterator
  51. {
  52. /**
  53. * This is a constant value that is returned to indicate that the end of
  54. * the string was encountered.
  55. */
  56. public static final int NULLORDER = -1;
  57. /**
  58. * This is the RuleBasedCollator this object was created from.
  59. */
  60. RuleBasedCollator collator;
  61. /**
  62. * This is the String that is being iterated over.
  63. */
  64. CharacterIterator text;
  65. /**
  66. * This is the index into the collation decomposition where we are currently scanning.
  67. */
  68. int index;
  69. /**
  70. * This is the index into the String where we are currently scanning.
  71. */
  72. int textIndex;
  73. /**
  74. * Array containing the collation decomposition of the
  75. * text given to the constructor.
  76. */
  77. private RuleBasedCollator.CollationElement[] textDecomposition;
  78. /**
  79. * Array containing the index of the specified block.
  80. */
  81. private int[] textIndexes;
  82. /**
  83. * This method initializes a new instance of <code>CollationElementIterator</code>
  84. * to iterate over the specified <code>String</code> using the rules in the
  85. * specified <code>RuleBasedCollator</code>.
  86. *
  87. * @param collator The <code>RuleBasedCollation</code> used for calculating collation values
  88. * @param text The <code>String</code> to iterate over.
  89. */
  90. CollationElementIterator(RuleBasedCollator collator, String text)
  91. {
  92. this.collator = collator;
  93. setText (text);
  94. }
  95. /**
  96. * This method initializes a new instance of <code>CollationElementIterator</code>
  97. * to iterate over the specified <code>String</code> using the rules in the
  98. * specified <code>RuleBasedCollator</code>.
  99. *
  100. * @param collator The <code>RuleBasedCollation</code> used for calculating collation values
  101. * @param text The character iterator to iterate over.
  102. */
  103. CollationElementIterator(RuleBasedCollator collator, CharacterIterator text)
  104. {
  105. this.collator = collator;
  106. setText (text);
  107. }
  108. RuleBasedCollator.CollationElement nextBlock()
  109. {
  110. if (index >= textDecomposition.length)
  111. return null;
  112. RuleBasedCollator.CollationElement e = textDecomposition[index];
  113. textIndex = textIndexes[index+1];
  114. index++;
  115. return e;
  116. }
  117. RuleBasedCollator.CollationElement previousBlock()
  118. {
  119. if (index == 0)
  120. return null;
  121. index--;
  122. RuleBasedCollator.CollationElement e = textDecomposition[index];
  123. textIndex = textIndexes[index+1];
  124. return e;
  125. }
  126. /**
  127. * This method returns the collation ordering value of the next character sequence
  128. * in the string (it may be an extended character following collation rules).
  129. * This method will return <code>NULLORDER</code> if the
  130. * end of the string was reached.
  131. *
  132. * @return The collation ordering value.
  133. */
  134. public int next()
  135. {
  136. RuleBasedCollator.CollationElement e = nextBlock();
  137. if (e == null)
  138. return NULLORDER;
  139. return e.getValue();
  140. }
  141. /**
  142. * This method returns the collation ordering value of the previous character
  143. * in the string. This method will return <code>NULLORDER</code> if the
  144. * beginning of the string was reached.
  145. *
  146. * @return The collation ordering value.
  147. */
  148. public int previous()
  149. {
  150. RuleBasedCollator.CollationElement e = previousBlock();
  151. if (e == null)
  152. return NULLORDER;
  153. return e.getValue();
  154. }
  155. /**
  156. * This method returns the primary order value for the given collation
  157. * value.
  158. *
  159. * @param order The collation value returned from <code>next()</code> or
  160. * <code>previous()</code>.
  161. *
  162. * @return The primary order value of the specified collation value. This is
  163. * the high 16 bits.
  164. */
  165. public static int primaryOrder(int order)
  166. {
  167. // From the JDK 1.2 spec.
  168. return order >>> 16;
  169. }
  170. /**
  171. * This method resets the internal position pointer to read from the
  172. * beginning of the <code>String</code> again.
  173. */
  174. public void reset()
  175. {
  176. index = 0;
  177. textIndex = 0;
  178. }
  179. /**
  180. * This method returns the secondary order value for the given collation
  181. * value.
  182. *
  183. * @param order The collation value returned from <code>next()</code> or
  184. * <code>previous()</code>.
  185. *
  186. * @return The secondary order value of the specified collation value. This
  187. * is the bits 8-15.
  188. */
  189. public static short secondaryOrder(int order)
  190. {
  191. // From the JDK 1.2 spec.
  192. return (short) ((order >>> 8) & 255);
  193. }
  194. /**
  195. * This method returns the tertiary order value for the given collation
  196. * value.
  197. *
  198. * @param order The collation value returned from <code>next()</code> or
  199. * <code>previous()</code>.
  200. *
  201. * @return The tertiary order value of the specified collation value. This
  202. * is the low eight bits.
  203. */
  204. public static short tertiaryOrder(int order)
  205. {
  206. // From the JDK 1.2 spec.
  207. return (short) (order & 255);
  208. }
  209. /**
  210. * This method sets the <code>String</code> that it is iterating over
  211. * to the specified <code>String</code>.
  212. *
  213. * @param text The new <code>String</code> to iterate over.
  214. *
  215. * @since 1.2
  216. */
  217. public void setText(String text)
  218. {
  219. int idx = 0;
  220. int idx_idx = 0;
  221. int alreadyExpanded = 0;
  222. int idxToMove = 0;
  223. this.text = new StringCharacterIterator(text);
  224. this.index = 0;
  225. String work_text = text.intern();
  226. ArrayList<RuleBasedCollator.CollationElement> aElement = new ArrayList<RuleBasedCollator.CollationElement>();
  227. ArrayList<Integer> aIdx = new ArrayList<Integer>();
  228. // Build element collection ordered as they come in "text".
  229. while (idx < work_text.length())
  230. {
  231. String key, keyOld;
  232. Object object = null;
  233. int p = 1;
  234. // IMPROVE: use a TreeMap with a prefix-ordering rule.
  235. keyOld = key = null;
  236. do
  237. {
  238. if (object != null)
  239. keyOld = key;
  240. key = work_text.substring (idx, idx+p);
  241. object = collator.prefix_tree.get (key);
  242. if (object != null && idx < alreadyExpanded)
  243. {
  244. RuleBasedCollator.CollationElement prefix = (RuleBasedCollator.CollationElement)object;
  245. if (prefix.expansion != null &&
  246. prefix.expansion.startsWith(work_text.substring(0, idx)))
  247. {
  248. object = null;
  249. key = keyOld;
  250. }
  251. }
  252. p++;
  253. }
  254. while (idx+p <= work_text.length());
  255. if (object == null)
  256. key = keyOld;
  257. RuleBasedCollator.CollationElement prefix =
  258. (RuleBasedCollator.CollationElement) collator.prefix_tree.get (key);
  259. /*
  260. * First case: There is no such sequence in the database.
  261. * We will have to build one from the context.
  262. */
  263. if (prefix == null)
  264. {
  265. /*
  266. * We are dealing with sequences in an expansion. They
  267. * are treated as accented characters (tertiary order).
  268. */
  269. if (alreadyExpanded > 0)
  270. {
  271. RuleBasedCollator.CollationElement e =
  272. collator.getDefaultAccentedElement (work_text.charAt (idx));
  273. aElement.add (e);
  274. aIdx.add (Integer.valueOf(idx_idx));
  275. idx++;
  276. alreadyExpanded--;
  277. if (alreadyExpanded == 0)
  278. {
  279. /* There is not any characters left in the expansion set.
  280. * We can increase the pointer in the source string.
  281. */
  282. idx_idx += idxToMove;
  283. idxToMove = 0;
  284. }
  285. else
  286. idx_idx++;
  287. }
  288. else
  289. {
  290. /* This is a normal character. */
  291. RuleBasedCollator.CollationElement e =
  292. collator.getDefaultElement (work_text.charAt (idx));
  293. Integer iRef = Integer.valueOf(idx_idx);
  294. /* Don't forget to mark it as a special sequence so the
  295. * string can be ordered.
  296. */
  297. aElement.add (RuleBasedCollator.SPECIAL_UNKNOWN_SEQ);
  298. aIdx.add (iRef);
  299. aElement.add (e);
  300. aIdx.add (iRef);
  301. idx_idx++;
  302. idx++;
  303. }
  304. continue;
  305. }
  306. /*
  307. * Second case: Here we have found a matching sequence.
  308. * Here we have an expansion string prepend it to the "work text" and
  309. * add the corresponding sorting element. We must also mark
  310. */
  311. if (prefix.expansion != null)
  312. {
  313. work_text = prefix.expansion
  314. + work_text.substring (idx+prefix.key.length());
  315. idx = 0;
  316. aElement.add (prefix);
  317. aIdx.add (Integer.valueOf(idx_idx));
  318. if (alreadyExpanded == 0)
  319. idxToMove = prefix.key.length();
  320. alreadyExpanded += prefix.expansion.length()-prefix.key.length();
  321. }
  322. else
  323. {
  324. /* Third case: the simplest. We have got the prefix and it
  325. * has not to be expanded.
  326. */
  327. aElement.add (prefix);
  328. aIdx.add (Integer.valueOf(idx_idx));
  329. idx += prefix.key.length();
  330. /* If the sequence is in an expansion, we must decrease the
  331. * counter.
  332. */
  333. if (alreadyExpanded > 0)
  334. {
  335. alreadyExpanded -= prefix.key.length();
  336. if (alreadyExpanded == 0)
  337. {
  338. idx_idx += idxToMove;
  339. idxToMove = 0;
  340. }
  341. }
  342. else
  343. idx_idx += prefix.key.length();
  344. }
  345. }
  346. textDecomposition = aElement.toArray(new RuleBasedCollator.CollationElement[aElement.size()]);
  347. textIndexes = new int[aIdx.size()+1];
  348. for (int i = 0; i < aIdx.size(); i++)
  349. {
  350. textIndexes[i] = aIdx.get(i).intValue();
  351. }
  352. textIndexes[aIdx.size()] = text.length();
  353. }
  354. /**
  355. * This method sets the <code>String</code> that it is iterating over
  356. * to the <code>String</code> represented by the specified
  357. * <code>CharacterIterator</code>.
  358. *
  359. * @param source The <code>CharacterIterator</code> containing the new
  360. * <code>String</code> to iterate over.
  361. */
  362. public void setText(CharacterIterator source)
  363. {
  364. CPStringBuilder expand = new CPStringBuilder();
  365. // For now assume we read from the beginning of the string.
  366. for (char c = source.first();
  367. c != CharacterIterator.DONE;
  368. c = source.next())
  369. expand.append(c);
  370. setText(expand.toString());
  371. }
  372. /**
  373. * This method returns the current offset into the <code>String</code>
  374. * that is being iterated over.
  375. *
  376. * @return The iteration index position.
  377. *
  378. * @since 1.2
  379. */
  380. public int getOffset()
  381. {
  382. return textIndex;
  383. }
  384. /**
  385. * This method sets the iteration index position into the current
  386. * <code>String</code> to the specified value. This value must not
  387. * be negative and must not be greater than the last index position
  388. * in the <code>String</code>.
  389. *
  390. * @param offset The new iteration index position.
  391. *
  392. * @exception IllegalArgumentException If the new offset is not valid.
  393. */
  394. public void setOffset(int offset)
  395. {
  396. if (offset < 0)
  397. throw new IllegalArgumentException("Negative offset: " + offset);
  398. if (offset > (text.getEndIndex() - 1))
  399. throw new IllegalArgumentException("Offset too large: " + offset);
  400. for (index = 0; index < textDecomposition.length; index++)
  401. {
  402. if (offset <= textIndexes[index])
  403. break;
  404. }
  405. /*
  406. * As textIndexes[0] == 0, we should not have to take care whether index is
  407. * greater than 0. It is always.
  408. */
  409. if (textIndexes[index] == offset)
  410. textIndex = offset;
  411. else
  412. textIndex = textIndexes[index-1];
  413. }
  414. /**
  415. * This method returns the maximum length of any expansion sequence that
  416. * ends with the specified collation order value. (Whatever that means).
  417. *
  418. * @param value The collation order value
  419. *
  420. * @return The maximum length of an expansion sequence.
  421. */
  422. public int getMaxExpansion(int value)
  423. {
  424. return 1;
  425. }
  426. }