Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Restoring a StyledDocument?

Thread view: 
fiziwig - 16 Aug 2006 22:18 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.