Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » JET FAQ How do I run an individual JET template and get a string back?
JET FAQ How do I run an individual JET template and get a string back? [message #28428] Wed, 25 July 2007 11:14 Go to next message
J F is currently offline J F
Messages: 242
Registered: July 2009
Senior Member
Hi,

I tried using the code snippet in this FAQ and found I had to add a few
extra lines...

Gen1 genModel = new Gen1();
genModel.setClassName("BTestClass");
genModel.setPackageName("hisl.test");
final JET2Context context = new JET2Context(genModel);
context.addLogListener(new LogListener()
{
public void log(ContextLogEntry entry)
{
System.err.println("Error: " + entry.getSeverity() + ", " +
entry.getMessage() + ", Line" + entry.getLine());
}
}
);
final BodyContentWriter out = new BodyContentWriter();
TransformContextExtender contextExtender =
TransformContextExtender.getInstance(context);
IJETBundleManager manager = JET2Platform.getJETBundleManager();
contextExtender.setLoader(manager.getTemplateLoader("JETtest2 "));
String[] pluginIDs = { "JETtest2" };
/*
Test1Generator generator = new Test1Generator();
generator.generate(context, out);
System.out.println(out.getContent());
*/
JET2TemplateManager.run( pluginIDs, new
JET2TemplateManager.ITemplateOperation() {
public void run(JET2TemplateManager.ITemplateRunner templateRunner)
{
templateRunner.generate( "templates/test1.jet", context, out );
}
});
//JET2Platform.runTransformOnObject("JETtest2", genModel, new
SimpleProgressMonitor());
System.out.println(out.getContent());
Re: JET FAQ How do I run an individual JET template and get a string back? [message #28477 is a reply to message #28428] Wed, 25 July 2007 11:23 Go to previous messageGo to next message
J F is currently offline J F
Messages: 242
Registered: July 2009
Senior Member
Hi,

I tried using this code snippet but had to add some extra lines of code
to make the thing work. I don't mean the obvious source/model setting etc,
I mean the ContextExtender and Template loader. These were necessary to
use the include control taglib. Am I doing something wrong or should I
update the FAQ?

Gen1 genModel = new Gen1();
genModel.setClassName("BTestClass");
genModel.setPackageName("hisl.test");
final JET2Context context = new JET2Context(genModel);
context.addLogListener(new LogListener()
{
public void log(ContextLogEntry entry)
{
System.err.println("Error: " + entry.getMessage() + ", at Line " +
entry.getLine());
}
}
);
final BodyContentWriter out = new BodyContentWriter();
TransformContextExtender contextExtender =
TransformContextExtender.getInstance(context);
IJETBundleManager manager = JET2Platform.getJETBundleManager();
contextExtender.setLoader(manager.getTemplateLoader("JETtest2 "));
String[] pluginIDs = { "JETtest2" };
JET2TemplateManager.run( pluginIDs, new
JET2TemplateManager.ITemplateOperation() {
public void run(JET2TemplateManager.ITemplateRunner templateRunner) {
templateRunner.generate( "templates/test1.jet", context, out );
}
});
System.out.println(out.getContent());
Re: JET FAQ How do I run an individual JET template and get a string back? [message #28500 is a reply to message #28477] Wed, 25 July 2007 13:08 Go to previous message
J F is currently offline J F
Messages: 242
Registered: July 2009
Senior Member
Hi,

Not sure that the previous code worked by anything other than coincidence
in the debugger. Looking at the JET source the following seems to be a
better way of doing things...

Gen1 genModel = new Gen1();
genModel.setClassName("BTestClass");
genModel.setPackageName("hisl.test");
final JET2Context context = new JET2Context(genModel);
context.addLogListener(new LogListener()
{
public void log(ContextLogEntry entry)
{
System.err.println("Error: " + entry.getSeverity() + ", " +
entry.getMessage() + ", Line" + entry.getLine());
}
}
);
final BodyContentWriter out = new BodyContentWriter();
String[] pluginIDs = { "JETtest2" };
JET2TemplateManager.run( pluginIDs, new
JET2TemplateManager.ITemplateOperation() {
public void run(JET2TemplateManager.ITemplateRunner templateRunner) {
TransformContextExtender contextExtender =
TransformContextExtender.getInstance(context);
IJETBundleManager manager = JET2Platform.getJETBundleManager();
try
{
contextExtender.setLoader(manager.getTemplateLoader("JETtest2 "));
}
catch(BundleException bundleEx)
{
System.out.println(bundleEx);
bundleEx.printStackTrace();
return;
}
templateRunner.generate( "templates/test1.jet", context, out );
}
});
System.out.println(out.getContent());


I also should admit that I am running with:
EMF: 2.3.0.v200706262000
JET 0.8.1.v20070711
Eclipse 3.3.0.v20070607
and jre1.6.0_02 ( 1.4 compatibility switched on !)

Incidentaly when I update I cannot download the latest updates I get the
following error:

The current configuration contains errors and this operation can have
unpredictable results.
Java Emitter Templates (JET) (0.8.1.v20070711-17E19oA55P5N_8) requires
plug-in "org.eclipse.emf.codegen (2.2.0)", or compatible.

I assume that's because your not up to EMF 2.3 yet


Thanks
Previous Topic:How to access the package and class name from the template?
Next Topic:c:iterate and inheritance in a model
Goto Forum:
  


Current Time: Tue Jul 29 10:47:30 EDT 2014

Powered by FUDForum. Page generated in 0.02333 seconds