1 7 8 package javax.swing.text.html.parser; 9 10 import javax.swing.text.html.HTMLEditorKit ; 11 import java.io.BufferedInputStream ; 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import java.io.DataInputStream ; 15 import java.io.ObjectInputStream ; 16 import java.io.Reader ; 17 import java.io.Serializable ; 18 import java.lang.reflect.Method ; 19 20 28 29 public class ParserDelegator extends HTMLEditorKit.Parser implements Serializable { 30 31 private static DTD dtd = null; 32 33 protected static synchronized void setDefaultDTD() { 34 if (dtd == null) { 35 DTD _dtd = null; 36 String nm = "html32"; 38 try { 39 _dtd = DTD.getDTD(nm); 40 } catch (IOException e) { 41 System.out.println("Throw an exception: could not get default dtd: " + nm); 43 } 44 dtd = createDTD(_dtd, nm); 45 } 46 } 47 48 protected static DTD createDTD(DTD dtd, String name) { 49 50 InputStream in = null; 51 boolean debug = true; 52 try { 53 String path = name + ".bdtd"; 54 in = getResourceAsStream(path); 55 if (in != null) { 56 dtd.read(new DataInputStream (new BufferedInputStream (in))); 57 dtd.putDTDHash(name, dtd); 58 } 59 } catch (Exception e) { 60 System.out.println(e); 61 } 62 return dtd; 63 } 64 65 66 public ParserDelegator() { 67 if (dtd == null) { 68 setDefaultDTD(); 69 } 70 } 71 72 public void parse(Reader r, HTMLEditorKit.ParserCallback cb, boolean ignoreCharSet) throws IOException { 73 new DocumentParser (dtd).parse(r, cb, ignoreCharSet); 74 } 75 76 86 static InputStream getResourceAsStream(String name) { 87 try { 88 return ResourceLoader.getResourceAsStream(name); 89 } catch (Throwable e) { 90 return ParserDelegator .class.getResourceAsStream(name); 93 } 94 } 95 96 private void readObject(ObjectInputStream s) 97 throws ClassNotFoundException , IOException { 98 s.defaultReadObject(); 99 if (dtd == null) { 100 setDefaultDTD(); 101 } 102 } 103 } 104 105 106 | Popular Tags |