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