[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.modeling.m2t] Re: jet xpath expression to reference properties on an object valued model attribute


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