Geir, let me interject myself here:
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On
>
>
> Leon Messerschmidt wrote:
> >
> > > Sorry for not responding earlier, but I can't say I'm all that
> > > keep on adding methods to the context or template to transform
> > > your input.
> > >
> > > If I understand correctly you might be pull a string from
> a database,
> > > and that string needs to have the XML entities escape before being
> > > placed in your template? So:
> > >
> > > <text>[some text that may contain XML entities]</text>
> >
> > This is exactly what I'm talking about. If you have an
> idea please tell me
> > it 'cause I'm at a bit of a dead-end here. I'm using a
> hacked version of
> > Velocity at the moment - not very elegant :-(
> >
>
> You may have read some of the discussion between Jose and I, but since
> you are actually doing it, maybe you can answer some of the stuff we
> were talking about :
>
> 1) Is your listener sensitive to where you are in the template? I.e.
> how do you know if you should escape the insert?
>
The way I see it, from the point of view of velocity, whether the listener
is intelligent or not is irrelevant. It is upto the implementor of the
listener to decide how to proceed. The beauty here is that from velocity's
point of view you are just providing an entry point in which the application
can add its own processing logic.
I could probably write a small listener and acompaning macro utilities to do
that.
public class XMLEscape implements InsersionListener
{
private boolean off = false;
public String beforeInsert(String value)
{
if (off) return value;
return escape(value);
}
public void turnOff(boolean v)
{
off = v;
}
....
}
#macro(ident, $value)$xml.turnOff(true)$value$xml.turnOff(false)#end
Assume the same XMLEscape object is set as the listener and the putin the
context as "xml".
> 2) Why isn't post processing of the stream, or a smarter writer a
> possible solution? It might mean you would have to remove the
> pre-escaped entities in the constant parts of the templates, but that
> would seem to me to make the template even more flexible, as you could
> post process with different escape rules (although I am sure you don't
> care about that... you just want to get the XML done...)
>
Although postprocessing is a valid alternative. I think that defining such
postprocessor or smart writer will involve much much more than the simple
listener above. Granted, the listener above requires manual control on the
template, but as I said the interest is in the %90 - %10 rule. This is
useful in situations where you need to escape in more situations than not.
Not how complex needs to be the postprocessor to achive the same results,
what is the impact on performance of such approach.
Jose Alberto
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com