001/* Entity.java -- Stores information, obtained by parsing SGML DTL
002 * <!ENTITY % .. > tag
003   Copyright (C) 2005 Free Software Foundation, Inc.
004
005This file is part of GNU Classpath.
006
007GNU Classpath is free software; you can redistribute it and/or modify
008it under the terms of the GNU General Public License as published by
009the Free Software Foundation; either version 2, or (at your option)
010any later version.
011
012GNU Classpath is distributed in the hope that it will be useful, but
013WITHOUT ANY WARRANTY; without even the implied warranty of
014MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015General Public License for more details.
016
017You should have received a copy of the GNU General Public License
018along with GNU Classpath; see the file COPYING.  If not, write to the
019Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02002110-1301 USA.
021
022Linking this library statically or dynamically with other modules is
023making a combined work based on this library.  Thus, the terms and
024conditions of the GNU General Public License cover the whole
025combination.
026
027As a special exception, the copyright holders of this library give you
028permission to link this library with independent modules to produce an
029executable, regardless of the license terms of these independent
030modules, and to copy and distribute the resulting executable under
031terms of your choice, provided that you also meet, for each linked
032independent module, the terms and conditions of the license of that
033module.  An independent module is a module which is not derived from
034or based on this library.  If you modify this library, you may extend
035this exception to your version of the library, but you are not
036obligated to do so.  If you do not wish to do so, delete this
037exception statement from your version. */
038
039
040package javax.swing.text.html.parser;
041
042import gnu.javax.swing.text.html.parser.support.gnuStringIntMapper;
043
044/**
045 * <p>Stores information, obtained by parsing SGML DTL
046 * &lt;!ENTITY % .. &gt; tag.</p>
047 * <p>
048 * The entity defines some kind of macro that can be used elsewhere in
049 * the document.
050 * When the macro is referred to by the name in the DTD, it is expanded into
051 * a string
052 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
053 */
054public final class Entity
055  implements DTDConstants
056{
057  /**
058   * Package level mapper between type names and they string values.
059   */
060  final static gnuStringIntMapper mapper =
061    new gnuStringIntMapper()
062    {
063      protected void create()
064      {
065        add("ANY", DTDConstants.ANY);
066        add("CDATA", DTDConstants.CDATA);
067        add("PUBLIC", DTDConstants.PUBLIC);
068        add("SDATA", DTDConstants.SDATA);
069        add("PI", DTDConstants.PI);
070        add("STARTTAG", DTDConstants.STARTTAG);
071        add("ENDTAG", DTDConstants.ENDTAG);
072        add("MS", DTDConstants.MS);
073        add("MD", DTDConstants.MD);
074        add("SYSTEM", DTDConstants.SYSTEM);
075      }
076    };
077
078  /**
079   * The entity name.
080   */
081  public String name;
082
083  /**
084   * The entity data
085   */
086  public char[] data;
087
088  /**
089   *  The entity type.
090   */
091  public int type;
092
093  /**
094   * String representation of the entity data.
095   */
096  private String sdata;
097
098  /**
099   * Create a new entity
100   * @param a_name the entity name
101   * @param a_type the entity type
102   * @param a_data the data replacing the entity reference
103   */
104  public Entity(String a_name, int a_type, char[] a_data)
105  {
106    name = a_name;
107    type = a_type;
108    data = a_data;
109  }
110
111  /**
112   * Converts a given string to the corresponding entity type.
113   * @return a value, defined in DTDConstants (one of
114   * PUBLIC, CDATA, SDATA, PI, STARTTAG, ENDTAG, MS, MD, SYSTEM)
115   * or CDATA if the parameter is not a valid entity type.
116   */
117  public static int name2type(String an_entity)
118  {
119    int r = mapper.get(an_entity);
120    return (r == 0) ? DTDConstants.CDATA : r;
121  }
122
123  /**
124   * Get the entity data.
125   */
126  public char[] getData()
127  {
128    return data;
129  }
130
131  /**
132   * Returns true for general entities. Each general entity can be
133   * referenced as <code>&entity-name;</code>. Such entities are
134   * defined by the SGML DTD tag
135   * <code>&lt;!ENTITY <i>name</i>    "<i>value</i>"></code>. The general
136   * entities can be used anywhere in the document.
137   */
138  public boolean isGeneral()
139  {
140    return (type & DTDConstants.GENERAL) != 0;
141  }
142
143  /**
144   * Get the entity name.
145   */
146  public String getName()
147  {
148    return name;
149  }
150
151  /**
152   * Returns true for parameter entities. Each parameter entity can be
153   * referenced as <code>&entity-name;</code>. Such entities are
154   * defined by the SGML DTD tag
155   * <code>&lt;!ENTITY % <i>name</i>    "<i>value</i>"></code>. The parameter
156   * entities can be used only in SGML context.
157   */
158  public boolean isParameter()
159  {
160    return (type & DTDConstants.PARAMETER) != 0;
161  }
162
163  /**
164   * Returns a data as String
165   */
166  public String getString()
167  {
168    if (sdata == null)
169      sdata = new String(data);
170
171    return sdata;
172  }
173  
174  /**
175   * Get the entity type.
176   * @return the value of the {@link #type}.
177   */
178  public int getType() 
179  {
180    return type;
181  }  
182          
183}