You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cocoon.apache.org by Daniel Fagerstrom <da...@nada.kth.se> on 2005/01/17 11:37:27 UTC
[RFC] Plugable Expressions
I have implemented a first version of plugable expressions as discussed
in: http://marc.theaimsgroup.com/?t=109903957300001&r=1&w=2 and the end
of http://marc.theaimsgroup.com/?t=109901879000001&r=1&w=2. I use them
in the refactored JXTG but in a way that (hopefully) is completely
invisible for the user.
I would like to get your feedback on the approach, intefaces,
implementation etc. So that we can improve it and move it to the core as
a standard interface for expression usage when it is good enough.
Src
===
The code can be found in (in trunk):
src/blocks/template/java/org/apache/cocoon/components/expression/expression
tests in:
src/blocks/template/test/org/apache/cocoon/components/expression/expression
A FOM like context (slight modification of TemplateObjectHelper in
scratchpad):
src/blocks/template/java/org/apache/cocoon/environment/environment
And some test:
src/blocks/template/test/org/apache/cocoon/environment/environment
Background
==========
Expression languages (EL), mainly JXPath and Jexl are used in various
parts of Cocoon, e.g. in JXTG, CForms, various input modules etc. The
ELs often are used on some kind of Cocoon object model. It would be
easier to use Coocoon if expressions would be used in the same way
everywhere in Cocoon and it would simplify maintainance if there was
just one implementation.
Couldn't we just decide to use one EL everywhere? This has been
discussed in the threads cited above. While most people would prefer
having just one EL we could not agree on which one. Also JXPath makes
much more sense on DOM and Jexl on Java beans.
Design
======
The design this far has some focus on my needs for using it in JXTG. It
is inspired by JEX written Dmitri Plotnikov (JXPaths main author)
http://cvs.apache.org/viewcvs.cgi/jakarta-commons-sandbox/jex/ and
http://www.plotnix.com/jex/index.html. As JEX just consists of a two
year old initial code base and doesn't support Jexl, I thought that it
was better to build our own stuff. Compared to JEX my design is simpler.
There is e.g. no locale support as that IMO should be handled by a
separate convertor component. And I have not differed between compiled
and interpreted expressions.
ExpressionContext
-----------------
To make it possible to use several ELs at the same time as in JXTG there
is one concrete ExpressionContext class that are used by all ELs. It is
a Map of variable bindings but also has a contextBean property and
supports parent contexts. ExpressionContext is similar to JexlContext,
not because I found it especialy elegant (I don't), but rather because
the needs from Jexl constrained the design a bit if I wanted to avoid to
much work in the adapter, (better designs are certainly welcome).
Also the ExpressionContext is only a data object. In JXPath there are a
set of "evaluate expression" etc methods on the context but I preferd to
focus on the "CompiledExpression" scenario needed for JXTG furthermore I
don't like having two ways of acchieving about the same thing.
ExpressionFactory
-----------------
The expression factory is modeled after the SourceResolver. Typical use is:
ExpressionFactory factory =
(ExpressionFactory)this.lookup(ExpressionFactory.ROLE);
ExpressionContext context = new ExpressionContext());
context.put("a", new Float(1));
context.put("b", new Float(2));
// Geting a Jexl expression
Expression expression = factory.getExpression("jexl", "a+b");
assertEquals(new Long(3), expression.evaluate(context));
// Letting the EL be part of the expression string
expression = factory.getExpression("jexl:a+b");
assertEquals(new Long(3), expression.evaluate(context));
expression = factory.getExpression("jxpath:$a+$b");
assertEquals(new Double(3), expression.evaluate(context));
// Using the default EL
expression = factory.getExpression("$a+$b");
assertEquals(new Double(3), expression.evaluate(context));
this.release(factory);
The available ELs and the default EL are configured in the relevant
*.xconf. I only used release on the factory object as I didn't found any
use for that for Jexl and JXPath expressions.
Expression
----------
The Expression interface looks like:
public interface Expression {
public Object evaluate(ExpressionContext context) throws
ExpressionException;
public Iterator iterate(ExpressionContext context) throws
ExpressionException;
public void assign(ExpressionContext context, Object value) throws
ExpressionException;
public String getExpression();
public String getLanguage();
public void setProperty(String property, Object value);
public Object getNode(ExpressionContext context) throws
ExpressionException;
}
evaluate, iterate and assign should be rather obvious. getExpression and
getLanguage is mainly for making reasonable error messages possible.
I'm less certain about setProperty that I mainly added to be able to
handle the lenient property for JXPath, but it is not certain that
properties should be part of expressions, maybe they should be part of
the factory instead.
I like the getNode method even less and would like to get rid of it. I
introduced it as JXPath both has a getNode method that returns the raw
object pointed to and a getValue that does some extra work on it, (like
the irritating xsl:value-of stuff on DOM). Now Expression.evaluate
basically does JXPath getValue and Expression.getNode does JXPath getNode.
I would prefer to get rid of the Expression.getNode method and have Let
Expression.evaluate basically call JXPath getNode. But I don't know
enough about the consequances for e.g. JXTG, any opinions?
JXPath and Jexl
---------------
I have implemented a JXPath and Jexl variant of the intefaces.
Questions
=========
* Is the design reasonable?
* Does it work for other ELs?
* Is it efficient enough? Especially considering ExpressionContext
adaption and component lookup.
* What about getNode getValue in JXPath?
* The JXPathExpression contains some JXPath bug/inconvenience
workorounds from JXTG, are they still needed, could they be fixed in JXPath?
* etc
/Daniel
Re: [RFC] Plugable Expressions
Posted by Daniel Fagerstrom <da...@nada.kth.se>.
Leszek Gawron wrote:
<snip/>
> Daniel, what are your current plans for JXTG refactoring? I do not
> quite know what to start with now.
I answer your questions and try to write a summary and road map during
the weekend. More test cases and geting all of the current ones working
is of course always welcome.
Have to run now.
/Daniel
Re: [RFC] Plugable Expressions
Posted by Daniel Fagerstrom <da...@nada.kth.se>.
Leszek Gawron wrote:
> Daniel Fagerstrom wrote:
>
>> They should, but I don't see how that is related to getNode/getValue?
>> jx:set/@value uses getNode and jx:set/* executes the body and put the
>> result in a DOM.
>
> I see. I wasn't aware of the exact implementation here. Sorry for the
> noise.
>
> I have finally started to implement jx:call macro="${name}".
>
> Some time ago we discussed about implementing an "internal taglib
> design". What I mean every instruction was implemented as separate tag
> (no new tags could be introduced). Parser would not depend on a specific
> set of tags.
I'm working on that as you can see in Parser, the only thing that is
left to be done is that jx:define need to run some code both in the
start and the and tag. I have some ideas about what to do about that but
am not sure yet.
> Every Instruction in current JXTG apart from jx:macro could
> be implemented completely separately. jx:macro depends on jx:parameters
> parsed separately so the Parser needs to know the dependency details.
> Same goes for future jx:call/jx:parameter. Could we do something about
> that?
Hopefully but I don't know how yet, I'll research it further.
/Daniel
Re: [RFC] Plugable Expressions
Posted by Leszek Gawron <lg...@mobilebox.pl>.
Daniel Fagerstrom wrote:
> They should, but I don't see how that is related to getNode/getValue?
> jx:set/@value uses getNode and jx:set/* executes the body and put the
> result in a DOM.
I see. I wasn't aware of the exact implementation here. Sorry for the noise.
I have finally started to implement jx:call macro="${name}".
Some time ago we discussed about implementing an "internal taglib
design". What I mean every instruction was implemented as separate tag
(no new tags could be introduced). Parser would not depend on a specific
set of tags. Every Instruction in current JXTG apart from jx:macro could
be implemented completely separately. jx:macro depends on jx:parameters
parsed separately so the Parser needs to know the dependency details.
Same goes for future jx:call/jx:parameter. Could we do something about that?
--
Leszek Gawron lgawron@mobilebox.pl
Project Manager MobileBox sp. z o.o.
+48 (61) 855 06 67 http://www.mobilebox.pl
mobile: +48 (501) 720 812 fax: +48 (61) 853 29 65
Re: [RFC] Plugable Expressions
Posted by Daniel Fagerstrom <da...@nada.kth.se>.
Leszek Gawron wrote:
> Finally after 3 weeks I have some spare time that I can assignate for JXTG.
Excelent!
> Daniel Fagerstrom wrote:
<snip/>
>> I like the getNode method even less and would like to get rid of it. I
>> introduced it as JXPath both has a getNode method that returns the raw
>> object pointed to and a getValue that does some extra work on it,
>> (like the irritating xsl:value-of stuff on DOM). Now
>> Expression.evaluate basically does JXPath getValue and
>> Expression.getNode does JXPath getNode.
>
> Could you describe the difference in more detail?
I don't know that much about it, I hoped that someone more knowledgeable
about JXPath at the list would explain it. I also wonder how JXPath is
used in the rest of Cocoon is the behaviour of getValue needed somewhere
or would getNode work everywhere?
The main difference that I know about is that for DOM, getValue behave
like xsl:value-of i.e. it returns the concatenated text nodes of the DOM
as a string rather than the whole DOM tree. getNode is like xsl:copy-of
for a DOM and returns the DOM object.
getNode always returns the actual object that the xpath points to while
does some kind of transformation of the object for some object types. I
didn't find any precise description of the difference in the
documentation so we probably have to read the source to find out.
For JXTG, getNode is used for the value of jx:eval/@select,
jx:out/@value, x:set/@value and for expressions in element content,
everywhere else getValue or getIterator is used.
As there is no difference between getValue and getNode for Jexl in JXTG
one would assume that getNode could be use everywhere for JXPath, but
maybe there is a difference for DOM and people don't use Jexl for DOM.
Maybe getNode returns CharacterNodes instead of strings for DOM. If that
is the case I would anyway prefer leting expressions always returning
the actual object pointed to and let the (hopefully) forthcomming
converter component take care of converting objects to strings.
>> I would prefer to get rid of the Expression.getNode method and have
>> Let Expression.evaluate basically call JXPath getNode. But I don't
>> know enough about the consequances for e.g. JXTG, any opinions?
>
> I see only one place now that could be problematic:
> <jx:set name="var" value="something"/>
> vs.
>
> <jx:set name="var">something</jx:set>
>
> Both are allowed and both should be treated differently.
They should, but I don't see how that is related to getNode/getValue?
jx:set/@value uses getNode and jx:set/* executes the body and put the
result in a DOM.
>> Questions
>> =========
>>
>> * Is the design reasonable?
>> * Does it work for other ELs?
>
> There is only one more EL used in cocoon: XReporter EL. Should we take
> it into account?
Yes we should. I don't know that much about XReporter EL, any volounters
for implementing it?
/Daniel
Re: [RFC] Plugable Expressions
Posted by Leszek Gawron <lg...@mobilebox.pl>.
Finally after 3 weeks I have some spare time that I can assignate for JXTG.
Daniel Fagerstrom wrote:
> I have implemented a first version of plugable expressions as discussed
> in: http://marc.theaimsgroup.com/?t=109903957300001&r=1&w=2 and the end
> of http://marc.theaimsgroup.com/?t=109901879000001&r=1&w=2. I use them
> in the refactored JXTG but in a way that (hopefully) is completely
> invisible for the user.
>
> I would like to get your feedback on the approach, intefaces,
> implementation etc. So that we can improve it and move it to the core as
> a standard interface for expression usage when it is good enough.
>
> Src
> ===
>
> The code can be found in (in trunk):
>
> src/blocks/template/java/org/apache/cocoon/components/expression/expression
> tests in:
>
> src/blocks/template/test/org/apache/cocoon/components/expression/expression
>
> A FOM like context (slight modification of TemplateObjectHelper in
> scratchpad):
> src/blocks/template/java/org/apache/cocoon/environment/environment
> And some test:
> src/blocks/template/test/org/apache/cocoon/environment/environment
the code looks much clearer now.
> Expression
> ----------
>
> The Expression interface looks like:
>
> public interface Expression {
> public Object evaluate(ExpressionContext context) throws
> ExpressionException;
> public Iterator iterate(ExpressionContext context) throws
> ExpressionException;
> public void assign(ExpressionContext context, Object value) throws
> ExpressionException;
>
> public String getExpression();
> public String getLanguage();
>
> public void setProperty(String property, Object value);
> public Object getNode(ExpressionContext context) throws
> ExpressionException;
> }
>
> evaluate, iterate and assign should be rather obvious. getExpression and
> getLanguage is mainly for making reasonable error messages possible.
>
> I'm less certain about setProperty that I mainly added to be able to
> handle the lenient property for JXPath, but it is not certain that
> properties should be part of expressions, maybe they should be part of
> the factory instead.
>
> I like the getNode method even less and would like to get rid of it. I
> introduced it as JXPath both has a getNode method that returns the raw
> object pointed to and a getValue that does some extra work on it, (like
> the irritating xsl:value-of stuff on DOM). Now Expression.evaluate
> basically does JXPath getValue and Expression.getNode does JXPath getNode.
Could you describe the difference in more detail?
>
> I would prefer to get rid of the Expression.getNode method and have Let
> Expression.evaluate basically call JXPath getNode. But I don't know
> enough about the consequances for e.g. JXTG, any opinions?
I see only one place now that could be problematic:
<jx:set name="var" value="something"/>
vs.
<jx:set name="var">something</jx:set>
Both are allowed and both should be treated differently.
>
> JXPath and Jexl
> ---------------
>
> I have implemented a JXPath and Jexl variant of the intefaces.
>
> Questions
> =========
>
> * Is the design reasonable?
> * Does it work for other ELs?
There is only one more EL used in cocoon: XReporter EL. Should we take
it into account?
Daniel, what are your current plans for JXTG refactoring? I do not quite
know what to start with now.
--
Leszek Gawron lgawron@mobilebox.pl
Project Manager MobileBox sp. z o.o.
+48 (61) 855 06 67 http://www.mobilebox.pl
mobile: +48 (501) 720 812 fax: +48 (61) 853 29 65