Source for gnu.java.text.AttributedFormatBuffer

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