Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » jet xpath expression to reference properties on an object valued model attribute
jet xpath expression to reference properties on an object valued model attribute [message #481435] Fri, 21 August 2009 00:49 Go to next message
Stuart Mackey is currently offline Stuart Mackey
Messages: 2
Registered: July 2009
Junior Member
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] Fri, 21 August 2009 02:20 Go to previous message
Paul Elder is currently offline Paul Elder
Messages: 849
Registered: July 2009
Senior Member
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
Previous Topic:[JET] Problem with Jet editor in 3.5 RC4
Next Topic:Protected Regions in Acceleo for Java
Goto Forum:
  


Current Time: Thu Oct 23 10:14:05 GMT 2014

Powered by FUDForum. Page generated in 0.01528 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software