OSDN Git Service

Add license clarification.
[pf3gnuchains/gcc-fork.git] / libjava / java / text / AttributedString.java
1 /* AttributedString.java -- Models text with attributes
2    Copyright (C) 1998, 1999 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10  
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.
15
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.
20
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.
25
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. */
37
38
39 package java.text;
40
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;
46
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.
51   *
52   * @version 0.0
53   *
54   * @author Aaron M. Renn (arenn@urbanophile.com)
55   */
56 public class AttributedString
57 {
58
59 /*************************************************************************/
60
61 /*
62  * Inner Classes
63  */
64
65 /**
66   * This class contains the attributes and ranges of text over which
67   * that attributes apply.
68   */
69 final class AttributeRange
70 {
71
72 /*
73  * Instance Variables
74  */
75
76 /**
77   * A Map of the attributes
78   */
79 Map attribs;
80
81 /**
82   * The beginning index of the attributes
83   */
84 int begin_index;
85
86 /**
87   * The ending index of the attributes
88   */
89 int end_index;
90
91 /*************************************************************************/
92
93 /*
94  * Constructors
95  */
96
97 AttributeRange(Map attribs, int begin_index, int end_index)
98 {
99   this.attribs = attribs;
100   this.begin_index = begin_index;
101   this.end_index = end_index;
102 }
103
104 } // Inner class AttributeRange
105
106 /*************************************************************************/
107
108 /*
109  * Instance Variables
110  */
111
112 /**
113   * This object holds the string we are representing.
114   */
115 private StringCharacterIterator sci;
116
117 /**
118   * This is the attribute information 
119   */
120 private AttributeRange[] attribs;
121
122 /*************************************************************************/
123
124 /*
125  * Constructors
126  */
127
128 /**
129   * This method initializes a new instance of <code>AttributedString</code>
130   * that represents the specified <code>String</code> with no attributes.
131   *
132   * @param str The <code>String</code> to be attributed.
133   */
134 public
135 AttributedString(String str)
136 {
137   sci = new StringCharacterIterator(str);
138   attribs = new AttributeRange[0];
139 }
140
141 /*************************************************************************/
142
143 /**
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>.
147   *
148   * @param str The <code>String</code> to be attributed.
149   * @param attributes The attribute list.
150   */
151 public
152 AttributedString(String str, Map attributes)
153 {
154   this(str);
155
156   attribs = new AttributeRange[1];
157   attribs[0] = new AttributeRange(attributes, 0, str.length());
158 }
159
160 /*************************************************************************/
161
162 /**
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>.
166   *
167   * @param aci The <code>AttributedCharacterIterator</code> containing the text and attribute information.
168   */
169 public
170 AttributedString(AttributedCharacterIterator aci)
171 {
172   this(aci, aci.getBeginIndex(), aci.getEndIndex(), null);
173 }
174
175 /*************************************************************************/
176
177 /**
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>.
181   *
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.
185   */
186 public
187 AttributedString(AttributedCharacterIterator aci, int begin_index,
188                  int end_index)
189 {
190   this(aci, begin_index, end_index, null);
191 }
192
193 /*************************************************************************/
194
195 /**
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.
202   *
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.
207   */
208 public
209 AttributedString(AttributedCharacterIterator aci, int begin_index, 
210                  int end_index, AttributedCharacterIterator.Attribute[] attributes)
211 {
212   // Validate some arguments
213   if ((begin_index < 0) || (end_index < begin_index))
214     throw new IllegalArgumentException("Bad index values");
215
216   StringBuffer sb = new StringBuffer("");
217
218   // Get the valid attribute list
219   Set all_attribs = aci.getAllAttributeKeys();
220   if (attributes != null)
221     {
222       Set valid_attribs = new HashSet();
223       Iterator iter = all_attribs.iterator();
224       while (iter.hasNext())
225         {
226           Object obj = iter.next();
227
228           int i;
229           for (i = 0; i < attributes.length; i++)
230             if (obj.equals(attributes[0]))
231               break;
232
233           if (i == attributes.length)
234             continue;
235
236           valid_attribs.add(obj);
237         }
238
239       all_attribs = valid_attribs;
240     } 
241
242   // Loop through and extract the attributes
243   char c = aci.setIndex(begin_index);
244
245   do
246     { 
247       sb.append(c);
248
249       Iterator iter = all_attribs.iterator();
250       while(iter.hasNext())
251         {
252           Object obj = iter.next();
253
254           // What should we do if this is not true?
255           if (!(obj instanceof AttributedCharacterIterator.Attribute))
256             continue;
257
258           AttributedCharacterIterator.Attribute attrib = 
259             (AttributedCharacterIterator.Attribute)obj;
260
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;
268
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;
273
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)
278             {
279               if (attrib_obj instanceof Annotation)
280                  continue;
281
282               rs = begin_index;
283             }
284           else
285             {
286               rs -= begin_index;
287             }
288
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);
292
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);
298         }
299
300       c = aci.next();
301     }
302   while(c != CharacterIterator.DONE);
303
304   sci = new StringCharacterIterator(sb.toString());
305 }
306
307 /*************************************************************************/
308
309 /*
310  * Instance Methods
311  */
312
313 /**
314   * This method adds a new attribute that will cover the entire string.
315   *
316   * @param attrib The attribute to add.
317   * @param value The value of the attribute.
318   */
319 public void
320 addAttribute(AttributedCharacterIterator.Attribute attrib, Object value)
321 {
322   addAttribute(attrib, value, 0, sci.getEndIndex() - 1);
323 }
324
325 /*************************************************************************/
326
327 /**
328   * This method adds a new attribute that will cover the specified subrange
329   * of the string.
330   *
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.
335   *
336   * @exception IllegalArgumentException If attribute is <code>null</code> or the subrange is not valid.
337   */
338 public void
339 addAttribute(AttributedCharacterIterator.Attribute attrib, Object value,
340              int begin_index, int end_index)
341 {
342   if (attrib == null)
343     throw new IllegalArgumentException("null attribute");
344
345   Hashtable ht = new Hashtable();
346   ht.put(attrib, value);
347
348   addAttributes(ht, begin_index, end_index);
349 }
350
351 /*************************************************************************/
352
353 /**
354   * This method adds all of the attributes in the specified list to the
355   * specified subrange of the string.
356   *
357   * @param attributes The list of attributes.
358   * @param begin_index The beginning index.
359   * @param end_index The ending index
360   *
361   * @param IllegalArgumentException If the list is <code>null</code> or the subrange is not valid.
362   */
363 public void
364 addAttributes(Map attributes, int begin_index, int end_index)
365 {
366   if (attributes == null)
367     throw new IllegalArgumentException("null attribute");
368
369   if ((begin_index < 0) || (end_index > sci.getEndIndex()) ||
370       (end_index < begin_index))
371     throw new IllegalArgumentException("bad range");
372
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);
378
379
380 /*************************************************************************/
381
382 /**
383   * This method returns an <code>AttributedCharacterIterator</code> that 
384   * will iterate over the entire string.
385   *
386   * @return An <code>AttributedCharacterIterator</code> for the entire string.
387   */
388 public AttributedCharacterIterator
389 getIterator()
390 {
391   return(new AttributedStringIterator(sci, attribs, 0, sci.getEndIndex() - 1,
392                                       null));
393 }
394
395 /*************************************************************************/
396
397 /**
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.
403   *
404   * @param attributes A list of attributes to include in the returned iterator.
405   *
406   * @return An <code>AttributedCharacterIterator</code> for this string.
407   */
408 public AttributedCharacterIterator
409 getIterator(AttributedCharacterIterator.Attribute[] attributes)
410 {
411   return(getIterator(attributes, 0, sci.getEndIndex() - 1));
412 }
413
414 /*************************************************************************/
415
416 /**
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.  
422   *
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.
426   *
427   * @return An <code>AttributedCharacterIterator</code> for this string.
428   */
429 public AttributedCharacterIterator
430 getIterator(AttributedCharacterIterator.Attribute[] attributes, 
431             int begin_index, int end_index)
432 {
433   if ((begin_index < 0) || (end_index > sci.getEndIndex()) ||
434       (end_index < begin_index))
435     throw new IllegalArgumentException("bad range");
436
437   return(new AttributedStringIterator(sci, attribs, begin_index, end_index,
438                                       attributes));
439 }
440
441 } // class AttributedString
442