Source for gnu.javax.swing.text.html.parser.support.low.Buffer

   1: /* Buffer.java --
   2:    Copyright (C) 2005 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: 
  38: 
  39: package gnu.javax.swing.text.html.parser.support.low;
  40: 
  41: /**
  42:  * A string buffer that additionally holds line and absolute postion
  43:  * information.
  44:  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  45:  */
  46: public class Buffer
  47: {
  48:   public static int INITIAL_SIZE = 2048;
  49: 
  50:   /**
  51:    * True if the \n symbol has been seen.
  52:    */
  53:   public boolean n_seen;
  54: 
  55:   /**
  56:    * True if the \r symbol has been seen.
  57:    */
  58:   public boolean r_seen;
  59:   char[] chr = new char[ INITIAL_SIZE ];
  60:   int[] line = new int[ INITIAL_SIZE ];
  61:   int[] position = new int[ INITIAL_SIZE ];
  62: 
  63:   /**
  64:    * Current line.
  65:    */
  66:   int current_line = 0;
  67: 
  68:   /**
  69:    * Point to the next free position.
  70:    */
  71:   int length;
  72: 
  73:   public Buffer()
  74:   {
  75:   }
  76: 
  77:   public Buffer(String content)
  78:   {
  79:     for (int i = 0; i < content.length(); i++)
  80:       {
  81:         append(content.charAt(i), i);
  82:       }
  83:   }
  84: 
  85:   /**
  86:    * Get the characters into array.
  87:    * @param srcBegin From, inclusive
  88:    * @param srcEnd To, exclusive.
  89:    * @param dst Into
  90:    * @param dstBegin Offset.
  91:    */
  92:   public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
  93:   {
  94:     System.arraycopy(chr, srcBegin, dst, dstBegin, (srcEnd - srcBegin));
  95:   }
  96: 
  97:   /**
  98:    * Return the sequence, used to separate lines in the document.
  99:    * @return one of \n, \r or \r\n.
 100:    */
 101:   public String getEndOfLineSequence()
 102:   {
 103:     if (r_seen && n_seen)
 104:       return "\r\n";
 105:     else if (r_seen)
 106:       return "\r";
 107:     else
 108: 
 109:       // This also is returned for single-line document.
 110:       return "\n";
 111:   }
 112: 
 113:   /**
 114:    * Truncate.
 115:    * @param n The length to truncate till.
 116:    */
 117:   public void setLength(int n)
 118:   {
 119:     length = n;
 120:   }
 121: 
 122:   /**
 123:    * Get location information for the given region.
 124:    * @param from Region start, inclusive.
 125:    * @param to Region end, exclusive.
 126:    * @return The location, covering the region.
 127:    */
 128:   public Location getLocation(int from, int to)
 129:   {
 130:     Location l = new Location();
 131:     l.beginLine = line [ from ];
 132:     l.endLine = line [ to - 1 ];
 133: 
 134:     l.startPosition = position [ from ];
 135:     l.endPosition = position [ to - 1 ] + 1;
 136: 
 137:     return l;
 138:   }
 139: 
 140:   /**
 141:    * Add the character.
 142:    * @param c The character.
 143:    * @param pos The character position in the stream (the line number
 144:    * is handled internally in the buffer).
 145:    */
 146:   public void append(char c, int pos)
 147:   {
 148:     if (length >= chr.length)
 149:       expand();
 150:     chr [ length ] = c;
 151:     position [ length ] = pos;
 152: 
 153:     if (c == '\n')
 154:       {
 155:         if (!r_seen)
 156:           current_line++;
 157:         n_seen = true;
 158:       }
 159:     else if (c == '\r')
 160:       {
 161:         current_line++;
 162:         r_seen = true;
 163:       }
 164: 
 165:     line [ length ] = current_line;
 166: 
 167:     length++;
 168:   }
 169: 
 170:   /**
 171:    * Return char at the given positon.
 172:    */
 173:   public char charAt(int i)
 174:   {
 175:     return chr [ i ];
 176:   }
 177: 
 178:   /**
 179:    * Delete the range
 180:    * @param from Start position, inclusive.
 181:    * @param to End position, exclusive.
 182:    */
 183:   public void delete(int from, int to)
 184:   {
 185:     int len = to - from;
 186:     if (len < 1)
 187:       throw new AssertionError("Deleting " + from + " till " + to);
 188: 
 189:     int tail = length - to;
 190: 
 191:     System.arraycopy(chr, to, chr, from, tail);
 192:     System.arraycopy(position, to, position, from, tail);
 193:     System.arraycopy(line, to, line, from, tail);
 194:     length = length - len;
 195:   }
 196: 
 197:   /**
 198:    * Double the buffer size.
 199:    */
 200:   public void expand()
 201:   {
 202:     int nSize = 2 * chr.length;
 203: 
 204:     char[] nchr = new char[ nSize ];
 205:     int[] nposition = new int[ nSize ];
 206:     int[] nline = new int[ nSize ];
 207: 
 208:     System.arraycopy(chr, 0, nchr, 0, chr.length);
 209:     System.arraycopy(position, 0, nposition, 0, position.length);
 210:     System.arraycopy(line, 0, nline, 0, line.length);
 211: 
 212:     chr = nchr;
 213:     position = nposition;
 214:     line = nline;
 215:   }
 216: 
 217:   /**
 218:    * Return length of the occupied part of the buffer.
 219:    */
 220:   public int length()
 221:   {
 222:     return length;
 223:   }
 224: 
 225:   /**
 226:    * Prepare for parsing the new document.
 227:    */
 228:   public void reset()
 229:   {
 230:     setLength(0);
 231:     r_seen = n_seen = false;
 232:   }
 233: 
 234:   public String toString()
 235:   {
 236:     return new String(chr, 0, length);
 237:   }
 238: }