Restoring a StyledDocument?

Discussion in 'Java' started by fiziwig, Aug 16, 2006.

  1. fiziwig

    fiziwig Guest

    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
    fiziwig, Aug 16, 2006
    #1
    1. Advertising

  2. fiziwig

    Oliver Wong Guest

    "fiziwig" <> wrote in message
    news:...
    >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
    Oliver Wong, Aug 16, 2006
    #2
    1. Advertising

  3. fiziwig

    fiziwig Guest

    Oliver Wong wrote:
    > "fiziwig" <> wrote in message
    > news:...


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


    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
    fiziwig, Aug 16, 2006
    #3
  4. fiziwig

    Oliver Wong Guest

    "fiziwig" <> wrote in message
    news:...
    >
    > Oliver Wong wrote:
    >> "fiziwig" <> wrote in message
    >> news:...

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

    >
    > 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
    Oliver Wong, Aug 17, 2006
    #4
  5. fiziwig

    PofN Guest

    PofN, Aug 17, 2006
    #5
  6. fiziwig

    fiziwig Guest

    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.
    fiziwig, Aug 17, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Chesnay
    Replies:
    0
    Views:
    755
    Chris Chesnay
    Apr 6, 2005
  2. Replies:
    1
    Views:
    327
    Steve C. Orr [MVP, MCSD]
    Jan 12, 2004
  3. Craig Deelsnyder

    Restoring a calendar date

    Craig Deelsnyder, Feb 2, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    291
  4. =?Utf-8?B?RWFybCBUZWlncm9i?=

    Restoring ViewState to Dynamically Generated Controls

    =?Utf-8?B?RWFybCBUZWlncm9i?=, Oct 5, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    463
    =?Utf-8?B?RWFybCBUZWlncm9i?=
    Oct 5, 2004
  5. damijan

    StyledDocument -> XML

    damijan, Dec 15, 2003, in forum: Java
    Replies:
    0
    Views:
    663
    damijan
    Dec 15, 2003
Loading...

Share This Page