jet xpath expression to reference properties on an object valued model attribute [message #481435] |
Thu, 20 August 2009 20:49  |
Eclipse User |
|
|
|
Working with an EMF model similar to:
MyObject
point : MyPoint
So MyObject is an EObject with an attribute whose EType is MyPoint.
MyPoint is an EDataType that points at a typical POJO Java 'Point' object
with an x and y property.
I'd like to access those x and y values directly in a JET template and
haven't figured out how so far short of having to write a custom tag or
xpath function. Is there a way to do it?
For these examples, assume an instance of 'MyObject' is the context node.
I can't do this, obviously (b/c 'point' is an attribute):
x: <c:get select="point/@x"/>
But if I reference 'point' properly as an attribute, is there any way to
access the properties on it?
x: <c:get select="@point..."/>
One solution might be to have the model changed so that 'MyPoint' is a
normal model object rather than an EDataType so then I could write an
xpath like "point/@x".
A custom tag or xpath function could do it too I guess.
But for the sake of trivia, is there any built-in way to get at properties
on an EDataType via xpath?
|
|
|
Re: jet xpath expression to reference properties on an object valued model attribute [message #481440 is a reply to message #481435] |
Thu, 20 August 2009 22:20  |
Eclipse User |
|
|
|
Stuart:
Unfortunately, the JET XPath engine isn't capable (out-of-the-box) of
reflecting your Point object.
If you were truly determined, you could explore creating an 'inspector'
for Point, but that's likely to be a fair bit of code. Easier alternatives:
1) Just write a bit of Java code.
<%@jet imports="....MyObject"%>
<%@jet imports="org.eclipse.jet.XPathContextExtender"%>
<%
// the xpath context extender lets us execute XPath in Java..
XPathContextExtender xce = XPathContextExtender.getInstance(context);
%>
... assuming $o is a MyObject ...
<%= ((MyObject)xce.resolveSingle("$o")).getPoint().x %>
Still a little verbose, though.
2) Write an XPath function using the org.eclipse.jet.xpathFunctions
extension point. (You can define the extension/function right in your JET
project if you like.) Assuming you want to write XPath expressions
something like:
<c:get select="pointX($o)"/>
or the new 'embedded' equivalent:
${pointX($o)}
You'd code the function implementation class's evaluate() method something
like this:
public Object evaluate(List args) {
MyObject myObject;
Object arg1 = args.get(0);
if(arg1 instanceof Collection) {
// XPath expressions often return collections, ...
// get the first element
Iterator i = ((Collection)arg1).iterator();
if(i.hasNext()) arg1 = i.next();
}
if(arg1 instanceof MyObject) {
return ((MyObject)arg1).getPoint().x;
}
throw new XPathRuntimeException("Expected a MyObject as argument");
}
Advantage of this approach is your templates stay simple and compact.
Paul
|
|
|
Powered by
FUDForum. Page generated in 0.03306 seconds