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 47 public class ParserDelegator extends HTMLEditorKit.Parser implements Serializable { 48 49 private static final Object DTD_KEY = new Object(); 50 51 protected static void setDefaultDTD() { 52 getDefaultDTD(); 53 } 54 55 private static synchronized DTD getDefaultDTD() { 56 AppContext appContext = AppContext.getAppContext(); 57 58 DTD dtd = (DTD) appContext.get(DTD_KEY); 59 60 if (dtd == null) { 61 DTD _dtd = null; 62 // (PENDING) Hate having to hard code! 63 String nm = "html32"; 64 try { 65 _dtd = DTD.getDTD(nm); 66 } catch (IOException e) { 67 // (PENDING) UGLY! 68 System.out.println("Throw an exception: could not get default dtd: " + nm); 69 } 70 dtd = createDTD(_dtd, nm); 71 72 appContext.put(DTD_KEY, dtd); 73 } 74 75 return dtd; 76 } 77 78 protected static DTD createDTD(DTD dtd, String name) { 79 80 InputStream in = null; 81 boolean debug = true; 82 try { 83 String path = name + ".bdtd"; 84 in = getResourceAsStream(path); 85 if (in != null) { 86 dtd.read(new DataInputStream(new BufferedInputStream(in))); 87 DTD.putDTDHash(name, dtd); 88 } 89 } catch (Exception e) { 90 System.out.println(e); 91 } 92 return dtd; 93 } 94 95 96 public ParserDelegator() { 97 setDefaultDTD(); 98 } 99 100 public void parse(Reader r, HTMLEditorKit.ParserCallback cb, boolean ignoreCharSet) throws IOException { 101 new DocumentParser(getDefaultDTD()).parse(r, cb, ignoreCharSet); 102 } 103 104 /** 105 * Fetch a resource relative to the ParserDelegator classfile. 106 * If this is called on 1.2 the loading will occur under the 107 * protection of a doPrivileged call to allow the ParserDelegator 108 * to function when used in an applet. 109 * 110 * @param name the name of the resource, relative to the 111 * ParserDelegator class. 112 * @returns a stream representing the resource 113 */ 114 static InputStream getResourceAsStream(String name) { 115 try { 116 return ResourceLoader.getResourceAsStream(name); 117 } catch (Throwable e) { 118 // If the class doesn't exist or we have some other 119 // problem we just try to call getResourceAsStream directly. 120 return ParserDelegator.class.getResourceAsStream(name); 121 } 122 } 123 124 private void readObject(ObjectInputStream s) 125 throws ClassNotFoundException, IOException { 126 s.defaultReadObject(); 127 setDefaultDTD(); 128 } 129 }