Entity.java revision 11099:678faa7d1a6a
1/*
2 * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package javax.swing.text.html.parser;
27
28import java.util.Hashtable;
29import java.io.IOException;
30import java.io.InputStream;
31import java.io.InputStreamReader;
32import java.io.Reader;
33import java.io.CharArrayReader;
34import java.net.URL;
35
36/**
37 * An entity is described in a DTD using the ENTITY construct.
38 * It defines the type and value of the entity.
39 *
40 * @see DTD
41 * @author Arthur van Hoff
42 */
43public final
44class Entity implements DTDConstants {
45    /**
46     * The name of the entity.
47     */
48    public String name;
49
50    /**
51     * The type of the entity.
52     */
53    public int type;
54
55    /**
56     * The char array of data.
57     */
58    public char data[];
59
60    /**
61     * Creates an entity.
62     * @param name the name of the entity
63     * @param type the type of the entity
64     * @param data the char array of data
65     */
66    public Entity(String name, int type, char data[]) {
67        this.name = name;
68        this.type = type;
69        this.data = data;
70    }
71
72    /**
73     * Gets the name of the entity.
74     * @return the name of the entity, as a <code>String</code>
75     */
76    public String getName() {
77        return name;
78    }
79
80    /**
81     * Gets the type of the entity.
82     * @return the type of the entity
83     */
84    public int getType() {
85        return type & 0xFFFF;
86    }
87
88    /**
89     * Returns <code>true</code> if it is a parameter entity.
90     * @return <code>true</code> if it is a parameter entity
91     */
92    public boolean isParameter() {
93        return (type & PARAMETER) != 0;
94    }
95
96    /**
97     * Returns <code>true</code> if it is a general entity.
98     * @return <code>true</code> if it is a general entity
99     */
100    public boolean isGeneral() {
101        return (type & GENERAL) != 0;
102    }
103
104    /**
105     * Returns the <code>data</code>.
106     * @return the <code>data</code>
107     */
108    public char getData()[] {
109        return data;
110    }
111
112    /**
113     * Returns the data as a <code>String</code>.
114     * @return the data as a <code>String</code>
115     */
116    public String getString() {
117        return new String(data, 0, data.length);
118    }
119
120
121    static Hashtable<String, Integer> entityTypes = new Hashtable<String, Integer>();
122
123    static {
124        entityTypes.put("PUBLIC", Integer.valueOf(PUBLIC));
125        entityTypes.put("CDATA", Integer.valueOf(CDATA));
126        entityTypes.put("SDATA", Integer.valueOf(SDATA));
127        entityTypes.put("PI", Integer.valueOf(PI));
128        entityTypes.put("STARTTAG", Integer.valueOf(STARTTAG));
129        entityTypes.put("ENDTAG", Integer.valueOf(ENDTAG));
130        entityTypes.put("MS", Integer.valueOf(MS));
131        entityTypes.put("MD", Integer.valueOf(MD));
132        entityTypes.put("SYSTEM", Integer.valueOf(SYSTEM));
133    }
134
135    /**
136     * Converts <code>nm</code> string to the corresponding
137     * entity type.  If the string does not have a corresponding
138     * entity type, returns the type corresponding to "CDATA".
139     * Valid entity types are: "PUBLIC", "CDATA", "SDATA", "PI",
140     * "STARTTAG", "ENDTAG", "MS", "MD", "SYSTEM".
141     *
142     * @param nm the string to be converted
143     * @return the corresponding entity type, or the type corresponding
144     *   to "CDATA", if none exists
145     */
146    public static int name2type(String nm) {
147        Integer i = entityTypes.get(nm);
148        return (i == null) ? CDATA : i.intValue();
149    }
150}
151