Difference between revisions of "JET FAQ How do I run an individual JET template and get a string back?"

From Eclipsepedia

Jump to: navigation, search
(Initial content)
 
m (Refined some small typos in the example.)
 
(4 intermediate revisions by 2 users not shown)
Line 6: Line 6:
  
 
== Answer ==
 
== Answer ==
 +
 +
Note: a more detailed FAQ has been added here [[M2T-JET-FAQ/How do I run a JET template from Java?]]
 +
 
JET 0.8.0 has a class org.eclipse.jet.JET2TemplateManager. You use it something  
 
JET 0.8.0 has a class org.eclipse.jet.JET2TemplateManager. You use it something  
 
like this...
 
like this...
Line 15: Line 18:
 
run.jet ...";
 
run.jet ...";
 
final JET2Context context = new JET2Context( ... source model or null ...);
 
final JET2Context context = new JET2Context( ... source model or null ...);
 +
//Set up a context extender... ???
 +
//TransformContextExtender.getInstance(context);
 
// optionally set any context variables via
 
// optionally set any context variables via
 
context.setVariable("name", ... value ...);
 
context.setVariable("name", ... value ...);
 
final BufferedJET2Writer out = new BodyContentWriter();
 
final BufferedJET2Writer out = new BodyContentWriter();
JET2TemplateManager.run( jetTransformsToSeach, new  
+
JET2TemplateManager.run( jetTransformsToSearch, new JET2TemplateManager.ITemplateOperation() {
JET2TemplateManager.ITemplateOperation() {
+
     public void run(JET2TemplateManager.ITemplateRunner templateRunner) {
     public run(JET2TemplateManager.ITemplateRunner templateRunner) {
+
         templateRunner.generate( templatePath, context, out );
         String result = templateRunner.generate( templatePath, context, out );
+
 
     }
 
     }
 
});
 
});

Latest revision as of 13:07, 5 June 2010

[edit] Question

I've looked at the runTransformOnX() methods (x=Object,String, ...) in org.eclipse.jet.JET2Platform. Is there a JET2 class that does something like org.eclipse.emf.codegen.jet.JETEmitter in EMF: take a list of arguments, execute a template against them, and return a string?

[edit] Answer

Note: a more detailed FAQ has been added here M2T-JET-FAQ/How do I run a JET template from Java?

JET 0.8.0 has a class org.eclipse.jet.JET2TemplateManager. You use it something like this...

String[] jetTransformsToSearch = { "a.b.c", ... };

final String templatePath = "... project relative path of the template to 
run.jet ...";
final JET2Context context = new JET2Context( ... source model or null ...);
//Set up a context extender... ???
//TransformContextExtender.getInstance(context);
// optionally set any context variables via
context.setVariable("name", ... value ...);
final BufferedJET2Writer out = new BodyContentWriter();
JET2TemplateManager.run( jetTransformsToSearch, new JET2TemplateManager.ITemplateOperation() {
    public void run(JET2TemplateManager.ITemplateRunner templateRunner) {
        templateRunner.generate( templatePath, context, out );
     }
});
// do something with the result
String output = out.getContent();

The reason for passing the runnable-like class is that the manager may have to dynamically load JET projects from the workspace. JET is very careful to make sure such things get unloaded again - using the ITemplateOperation helps identify that boundary. In real life, you would probably execute multiple templates in a single ITemplateOperation.

Lastly, I noticed the Javadoc for ITemplateRunner.generate() says it throughs IllegalAccessException. It does not. It throughs IllegalArgumentException of templatePath does not map to a know template. I have submitted bug 195523.


Back to the M2T-JET-FAQ