jdk/src/share/classes/javax/swing/text/html/parser/Entity.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 438 2ae294e4518c
child 1299 027d966d5658
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 438
diff changeset
     2
 * Copyright 1998-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.text.html.parser;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InputStreamReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.Reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.CharArrayReader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * An entity is described in a DTD using the ENTITY construct.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * It defines the type and value of the the entity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see DTD
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
class Entity implements DTDConstants {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public int type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public char data[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * Creates an entity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * @param name the name of the entity
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * @param type the type of the entity
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * @param data the char array of data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public Entity(String name, int type, char data[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.data = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Gets the name of the entity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @return the name of the entity, as a <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Gets the type of the entity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @return the type of the entity
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public int getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return type & 0xFFFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Returns <code>true</code> if it is a parameter entity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @return <code>true</code> if it is a parameter entity
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public boolean isParameter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return (type & PARAMETER) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Returns <code>true</code> if it is a general entity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @return <code>true</code> if it is a general entity
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public boolean isGeneral() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return (type & GENERAL) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Returns the <code>data</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @return the <code>data</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public char getData()[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Returns the data as a <code>String</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @return the data as a <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public String getString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return new String(data, 0, data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    static Hashtable entityTypes = new Hashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    static {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   113
        entityTypes.put("PUBLIC", Integer.valueOf(PUBLIC));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   114
        entityTypes.put("CDATA", Integer.valueOf(CDATA));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   115
        entityTypes.put("SDATA", Integer.valueOf(SDATA));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   116
        entityTypes.put("PI", Integer.valueOf(PI));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   117
        entityTypes.put("STARTTAG", Integer.valueOf(STARTTAG));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   118
        entityTypes.put("ENDTAG", Integer.valueOf(ENDTAG));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   119
        entityTypes.put("MS", Integer.valueOf(MS));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   120
        entityTypes.put("MD", Integer.valueOf(MD));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   121
        entityTypes.put("SYSTEM", Integer.valueOf(SYSTEM));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Converts <code>nm</code> string to the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * entity type.  If the string does not have a corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * entity type, returns the type corresponding to "CDATA".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Valid entity types are: "PUBLIC", "CDATA", "SDATA", "PI",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * "STARTTAG", "ENDTAG", "MS", "MD", "SYSTEM".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param nm the string to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @return the corresponding entity type, or the type corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *   to "CDATA", if none exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public static int name2type(String nm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        Integer i = (Integer)entityTypes.get(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return (i == null) ? CDATA : i.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
}