Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7104635

HTMLEditorKit fails to write down some html files

    XMLWordPrintable

Details

    • b105
    • generic
    • generic

    Description

      I'm getting the following exception trying to save some html files with HTMLEditorKit:

      java.lang.IndexOutOfBoundsException
              at java.io.CharArrayWriter.write(CharArrayWriter.java:98)
              at javax.swing.text.AbstractWriter.output(AbstractWriter.java:696)
              at javax.swing.text.html.HTMLWriter.output(HTMLWriter.java:1186)
              at javax.swing.text.AbstractWriter.indent(AbstractWriter.java:477)
              at javax.swing.text.html.HTMLWriter.indentSmart(HTMLWriter.java:1269)
              at javax.swing.text.html.HTMLWriter.endTag(HTMLWriter.java:596)
              at javax.swing.text.html.HTMLWriter.write(HTMLWriter.java:229)
              at javax.swing.text.html.HTMLEditorKit.write(HTMLEditorKit.java:319)
              at Bug.main(Bug.java:20)

      The minimized html that causes the exception is "<pre><p> </pre>", while "<pre><p></pre>" is handling correctly.

      To reproduce the problem just compile and run the program below:
      ---- Bug.java -----
      import java.io.CharArrayReader;
      import java.io.CharArrayWriter;
      import javax.swing.text.html.HTMLDocument;
      import javax.swing.text.html.HTMLEditorKit;


      public class Bug {
          public static void main(String[] args) {
              String htmlDoc = "<pre><p> </pre>";
              try {
                  HTMLEditorKit kit = new HTMLEditorKit();
                  Class c = Class.forName("javax.swing.text.html.parser.ParserDelegator");
                  HTMLEditorKit.Parser parser = (HTMLEditorKit.Parser) c.newInstance();
                  HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
                  HTMLEditorKit.ParserCallback htmlReader = doc.getReader(0);
                  parser.parse(new CharArrayReader(htmlDoc.toCharArray()), htmlReader, true);
                  htmlReader.flush();
                  CharArrayWriter writer = new CharArrayWriter(1000);
                  kit.write(writer, doc, 0, doc.getLength());
                  writer.flush();
                  System.out.println(new String(writer.toCharArray()));
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      }
      -----------------
      the bug is reproducible on available JDKs by the moment:
      jdk6u29
      jdk7-b147
      jdk8-b09

      Attachments

        Activity

          People

            rchamyal Rajeev Chamyal (Inactive)
            dfazunen Dmitry Fazunenko (Inactive)
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: