Re: Can WOText Generate HTML Automatically On Save?
Re: Can WOText Generate HTML Automatically On Save?
- Subject: Re: Can WOText Generate HTML Automatically On Save?
- From: Jonathan Rochkind <email@hidden>
- Date: Mon, 03 Mar 2003 13:08:08 -0600
If you are only talking about line-breaks, here's a java.text.Format object
I've written that will take ordinary text with actual line-breaks in it,
and convert the line-breaks to HTML BR and P tags. It will also escape
other < or > symbols so they remain < and > symbols and don't accidentally
turn into HTML code. My code depends on the gnu.regexp.* package though.
You'll need to download this free package and install it somewhere in your
class path to compile and use my code.
Me, I save ordinary non-HTML text in the db, and bind this Format object to
the WOString to convert it to HTML on the fly upon display. But you could
also use this Format object to convert to HTML upon input and save that in
the db, I suppose. Here's the code:
/* TextDisplayFormatter.java created by Administrator on Mon 19-Jun-2000 */
import com.webobjects.foundation.*;
import com.webobjects.eocontrol.*;
import com.webobjects.eoaccess.*;
import com.webobjects.appserver.*;
import gnu.regexp.*;
import java.text.*;
public class TextDisplayFormatter extends java.text.Format {
//if true, the formatter will turn \n's into <BR>s and <P>s. If false,
//leave them alone.
protected boolean convertsNewLines = true;
//Leaves html code in output. Might as well not use the formatter then,
//but provided anyway.
public static final int HTMLBehaviorIgnore = 1;
//Escapes &, \, <, and > to their html entities.
public static final int HTMLBehaviorEscape = 2;
//Removes anything that looks like an HTML tag from output entirely.
//Still escapes & and \ found outside of html tags, to html entities.
public static final int HTMLBehaviorRemove = 3;
protected int htmlBehavior = HTMLBehaviorEscape;
protected RE htmlTagRE;
public TextDisplayFormatter() {
this(true, HTMLBehaviorEscape);
}
public TextDisplayFormatter(boolean argConvertsNewLines, int
argHTMLBehavior) {
super();
setConvertsNewLines(argConvertsNewLines);
setHTMLBehavior(argHTMLBehavior);
try {
htmlTagRE = new RE("(<[^<>]*>)", RE.REG_DOT_NEWLINE);
}
catch (REException e) {
//Will never happen. If it does, that sucks. Let's print out a
diagnostic.
System.err.println("Exception creating regexp for
TextDisplayFormatter! : " + e);
}
}
public void setConvertsNewLines(boolean v) {
convertsNewLines = v;
}
public boolean convertsNewLines() {
return convertsNewLines;
}
public void setHTMLBehavior(int v) {
htmlBehavior = v;
}
public int htmlBehavior() {
return htmlBehavior;
}
public StringBuffer format(Object oString, StringBuffer toAppendTo,
FieldPosition pos) {
if (oString != null &&
oString != NSKeyValueCoding.NullValue) {
String string = (String) oString;
//Trim it, just becuase it's nicer that way.
string = string.trim();
//If we're removing HTML tags, then do so.
if (htmlBehavior == HTMLBehaviorRemove) {
string = htmlTagRE.substituteAll(string, "");
}
//Escape chars that need to be replaced by HTML entities for HTML
//output.
//We do this for HTMLBehaviorRemove too, cause if any <s or >s
//are somehow left over after above remove operation, we still
//want to escape them. And we want to escape & and \ left over
too.
if (htmlBehavior == HTMLBehaviorEscape ||
htmlBehavior == HTMLBehaviorRemove) {
string = CollabUtil.strReplace(string, "&", "&");
string = CollabUtil.strReplace(string, "\"", """);
string = CollabUtil.strReplace(string, "<", "<");
string = CollabUtil.strReplace(string, ">", ">");
}
//fix line and paragraph breaks
if (convertsNewLines) {
string = CollabUtil.strReplace(string, "\n\n", "<P>");
string = CollabUtil.strReplace(string, "\n", "<BR>");
}
toAppendTo.append(string);
}
return toAppendTo;
}
//don't use this. One way only.
public Object parseObject(String source, ParsePosition status){
//We can't parse anything at all.
return null;
}
}
At 01:36 AM 2/28/2003 +0000, Jonathan Fleming wrote:
Hello Guys
Is there a way to generate HTML automatically say on a save after a user
has entered their text in a WOText box. I know it can read or escape HTML
that is already written into text if you use escapeHtml option, but that
is not what I want to do.
I'm trying to find out if it can be put in by the application itself just
like you can save a word document in HTML format.
I've been asked about this by the client I am building an app for. They
know nothing of HTML and do not want to have to write a document in Word
for example to then save it in HTML Format so that they have to then cut
and paste the source into a WOText area in order for it to generate the
line breaks or paragraphs that the text might need.
The client simoly want to enter text and the WOText box and not have to
worry about HTML
I'm using Win Ver of WebObjects 5.1.3
Kind regards
Jonathan
_________________________________________________________________
Stay in touch with MSN Messenger http://messenger.msn.co.uk
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.