JET var in scriptlets [message #51792] |
Mon, 06 October 2008 11:01  |
Eclipse User |
|
|
|
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 16:53   |
Eclipse User |
|
|
|
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 17:27  |
Eclipse User |
|
|
|
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
|
|
|
Powered by
FUDForum. Page generated in 0.04953 seconds