javahtmleditorkithtmlwriter

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


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!

Solution

  • 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.