You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by "Christoph Gaffga (triplemind.com)" <cg...@triplemind.com> on 2007/07/06 16:46:55 UTC
[Jexl] String Literals with Newline do not work
hi all,
I have some problems to use Jexl expressions to look for special
characters in strings, like newline or tabs:
e = ExpressionFactory.createExpression("foo.bar.indexOf('\n')");
o = e.evaluate(jc);
there seem to be no way to encode the linebreak, I also tried '\\n' etc.
No chance, always get an exception.
Is this a bug in Jexl or is it my fault? Has anybody done this? How?
I would appreciate any help...
kind regards,
Christoph
P.S.: Here is my test programm:
import org.apache.commons.jexl.Expression;
import org.apache.commons.jexl.ExpressionFactory;
import org.apache.commons.jexl.JexlContext;
import org.apache.commons.jexl.JexlHelper;
public class TestJexlLinebreaks {
public static class Foo {
String bar = "This is line 1,\nline 2,\nline 3.";
public String getBar() {
return this.bar;
}
}
public static void main(String[] args) throws Exception {
// First make a jexlContext and put stuff in it...
JexlContext jc = JexlHelper.createContext();
jc.getVars().put("foo", new Foo());
jc.getVars().put("number", new Integer(10));
// access a property...
Expression e = ExpressionFactory.createExpression("foo.bar");
Object o = e.evaluate(jc);
System.out.println("Return foo.bar: " + o);
// try to search for newline...
e = ExpressionFactory.createExpression("foo.bar.indexOf('\n')");
o = e.evaluate(jc);
System.out.println("Return foo.bar.indexOf('\\n'): " + o);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [Jexl] String Literals with Newline do not work
Posted by Christoph Gaffga <cg...@triplemind.com>.
> Sorry, yes, I was thinking of another language.
> Try using a variable instead:
>
> jc.getVars().put("nl","\n");
> e = ExpressionFactory.createExpression("foo.bar.indexOf(nl)");
>
thanks a lot, works great, also with JXGenerator in Apache-Cocoon:
<jx:set var="nl" value=" "/>
${foo.bar.indexOf(nl)}
Christoph
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [Jexl] String Literals with Newline do not work
Posted by sebb <se...@gmail.com>.
On 06/07/07, Christoph Gaffga <cg...@triplemind.com> wrote:
> hi,
>
> thanks for your quick reply.
>
> > Try
> > e = ExpressionFactory.createExpression("foo.bar.indexOf('"\n'")");
>
> now I got a syntax error, do you mean:
> e = ExpressionFactory.createExpression("foo.bar.indexOf('\"\n'\")");
Sorry, yes, I was thinking of another language.
Try using a variable instead:
jc.getVars().put("nl","\n");
e = ExpressionFactory.createExpression("foo.bar.indexOf(nl)");
>
> I tried some variations, still the same exception.
>
> regards,
> Christoph
>
>
> sebb wrote:
> >> I have some problems to use Jexl expressions to look for special
> >> characters in strings, like newline or tabs:
> >>
> >> e = ExpressionFactory.createExpression("foo.bar.indexOf('\n')");
> >
> > Does not look valid syntax.
> >
> > Try
> >
> > e = ExpressionFactory.createExpression("foo.bar.indexOf('"\n'")");
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [Jexl] String Literals with Newline do not work
Posted by Christoph Gaffga <cg...@triplemind.com>.
hi,
thanks for your quick reply.
> Try
> e = ExpressionFactory.createExpression("foo.bar.indexOf('"\n'")");
now I got a syntax error, do you mean:
e = ExpressionFactory.createExpression("foo.bar.indexOf('\"\n'\")");
I tried some variations, still the same exception.
regards,
Christoph
sebb wrote:
>> I have some problems to use Jexl expressions to look for special
>> characters in strings, like newline or tabs:
>>
>> e = ExpressionFactory.createExpression("foo.bar.indexOf('\n')");
>
> Does not look valid syntax.
>
> Try
>
> e = ExpressionFactory.createExpression("foo.bar.indexOf('"\n'")");
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org
Re: [Jexl] String Literals with Newline do not work
Posted by sebb <se...@gmail.com>.
On 06/07/07, Christoph Gaffga (triplemind.com) <cg...@triplemind.com> wrote:
> hi all,
>
> I have some problems to use Jexl expressions to look for special
> characters in strings, like newline or tabs:
>
> e = ExpressionFactory.createExpression("foo.bar.indexOf('\n')");
Does not look valid syntax.
Try
e = ExpressionFactory.createExpression("foo.bar.indexOf('"\n'")");
> o = e.evaluate(jc);
>
> there seem to be no way to encode the linebreak, I also tried '\\n' etc.
> No chance, always get an exception.
>
> Is this a bug in Jexl or is it my fault? Has anybody done this? How?
> I would appreciate any help...
>
> kind regards,
> Christoph
>
>
>
>
> P.S.: Here is my test programm:
> import org.apache.commons.jexl.Expression;
> import org.apache.commons.jexl.ExpressionFactory;
> import org.apache.commons.jexl.JexlContext;
> import org.apache.commons.jexl.JexlHelper;
>
> public class TestJexlLinebreaks {
> public static class Foo {
> String bar = "This is line 1,\nline 2,\nline 3.";
> public String getBar() {
> return this.bar;
> }
> }
>
> public static void main(String[] args) throws Exception {
> // First make a jexlContext and put stuff in it...
> JexlContext jc = JexlHelper.createContext();
> jc.getVars().put("foo", new Foo());
> jc.getVars().put("number", new Integer(10));
>
> // access a property...
> Expression e = ExpressionFactory.createExpression("foo.bar");
> Object o = e.evaluate(jc);
> System.out.println("Return foo.bar: " + o);
>
> // try to search for newline...
> e = ExpressionFactory.createExpression("foo.bar.indexOf('\n')");
> o = e.evaluate(jc);
> System.out.println("Return foo.bar.indexOf('\\n'): " + o);
> }
> }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org