jdk/src/share/classes/javax/swing/text/html/parser/Element.java
author darcy
Sun, 23 Mar 2014 13:49:48 -0700
changeset 23697 e556a715949f
parent 23010 6dadb192ad81
child 24494 67129b9360d2
permissions -rw-r--r--
8034169: Fix serial lint warnings in javax.swing Reviewed-by: alanb, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
     2
 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1299
diff changeset
    23
 * questions.
2
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.util.BitSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
18764
12a1993f109b 8017492: Static field in HTML parser affects all applications
malenkov
parents: 5506
diff changeset
    31
import sun.awt.AppContext;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * An element as described in a DTD using the ELEMENT construct.
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 18764
diff changeset
    35
 * This is essential the description of a tag. It describes the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * type, content model, attributes, attribute types etc. It is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * to correctly parse a document by the Parser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see DTD
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see AttributeList
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 23010
diff changeset
    43
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
class Element implements DTDConstants, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public boolean oStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public boolean oEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public BitSet inclusions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public BitSet exclusions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public int type = ANY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public ContentModel content;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public AttributeList atts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * A field to store user data. Mostly used to store
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * style sheets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public Object data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    Element() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Create a new element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    Element(String name, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.index = index;
18764
12a1993f109b 8017492: Static field in HTML parser affects all applications
malenkov
parents: 5506
diff changeset
    71
        if (index > getMaxIndex()) {
12a1993f109b 8017492: Static field in HTML parser affects all applications
malenkov
parents: 5506
diff changeset
    72
            AppContext.getAppContext().put(MAX_INDEX_KEY, index);
12a1993f109b 8017492: Static field in HTML parser affects all applications
malenkov
parents: 5506
diff changeset
    73
        }
12a1993f109b 8017492: Static field in HTML parser affects all applications
malenkov
parents: 5506
diff changeset
    74
    }
12a1993f109b 8017492: Static field in HTML parser affects all applications
malenkov
parents: 5506
diff changeset
    75
12a1993f109b 8017492: Static field in HTML parser affects all applications
malenkov
parents: 5506
diff changeset
    76
    private static final Object MAX_INDEX_KEY = new Object();
12a1993f109b 8017492: Static field in HTML parser affects all applications
malenkov
parents: 5506
diff changeset
    77
12a1993f109b 8017492: Static field in HTML parser affects all applications
malenkov
parents: 5506
diff changeset
    78
    static int getMaxIndex() {
12a1993f109b 8017492: Static field in HTML parser affects all applications
malenkov
parents: 5506
diff changeset
    79
        Integer value = (Integer) AppContext.getAppContext().get(MAX_INDEX_KEY);
12a1993f109b 8017492: Static field in HTML parser affects all applications
malenkov
parents: 5506
diff changeset
    80
        return (value != null)
12a1993f109b 8017492: Static field in HTML parser affects all applications
malenkov
parents: 5506
diff changeset
    81
                ? value.intValue()
12a1993f109b 8017492: Static field in HTML parser affects all applications
malenkov
parents: 5506
diff changeset
    82
                : 0;
2
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
     * Get the name of the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Return true if the start tag can be omitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public boolean omitStart() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return oStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Return true if the end tag can be omitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public boolean omitEnd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return oEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Get type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public int getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Get content model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public ContentModel getContent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return content;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Get the attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public AttributeList getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return atts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Get index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public int getIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Check if empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return type == EMPTY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Convert to a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Get an attribute by name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public AttributeList getAttribute(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        for (AttributeList a = atts ; a != null ; a = a.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            if (a.name.equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Get an attribute by value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public AttributeList getAttributeByValue(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        for (AttributeList a = atts ; a != null ; a = a.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if ((a.values != null) && a.values.contains(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   173
    static Hashtable<String, Integer> contentTypes = new Hashtable<String, Integer>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    static {
438
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   176
        contentTypes.put("CDATA", Integer.valueOf(CDATA));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   177
        contentTypes.put("RCDATA", Integer.valueOf(RCDATA));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   178
        contentTypes.put("EMPTY", Integer.valueOf(EMPTY));
2ae294e4518c 6613529: Avoid duplicate object creation within JDK packages
dav
parents: 2
diff changeset
   179
        contentTypes.put("ANY", Integer.valueOf(ANY));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public static int name2type(String nm) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 438
diff changeset
   183
        Integer val = contentTypes.get(nm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return (val != null) ? val.intValue() : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}