001    /* Entity.java -- Stores information, obtained by parsing SGML DTL
002     * <!ENTITY % .. > tag
003       Copyright (C) 2005 Free Software Foundation, Inc.
004    
005    This file is part of GNU Classpath.
006    
007    GNU Classpath is free software; you can redistribute it and/or modify
008    it under the terms of the GNU General Public License as published by
009    the Free Software Foundation; either version 2, or (at your option)
010    any later version.
011    
012    GNU Classpath is distributed in the hope that it will be useful, but
013    WITHOUT ANY WARRANTY; without even the implied warranty of
014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015    General Public License for more details.
016    
017    You should have received a copy of the GNU General Public License
018    along with GNU Classpath; see the file COPYING.  If not, write to the
019    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
020    02110-1301 USA.
021    
022    Linking this library statically or dynamically with other modules is
023    making a combined work based on this library.  Thus, the terms and
024    conditions of the GNU General Public License cover the whole
025    combination.
026    
027    As a special exception, the copyright holders of this library give you
028    permission to link this library with independent modules to produce an
029    executable, regardless of the license terms of these independent
030    modules, and to copy and distribute the resulting executable under
031    terms of your choice, provided that you also meet, for each linked
032    independent module, the terms and conditions of the license of that
033    module.  An independent module is a module which is not derived from
034    or based on this library.  If you modify this library, you may extend
035    this exception to your version of the library, but you are not
036    obligated to do so.  If you do not wish to do so, delete this
037    exception statement from your version. */
038    
039    
040    package javax.swing.text.html.parser;
041    
042    import gnu.javax.swing.text.html.parser.support.gnuStringIntMapper;
043    
044    import java.io.Serializable;
045    
046    /**
047     * <p>Stores information, obtained by parsing SGML DTL
048     * &lt;!ENTITY % .. &gt; tag.</p>
049     * <p>
050     * The entity defines some kind of macro that can be used elsewhere in
051     * the document.
052     * When the macro is referred to by the name in the DTD, it is expanded into
053     * a string
054     * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
055     */
056    public final class Entity
057      implements DTDConstants
058    {
059      /**
060       * Package level mapper between type names and they string values.
061       */
062      final static gnuStringIntMapper mapper =
063        new gnuStringIntMapper()
064        {
065          protected void create()
066          {
067            add("ANY", DTDConstants.ANY);
068            add("CDATA", DTDConstants.CDATA);
069            add("PUBLIC", DTDConstants.PUBLIC);
070            add("SDATA", DTDConstants.SDATA);
071            add("PI", DTDConstants.PI);
072            add("STARTTAG", DTDConstants.STARTTAG);
073            add("ENDTAG", DTDConstants.ENDTAG);
074            add("MS", DTDConstants.MS);
075            add("MD", DTDConstants.MD);
076            add("SYSTEM", DTDConstants.SYSTEM);
077          }
078        };
079    
080      /**
081       * The entity name.
082       */
083      public String name;
084    
085      /**
086       * The entity data
087       */
088      public char[] data;
089    
090      /**
091       *  The entity type.
092       */
093      public int type;
094    
095      /**
096       * String representation of the entity data.
097       */
098      private String sdata;
099    
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    }