1

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!
Tony Eastwood
  • 781
  • 2
  • 9
  • 23

2 Answers2

1

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.

Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53
  • Thank you for the answers. Yes, I'd clean forgotten about how historic HTML 3.2 worked (past tense.) The history of the 'request for enhancement' makes very sad reading . . . – Tony Eastwood Jul 19 '12 at 19:47
0

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.

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66