Restoring a StyledDocument?

F

fiziwig

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
 
O

Oliver Wong

fiziwig said:
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?

If your HTML is on disk somewhere:
http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html#editorpane

- Oliver
 
O

Oliver Wong

fiziwig said:
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?

I don't know of a way. Sorry. Maybe someone else can help.

- Oliver
 
F

fiziwig

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top