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