Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » Setting a Template context source in JET2
Setting a Template context source in JET2 [message #57795] Thu, 26 February 2009 15:08 Go to next message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
Given that I am setting the Context source directly (see
http://www.eclipse.org/newsportal/article.php?id=1384&gr oup=eclipse.modeling.m2t#1384)
I could set the source directly to an EObject. So if I set it to an
EPackage I could have a main templates as follows;

<c:iterate select="." var="currentPackage">
<c:include template="templates/packageControl.jet"/>
<c:iterate select="$currentPackage/eClassifiers" var="currentClass">
<c:include template="templates/classControl.jet"/>
</c:iterate>
</c:iterate>

If my Eobject source is not in a Resource I would have to avoid the XPath
"/", but thats okay.

Moving on from here I wish to write each of my templates based on a given
type of EObject; i.e. the current node is a single object of a given type.
So in the above example packageControl.jet operates on an EPackage and
could look like...

<%@taglib prefix="ws" id="org.eclipse.jet.workspaceTags" %>

<ws:file template="templates/packageTemplate.jet"
path=" {$org.eclipse.jet.resource.project.name}/src/{@name}/{@name} Package.java "/>

with packageTemplate.jet as follows;

package <c:get select="@name" />;

class <c:get select="@name" />Package {
public String getName() {
return "<c:get select="@name" />";
}
public void shout() {
System.out.println("HelloX!!!");
}
}

In this way I can create sub templates which I can reuse if an object type
appears in a different place, but more importantly it enables easier
maintenance.

However, suppose the nesting is deeper. In the above example I wish to set
the context of
templates/classControl.jet to a single eClassifier, then I can rewrite my
main.jet to do something like;

<c:iterate select="." var="currentPackage">
<c:include template="templates/packageControl.jet"/>
<c:iterate select="$currentPackage/eClassifiers" var="currentClass">
<%
Object oldSource = context.getSource();
context.setSource(context.getVariable("currentClass"));
%>
<c:include template="templates/classControl.jet"/>
<%
context.setSource(oldSource);
%>
</c:iterate>
</c:iterate>


and templates/classControl.jet

<%@taglib prefix="ws" id="org.eclipse.jet.workspaceTags" %>

<ws:file template="templates/classTemplate.jet"
path=" {$org.eclipse.jet.resource.project.name}/src/{$../@name}/H{@ name}Class.java "/>


and classTemplate.jet;

package <c:get select="../@name" />;

class H<c:get select="@name" />Class {
public String getName() {
return "<c:get select="@name" />";
}
public void shout() {
System.out.println("Hello2!!!");
}
}

My questions are;
* Is there a better way to set the context source (some implicit mechanism
like xslt) ?
* Is this setting of context source to arbitray objects as I go fraught
with danger !?
Re: Setting a Template context source in JET2 [message #57917 is a reply to message #57795] Fri, 27 February 2009 18:29 Go to previous messageGo to next message
Paul Elder is currently offline Paul ElderFriend
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 &lt;x:withContext select=""/&gt; 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 #58061 is a reply to message #57917] Mon, 09 March 2009 16:55 Go to previous messageGo to next message
Paul Elder is currently offline Paul ElderFriend
Messages: 849
Registered: July 2009
Senior Member
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
Re: Setting a Template context source in JET2 [message #58309 is a reply to message #58061] Wed, 11 March 2009 15:11 Go to previous messageGo to next message
Eclipse UserFriend
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
>
Re: Setting a Template context source in JET2 [message #58356 is a reply to message #58309] Wed, 11 March 2009 15:35 Go to previous message
Paul Elder is currently offline Paul ElderFriend
Messages: 849
Registered: July 2009
Senior Member
Johannes Nel wrote:

> Would the /plugin/transform override the default context of the
> containing iterator?

Yes, for the scope of the c:iterate selecting /plugin/transform. You would
have to set and use variables (using the var attribute) preserve access to
the outer default context while in the inner iterator. Your example
appears to do this.

Paul
Previous Topic:newbie: including external jars
Next Topic:Formatting text
Goto Forum:
  


Current Time: Thu Apr 25 15:15:43 GMT 2024

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

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

Back to the top