1 /* AttributedString.java -- Models text with attributes
2 Copyright (C) 1998, 1999, 2004, 2005, 2006, 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., 51 Franklin Street, Fifth Floor, 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.ArrayList;
42 import java.util.Arrays;
43 import java.util.HashMap;
44 import java.util.Hashtable;
45 import java.util.Iterator;
50 * This class models a <code>String</code> with attributes over various
51 * subranges of the string. It allows applications to access this
52 * information via the <code>AttributedCharacterIterator</code> interface.
56 * @author Aaron M. Renn (arenn@urbanophile.com)
59 public class AttributedString
63 * The attributes and ranges of text over which those attributes apply.
65 final class AttributeRange
68 /** A Map of the attributes */
71 /** The beginning index of the attributes */
74 /** The ending index of the attributes */
78 * Creates a new attribute range.
80 * @param attribs the attributes.
81 * @param beginIndex the start index.
82 * @param endIndex the end index.
84 AttributeRange(Map attribs, int beginIndex, int endIndex)
86 this.attribs = attribs;
87 this.beginIndex = beginIndex;
88 this.endIndex = endIndex;
91 } // Inner class AttributeRange
93 /** The string we are representing. */
94 private StringCharacterIterator sci;
96 /** The attribute information */
97 private AttributeRange[] attribs;
100 * Creates a new instance of <code>AttributedString</code>
101 * that represents the specified <code>String</code> with no attributes.
103 * @param str The <code>String</code> to be attributed (<code>null</code> not
106 * @throws NullPointerException if <code>str</code> is <code>null</code>.
108 public AttributedString(String str)
110 sci = new StringCharacterIterator(str);
111 attribs = new AttributeRange[0];
115 * Creates a new instance of <code>AttributedString</code>
116 * that represents that specified <code>String</code> with the specified
117 * attributes over the entire length of the <code>String</code>.
119 * @param str The <code>String</code> to be attributed.
120 * @param attributes The attribute list.
122 public AttributedString(String str,
123 Map<? extends AttributedCharacterIterator.Attribute, ?> attributes)
127 attribs = new AttributeRange[1];
128 attribs[0] = new AttributeRange(attributes, 0, str.length());
132 * Initializes a new instance of <code>AttributedString</code>
133 * that will use the text and attribute information from the specified
134 * <code>AttributedCharacterIterator</code>.
136 * @param aci The <code>AttributedCharacterIterator</code> containing the
137 * text and attribute information (<code>null</code> not
140 * @throws NullPointerException if <code>aci</code> is <code>null</code>.
142 public AttributedString(AttributedCharacterIterator aci)
144 this(aci, aci.getBeginIndex(), aci.getEndIndex(), null);
148 * Initializes a new instance of <code>AttributedString</code>
149 * that will use the text and attribute information from the specified
150 * subrange of the specified <code>AttributedCharacterIterator</code>.
152 * @param aci The <code>AttributedCharacterIterator</code> containing the
153 * text and attribute information.
154 * @param beginIndex The beginning index of the text subrange.
155 * @param endIndex The ending index of the text subrange.
157 public AttributedString(AttributedCharacterIterator aci, int beginIndex,
160 this(aci, beginIndex, endIndex, null);
164 * Initializes a new instance of <code>AttributedString</code>
165 * that will use the text and attribute information from the specified
166 * subrange of the specified <code>AttributedCharacterIterator</code>.
167 * Only attributes from the source iterator that are present in the
168 * specified array of attributes will be included in the attribute list
171 * @param aci The <code>AttributedCharacterIterator</code> containing the
172 * text and attribute information.
173 * @param begin The beginning index of the text subrange.
174 * @param end The ending index of the text subrange.
175 * @param attributes A list of attributes to include from the iterator, or
176 * <code>null</code> to include all attributes.
178 public AttributedString(AttributedCharacterIterator aci, int begin, int end,
179 AttributedCharacterIterator.Attribute[] attributes)
181 // Validate some arguments
182 if ((begin < 0) || (end < begin) || end > aci.getEndIndex())
183 throw new IllegalArgumentException("Bad index values");
185 StringBuffer sb = new StringBuffer("");
187 // Get the valid attribute list
188 Set allAttribs = aci.getAllAttributeKeys();
189 if (attributes != null)
190 allAttribs.retainAll(Arrays.asList(attributes));
192 // Loop through and extract the attributes
193 char c = aci.setIndex(begin);
195 ArrayList accum = new ArrayList();
200 Iterator iter = allAttribs.iterator();
201 while(iter.hasNext())
203 Object obj = iter.next();
205 // What should we do if this is not true?
206 if (!(obj instanceof AttributedCharacterIterator.Attribute))
209 AttributedCharacterIterator.Attribute attrib =
210 (AttributedCharacterIterator.Attribute)obj;
212 // Make sure the attribute is defined.
213 Object attribObj = aci.getAttribute(attrib);
214 if (attribObj == null)
216 int rl = aci.getRunLimit(attrib);
221 // Check to see if we already processed this one
222 int rs = aci.getRunStart(attrib);
223 if ((rs < aci.getIndex()) && (aci.getIndex() != begin))
226 // If the attribute run starts before the beginning index, we
227 // need to junk it if it is an Annotation.
231 if (attribObj instanceof Annotation)
237 // Create a map object. Yes this will only contain one attribute
238 Map newMap = new Hashtable();
239 newMap.put(attrib, attribObj);
241 // Add it to the attribute list.
242 accum.add(new AttributeRange(newMap, rs, rl));
247 while( aci.getIndex() < end );
249 attribs = new AttributeRange[accum.size()];
250 attribs = (AttributeRange[]) accum.toArray(attribs);
252 sci = new StringCharacterIterator(sb.toString());
256 * Adds a new attribute that will cover the entire string.
258 * @param attrib The attribute to add.
259 * @param value The value of the attribute.
261 public void addAttribute(AttributedCharacterIterator.Attribute attrib,
264 addAttribute(attrib, value, 0, sci.getEndIndex());
268 * Adds a new attribute that will cover the specified subrange
271 * @param attrib The attribute to add.
272 * @param value The value of the attribute, which may be <code>null</code>.
273 * @param begin The beginning index of the subrange.
274 * @param end The ending index of the subrange.
276 * @exception IllegalArgumentException If attribute is <code>null</code> or
277 * the subrange is not valid.
279 public void addAttribute(AttributedCharacterIterator.Attribute attrib,
280 Object value, int begin, int end)
283 throw new IllegalArgumentException("null attribute");
285 throw new IllegalArgumentException("Requires end > begin");
286 HashMap hm = new HashMap();
287 hm.put(attrib, value);
289 addAttributes(hm, begin, end);
293 * Adds all of the attributes in the specified list to the
294 * specified subrange of the string.
296 * @param attributes The list of attributes.
297 * @param beginIndex The beginning index.
298 * @param endIndex The ending index
300 * @throws NullPointerException if <code>attributes</code> is
302 * @throws IllegalArgumentException if the subrange is not valid.
304 public void addAttributes(Map<? extends AttributedCharacterIterator.Attribute, ?> attributes,
305 int beginIndex, int endIndex)
307 if (attributes == null)
308 throw new NullPointerException("null attribute");
310 if ((beginIndex < 0) || (endIndex > sci.getEndIndex()) ||
311 (endIndex <= beginIndex))
312 throw new IllegalArgumentException("bad range");
314 AttributeRange[] new_list = new AttributeRange[attribs.length + 1];
315 System.arraycopy(attribs, 0, new_list, 0, attribs.length);
317 attribs[attribs.length - 1] = new AttributeRange(attributes, beginIndex,
322 * Returns an <code>AttributedCharacterIterator</code> that
323 * will iterate over the entire string.
325 * @return An <code>AttributedCharacterIterator</code> for the entire string.
327 public AttributedCharacterIterator getIterator()
329 return(new AttributedStringIterator(sci, attribs, 0, sci.getEndIndex(),
334 * Returns an <code>AttributedCharacterIterator</code> that
335 * will iterate over the entire string. This iterator will return information
336 * about the list of attributes in the specified array. Attributes not in
337 * the array may or may not be returned by the iterator. If the specified
338 * array is <code>null</code>, all attributes will be returned.
340 * @param attributes A list of attributes to include in the returned iterator.
342 * @return An <code>AttributedCharacterIterator</code> for this string.
344 public AttributedCharacterIterator getIterator(
345 AttributedCharacterIterator.Attribute[] attributes)
347 return(getIterator(attributes, 0, sci.getEndIndex()));
351 * Returns an <code>AttributedCharacterIterator</code> that
352 * will iterate over the specified subrange. This iterator will return
353 * information about the list of attributes in the specified array.
354 * Attributes not in the array may or may not be returned by the iterator.
355 * If the specified array is <code>null</code>, all attributes will be
358 * @param attributes A list of attributes to include in the returned iterator.
359 * @param beginIndex The beginning index of the subrange.
360 * @param endIndex The ending index of the subrange.
362 * @return An <code>AttributedCharacterIterator</code> for this string.
364 public AttributedCharacterIterator getIterator(
365 AttributedCharacterIterator.Attribute[] attributes,
366 int beginIndex, int endIndex)
368 if ((beginIndex < 0) || (endIndex > sci.getEndIndex()) ||
369 (endIndex < beginIndex))
370 throw new IllegalArgumentException("bad range");
372 return(new AttributedStringIterator(sci, attribs, beginIndex, endIndex,
376 } // class AttributedString