1. /*
  2. * @(#)ParserDelegator.java 1.13 03/01/23
  3. *
  4. * Copyright 2003 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.13, 01/23/03
  24. */
  25. public class ParserDelegator extends HTMLEditorKit.Parser implements Serializable {
  26. private static DTD dtd = null;
  27. protected static void setDefaultDTD() {
  28. if (dtd == null) {
  29. synchronized(ParserDelegator.class) {
  30. if (dtd == null) {
  31. DTD _dtd = null;
  32. // (PENDING) Hate having to hard code!
  33. String nm = "html32";
  34. try {
  35. _dtd = DTD.getDTD(nm);
  36. } catch (IOException e) {
  37. // (PENDING) UGLY!
  38. System.out.println("Throw an exception: could not get default dtd: " + nm);
  39. }
  40. _dtd = createDTD(_dtd, nm);
  41. dtd = _dtd;
  42. }
  43. }
  44. }
  45. }
  46. protected static DTD createDTD(DTD dtd, String name) {
  47. InputStream in = null;
  48. boolean debug = true;
  49. try {
  50. String path = name + ".bdtd";
  51. in = getResourceAsStream(path);
  52. if (in != null) {
  53. dtd.read(new DataInputStream(new BufferedInputStream(in)));
  54. dtd.putDTDHash(name, dtd);
  55. }
  56. } catch (Exception e) {
  57. System.out.println(e);
  58. }
  59. return dtd;
  60. }
  61. public ParserDelegator() {
  62. if (dtd == null) {
  63. setDefaultDTD();
  64. }
  65. }
  66. public void parse(Reader r, HTMLEditorKit.ParserCallback cb, boolean ignoreCharSet) throws IOException {
  67. new DocumentParser(dtd).parse(r, cb, ignoreCharSet);
  68. }
  69. /**
  70. * Fetch a resource relative to the ParserDelegator classfile.
  71. * If this is called on 1.2 the loading will occur under the
  72. * protection of a doPrivileged call to allow the ParserDelegator
  73. * to function when used in an applet.
  74. *
  75. * @param name the name of the resource, relative to the
  76. * ParserDelegator class.
  77. * @returns a stream representing the resource
  78. */
  79. static InputStream getResourceAsStream(String name) {
  80. try {
  81. return ResourceLoader.getResourceAsStream(name);
  82. } catch (Throwable e) {
  83. // If the class doesn't exist or we have some other
  84. // problem we just try to call getResourceAsStream directly.
  85. return ParserDelegator.class.getResourceAsStream(name);
  86. }
  87. }
  88. private void readObject(ObjectInputStream s)
  89. throws ClassNotFoundException, IOException {
  90. s.defaultReadObject();
  91. if (dtd == null) {
  92. setDefaultDTD();
  93. }
  94. }
  95. }