Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » JET var in scriptlets
JET var in scriptlets [message #51792] Mon, 06 October 2008 15:01 Go to next message
Eclipse UserFriend
Originally posted by: mauro.bglug.it

Is it possible to access a JET variable in java scriptlet?
I would like to reverse the list of items produced by a c:iterate and I
don't think that is possible directly in JET... am I wrong?

thank you
Mauro
Re: JET var in scriptlets [message #51822 is a reply to message #51792] Mon, 06 October 2008 20:53 Go to previous messageGo to next message
Paul Elder is currently offline Paul ElderFriend
Messages: 849
Registered: July 2009
Senior Member
Mauro:

With JET 0.9, I added a sort function:

http://help.eclipse.org/ganymede/index.jsp?topic=/org.eclips e.jet.doc/references/xpathFunctions/sortFunction.html

Amongst other things, it can sort in ascending or descending order. So,
something like:
<c:iterate select="sort(original-select, 'key-expr::descending')" var="x">
....
</c:iterate>

If you don't want to sort, but just reverse a collection, that would be
possible with a custom XPath function. If that's what you'd like, why not
submit a bugzilla enhancement (long, ugly link below). I imagine something
like:

<c:iterate select="reverse(orginal-select)" var="x">

In fact, XPath 2.0 defines such reverse function:

http://www.w3.org/TR/xquery-operators/#func-reverse

I'd be happy to help develop an implementation, and they put it into the
product.

Finally, you can get at JET variable values in scriptlets, but it is not
pretty

<%@jet imports="fully.qualified.name.TypeOfX"%>
<c:iterate select="..." var="x">
<% // get the value of X
TypeOfX x = (TypeOfX)context.getVariable("x");
%>

</c:iterate>

If the variable is a collection (XPath expressions except for constant
expressions return collections), then you'd have to do more work:

<%@jet imports="java.util.*"%>
<c:setVarible select="original xpath" var="originalColl">
<%
Collection c = context.getVariable("originalColl");
List reverseList = new ArrayList(c);
Collections.reverse(reverseList);
// set the variable reverseColl
context.setVariable("reverseColl", XPathUtil.asNodeSet(reverseList));
%>

Personally, I'd rather not write Java in my templates.

Hope this helps,
Paul
Re: JET var in scriptlets [message #51850 is a reply to message #51822] Mon, 06 October 2008 21:27 Go to previous message
Eclipse UserFriend
Originally posted by: mauro.bglug.it

Paul Elder wrote:

> With JET 0.9, I added a sort function:
> <c:iterate select="sort(original-select, 'key-expr::descending')" var="x">
> </c:iterate>
I used that function in this way reversing the ancestors order
<c:iterate select="sort($class/ancestor::*/@name,'count(ancestor::*)') "
var="p"><c:get select="$p"/></c:iterate >

but I would prefer to have the whole string in a JET variable so a reverse
function and also a merge one that works on collections would be great!

> <%@jet imports="java.util.*"%>
> <c:setVarible select="original xpath" var="originalColl">
> <%
> Collection c = context.getVariable("originalColl");
> List reverseList = new ArrayList(c);
> Collections.reverse(reverseList);
> // set the variable reverseColl
> context.setVariable("reverseColl", XPathUtil.asNodeSet(reverseList));
> %>

This will be very usefull in the meanwhile

thank you very much
Mauro
Previous Topic:JET tag to Check if file exists
Next Topic:Problems with java:package
Goto Forum:
  


Current Time: Thu Apr 25 13:53:40 GMT 2024

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

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

Back to the top