You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by Henri Yandell <ba...@generationjava.com> on 2002/10/25 23:24:03 UTC

[string] String taglib and newlines

For those who have found this to be a frustration with the String taglib.
I've finally come up with what seems to be a simple improvement [hack] to
allow newlines to be nicer to work with. To recap:

Doing a String replace in the String taglib for a newline involved:

<str:replace replace="h
e" with="h_e">h
ello</str:replace>

to get:   h_ello

The forced newline was a grumble for many. So I've now implemented a new
attribute named newlineToken which is a special token to replace the
forced newline. So you can do:

<str:replace replace="hNL" with="j:NL:y" newlineToken="NL">ih
huw</str:replace>

and will match the newline. Also note that the same newlineToken may be
used in the with attribute.

Now for some small-print bits:

1) This will be in the nightly build tonight. And in v1.1 whenever that is
released. There's a major bug to fix, so a 1.1 should come sooner rather
than later.

2) It may not work cross-platform. I don't trust machines and containers
to all happily pass me a \n instead of a \n\r or anything stupid. But
hopefully it will be fine.

If anyone's interested and tries it, I'd be very interested in hearing if
it works. Also if anyone thinks the solution is too much of a hack, please
complain.

Hen

On Tue, 22 Oct 2002, Henri Yandell wrote:

>
> So I've been thinking on this.
>
> My favourite idea atm:
>
> <str:replace replace="hellNEWLINo jim" with="hi bob"
> newlineToken="NEWLIN">...</str:replace>
>
> Any thoughts on that?
>
> Such as would I need a separate token for replace and with?
>
> Hen
> On Tue, 17 Sep 2002, Henri Yandell wrote:
>
> >
> > I'm not sure. There's nothing really 'html'y about the problem of 'we want
> > to replace \n'. It's only the with bit that gets htmly. So I'm just
> > grumbling about the name :)
> >
> > I was aiming to have a future:
> >
> > <escape type="html> tag, so your extra options will need to fit in there
> > maybe.
> >
> > How about just overriding the replace attribute of the replace tag with:
> >
> > replaceNewline
> > replaceTab
> > replaceHardspace
> >
> > etc??
> >
> > On Tue, 17 Sep 2002, Hans Bergsten wrote:
> >
> > > Because of all the escaping problems, I think a separate action is the
> > > best solution for this instead of trying to do some tricks with the
> > > replace action. How about something like this:
> > >
> > >    <str:htmlEscape
> > >      [nls="true|false"]
> > >      [blanks="true|false"]
> > >      [tabs="true|false" [tabLength="noOfChars"]]>
> > >      text to be converted
> > >    </str:htmlEscape>
> > >
> > >    nls:       replace all \n with <br>
> > >    blanks:    replace multiple blanks with &nbsp;, but leave single
> > >               blanks alone
> > >    tabs:      replace tabs with a number of &nbsp;
> > >    tabLength: how many characters a tab represents (default is 8)
> > >
> > > The "blanks" and "tabs" replacements make it possible to handle simple
> > > formatted (indented) text. Other replacements are possible, of course,
> > > such as "*" to "<li>", but this is a start.
> > >
> > > Hans
> > >
> > > Henri Yandell wrote:
> > > >
> > > > On Tue, 17 Sep 2002, zze-JEANJEAN S ext FTRD/DMI/SOP wrote:
> > > >
> > > >
> > > >>>>Is there a tag to convert \n to <br/> ??
> > > >>>
> > > >>>You should be able to use the String Taglib or the Regexp Taglib.
> > > >>>
> > > >>
> > > >>The str:replace tag is not easy to use : in the replace propertie the \n
> > > >>is not understood :(( You have to put in this propetie a real new line,
> > > >>so the code is not really readable :(
> > > >>For instance :
> > > >>...
> > > >><str:replace replace="
> > > >>" with="<BR/>"><c:out
> > > >>value="${requestScope[theKey].address1}"/></str:replace>
> > > >>...
> > > >
> > > >
> > > > Yeah. This needs to be solved doesn't it.
> > > >
> > > > Solutions:
> > > >
> > > > Make replace accept \n as being a newline. This would be doable, but what
> > > > if someone wanted to replace a \n sequence? Is it acceptable to make
> > > > 'tabs' illegal in the replace etc. Probably not.
> > > >
> > > > Add a replaceJava attribute, for specifying the replace tag as a Java
> > > > attribute. Would want a withJava attribute too.
> > > > Or maybe this is an encoding="java" attribute.
> > > >
> > > > Have a replaceNewline tag. A bit weak as you won't be able to replace
> > > > something with a newline in. Also has weaknesses as I'm trying to collate
> > > > similar functionality and not create more tags.
> > > >
> > > > ====
> > > > I seem to recall that there are no XML character entities that work
> > > > there, but possible I could make one work. Whether this would be
> > > > cross-server though, I don't know.
> > > >
> > > > Most people will want \n to work I imagine, and won't care that they have
> > > > to do \\n to replace a \ and a n. If I change it so that all
> > > > String attributes get unescaped for Java entities, and maybe even all
> > > > content as well. I wonder if this will hurt anything...
> > > >
> > > > If not, I'll aim for this feature to be in a 2.0 release. My apologies for
> > > > not being able to get it in sooner.
> > > >
> > > > Hen
> > > >
> > > >
> > > > --
> > > > To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> > > > For additional commands, e-mail: <ma...@jakarta.apache.org>
> > > >
> > >
> > > --
> > > Hans Bergsten		hans@gefionsoftware.com
> > > Gefion Software		http://www.gefionsoftware.com
> > > JavaServer Pages	http://TheJSPBook.com
> > >
> > >
> > > --
> > > To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> > > For additional commands, e-mail: <ma...@jakarta.apache.org>
> > >
> > >
> >
> >
> > --
> > To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> > For additional commands, e-mail: <ma...@jakarta.apache.org>
> >
> >
>
>
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>