AttributedStringIterator.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* AttributedStringIterator.java -- Class to iterate over AttributedString
  2. Copyright (C) 1998, 1999, 2004, 2005, 2006, 2012 Free Software Foundation, Inc.
  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 java.util.HashMap;
  33. import java.util.HashSet;
  34. import java.util.Iterator;
  35. import java.util.Map;
  36. import java.util.Set;
  37. import static java.text.AttributedCharacterIterator.Attribute;
  38. /**
  39. * This class implements the AttributedCharacterIterator interface. It
  40. * is used by AttributedString.getIterator().
  41. *
  42. * @version 0.0
  43. *
  44. * @author Aaron M. Renn (arenn@urbanophile.com)
  45. */
  46. class AttributedStringIterator implements AttributedCharacterIterator
  47. {
  48. /*************************************************************************/
  49. /** The character iterator containing the text */
  50. private CharacterIterator ci;
  51. /** The list of attributes and ranges */
  52. private AttributedString.AttributeRange[] attribs;
  53. /**
  54. * The list of attributes that the user is interested in. We may,
  55. * at our option, not return any other attributes.
  56. */
  57. private Attribute[] restricts;
  58. /*************************************************************************/
  59. /**
  60. * Creates a new instance.
  61. *
  62. * @param sci an iterator for the string content.
  63. * @param attribs the attribute ranges.
  64. * @param beginIndex the start index.
  65. * @param endIndex the end index.
  66. * @param restricts the attributes that the user is interested in.
  67. */
  68. AttributedStringIterator(StringCharacterIterator sci,
  69. AttributedString.AttributeRange[] attribs,
  70. int beginIndex, int endIndex,
  71. AttributedCharacterIterator.Attribute[] restricts)
  72. {
  73. this.ci = new StringCharacterIterator(sci, beginIndex, endIndex);
  74. this.attribs = attribs;
  75. this.restricts = restricts;
  76. }
  77. /*************************************************************************/
  78. // First we have a bunch of stupid redirects. If StringCharacterIterator
  79. // weren't final, I just would have extended that for this class. Alas, no.
  80. public Object clone()
  81. {
  82. return(ci.clone());
  83. }
  84. public char current()
  85. {
  86. return(ci.current());
  87. }
  88. public char next()
  89. {
  90. return(ci.next());
  91. }
  92. public char previous()
  93. {
  94. return(ci.previous());
  95. }
  96. public char first()
  97. {
  98. return(ci.first());
  99. }
  100. public char last()
  101. {
  102. return(ci.last());
  103. }
  104. public int getIndex()
  105. {
  106. return(ci.getIndex());
  107. }
  108. public char setIndex(int index)
  109. {
  110. return(ci.setIndex(index));
  111. }
  112. public int getBeginIndex()
  113. {
  114. return(ci.getBeginIndex());
  115. }
  116. public int getEndIndex()
  117. {
  118. return(ci.getEndIndex());
  119. }
  120. /*
  121. * Here is where the AttributedCharacterIterator methods start.
  122. */
  123. /*************************************************************************/
  124. /**
  125. * Returns a list of all the attribute keys that are defined anywhere
  126. * on this string.
  127. */
  128. public Set<Attribute> getAllAttributeKeys()
  129. {
  130. HashSet<Attribute> s = new HashSet<Attribute>();
  131. if (attribs == null)
  132. return(s);
  133. for (int i = 0; i < attribs.length; i++)
  134. {
  135. if (attribs[i].beginIndex > getEndIndex()
  136. || attribs[i].endIndex <= getBeginIndex())
  137. continue;
  138. Iterator<? extends Attribute> iter = attribs[i].attribs.keySet().iterator();
  139. while (iter.hasNext())
  140. {
  141. s.add(iter.next());
  142. }
  143. }
  144. return(s);
  145. }
  146. /*************************************************************************/
  147. /**
  148. * Various methods that determine how far the run extends for various
  149. * attribute combinations.
  150. */
  151. public int getRunLimit()
  152. {
  153. return getRunLimit(getAllAttributeKeys());
  154. }
  155. public int getRunLimit(Attribute attrib)
  156. {
  157. HashSet<Attribute> s = new HashSet<Attribute>();
  158. s.add(attrib);
  159. return(getRunLimit(s));
  160. }
  161. public synchronized int getRunLimit(Set<? extends Attribute> attributeSet)
  162. {
  163. if (attributeSet == null)
  164. return ci.getEndIndex();
  165. int current = ci.getIndex();
  166. int end = ci.getEndIndex();
  167. int limit = current;
  168. if (current == end)
  169. return end;
  170. Map<Attribute,Object> runValues = getAttributes();
  171. while (limit < end)
  172. {
  173. Iterator<? extends Attribute> iterator = attributeSet.iterator();
  174. while (iterator.hasNext())
  175. {
  176. Attribute attributeKey = iterator.next();
  177. Object v1 = runValues.get(attributeKey);
  178. Object v2 = getAttribute(attributeKey, limit + 1);
  179. boolean changed = false;
  180. // check for equal or both null, if NO return start
  181. if (v1 != null)
  182. {
  183. changed = !v1.equals(v2);
  184. }
  185. else
  186. {
  187. changed = (v2 != null);
  188. }
  189. if (changed)
  190. return limit + 1;
  191. }
  192. // no differences, so increment limit and next and loop again
  193. limit++;
  194. }
  195. return end;
  196. }
  197. /*************************************************************************/
  198. /**
  199. * Various methods that determine where the run begins for various
  200. * attribute combinations.
  201. */
  202. /**
  203. * Returns the index of the first character in the run containing the current
  204. * character and defined by all the attributes defined for that character
  205. * position.
  206. *
  207. * @return The run start index.
  208. */
  209. public int getRunStart()
  210. {
  211. return(getRunStart(getAttributes().keySet()));
  212. }
  213. /**
  214. * Returns the index of the first character in the run, defined by the
  215. * specified attribute, that contains the current character.
  216. *
  217. * @param attrib the attribute (<code>null</code> permitted).
  218. *
  219. * return The index of the first character in the run.
  220. */
  221. public int getRunStart(Attribute attrib)
  222. {
  223. if (attrib == null)
  224. return ci.getBeginIndex();
  225. HashSet<Attribute> s = new HashSet<Attribute>();
  226. s.add(attrib);
  227. return(getRunStart(s));
  228. }
  229. /**
  230. * Returns the index of the first character in the run, defined by the
  231. * specified attribute set, that contains the current character.
  232. *
  233. * @param attributeSet the attribute set (<code>null</code> permitted).
  234. *
  235. * return The index of the first character in the run.
  236. */
  237. public int getRunStart(Set<? extends Attribute> attributeSet)
  238. {
  239. if (attributeSet == null)
  240. return ci.getBeginIndex();
  241. int current = ci.getIndex();
  242. int begin = ci.getBeginIndex();
  243. int start = current;
  244. if (start == begin)
  245. return begin;
  246. Map<Attribute, Object> runValues = getAttributes();
  247. int prev = start - 1;
  248. while (start > begin)
  249. {
  250. Iterator<? extends Attribute> iterator = attributeSet.iterator();
  251. while (iterator.hasNext())
  252. {
  253. Attribute attributeKey = iterator.next();
  254. Object v1 = runValues.get(attributeKey);
  255. Object v2 = getAttribute(attributeKey, prev);
  256. boolean changed = false;
  257. // check for equal or both null, if NO return start
  258. if (v1 != null)
  259. {
  260. changed = !v1.equals(v2);
  261. }
  262. else
  263. {
  264. changed = (v2 != null);
  265. }
  266. if (changed)
  267. return start;
  268. }
  269. // no differences, so decrement start and prev and loop again
  270. start--;
  271. prev--;
  272. }
  273. return start;
  274. }
  275. /*************************************************************************/
  276. /**
  277. * Returns the value for an attribute at the specified position. If the
  278. * attribute key (<code>key</code>) is <code>null</code>, the method returns
  279. * <code>null</code>.
  280. *
  281. * @param key the key (<code>null</code> permitted).
  282. * @param pos the character position.
  283. *
  284. * @return The attribute value (possibly <code>null</code>).
  285. */
  286. private Object getAttribute(AttributedCharacterIterator.Attribute key,
  287. int pos)
  288. {
  289. if (attribs == null)
  290. return null;
  291. for (int i = attribs.length - 1; i >= 0; i--)
  292. {
  293. if (pos >= attribs[i].beginIndex && pos < attribs[i].endIndex)
  294. {
  295. Set<? extends Attribute> keys = attribs[i].attribs.keySet();
  296. if (keys.contains(key))
  297. {
  298. return attribs[i].attribs.get(key);
  299. }
  300. }
  301. }
  302. return null;
  303. }
  304. /**
  305. * Returns the value for an attribute at the current position. If the
  306. * attribute key (<code>key</code>) is <code>null</code>, the method returns
  307. * <code>null</code>.
  308. *
  309. * @param key the key (<code>null</code> permitted).
  310. *
  311. * @return The attribute value (possibly <code>null</code>).
  312. */
  313. public Object getAttribute(AttributedCharacterIterator.Attribute key)
  314. {
  315. return getAttribute(key, ci.getIndex());
  316. }
  317. /*************************************************************************/
  318. /**
  319. * Return a list of all the attributes and values defined for this
  320. * character
  321. */
  322. public Map<Attribute,Object> getAttributes()
  323. {
  324. HashMap<Attribute,Object> m = new HashMap<Attribute,Object>();
  325. if (attribs == null)
  326. return(m);
  327. for (int i = 0; i < attribs.length; i++)
  328. {
  329. if ((ci.getIndex() >= attribs[i].beginIndex) &&
  330. (ci.getIndex() < attribs[i].endIndex))
  331. m.putAll(attribs[i].attribs);
  332. }
  333. return(m);
  334. }
  335. } // class AttributedStringIterator