AttributedString.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* AttributedString.java -- Models text with attributes
  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 gnu.java.lang.CPStringBuilder;
  33. import java.util.ArrayList;
  34. import java.util.Arrays;
  35. import java.util.HashMap;
  36. import java.util.Hashtable;
  37. import java.util.Iterator;
  38. import java.util.Map;
  39. import java.util.Set;
  40. import static java.text.AttributedCharacterIterator.Attribute;
  41. /**
  42. * This class models a <code>String</code> with attributes over various
  43. * subranges of the string. It allows applications to access this
  44. * information via the <code>AttributedCharacterIterator</code> interface.
  45. *
  46. * @since 1.2
  47. *
  48. * @author Aaron M. Renn (arenn@urbanophile.com)
  49. * @since 1.2
  50. */
  51. public class AttributedString
  52. {
  53. /**
  54. * The attributes and ranges of text over which those attributes apply.
  55. */
  56. final class AttributeRange
  57. {
  58. /** A Map of the attributes */
  59. Map<? extends Attribute, ?> attribs;
  60. /** The beginning index of the attributes */
  61. int beginIndex;
  62. /** The ending index of the attributes */
  63. int endIndex;
  64. /**
  65. * Creates a new attribute range.
  66. *
  67. * @param attribs the attributes.
  68. * @param beginIndex the start index.
  69. * @param endIndex the end index.
  70. */
  71. AttributeRange(Map<? extends Attribute, ?> attribs,
  72. int beginIndex, int endIndex)
  73. {
  74. this.attribs = attribs;
  75. this.beginIndex = beginIndex;
  76. this.endIndex = endIndex;
  77. }
  78. } // Inner class AttributeRange
  79. /** The string we are representing. */
  80. private StringCharacterIterator sci;
  81. /** The attribute information */
  82. private AttributeRange[] attribs;
  83. /**
  84. * Creates a new instance of <code>AttributedString</code>
  85. * that represents the specified <code>String</code> with no attributes.
  86. *
  87. * @param str The <code>String</code> to be attributed (<code>null</code> not
  88. * permitted).
  89. *
  90. * @throws NullPointerException if <code>str</code> is <code>null</code>.
  91. */
  92. public AttributedString(String str)
  93. {
  94. sci = new StringCharacterIterator(str);
  95. attribs = new AttributeRange[0];
  96. }
  97. /**
  98. * Creates a new instance of <code>AttributedString</code>
  99. * that represents that specified <code>String</code> with the specified
  100. * attributes over the entire length of the <code>String</code>.
  101. *
  102. * @param str The <code>String</code> to be attributed.
  103. * @param attributes The attribute list.
  104. */
  105. public AttributedString(String str,
  106. Map<? extends Attribute, ?> attributes)
  107. {
  108. this(str);
  109. attribs = new AttributeRange[1];
  110. attribs[0] = new AttributeRange(attributes, 0, str.length());
  111. }
  112. /**
  113. * Initializes a new instance of <code>AttributedString</code>
  114. * that will use the text and attribute information from the specified
  115. * <code>AttributedCharacterIterator</code>.
  116. *
  117. * @param aci The <code>AttributedCharacterIterator</code> containing the
  118. * text and attribute information (<code>null</code> not
  119. * permitted).
  120. *
  121. * @throws NullPointerException if <code>aci</code> is <code>null</code>.
  122. */
  123. public AttributedString(AttributedCharacterIterator aci)
  124. {
  125. this(aci, aci.getBeginIndex(), aci.getEndIndex(), null);
  126. }
  127. /**
  128. * Initializes a new instance of <code>AttributedString</code>
  129. * that will use the text and attribute information from the specified
  130. * subrange of the specified <code>AttributedCharacterIterator</code>.
  131. *
  132. * @param aci The <code>AttributedCharacterIterator</code> containing the
  133. * text and attribute information.
  134. * @param beginIndex The beginning index of the text subrange.
  135. * @param endIndex The ending index of the text subrange.
  136. */
  137. public AttributedString(AttributedCharacterIterator aci, int beginIndex,
  138. int endIndex)
  139. {
  140. this(aci, beginIndex, endIndex, null);
  141. }
  142. /**
  143. * Initializes a new instance of <code>AttributedString</code>
  144. * that will use the text and attribute information from the specified
  145. * subrange of the specified <code>AttributedCharacterIterator</code>.
  146. * Only attributes from the source iterator that are present in the
  147. * specified array of attributes will be included in the attribute list
  148. * for this object.
  149. *
  150. * @param aci The <code>AttributedCharacterIterator</code> containing the
  151. * text and attribute information.
  152. * @param begin The beginning index of the text subrange.
  153. * @param end The ending index of the text subrange.
  154. * @param attributes A list of attributes to include from the iterator, or
  155. * <code>null</code> to include all attributes.
  156. */
  157. public AttributedString(AttributedCharacterIterator aci, int begin, int end,
  158. Attribute[] attributes)
  159. {
  160. // Validate some arguments
  161. if ((begin < 0) || (end < begin) || end > aci.getEndIndex())
  162. throw new IllegalArgumentException("Bad index values");
  163. CPStringBuilder sb = new CPStringBuilder("");
  164. // Get the valid attribute list
  165. Set<Attribute> allAttribs = aci.getAllAttributeKeys();
  166. if (attributes != null)
  167. allAttribs.retainAll(Arrays.asList(attributes));
  168. // Loop through and extract the attributes
  169. char c = aci.setIndex(begin);
  170. ArrayList<AttributeRange> accum = new ArrayList<AttributeRange>();
  171. do
  172. {
  173. sb.append(c);
  174. Iterator<Attribute> iter = allAttribs.iterator();
  175. while(iter.hasNext())
  176. {
  177. Object obj = iter.next();
  178. // What should we do if this is not true?
  179. if (!(obj instanceof Attribute))
  180. continue;
  181. Attribute attrib = (Attribute)obj;
  182. // Make sure the attribute is defined.
  183. Object attribObj = aci.getAttribute(attrib);
  184. if (attribObj == null)
  185. continue;
  186. int rl = aci.getRunLimit(attrib);
  187. if (rl > end)
  188. rl = end;
  189. rl -= begin;
  190. // Check to see if we already processed this one
  191. int rs = aci.getRunStart(attrib);
  192. if ((rs < aci.getIndex()) && (aci.getIndex() != begin))
  193. continue;
  194. // If the attribute run starts before the beginning index, we
  195. // need to junk it if it is an Annotation.
  196. rs -= begin;
  197. if (rs < 0)
  198. {
  199. if (attribObj instanceof Annotation)
  200. continue;
  201. rs = 0;
  202. }
  203. // Create a map object. Yes this will only contain one attribute
  204. Map<Attribute,Object> newMap = new Hashtable<Attribute,Object>();
  205. newMap.put(attrib, attribObj);
  206. // Add it to the attribute list.
  207. accum.add(new AttributeRange(newMap, rs, rl));
  208. }
  209. c = aci.next();
  210. }
  211. while( aci.getIndex() < end );
  212. attribs = new AttributeRange[accum.size()];
  213. attribs = accum.toArray(attribs);
  214. sci = new StringCharacterIterator(sb.toString());
  215. }
  216. /**
  217. * Adds a new attribute that will cover the entire string.
  218. *
  219. * @param attrib The attribute to add.
  220. * @param value The value of the attribute.
  221. */
  222. public void addAttribute(Attribute attrib, Object value)
  223. {
  224. addAttribute(attrib, value, 0, sci.getEndIndex());
  225. }
  226. /**
  227. * Adds a new attribute that will cover the specified subrange
  228. * of the string.
  229. *
  230. * @param attrib The attribute to add.
  231. * @param value The value of the attribute, which may be <code>null</code>.
  232. * @param begin The beginning index of the subrange.
  233. * @param end The ending index of the subrange.
  234. *
  235. * @exception IllegalArgumentException If attribute is <code>null</code> or
  236. * the subrange is not valid.
  237. */
  238. public void addAttribute(Attribute attrib, Object value, int begin, int end)
  239. {
  240. if (attrib == null)
  241. throw new IllegalArgumentException("null attribute");
  242. if (end <= begin)
  243. throw new IllegalArgumentException("Requires end > begin");
  244. HashMap<Attribute,Object> hm = new HashMap<Attribute,Object>();
  245. hm.put(attrib, value);
  246. addAttributes(hm, begin, end);
  247. }
  248. /**
  249. * Adds all of the attributes in the specified list to the
  250. * specified subrange of the string.
  251. *
  252. * @param attributes The list of attributes.
  253. * @param beginIndex The beginning index.
  254. * @param endIndex The ending index
  255. *
  256. * @throws NullPointerException if <code>attributes</code> is
  257. * <code>null</code>.
  258. * @throws IllegalArgumentException if the subrange is not valid.
  259. */
  260. public void addAttributes(Map<? extends Attribute, ?> attributes,
  261. int beginIndex, int endIndex)
  262. {
  263. if (attributes == null)
  264. throw new NullPointerException("null attribute");
  265. if ((beginIndex < 0) || (endIndex > sci.getEndIndex()) ||
  266. (endIndex <= beginIndex))
  267. throw new IllegalArgumentException("bad range");
  268. AttributeRange[] new_list = new AttributeRange[attribs.length + 1];
  269. System.arraycopy(attribs, 0, new_list, 0, attribs.length);
  270. attribs = new_list;
  271. attribs[attribs.length - 1] = new AttributeRange(attributes, beginIndex,
  272. endIndex);
  273. }
  274. /**
  275. * Returns an <code>AttributedCharacterIterator</code> that
  276. * will iterate over the entire string.
  277. *
  278. * @return An <code>AttributedCharacterIterator</code> for the entire string.
  279. */
  280. public AttributedCharacterIterator getIterator()
  281. {
  282. return(new AttributedStringIterator(sci, attribs, 0, sci.getEndIndex(),
  283. null));
  284. }
  285. /**
  286. * Returns an <code>AttributedCharacterIterator</code> that
  287. * will iterate over the entire string. This iterator will return information
  288. * about the list of attributes in the specified array. Attributes not in
  289. * the array may or may not be returned by the iterator. If the specified
  290. * array is <code>null</code>, all attributes will be returned.
  291. *
  292. * @param attributes A list of attributes to include in the returned iterator.
  293. *
  294. * @return An <code>AttributedCharacterIterator</code> for this string.
  295. */
  296. public AttributedCharacterIterator getIterator(Attribute[] attributes)
  297. {
  298. return(getIterator(attributes, 0, sci.getEndIndex()));
  299. }
  300. /**
  301. * Returns an <code>AttributedCharacterIterator</code> that
  302. * will iterate over the specified subrange. This iterator will return
  303. * information about the list of attributes in the specified array.
  304. * Attributes not in the array may or may not be returned by the iterator.
  305. * If the specified array is <code>null</code>, all attributes will be
  306. * returned.
  307. *
  308. * @param attributes A list of attributes to include in the returned iterator.
  309. * @param beginIndex The beginning index of the subrange.
  310. * @param endIndex The ending index of the subrange.
  311. *
  312. * @return An <code>AttributedCharacterIterator</code> for this string.
  313. */
  314. public AttributedCharacterIterator getIterator(Attribute[] attributes,
  315. int beginIndex, int endIndex)
  316. {
  317. if ((beginIndex < 0) || (endIndex > sci.getEndIndex()) ||
  318. (endIndex < beginIndex))
  319. throw new IllegalArgumentException("bad range");
  320. return(new AttributedStringIterator(sci, attribs, beginIndex, endIndex,
  321. attributes));
  322. }
  323. } // class AttributedString