jdk/src/share/classes/javax/swing/text/html/parser/ParserDelegator.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7960 440dbbdb5cbd
child 22567 5816a47fa4dd
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7960
diff changeset
     2
 * Copyright (c) 1998, 2011, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
6862
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
    28
import sun.awt.AppContext;
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
    29
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.text.html.HTMLEditorKit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.BufferedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.DataInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.Reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Responsible for starting up a new DocumentParser
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * each time its parse method is invoked. Stores a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * reference to the dtd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author  Sunita Mani
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class ParserDelegator extends HTMLEditorKit.Parser implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
6862
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
    49
    private static final Object DTD_KEY = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
7960
440dbbdb5cbd 6990651: Regression: NPE when refreshing applet since 6u22-b01
rupashka
parents: 6862
diff changeset
    51
    protected static void setDefaultDTD() {
440dbbdb5cbd 6990651: Regression: NPE when refreshing applet since 6u22-b01
rupashka
parents: 6862
diff changeset
    52
        getDefaultDTD();
440dbbdb5cbd 6990651: Regression: NPE when refreshing applet since 6u22-b01
rupashka
parents: 6862
diff changeset
    53
    }
440dbbdb5cbd 6990651: Regression: NPE when refreshing applet since 6u22-b01
rupashka
parents: 6862
diff changeset
    54
440dbbdb5cbd 6990651: Regression: NPE when refreshing applet since 6u22-b01
rupashka
parents: 6862
diff changeset
    55
    private static synchronized DTD getDefaultDTD() {
6862
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
    56
        AppContext appContext = AppContext.getAppContext();
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
    57
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
    58
        DTD dtd = (DTD) appContext.get(DTD_KEY);
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
    59
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (dtd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            DTD _dtd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            // (PENDING) Hate having to hard code!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            String nm = "html32";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                _dtd = DTD.getDTD(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                // (PENDING) UGLY!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                System.out.println("Throw an exception: could not get default dtd: " + nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            dtd = createDTD(_dtd, nm);
6862
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
    71
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
    72
            appContext.put(DTD_KEY, dtd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
7960
440dbbdb5cbd 6990651: Regression: NPE when refreshing applet since 6u22-b01
rupashka
parents: 6862
diff changeset
    74
440dbbdb5cbd 6990651: Regression: NPE when refreshing applet since 6u22-b01
rupashka
parents: 6862
diff changeset
    75
        return dtd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    protected static DTD createDTD(DTD dtd, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        boolean debug = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            String path = name + ".bdtd";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            in = getResourceAsStream(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                dtd.read(new DataInputStream(new BufferedInputStream(in)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                dtd.putDTDHash(name, dtd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            System.out.println(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return dtd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public ParserDelegator() {
6862
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
    97
        setDefaultDTD();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public void parse(Reader r, HTMLEditorKit.ParserCallback cb, boolean ignoreCharSet) throws IOException {
7960
440dbbdb5cbd 6990651: Regression: NPE when refreshing applet since 6u22-b01
rupashka
parents: 6862
diff changeset
   101
        new DocumentParser(getDefaultDTD()).parse(r, cb, ignoreCharSet);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Fetch a resource relative to the ParserDelegator classfile.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * If this is called on 1.2 the loading will occur under the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * protection of a doPrivileged call to allow the ParserDelegator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * to function when used in an applet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param name the name of the resource, relative to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *  ParserDelegator class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @returns a stream representing the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    static InputStream getResourceAsStream(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return ResourceLoader.getResourceAsStream(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            // If the class doesn't exist or we have some other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            // problem we just try to call getResourceAsStream directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return ParserDelegator.class.getResourceAsStream(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        throws ClassNotFoundException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        s.defaultReadObject();
6862
f66eb6b6a6b9 6938813: Swing mutable statics
rupashka
parents: 5506
diff changeset
   127
        setDefaultDTD();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
}