jdk/src/share/classes/javax/swing/text/html/parser/ParserDelegator.java
changeset 6862 f66eb6b6a6b9
parent 5506 202f599c92aa
child 7960 440dbbdb5cbd
equal deleted inserted replaced
6861:0c879c7c2ea2 6862:f66eb6b6a6b9
     1 /*
     1 /*
     2  * Copyright (c) 1998, 2002, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package javax.swing.text.html.parser;
    26 package javax.swing.text.html.parser;
    27 
    27 
       
    28 import sun.awt.AppContext;
       
    29 
    28 import javax.swing.text.html.HTMLEditorKit;
    30 import javax.swing.text.html.HTMLEditorKit;
    29 import java.io.BufferedInputStream;
    31 import java.io.BufferedInputStream;
    30 import java.io.IOException;
    32 import java.io.IOException;
    31 import java.io.InputStream;
    33 import java.io.InputStream;
    32 import java.io.DataInputStream;
    34 import java.io.DataInputStream;
    33 import java.io.ObjectInputStream;
    35 import java.io.ObjectInputStream;
    34 import java.io.Reader;
    36 import java.io.Reader;
    35 import java.io.Serializable;
    37 import java.io.Serializable;
    36 import java.lang.reflect.Method;
       
    37 
    38 
    38 /**
    39 /**
    39  * Responsible for starting up a new DocumentParser
    40  * Responsible for starting up a new DocumentParser
    40  * each time its parse method is invoked. Stores a
    41  * each time its parse method is invoked. Stores a
    41  * reference to the dtd.
    42  * reference to the dtd.
    43  * @author  Sunita Mani
    44  * @author  Sunita Mani
    44  */
    45  */
    45 
    46 
    46 public class ParserDelegator extends HTMLEditorKit.Parser implements Serializable {
    47 public class ParserDelegator extends HTMLEditorKit.Parser implements Serializable {
    47 
    48 
    48     private static DTD dtd = null;
    49     private static final Object DTD_KEY = new Object();
    49 
    50 
    50     protected static synchronized void setDefaultDTD() {
    51     protected static synchronized void setDefaultDTD() {
       
    52         AppContext appContext = AppContext.getAppContext();
       
    53 
       
    54         DTD dtd = (DTD) appContext.get(DTD_KEY);
       
    55 
    51         if (dtd == null) {
    56         if (dtd == null) {
    52             DTD _dtd = null;
    57             DTD _dtd = null;
    53             // (PENDING) Hate having to hard code!
    58             // (PENDING) Hate having to hard code!
    54             String nm = "html32";
    59             String nm = "html32";
    55             try {
    60             try {
    57             } catch (IOException e) {
    62             } catch (IOException e) {
    58                 // (PENDING) UGLY!
    63                 // (PENDING) UGLY!
    59                 System.out.println("Throw an exception: could not get default dtd: " + nm);
    64                 System.out.println("Throw an exception: could not get default dtd: " + nm);
    60             }
    65             }
    61             dtd = createDTD(_dtd, nm);
    66             dtd = createDTD(_dtd, nm);
       
    67 
       
    68             appContext.put(DTD_KEY, dtd);
    62         }
    69         }
    63     }
    70     }
    64 
    71 
    65     protected static DTD createDTD(DTD dtd, String name) {
    72     protected static DTD createDTD(DTD dtd, String name) {
    66 
    73 
    79         return dtd;
    86         return dtd;
    80     }
    87     }
    81 
    88 
    82 
    89 
    83     public ParserDelegator() {
    90     public ParserDelegator() {
    84         if (dtd == null) {
    91         setDefaultDTD();
    85             setDefaultDTD();
       
    86         }
       
    87     }
    92     }
    88 
    93 
    89     public void parse(Reader r, HTMLEditorKit.ParserCallback cb, boolean ignoreCharSet) throws IOException {
    94     public void parse(Reader r, HTMLEditorKit.ParserCallback cb, boolean ignoreCharSet) throws IOException {
    90         new DocumentParser(dtd).parse(r, cb, ignoreCharSet);
    95         new DocumentParser((DTD) AppContext.getAppContext().get(DTD_KEY)).parse(r, cb, ignoreCharSet);
    91     }
    96     }
    92 
    97 
    93     /**
    98     /**
    94      * Fetch a resource relative to the ParserDelegator classfile.
    99      * Fetch a resource relative to the ParserDelegator classfile.
    95      * If this is called on 1.2 the loading will occur under the
   100      * If this is called on 1.2 the loading will occur under the
   111     }
   116     }
   112 
   117 
   113     private void readObject(ObjectInputStream s)
   118     private void readObject(ObjectInputStream s)
   114         throws ClassNotFoundException, IOException {
   119         throws ClassNotFoundException, IOException {
   115         s.defaultReadObject();
   120         s.defaultReadObject();
   116         if (dtd == null) {
   121         setDefaultDTD();
   117             setDefaultDTD();
       
   118         }
       
   119     }
   122     }
   120 }
   123 }