Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [JET] Implicit objects 'context' and 'out'
[JET] Implicit objects 'context' and 'out' [message #520333] Thu, 11 March 2010 18:08 Go to next message
Michael Wahler is currently offline Michael Wahler
Messages: 14
Registered: July 2009
Junior Member
Hi everyone,

I am currently getting started with JET and I have been able to do some
cool things so far! However, when I started to use Java declarations in
my templates, my JET skills came to an end :)

Here's what I am trying to do: I use a template in which I successfully
produce some output using tags such as <c:iterate> or <c:get>. I have
also defined some Java functions within <%! %> tags. My problem is the
interface between the template processor and the Java functions.

First, I need a reference to the top model element, which is of type
'Application', in the Java code. I have tried the statement
Application app = (Application) context.getSource();
because context is supposed to be an "implicit" (whatever that means)
object. The compiler complains that context cannot be resolved.

After I have invoked my functions, I want to print the result into the
output file. Again, I am using an implicit object:
out.write (s);
where s is of type String. However, this causes the following errors:
- Syntax error on token "s", VariableDeclaratorId expected after this token
- Syntax error on token(s), misplaced construct(s)

It seems that the "implicit" objects aren't so implicit afterall. Could
somebody please hint me to a solution?

Thanks and best regards

Michael
Re: [JET] Implicit objects 'context' and 'out' [message #522346 is a reply to message #520333] Mon, 22 March 2010 09:06 Go to previous message
Paul Elder is currently offline Paul Elder
Messages: 849
Registered: July 2009
Senior Member
Michael:

The 'context' and 'out' objects are implicit only to scriptlets <% ...
%> and expressions <%= ... %>. Implementation-wise, the JET template
gets turned into a Java class:

public class YourTemplate implements JET2Template {

// Your <%! ... %> functions. E.g
// <%!
// String hello(String name)
// return "Hello " + name;
// } %>
String hello(String name) {
return "Hello " + name;
}

// implement JET2Template#generate(JET2Context,JET2Writer)
public void generate(JET2Context context, JET2Writer out) {

...
// expressions, e.g. <%= foo %> become
out.write(foo);
// scriptlets, e.g. <% int x = 3; %> become
int x = 3;
}
}

So, if you want your functions to have access to context and out, you
can either pass them to the functions as explicit arguments, or you can
initialize instance variables:

<%!
JET2Context context;
JET2Writer out;
%>

<%
this.context = context;
this.out = out;
%>

Paul
Previous Topic:Assert Statement in Jet?
Next Topic:[Acceleo] Controlling whitespace in output text
Goto Forum:
  


Current Time: Thu Jul 31 09:35:39 EDT 2014

Powered by FUDForum. Page generated in 0.13410 seconds