Source for javax.swing.text.html.parser.Entity

   1: /* Entity.java -- Stores information, obtained by parsing SGML DTL
   2:  * <!ENTITY % .. > tag
   3:    Copyright (C) 2005 Free Software Foundation, Inc.
   4: 
   5: This file is part of GNU Classpath.
   6: 
   7: GNU Classpath is free software; you can redistribute it and/or modify
   8: it under the terms of the GNU General Public License as published by
   9: the Free Software Foundation; either version 2, or (at your option)
  10: any later version.
  11: 
  12: GNU Classpath is distributed in the hope that it will be useful, but
  13: WITHOUT ANY WARRANTY; without even the implied warranty of
  14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15: General Public License for more details.
  16: 
  17: You should have received a copy of the GNU General Public License
  18: along with GNU Classpath; see the file COPYING.  If not, write to the
  19: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20: 02110-1301 USA.
  21: 
  22: Linking this library statically or dynamically with other modules is
  23: making a combined work based on this library.  Thus, the terms and
  24: conditions of the GNU General Public License cover the whole
  25: combination.
  26: 
  27: As a special exception, the copyright holders of this library give you
  28: permission to link this library with independent modules to produce an
  29: executable, regardless of the license terms of these independent
  30: modules, and to copy and distribute the resulting executable under
  31: terms of your choice, provided that you also meet, for each linked
  32: independent module, the terms and conditions of the license of that
  33: module.  An independent module is a module which is not derived from
  34: or based on this library.  If you modify this library, you may extend
  35: this exception to your version of the library, but you are not
  36: obligated to do so.  If you do not wish to do so, delete this
  37: exception statement from your version. */
  38: 
  39: 
  40: package javax.swing.text.html.parser;
  41: 
  42: import gnu.javax.swing.text.html.parser.support.gnuStringIntMapper;
  43: 
  44: import java.io.Serializable;
  45: 
  46: /**
  47:  * <p>Stores information, obtained by parsing SGML DTL
  48:  * &lt;!ENTITY % .. &gt; tag.</p>
  49:  * <p>
  50:  * The entity defines some kind of macro that can be used elsewhere in
  51:  * the document.
  52:  * When the macro is referred to by the name in the DTD, it is expanded into
  53:  * a string
  54:  * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  55:  */
  56: public final class Entity
  57:   implements DTDConstants
  58: {
  59:   /**
  60:    * Package level mapper between type names and they string values.
  61:    */
  62:   final static gnuStringIntMapper mapper =
  63:     new gnuStringIntMapper()
  64:     {
  65:       protected void create()
  66:       {
  67:         add("ANY", DTDConstants.ANY);
  68:         add("CDATA", DTDConstants.CDATA);
  69:         add("PUBLIC", DTDConstants.PUBLIC);
  70:         add("SDATA", DTDConstants.SDATA);
  71:         add("PI", DTDConstants.PI);
  72:         add("STARTTAG", DTDConstants.STARTTAG);
  73:         add("ENDTAG", DTDConstants.ENDTAG);
  74:         add("MS", DTDConstants.MS);
  75:         add("MD", DTDConstants.MD);
  76:         add("SYSTEM", DTDConstants.SYSTEM);
  77:       }
  78:     };
  79: 
  80:   /**
  81:    * The entity name.
  82:    */
  83:   public String name;
  84: 
  85:   /**
  86:    * The entity data
  87:    */
  88:   public char[] data;
  89: 
  90:   /**
  91:    *  The entity type.
  92:    */
  93:   public int type;
  94: 
  95:   /**
  96:    * String representation of the entity data.
  97:    */
  98:   private String sdata;
  99: 
 100:   /**
 101:    * Create a new entity
 102:    * @param a_name the entity name
 103:    * @param a_type the entity type
 104:    * @param a_data the data replacing the entity reference
 105:    */
 106:   public Entity(String a_name, int a_type, char[] a_data)
 107:   {
 108:     name = a_name;
 109:     type = a_type;
 110:     data = a_data;
 111:   }
 112: 
 113:   /**
 114:    * Converts a given string to the corresponding entity type.
 115:    * @return a value, defined in DTDConstants (one of
 116:    * PUBLIC, CDATA, SDATA, PI, STARTTAG, ENDTAG, MS, MD, SYSTEM)
 117:    * or CDATA if the parameter is not a valid entity type.
 118:    */
 119:   public static int name2type(String an_entity)
 120:   {
 121:     int r = mapper.get(an_entity);
 122:     return (r == 0) ? DTDConstants.CDATA : r;
 123:   }
 124: 
 125:   /**
 126:    * Get the entity data.
 127:    */
 128:   public char[] getData()
 129:   {
 130:     return data;
 131:   }
 132: 
 133:   /**
 134:    * Returns true for general entities. Each general entity can be
 135:    * referenced as <code>&entity-name;</code>. Such entities are
 136:    * defined by the SGML DTD tag
 137:    * <code>&lt;!ENTITY <i>name</i>    "<i>value</i>"></code>. The general
 138:    * entities can be used anywhere in the document.
 139:    */
 140:   public boolean isGeneral()
 141:   {
 142:     return (type & DTDConstants.GENERAL) != 0;
 143:   }
 144: 
 145:   /**
 146:    * Get the entity name.
 147:    */
 148:   public String getName()
 149:   {
 150:     return name;
 151:   }
 152: 
 153:   /**
 154:    * Returns true for parameter entities. Each parameter entity can be
 155:    * referenced as <code>&entity-name;</code>. Such entities are
 156:    * defined by the SGML DTD tag
 157:    * <code>&lt;!ENTITY % <i>name</i>    "<i>value</i>"></code>. The parameter
 158:    * entities can be used only in SGML context.
 159:    */
 160:   public boolean isParameter()
 161:   {
 162:     return (type & DTDConstants.PARAMETER) != 0;
 163:   }
 164: 
 165:   /**
 166:    * Returns a data as String
 167:    */
 168:   public String getString()
 169:   {
 170:     if (sdata == null)
 171:       sdata = new String(data);
 172: 
 173:     return sdata;
 174:   }
 175:   
 176:   /**
 177:    * Get the entity type.
 178:    * @return the value of the {@link #type}.
 179:    */
 180:   public int getType() 
 181:   {
 182:     return type;
 183:   }  
 184:           
 185: }