I have written out the contents of a JTextPane StyledDocument as HTML
using this code:
....
try {
writer = new CharArrayWriter();
MinimalHTMLWriter htmlWriter = new
MinimalHTMLWriter(writer, (StyledDocument)textRegion.getDocument());
htmlWriter.write();
}
catch (IOException ex) {
.....
Now I need to restore the contents into a new JTextPane from that saved
HTML. Since there's a way to write HTML from a StyledDocument I'm
assuming there's a way to read HTML into a StyledDocument. I just can't
seem to find it anywhere.
Can anyone point me in the right direction?
Thanks,
---gary
Oliver Wong - 16 Aug 2006 22:29 GMT
>I have written out the contents of a JTextPane StyledDocument as HTML
> using this code:
[quoted text clipped - 15 lines]
>
> Can anyone point me in the right direction?
If your HTML is on disk somewhere:
http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html#editorpane
- Oliver
fiziwig - 16 Aug 2006 23:30 GMT
<snip>
> > Now I need to restore the contents into a new JTextPane from that saved
> > HTML. Since there's a way to write HTML from a StyledDocument I'm
[quoted text clipped - 7 lines]
>
> - Oliver
Thanks.
Problem is, my HTML is is a String. It's been parsed out of a larger
XML document that contains several such HTML strings, one for each
JTextPane in the application. Can I point a URL to a String object?
--gary
Oliver Wong - 17 Aug 2006 15:19 GMT
> <snip>
>
[quoted text clipped - 15 lines]
> XML document that contains several such HTML strings, one for each
> JTextPane in the application. Can I point a URL to a String object?
I don't know of a way. Sorry. Maybe someone else can help.
- Oliver
PofN - 17 Aug 2006 17:52 GMT
> Now I need to restore the contents into a new JTextPane from that saved
> HTML. Since there's a way to write HTML from a StyledDocument I'm
> assuming there's a way to read HTML into a StyledDocument. I just can't
> seem to find it anywhere.
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/text/html/HTMLEditorKit.html
#read(java.io.Reader,%20javax.swing.text.Document,%20int)
PofN
fiziwig - 17 Aug 2006 21:23 GMT
FWIW: The HTML Editor Kit makes a complete mess of the result! But
here's what DID work: (I'm only interested in font, size, color, bold,
and italic attributes)
>From the HTML string, extract everything between the body tags and
discard the rest.
>From each span tag extract the font, color, and size attributes into a
Muttable Attribute Set. If a bold or italic tag is encountered in the
text, add the bold or italic attribute to the attribute set.
Extract the pure text from between the markup tags and put it into a
string.
Add the attribute set and string to a list.
When the whole HTML document has been parsed loop through for each pair
in the list and do an insertString() with the next attribute/text pair.
This restores the JTextPane to perfect condition.