2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / text / AttributedString.java
blobd689d87ac58d4fc7fd3ca6d6622315d1e1131e61
1 /* AttributedString.java -- Models text with attributes
2 Copyright (C) 1998, 1999 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)
9 any later version.
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
19 02111-1307 USA.
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
24 combination.
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. */
39 package java.text;
41 import java.util.Iterator;
42 import java.util.Hashtable;
43 import java.util.HashSet;
44 import java.util.Map;
45 import java.util.Set;
47 /**
48 * This class models a <code>String</code> with attributes over various
49 * subranges of the string. It allows applications to access this
50 * information via the <code>AttributedCharcterIterator</code> interface.
52 * @version 0.0
54 * @author Aaron M. Renn (arenn@urbanophile.com)
56 public class AttributedString
59 /*************************************************************************/
62 * Inner Classes
65 /**
66 * This class contains the attributes and ranges of text over which
67 * that attributes apply.
69 final class AttributeRange
73 * Instance Variables
76 /**
77 * A Map of the attributes
79 Map attribs;
81 /**
82 * The beginning index of the attributes
84 int begin_index;
86 /**
87 * The ending index of the attributes
89 int end_index;
91 /*************************************************************************/
94 * Constructors
97 AttributeRange(Map attribs, int begin_index, int end_index)
99 this.attribs = attribs;
100 this.begin_index = begin_index;
101 this.end_index = end_index;
104 } // Inner class AttributeRange
106 /*************************************************************************/
109 * Instance Variables
113 * This object holds the string we are representing.
115 private StringCharacterIterator sci;
118 * This is the attribute information
120 private AttributeRange[] attribs;
122 /*************************************************************************/
125 * Constructors
129 * This method initializes a new instance of <code>AttributedString</code>
130 * that represents the specified <code>String</code> with no attributes.
132 * @param str The <code>String</code> to be attributed.
134 public
135 AttributedString(String str)
137 sci = new StringCharacterIterator(str);
138 attribs = new AttributeRange[0];
141 /*************************************************************************/
144 * This method initializes a new instance of <code>AttributedString</code>
145 * that represents that specified <code>String</code> with the specified
146 * attributes over the entire length of the <code>String</code>.
148 * @param str The <code>String</code> to be attributed.
149 * @param attributes The attribute list.
151 public
152 AttributedString(String str, Map attributes)
154 this(str);
156 attribs = new AttributeRange[1];
157 attribs[0] = new AttributeRange(attributes, 0, str.length());
160 /*************************************************************************/
163 * This method initializes a new instance of <code>AttributedString</code>
164 * that will use the text and attribute information from the specified
165 * <code>AttributedCharacterIterator</code>.
167 * @param aci The <code>AttributedCharacterIterator</code> containing the text and attribute information.
169 public
170 AttributedString(AttributedCharacterIterator aci)
172 this(aci, aci.getBeginIndex(), aci.getEndIndex(), null);
175 /*************************************************************************/
178 * This method initializes a new instance of <code>AttributedString</code>
179 * that will use the text and attribute information from the specified
180 * subrange of the specified <code>AttributedCharacterIterator</code>.
182 * @param aci The <code>AttributedCharacterIterator</code> containing the text and attribute information.
183 * @param begin_index The beginning index of the text subrange.
184 * @param end_index The ending index of the text subrange.
186 public
187 AttributedString(AttributedCharacterIterator aci, int begin_index,
188 int end_index)
190 this(aci, begin_index, end_index, null);
193 /*************************************************************************/
196 * This method initializes a new instance of <code>AttributedString</code>
197 * that will use the text and attribute information from the specified
198 * subrange of the specified <code>AttributedCharacterIterator</code>.
199 * Only attributes from the source iterator that are present in the
200 * specified array of attributes will be included in the attribute list
201 * for this object.
203 * @param aci The <code>AttributedCharacterIterator</code> containing the text and attribute information.
204 * @param begin_index The beginning index of the text subrange.
205 * @param end_index The ending index of the text subrange.
206 * @param attributes A list of attributes to include from the iterator, or <code>null</code> to include all attributes.
208 public
209 AttributedString(AttributedCharacterIterator aci, int begin_index,
210 int end_index, AttributedCharacterIterator.Attribute[] attributes)
212 // Validate some arguments
213 if ((begin_index < 0) || (end_index < begin_index))
214 throw new IllegalArgumentException("Bad index values");
216 StringBuffer sb = new StringBuffer("");
218 // Get the valid attribute list
219 Set all_attribs = aci.getAllAttributeKeys();
220 if (attributes != null)
222 Set valid_attribs = new HashSet();
223 Iterator iter = all_attribs.iterator();
224 while (iter.hasNext())
226 Object obj = iter.next();
228 int i;
229 for (i = 0; i < attributes.length; i++)
230 if (obj.equals(attributes[0]))
231 break;
233 if (i == attributes.length)
234 continue;
236 valid_attribs.add(obj);
239 all_attribs = valid_attribs;
242 // Loop through and extract the attributes
243 char c = aci.setIndex(begin_index);
247 sb.append(c);
249 Iterator iter = all_attribs.iterator();
250 while(iter.hasNext())
252 Object obj = iter.next();
254 // What should we do if this is not true?
255 if (!(obj instanceof AttributedCharacterIterator.Attribute))
256 continue;
258 AttributedCharacterIterator.Attribute attrib =
259 (AttributedCharacterIterator.Attribute)obj;
261 // Make sure the attribute is defined.
262 int rl = aci.getRunLimit(attrib);
263 if (rl == -1)
264 continue;
265 if (rl > end_index)
266 rl = end_index;
267 rl -= begin_index;
269 // Check to see if we already processed this one
270 int rs = aci.getRunStart(attrib);
271 if ((rs < aci.getIndex()) && (aci.getIndex() != begin_index))
272 continue;
274 // If the attribute run starts before the beginning index, we
275 // need to junk it if it is an Annotation.
276 Object attrib_obj = aci.getAttribute(attrib);
277 if (rs < begin_index)
279 if (attrib_obj instanceof Annotation)
280 continue;
282 rs = begin_index;
284 else
286 rs -= begin_index;
289 // Create a map object. Yes this will only contain one attribute
290 Map new_map = new Hashtable();
291 new_map.put(attrib, attrib_obj);
293 // Add it to the attribute list. Yes this is a bad way to do things.
294 AttributeRange[] new_list = new AttributeRange[attribs.length + 1];
295 System.arraycopy(attribs, 0, new_list, 0, attribs.length);
296 attribs = new_list;
297 attribs[attribs.length - 1] = new AttributeRange(new_map, rs, rl);
300 c = aci.next();
302 while(c != CharacterIterator.DONE);
304 sci = new StringCharacterIterator(sb.toString());
307 /*************************************************************************/
310 * Instance Methods
314 * This method adds a new attribute that will cover the entire string.
316 * @param attrib The attribute to add.
317 * @param value The value of the attribute.
319 public void
320 addAttribute(AttributedCharacterIterator.Attribute attrib, Object value)
322 addAttribute(attrib, value, 0, sci.getEndIndex() - 1);
325 /*************************************************************************/
328 * This method adds a new attribute that will cover the specified subrange
329 * of the string.
331 * @param attrib The attribute to add.
332 * @param value The value of the attribute.
333 * @param begin_index The beginning index of the subrange.
334 * @param end_index The ending index of the subrange.
336 * @exception IllegalArgumentException If attribute is <code>null</code> or the subrange is not valid.
338 public void
339 addAttribute(AttributedCharacterIterator.Attribute attrib, Object value,
340 int begin_index, int end_index)
342 if (attrib == null)
343 throw new IllegalArgumentException("null attribute");
345 Hashtable ht = new Hashtable();
346 ht.put(attrib, value);
348 addAttributes(ht, begin_index, end_index);
351 /*************************************************************************/
354 * This method adds all of the attributes in the specified list to the
355 * specified subrange of the string.
357 * @param attributes The list of attributes.
358 * @param begin_index The beginning index.
359 * @param end_index The ending index
361 * @param IllegalArgumentException If the list is <code>null</code> or the subrange is not valid.
363 public void
364 addAttributes(Map attributes, int begin_index, int end_index)
366 if (attributes == null)
367 throw new IllegalArgumentException("null attribute");
369 if ((begin_index < 0) || (end_index > sci.getEndIndex()) ||
370 (end_index < begin_index))
371 throw new IllegalArgumentException("bad range");
373 AttributeRange[] new_list = new AttributeRange[attribs.length + 1];
374 System.arraycopy(attribs, 0, new_list, 0, attribs.length);
375 attribs = new_list;
376 attribs[attribs.length - 1] = new AttributeRange(attributes, begin_index,
377 end_index);
380 /*************************************************************************/
383 * This method returns an <code>AttributedCharacterIterator</code> that
384 * will iterate over the entire string.
386 * @return An <code>AttributedCharacterIterator</code> for the entire string.
388 public AttributedCharacterIterator
389 getIterator()
391 return(new AttributedStringIterator(sci, attribs, 0, sci.getEndIndex() - 1,
392 null));
395 /*************************************************************************/
398 * This method returns an <code>AttributedCharacterIterator</code> that
399 * will iterate over the entire string. 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.
406 * @return An <code>AttributedCharacterIterator</code> for this string.
408 public AttributedCharacterIterator
409 getIterator(AttributedCharacterIterator.Attribute[] attributes)
411 return(getIterator(attributes, 0, sci.getEndIndex() - 1));
414 /*************************************************************************/
417 * This method returns an <code>AttributedCharacterIterator</code> that
418 * will iterate over the specified subrange. This iterator will return information
419 * about the list of attributes in the specified array. Attributes not in
420 * the array may or may not be returned by the iterator. If the specified
421 * array is <code>null</code>, all attributes will be returned.
423 * @param attributes A list of attributes to include in the returned iterator.
424 * @param begin_index The beginning index of the subrange.
425 * @param end_index The ending index of the subrange.
427 * @return An <code>AttributedCharacterIterator</code> for this string.
429 public AttributedCharacterIterator
430 getIterator(AttributedCharacterIterator.Attribute[] attributes,
431 int begin_index, int end_index)
433 if ((begin_index < 0) || (end_index > sci.getEndIndex()) ||
434 (end_index < begin_index))
435 throw new IllegalArgumentException("bad range");
437 return(new AttributedStringIterator(sci, attribs, begin_index, end_index,
438 attributes));
441 } // class AttributedString