|
Re: Setting a Template context source in JET2 [message #57917 is a reply to message #57795] |
Fri, 27 February 2009 18:29 |
Paul Elder Messages: 849 Registered: July 2009 |
Senior Member |
|
|
JF:
The context object of XPath expressions is obtained from the
JET2Context.getSource() (and set via JET2Context.setSource()). These methods
are only used for XPath expression resolution (I just checked).
So, it would be safe to modify the source object as you suggest. To be
clear, changing the context object may affect the interpretation of the
following XPath expressions:
. - the current context object (by default, it is typically the document
root: /)
.. - the parent of the context object (by default, it is undefined)
/ - the document root - this is the document containing the current
context object.
For the most part, this is exactly what you want.
I think XSLT, the context object gets set by the tags that navigate the
document. The equivalent in JET would be for tags such as <c:iterate> (and
optionally <c:if>) to set the context object (and restore it on existing the
tag handler). Would that be something if interest as a JET enhancement?
Also, and alternative to embedding Java code in your template, you could
implement a custom JET tag that temporarily sets adjusts the context:
<x:withContext select="...your XPath context expression...">
<ws:file template="templates/classTemplate.jet"
path=" {$org.eclipse.jet.resource.project.name}/src/{../@name}/H{@n ame}Class.java "/>
</x:withContext>
The tag can be implemented as a standard 'containerTag'. Here's a quick
crack at an implementation:
package org.eclipse.jet.examples.tags;
import org.eclipse.jet.JET2Context;
import org.eclipse.jet.JET2Writer;
import org.eclipse.jet.XPathContextExtender;
import org.eclipse.jet.taglib.AbstractContainerTag;
import org.eclipse.jet.taglib.JET2TagException;
import org.eclipse.jet.taglib.TagInfo;
/**
* Implementation of <x:withContext select=""/> tag
*
*/
public class WithContextTag extends AbstractContainerTag {
private Object savedContext;
public void doBeforeBody(TagInfo td, JET2Context context, JET2Writer
out)
throws JET2TagException {
// save the current context object so that we can restore when
// the tag handler completes
savedContext = context.getSource();
// resolve the 'select' attribute as an object, and set the new
context
// object
final XPathContextExtender xpc = XPathContextExtender
.getInstance(context);
final String select = getAttribute("select");
final Object newContext = xpc.resolveSingle(xpc
.currentXPathContextObject(), select);
if (newContext != null) {
context.setSource(newContext);
} else {
throw new JET2TagException("XPath expression returned empty
result");
}
}
public void doAfterBody(TagInfo td, JET2Context context, JET2Writer out)
throws JET2TagException {
context.setSource(savedContext);
}
}
|
|
|
|
Re: Setting a Template context source in JET2 [message #58309 is a reply to message #58061] |
Wed, 11 March 2009 15:11 |
Eclipse User |
|
|
|
Originally posted by: johannes.nel.gmail.com
In the case of
<c:with select="/plugin">
<c:iterate select="extension" var="topL">
I'm at point: <c:get select="@point"/>
<c:iterate select="/plugin/transform">
main template: <c:get select="@startTemplate"/>
and more text from top level <c:get select="$topL/@att"/>
</c:iterate>
</c:iterate>
</c:with>
where in my nested iterator i want to repeat a different path for each
item in the top one.
Would the /plugin/transform override the default context of the
containing iterator?
johan
Paul Elder wrote:
> JF:
>
> FYI. After some reflection, I've decided to make some changes to so that
> the XPath context is set by the c:iterate tag. I've also implemented a
> c:with tag.
>
> Implementation will appear in this weeks integration build.
>
> Here are some of the details:
>
> https://bugs.eclipse.org/bugs/show_bug.cgi?id=267644
>
|
|
|
|
Powered by
FUDForum. Page generated in 0.03851 seconds