Source for gnu.java.text.AttributedFormatBuffer

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