1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
/* * @(#)ParserDelegator.java 1.16 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.swing.text.html.parser; import javax.swing.text.html.HTMLEditorKit; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.io.DataInputStream; import java.io.ObjectInputStream; import java.io.Reader; import java.io.Serializable; import java.lang.reflect.Method; /** * Responsible for starting up a new DocumentParser * each time its parse method is invoked. Stores a * reference to the dtd. * * @author Sunita Mani * @version 1.16, 11/17/05 */ public class ParserDelegator extends HTMLEditorKit.Parser implements Serializable { private static DTD dtd = null; protected static synchronized void setDefaultDTD() { if (dtd == null) { DTD _dtd = null; // (PENDING) Hate having to hard code! String nm = "html32"; try { _dtd = DTD.getDTD(nm); } catch (IOException e) { // (PENDING) UGLY! System.out.println("Throw an exception: could not get default dtd: " + nm); } dtd = createDTD(_dtd, nm); } } protected static DTD createDTD(DTD dtd, String name) { InputStream in = null; boolean debug = true; try { String path = name + ".bdtd"; in = getResourceAsStream(path); if (in != null) { dtd.read(new DataInputStream(new BufferedInputStream(in))); dtd.putDTDHash(name, dtd); } } catch (Exception e) { System.out.println(e); } return dtd; } public ParserDelegator() { if (dtd == null) { setDefaultDTD(); } } public void parse(Reader r, HTMLEditorKit.ParserCallback cb, boolean ignoreCharSet) throws IOException { new DocumentParser(dtd).parse(r, cb, ignoreCharSet); } /** * Fetch a resource relative to the ParserDelegator classfile. * If this is called on 1.2 the loading will occur under the * protection of a doPrivileged call to allow the ParserDelegator * to function when used in an applet. * * @param name the name of the resource, relative to the * ParserDelegator class. * @returns a stream representing the resource */ static InputStream getResourceAsStream(String name) { try { return ResourceLoader.getResourceAsStream(name); } catch (Throwable e) { // If the class doesn't exist or we have some other // problem we just try to call getResourceAsStream directly. return ParserDelegator.class.getResourceAsStream(name); } } private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException { s.defaultReadObject(); if (dtd == null) { setDefaultDTD(); } } }