1 /* AttributedString.java -- Models text with attributes
2 Copyright (C) 1998, 1999, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java.util.Arrays;
42 import java.util.HashMap;
43 import java.util.Hashtable;
44 import java.util.Iterator;
49 * This class models a <code>String</code> with attributes over various
50 * subranges of the string. It allows applications to access this
51 * information via the <code>AttributedCharcterIterator</code> interface.
55 * @author Aaron M. Renn (arenn@urbanophile.com)
57 public class AttributedString
60 /*************************************************************************/
67 * This class contains the attributes and ranges of text over which
68 * that attributes apply.
70 final class AttributeRange
78 * A Map of the attributes
83 * The beginning index of the attributes
88 * The ending index of the attributes
92 /*************************************************************************/
98 AttributeRange(Map attribs, int begin_index, int end_index)
100 this.attribs = attribs;
101 this.begin_index = begin_index;
102 this.end_index = end_index;
105 } // Inner class AttributeRange
107 /*************************************************************************/
114 * This object holds the string we are representing.
116 private StringCharacterIterator sci;
119 * This is the attribute information
121 private AttributeRange[] attribs;
123 /*************************************************************************/
130 * This method initializes a new instance of <code>AttributedString</code>
131 * that represents the specified <code>String</code> with no attributes.
133 * @param str The <code>String</code> to be attributed.
136 AttributedString(String str)
138 sci = new StringCharacterIterator(str);
139 attribs = new AttributeRange[0];
142 /*************************************************************************/
145 * This method initializes a new instance of <code>AttributedString</code>
146 * that represents that specified <code>String</code> with the specified
147 * attributes over the entire length of the <code>String</code>.
149 * @param str The <code>String</code> to be attributed.
150 * @param attributes The attribute list.
153 AttributedString(String str, Map attributes)
157 attribs = new AttributeRange[1];
158 attribs[0] = new AttributeRange(attributes, 0, str.length());
161 /*************************************************************************/
164 * This method initializes a new instance of <code>AttributedString</code>
165 * that will use the text and attribute information from the specified
166 * <code>AttributedCharacterIterator</code>.
168 * @param aci The <code>AttributedCharacterIterator</code> containing the text and attribute information.
171 AttributedString(AttributedCharacterIterator aci)
173 this(aci, aci.getBeginIndex(), aci.getEndIndex(), null);
176 /*************************************************************************/
179 * This method initializes a new instance of <code>AttributedString</code>
180 * that will use the text and attribute information from the specified
181 * subrange of the specified <code>AttributedCharacterIterator</code>.
183 * @param aci The <code>AttributedCharacterIterator</code> containing the text and attribute information.
184 * @param begin_index The beginning index of the text subrange.
185 * @param end_index The ending index of the text subrange.
188 AttributedString(AttributedCharacterIterator aci, int begin_index,
191 this(aci, begin_index, end_index, null);
194 /*************************************************************************/
197 * This method initializes a new instance of <code>AttributedString</code>
198 * that will use the text and attribute information from the specified
199 * subrange of the specified <code>AttributedCharacterIterator</code>.
200 * Only attributes from the source iterator that are present in the
201 * specified array of attributes will be included in the attribute list
204 * @param aci The <code>AttributedCharacterIterator</code> containing the text and attribute information.
205 * @param begin_index The beginning index of the text subrange.
206 * @param end_index The ending index of the text subrange.
207 * @param attributes A list of attributes to include from the iterator, or <code>null</code> to include all attributes.
210 AttributedString(AttributedCharacterIterator aci, int begin_index,
211 int end_index, AttributedCharacterIterator.Attribute[] attributes)
213 // Validate some arguments
214 if ((begin_index < 0) || (end_index < begin_index))
215 throw new IllegalArgumentException("Bad index values");
217 StringBuffer sb = new StringBuffer("");
219 // Get the valid attribute list
220 Set all_attribs = aci.getAllAttributeKeys();
221 if (attributes != null)
222 all_attribs.retainAll(Arrays.asList(attributes));
224 // Loop through and extract the attributes
225 char c = aci.setIndex(begin_index);
231 Iterator iter = all_attribs.iterator();
232 while(iter.hasNext())
234 Object obj = iter.next();
236 // What should we do if this is not true?
237 if (!(obj instanceof AttributedCharacterIterator.Attribute))
240 AttributedCharacterIterator.Attribute attrib =
241 (AttributedCharacterIterator.Attribute)obj;
243 // Make sure the attribute is defined.
244 int rl = aci.getRunLimit(attrib);
251 // Check to see if we already processed this one
252 int rs = aci.getRunStart(attrib);
253 if ((rs < aci.getIndex()) && (aci.getIndex() != begin_index))
256 // If the attribute run starts before the beginning index, we
257 // need to junk it if it is an Annotation.
258 Object attrib_obj = aci.getAttribute(attrib);
259 if (rs < begin_index)
261 if (attrib_obj instanceof Annotation)
271 // Create a map object. Yes this will only contain one attribute
272 Map new_map = new Hashtable();
273 new_map.put(attrib, attrib_obj);
275 // Add it to the attribute list. Yes this is a bad way to do things.
276 AttributeRange[] new_list = new AttributeRange[attribs.length + 1];
277 System.arraycopy(attribs, 0, new_list, 0, attribs.length);
279 attribs[attribs.length - 1] = new AttributeRange(new_map, rs, rl);
284 while(c != CharacterIterator.DONE);
286 sci = new StringCharacterIterator(sb.toString());
289 /*************************************************************************/
296 * This method adds a new attribute that will cover the entire string.
298 * @param attrib The attribute to add.
299 * @param value The value of the attribute.
302 addAttribute(AttributedCharacterIterator.Attribute attrib, Object value)
304 addAttribute(attrib, value, 0, sci.getEndIndex());
307 /*************************************************************************/
310 * This method adds a new attribute that will cover the specified subrange
313 * @param attrib The attribute to add.
314 * @param value The value of the attribute, which may be null.
315 * @param begin_index The beginning index of the subrange.
316 * @param end_index The ending index of the subrange.
318 * @exception IllegalArgumentException If attribute is <code>null</code> or the subrange is not valid.
321 addAttribute(AttributedCharacterIterator.Attribute attrib, Object value,
322 int begin_index, int end_index)
325 throw new IllegalArgumentException("null attribute");
327 HashMap hm = new HashMap();
328 hm.put(attrib, value);
330 addAttributes(hm, begin_index, end_index);
333 /*************************************************************************/
336 * This method adds all of the attributes in the specified list to the
337 * specified subrange of the string.
339 * @param attributes The list of attributes.
340 * @param begin_index The beginning index.
341 * @param end_index The ending index
343 * @param IllegalArgumentException If the list is <code>null</code> or the subrange is not valid.
346 addAttributes(Map attributes, int begin_index, int end_index)
348 if (attributes == null)
349 throw new IllegalArgumentException("null attribute");
351 if ((begin_index < 0) || (end_index > sci.getEndIndex()) ||
352 (end_index < begin_index))
353 throw new IllegalArgumentException("bad range");
355 AttributeRange[] new_list = new AttributeRange[attribs.length + 1];
356 System.arraycopy(attribs, 0, new_list, 0, attribs.length);
358 attribs[attribs.length - 1] = new AttributeRange(attributes, begin_index,
362 /*************************************************************************/
365 * This method returns an <code>AttributedCharacterIterator</code> that
366 * will iterate over the entire string.
368 * @return An <code>AttributedCharacterIterator</code> for the entire string.
370 public AttributedCharacterIterator
373 return(new AttributedStringIterator(sci, attribs, 0, sci.getEndIndex(), null));
376 /*************************************************************************/
379 * This method returns an <code>AttributedCharacterIterator</code> that
380 * will iterate over the entire string. This iterator will return information
381 * about the list of attributes in the specified array. Attributes not in
382 * the array may or may not be returned by the iterator. If the specified
383 * array is <code>null</code>, all attributes will be returned.
385 * @param attributes A list of attributes to include in the returned iterator.
387 * @return An <code>AttributedCharacterIterator</code> for this string.
389 public AttributedCharacterIterator
390 getIterator(AttributedCharacterIterator.Attribute[] attributes)
392 return(getIterator(attributes, 0, sci.getEndIndex()));
395 /*************************************************************************/
398 * This method returns an <code>AttributedCharacterIterator</code> that
399 * will iterate over the specified subrange. This iterator will return information
400 * about the list of attributes in the specified array. Attributes not in
401 * the array may or may not be returned by the iterator. If the specified
402 * array is <code>null</code>, all attributes will be returned.
404 * @param attributes A list of attributes to include in the returned iterator.
405 * @param begin_index The beginning index of the subrange.
406 * @param end_index The ending index of the subrange.
408 * @return An <code>AttributedCharacterIterator</code> for this string.
410 public AttributedCharacterIterator
411 getIterator(AttributedCharacterIterator.Attribute[] attributes,
412 int begin_index, int end_index)
414 if ((begin_index < 0) || (end_index > sci.getEndIndex()) ||
415 (end_index < begin_index))
416 throw new IllegalArgumentException("bad range");
418 return(new AttributedStringIterator(sci, attribs, begin_index, end_index,
422 } // class AttributedString