| 1 |
769 |
jeremybenn |
/* AttributedFormatBuffer.java -- Implements an attributed FormatBuffer.
|
| 2 |
|
|
Copyright (C) 2004 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., 51 Franklin Street, Fifth Floor, Boston, MA
|
| 19 |
|
|
02110-1301 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 |
|
|
package gnu.java.text;
|
| 38 |
|
|
|
| 39 |
|
|
import gnu.java.lang.CPStringBuilder;
|
| 40 |
|
|
|
| 41 |
|
|
import java.text.AttributedCharacterIterator;
|
| 42 |
|
|
import java.util.ArrayList;
|
| 43 |
|
|
import java.util.HashMap;
|
| 44 |
|
|
|
| 45 |
|
|
/**
|
| 46 |
|
|
* This class is an implementation of a FormatBuffer with attributes.
|
| 47 |
|
|
* Note that this class is not thread-safe; external synchronisation
|
| 48 |
|
|
* should be used if an instance is to be accessed from multiple threads.
|
| 49 |
|
|
*
|
| 50 |
|
|
* @author Guilhem Lavaux <guilhem@kaffe.org>
|
| 51 |
|
|
* @date April 10, 2004
|
| 52 |
|
|
*/
|
| 53 |
|
|
public class AttributedFormatBuffer implements FormatBuffer
|
| 54 |
|
|
{
|
| 55 |
|
|
private final CPStringBuilder buffer;
|
| 56 |
|
|
private final ArrayList ranges;
|
| 57 |
|
|
private final ArrayList attributes;
|
| 58 |
|
|
private int[] a_ranges;
|
| 59 |
|
|
private HashMap[] a_attributes;
|
| 60 |
|
|
private int startingRange;
|
| 61 |
|
|
AttributedCharacterIterator.Attribute defaultAttr;
|
| 62 |
|
|
|
| 63 |
|
|
/**
|
| 64 |
|
|
* This constructor accepts a StringBuffer. If the buffer contains
|
| 65 |
|
|
* already some characters they will not be attributed.
|
| 66 |
|
|
*/
|
| 67 |
|
|
public AttributedFormatBuffer(CPStringBuilder buffer)
|
| 68 |
|
|
{
|
| 69 |
|
|
this.buffer = new CPStringBuilder(buffer);
|
| 70 |
|
|
this.ranges = new ArrayList();
|
| 71 |
|
|
this.attributes = new ArrayList();
|
| 72 |
|
|
this.defaultAttr = null;
|
| 73 |
|
|
if (buffer.length() != 0)
|
| 74 |
|
|
{
|
| 75 |
|
|
this.startingRange = buffer.length();
|
| 76 |
|
|
addAttribute(buffer.length(), null);
|
| 77 |
|
|
}
|
| 78 |
|
|
else
|
| 79 |
|
|
this.startingRange = -1;
|
| 80 |
|
|
}
|
| 81 |
|
|
|
| 82 |
|
|
public AttributedFormatBuffer(int prebuffer)
|
| 83 |
|
|
{
|
| 84 |
|
|
this(new CPStringBuilder(prebuffer));
|
| 85 |
|
|
}
|
| 86 |
|
|
|
| 87 |
|
|
public AttributedFormatBuffer()
|
| 88 |
|
|
{
|
| 89 |
|
|
this(10);
|
| 90 |
|
|
}
|
| 91 |
|
|
|
| 92 |
|
|
/**
|
| 93 |
|
|
* This method is a helper function for formatters. Given a set of ranges
|
| 94 |
|
|
* and attributes it adds exactly one attribute for the range of characters
|
| 95 |
|
|
* comprised between the last entry in 'ranges' and the specified new range.
|
| 96 |
|
|
*
|
| 97 |
|
|
* @param new_range A new range to insert in the list.
|
| 98 |
|
|
* @param attr A new attribute to insert in the list.
|
| 99 |
|
|
*/
|
| 100 |
|
|
private final void addAttribute(int new_range, AttributedCharacterIterator.Attribute attr)
|
| 101 |
|
|
{
|
| 102 |
|
|
HashMap map;
|
| 103 |
|
|
|
| 104 |
|
|
if (attr != null)
|
| 105 |
|
|
{
|
| 106 |
|
|
map = new HashMap();
|
| 107 |
|
|
map.put(attr, attr);
|
| 108 |
|
|
attributes.add(map);
|
| 109 |
|
|
}
|
| 110 |
|
|
else
|
| 111 |
|
|
attributes.add(null);
|
| 112 |
|
|
|
| 113 |
|
|
ranges.add(new Integer(new_range));
|
| 114 |
|
|
}
|
| 115 |
|
|
|
| 116 |
|
|
public void append(String s)
|
| 117 |
|
|
{
|
| 118 |
|
|
if (startingRange < 0)
|
| 119 |
|
|
startingRange = 0;
|
| 120 |
|
|
buffer.append(s);
|
| 121 |
|
|
}
|
| 122 |
|
|
|
| 123 |
|
|
public void append(String s, AttributedCharacterIterator.Attribute attr)
|
| 124 |
|
|
{
|
| 125 |
|
|
setDefaultAttribute(attr);
|
| 126 |
|
|
startingRange = buffer.length();
|
| 127 |
|
|
append(s);
|
| 128 |
|
|
setDefaultAttribute(null);
|
| 129 |
|
|
}
|
| 130 |
|
|
|
| 131 |
|
|
public void append(String s, int[] ranges, HashMap[] attrs)
|
| 132 |
|
|
{
|
| 133 |
|
|
int curPos = buffer.length();
|
| 134 |
|
|
|
| 135 |
|
|
setDefaultAttribute(null);
|
| 136 |
|
|
if (ranges != null)
|
| 137 |
|
|
{
|
| 138 |
|
|
for (int i = 0; i < ranges.length; i++)
|
| 139 |
|
|
{
|
| 140 |
|
|
this.ranges.add(new Integer(ranges[i] + curPos));
|
| 141 |
|
|
this.attributes.add(attrs[i]);
|
| 142 |
|
|
}
|
| 143 |
|
|
}
|
| 144 |
|
|
startingRange = buffer.length();
|
| 145 |
|
|
buffer.append(s);
|
| 146 |
|
|
}
|
| 147 |
|
|
|
| 148 |
|
|
public void append(char c)
|
| 149 |
|
|
{
|
| 150 |
|
|
if (startingRange < 0)
|
| 151 |
|
|
startingRange = buffer.length();
|
| 152 |
|
|
buffer.append(c);
|
| 153 |
|
|
}
|
| 154 |
|
|
|
| 155 |
|
|
public void append(char c, AttributedCharacterIterator.Attribute attr)
|
| 156 |
|
|
{
|
| 157 |
|
|
setDefaultAttribute(attr);
|
| 158 |
|
|
buffer.append(c);
|
| 159 |
|
|
setDefaultAttribute(null);
|
| 160 |
|
|
}
|
| 161 |
|
|
|
| 162 |
|
|
public void setDefaultAttribute(AttributedCharacterIterator.Attribute attr)
|
| 163 |
|
|
{
|
| 164 |
|
|
if (attr == defaultAttr)
|
| 165 |
|
|
return;
|
| 166 |
|
|
|
| 167 |
|
|
int currentPos = buffer.length();
|
| 168 |
|
|
|
| 169 |
|
|
if (startingRange != currentPos && startingRange >= 0)
|
| 170 |
|
|
{
|
| 171 |
|
|
addAttribute(currentPos, defaultAttr);
|
| 172 |
|
|
}
|
| 173 |
|
|
defaultAttr = attr;
|
| 174 |
|
|
startingRange = currentPos;
|
| 175 |
|
|
}
|
| 176 |
|
|
|
| 177 |
|
|
public AttributedCharacterIterator.Attribute getDefaultAttribute()
|
| 178 |
|
|
{
|
| 179 |
|
|
return defaultAttr;
|
| 180 |
|
|
}
|
| 181 |
|
|
|
| 182 |
|
|
public void cutTail(int length)
|
| 183 |
|
|
{
|
| 184 |
|
|
buffer.setLength(buffer.length()-length);
|
| 185 |
|
|
}
|
| 186 |
|
|
|
| 187 |
|
|
public int length()
|
| 188 |
|
|
{
|
| 189 |
|
|
return buffer.length();
|
| 190 |
|
|
}
|
| 191 |
|
|
|
| 192 |
|
|
public void clear()
|
| 193 |
|
|
{
|
| 194 |
|
|
buffer.setLength(0);
|
| 195 |
|
|
ranges.clear();
|
| 196 |
|
|
attributes.clear();
|
| 197 |
|
|
defaultAttr = null;
|
| 198 |
|
|
startingRange = -1;
|
| 199 |
|
|
}
|
| 200 |
|
|
|
| 201 |
|
|
/**
|
| 202 |
|
|
* This method synchronizes the state of the attribute array.
|
| 203 |
|
|
* After calling it you may call {@link #getDefaultAttribute()}.
|
| 204 |
|
|
*/
|
| 205 |
|
|
public void sync()
|
| 206 |
|
|
{
|
| 207 |
|
|
if (startingRange < 0 || startingRange == buffer.length())
|
| 208 |
|
|
return;
|
| 209 |
|
|
|
| 210 |
|
|
addAttribute(buffer.length(), defaultAttr);
|
| 211 |
|
|
|
| 212 |
|
|
a_ranges = new int[ranges.size()];
|
| 213 |
|
|
for (int i = 0; i < a_ranges.length; i++)
|
| 214 |
|
|
a_ranges[i] = ((Integer)(ranges.get (i))).intValue();
|
| 215 |
|
|
|
| 216 |
|
|
a_attributes = new HashMap[attributes.size()];
|
| 217 |
|
|
System.arraycopy(attributes.toArray(), 0, a_attributes, 0, a_attributes.length);
|
| 218 |
|
|
}
|
| 219 |
|
|
|
| 220 |
|
|
/**
|
| 221 |
|
|
* This method returns the internal CPStringBuilder describing
|
| 222 |
|
|
* the attributed string.
|
| 223 |
|
|
*
|
| 224 |
|
|
* @return An instance of CPStringBuilder which contains the string.
|
| 225 |
|
|
*/
|
| 226 |
|
|
public CPStringBuilder getBuffer()
|
| 227 |
|
|
{
|
| 228 |
|
|
return buffer;
|
| 229 |
|
|
}
|
| 230 |
|
|
|
| 231 |
|
|
/**
|
| 232 |
|
|
* This method returns the ranges for the attributes.
|
| 233 |
|
|
*
|
| 234 |
|
|
* @return An array of int describing the ranges.
|
| 235 |
|
|
*/
|
| 236 |
|
|
public int[] getRanges()
|
| 237 |
|
|
{
|
| 238 |
|
|
return a_ranges;
|
| 239 |
|
|
}
|
| 240 |
|
|
|
| 241 |
|
|
/**
|
| 242 |
|
|
* This method returns the array containing the map on the
|
| 243 |
|
|
* attributes.
|
| 244 |
|
|
*
|
| 245 |
|
|
* @return An array of {@link java.util.Map} containing the attributes.
|
| 246 |
|
|
*/
|
| 247 |
|
|
public HashMap[] getAttributes()
|
| 248 |
|
|
{
|
| 249 |
|
|
return a_attributes;
|
| 250 |
|
|
}
|
| 251 |
|
|
}
|