The Artima Developer Community
Sponsored Link

Java Answers Forum
How to write formatted text to html file?

2 replies on 1 page. Most recent reply: Aug 7, 2002 5:57 PM by Markus Weigert

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Markus Weigert

Posts: 9
Nickname: weigi
Registered: Jul, 2002

How to write formatted text to html file? Posted: Aug 7, 2002 3:35 AM
Reply to this message Reply
Advertisement
Hi,

I want to write a guestbook - applet for my website.
The applet contains a JTextPane and supports formatted
text like italic, bold etc.
So, when the user clicks some button, I want to update
a special html-file called guestlog.
Here I want to insert the formatted text from the Textpane directly after the BODY-Tag.
My problem is that I didn't find out how to do that!
Is it possible to convert the Text from the textpane to HTML and keep the format?
Codesamples are welcome!

Thanks in advance,
Markus


Markus Weigert

Posts: 9
Nickname: weigi
Registered: Jul, 2002

Re: How to write formatted text to html file? Posted: Aug 7, 2002 3:34 PM
Reply to this message Reply
Ok, here is what I already have (it's not very much!)
Important is method update().
My problem is that I just don't know how to insert code
after the Body-tag.
The code below creates a new BODY tag at the wrong place
(inside of <Head>).
I also tried to iterate through the document and retrieve
the Position of <Body> what also didn't work.
(Element.getStartOffset() returnd a value of 3 what I can't
apply to the HTML-file)

import javax.swing.text.html.*;
import javax.swing.text.*;
import java.net.*;
import java.io.*;
import javax.swing.JTextPane;
import javax.swing.text.html.parser.*;

public class HTMLUpdator {
//Document with new text from JTextPane
HTMLDocument textDoc;
//Document with existing Text from "guestlog.htm"
HTMLDocument doc;
JTextPane textpane;
URL base;
HTMLEditorKit kit;

public HTMLUpdator(URL base) {
//Place of "guestlog.htm"
this.base = base;

}

public void update(){


try{
//Construct a reader to load "guestlog.htm"
Reader rd = getReader(base.toString());

//Read guestlog.htm into doc;
kit.read(rd, doc,0);
System.out.println("Content of guestlog.htm: " + doc.getText(0,doc.getLength()));
System.out.println("Content of JTextPane: "+ textDoc.getText(0,textDoc.getLength()));
//Insert new text from JTextPane into doc
kit.insertHTML(doc,doc.getStartPosition().getOffset(),
"<p>" + textDoc.getText(0,textDoc.getLength()) + "</p>", 0,0,HTML.Tag.BODY);

//Write doc back to disk as "guestlog.htm"
File f = new File("guestlog.htm");
try {
FileOutputStream fstrm = new FileOutputStream(f);
kit.write(fstrm,doc,0,doc.getLength());
} catch (IOException io) {System.out.println(io.toString());};


} catch (Exception e) {
e.printStackTrace();
}
}


public void setJTextPane(JTextPane in){
this.textpane = in;
this.textDoc = (HTMLDocument)in.getDocument();
this.kit = new HTMLEditorKit();
this.doc = (HTMLDocument)kit.createDefaultDocument();
}

static Reader getReader(String uri) throws IOException {
if (uri.startsWith("http:")|| uri.startsWith("file:")) {
// Retrieve from Internet.
URLConnection conn = new URL(uri).openConnection();
return new InputStreamReader(conn.getInputStream());
} else {
// Retrieve from file.
return new FileReader(uri);
}
}

}

The Created HTML-Code looks like follows
(look at the wrong body tag inside head):

<html>
<head>
<body>
<p>
"Text from JTextPane"
</p>
</body>
<title>G?stebuch </title>

</head>
<body bgcolor="#000000" text="#FFFFCC" link="#FF9900" alink="#669933" background="_themes/artsy/arttilea.jpg" vlink="#999900">

</body>
</html>

Help is very appreciated!

Greetings,
Markus

Markus Weigert

Posts: 9
Nickname: weigi
Registered: Jul, 2002

Re: How to write formatted text to html file? Posted: Aug 7, 2002 5:57 PM
Reply to this message Reply
Ok, i found out how to do it!

Regards,
Markus

Flat View: This topic has 2 replies on 1 page
Topic: Hey guys I need some help Previous Topic   Next Topic Topic: duplicate column name

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use