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