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

   1: /* Token.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 token.
  43:  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  44:  */
  45: public class Token
  46: {
  47:   /**
  48:    * The place of this token in the document.
  49:    */
  50:   public Location where;
  51: 
  52:   /**
  53:    * The additional category of token.
  54:    */
  55:   public int category;
  56: 
  57:   /**
  58:    * An integer that describes the kind of this token.
  59:    */
  60:   public int kind;
  61: 
  62:   /**
  63:    * The string image of the token, null if the char image must be used.
  64:    */
  65:   private String stringImage;
  66: 
  67:   /**
  68:    * The char image of the token.
  69:    */
  70:   private char charImage;
  71: 
  72:   /**
  73:    * Creates a new token with fields, initialized to the default values.
  74:    */
  75:   public Token()
  76:   {
  77:   }
  78: 
  79:   /**
  80:    * Creates a new token of the given kind.
  81:    */
  82:   public Token(int _kind, Location _where)
  83:   {
  84:     kind = _kind;
  85:     where = _where;
  86:   }
  87: 
  88:   /**
  89:    * Creates a new token of the given kind and given single char image.
  90:    */
  91:   public Token(int _kind, char _image, Location _where)
  92:   {
  93:     kind = _kind;
  94:     charImage = _image;
  95:     where = _where;
  96:   }
  97: 
  98:   /**
  99:    * Creates a new token of the given kind and given string image.
 100:    */
 101:   public Token(int _kind, String _image, Location _where)
 102:   {
 103:     kind = _kind;
 104:     stringImage = _image;
 105:     where = _where;
 106:   }
 107: 
 108:   /**
 109:    * Creates a new token of the given kind, category and given string image.
 110:    */
 111:   public Token(int _kind, int _category, String _image, Location _where)
 112:   {
 113:     kind = _kind;
 114:     category = _category;
 115:     stringImage = _image;
 116:     where = _where;
 117:   }
 118: 
 119:   /**
 120:    * Creates a new token, where location fields are set as for token,
 121:    * spanning over two provided tokens and any tokens between them.
 122:    * The image field is initialized to null, the kind field is set to -1.
 123:    */
 124:   public Token(Token fromInclusive, Token toInclusive)
 125:   {
 126:     where = new Location();
 127:     where.beginLine = fromInclusive.where.beginLine;
 128:     where.startPosition = fromInclusive.where.startPosition;
 129: 
 130:     where.endLine = toInclusive.where.endLine;
 131:     where.endPosition = toInclusive.where.endPosition;
 132:   }
 133: 
 134:   public String getImage()
 135:   {
 136:     if (kind == 3)
 137:       return "#";
 138:     if (stringImage == null)
 139:       {
 140:         if (charImage == 0)
 141:           return null;
 142:         stringImage = new String(new char[] { charImage });
 143:       }
 144:     return stringImage;
 145:   }
 146: 
 147:   /**
 148:    * Append the token image to the given string buffer.
 149:    * This may be more effective that buffer.append(this.getImage()).
 150:    * @param buffer A buffer to append.
 151:    */
 152:   public void appendTo(StringBuffer buffer)
 153:   {
 154:     if (charImage == 0)
 155:       buffer.append(getImage());
 156:     else
 157:       buffer.append(charImage);
 158:   }
 159: 
 160:   /**
 161:    * Returns the string image or, if null, the bounding positions.
 162:    */
 163:   public String toString()
 164:   {
 165:     return getImage() != null ? kind + "'" + getImage()
 166:            : "<line " + where.beginLine + ", abs pos " + where.startPosition +
 167:            ".." + where.endPosition + ">";
 168:   }
 169: }