How do I stop HTMLWriter from writing bad HTML? (using HTMLEditorKit)

718 Views Asked by At

Here is surprising behaviour. I create a JTextPane , set it up to use HTMLEditorKit, and fill it with valid HTML. But by default Java's HTMLWriter creates invalid HTML. Most items are serialised out correctly but img tags lose their closing slash so :

<img src="https://localhost:9443/ccm/service/com.ibm.team.workitem.common.internal.model.IImageContentService/processattachment/_7rfpIMXdEeGLRroh_7O2yQ/workflow/resolve.gif" alt="Resolved" border="0"/>

is written as :

<img src="https://localhost:9443/ccm/service/com.ibm.team.workitem.common.internal.model.IImageContentService/processattachment/_7rfpIMXdEeGLRroh_7O2yQ/workflow/resolve.gif" alt="Resolved" border="0">

I'm using defaults for everything. Why does it not work, is there any easy fix?

Here is a code snippet:

    JTextPane editor = new JTextPane();
    HTMLEditorKit htmlKit = new HTMLEditorKit();
    editor.setContentType("text/html");
    editor.setEditorKit(htmlKit);   
    editor.setText( '*<ADD SOME VALID HTML FROM A FILE>*'  );       
    HTMLDocument d = (HTMLDocument)editor.getDocument();
    StringWriter fw = new StringWriter();
    HTMLWriter aHTMLWriter = new HTMLWriter(fw,d);
    aHTMLWriter.write();
    String txt = fw.toString();
    //  Now  txt is not valid HTML ... eek!
2

There are 2 best solutions below

0
On BEST ANSWER

Actually, it is valid HTML, but it's not valid XHTML. As far as I know it's not possible to get it to output XHTML. You could post-process the output using regular expressions, or extend HTMLWriter like Freeplane did when they wrote XHTMLWriter.

1
On

Sadly the HTMLEditorKit only supports HTML 3.2, and for that the img tags should not be closed . So it is behaving "correctly".

A Request for enhancement was issued 1999, so maybe it will be implemented soon.