1 /* 2 * Copyright (c) 1998, 2014, 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 26 package javax.swing.text.html.parser; 27 28 import sun.awt.AppContext; 29 30 import javax.swing.text.html.HTMLEditorKit; 31 import java.io.BufferedInputStream; 32 import java.io.IOException; 33 import java.io.InputStream; 34 import java.io.DataInputStream; 35 import java.io.ObjectInputStream; 36 import java.io.Reader; 37 import java.io.Serializable; 38 39 /** 40 * Responsible for starting up a new DocumentParser 41 * each time its parse method is invoked. Stores a 42 * reference to the dtd. 43 * 44 * @author Sunita Mani 45 */ 46 @SuppressWarnings("serial") // Same-version serialization only 47 public class ParserDelegator extends HTMLEditorKit.Parser implements Serializable { 48 private static final Object DTD_KEY = new Object(); 49 50 protected static void setDefaultDTD() { 51 getDefaultDTD(); 52 } 53 54 private static synchronized DTD getDefaultDTD() { 55 AppContext appContext = AppContext.getAppContext(); 56 57 DTD dtd = (DTD) appContext.get(DTD_KEY); 58 59 if (dtd == null) { 60 DTD _dtd = null; 61 // (PENDING) Hate having to hard code! 62 String nm = "html32"; 63 try { 64 _dtd = DTD.getDTD(nm); 65 } catch (IOException e) { 66 // (PENDING) UGLY! 67 System.out.println("Throw an exception: could not get default dtd: " + nm); 68 } 69 dtd = createDTD(_dtd, nm); 70 71 appContext.put(DTD_KEY, dtd); 72 } 73 74 return dtd; 75 } 76 77 protected static DTD createDTD(DTD dtd, String name) { 78 79 InputStream in = null; 80 boolean debug = true; 81 try { 82 String path = name + ".bdtd"; 83 in = getResourceAsStream(path); 84 if (in != null) { 85 dtd.read(new DataInputStream(new BufferedInputStream(in))); 86 DTD.putDTDHash(name, dtd); 87 } 88 } catch (Exception e) { 89 System.out.println(e); 90 } 91 return dtd; 92 } 93 94 95 public ParserDelegator() { 96 setDefaultDTD(); 97 } 98 99 public void parse(Reader r, HTMLEditorKit.ParserCallback cb, boolean ignoreCharSet) throws IOException { 100 new DocumentParser(getDefaultDTD()).parse(r, cb, ignoreCharSet); 101 } 102 103 /** 104 * Fetch a resource relative to the ParserDelegator classfile. 105 * If this is called on 1.2 the loading will occur under the 106 * protection of a doPrivileged call to allow the ParserDelegator 107 * to function when used in an applet. 108 * 109 * @param name the name of the resource, relative to the 110 * ParserDelegator class. 111 * @returns a stream representing the resource 112 */ 113 static InputStream getResourceAsStream(String name) { 114 try { 115 return ResourceLoader.getResourceAsStream(name); 116 } catch (Throwable e) { 117 // If the class doesn't exist or we have some other 118 // problem we just try to call getResourceAsStream directly. 119 return ParserDelegator.class.getResourceAsStream(name); 120 } 121 } 122 123 private void readObject(ObjectInputStream s) 124 throws ClassNotFoundException, IOException { 125 s.defaultReadObject(); 126 setDefaultDTD(); 127 } 128 }