Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [JET] invoke a startTemplate (ie a template which use other templates) from Java?
[JET] invoke a startTemplate (ie a template which use other templates) from Java? [message #722971] Wed, 07 September 2011 10:48 Go to next message
andiqo  is currently offline andiqo Friend
Messages: 32
Registered: July 2009
Member
I created a JET Transformation project in order to have some classes generated. Basically, my main.jet template looks like this:

<c:setVariable select="/input/project/srcFolder" var="srcFolder" />

<c:iterate select="/input/elmt/part" var="currPart">
  <java:class name="{$currPart/name}Part" package="{$currPart/package}" srcFolder="{$srcFolder}" template="templates/part/MyNamePart.java.jet"/>
  <java:class name="{$currPart/name}PartReqAO" package="{$currPart/package}" srcFolder="{$srcFolder}" template="templates/part/MyNamePartReqAO.java.jet"/>
  <java:class name="Modele{$currPart/name}" package="{$currPart/package}" srcFolder="{$srcFolder}" template="templates/part/ModeleMyName.java.jet"/>
  <java:class name="IModele{$currPart/name}" package="{$currPart/package}" srcFolder="{$srcFolder}" template="templates/part/IModeleMyName.java.jet"/>
  <java:class name="Composite{$currPart/name}" package="{$currPart/package}" srcFolder="{$srcFolder}" template="templates/part/CompositeMyName.java.jet"/>
</c:iterate>


And my inputs are:

<input>
  <project>
    <bundleName>my.bundle.name</bundleName>
    <srcFolder>my.bundle.name/src</srcFolder>
    <author>me</author>
  </project>
  <elmt>
    <part>
      <package>com.blahblah.output</package>
      <name>MyUC1</name>
    </part>
  </elmt>
</input>



All is working fine when using the Eclipse launcher. But then, I wanted my generation be accessible from an Eclipse menu. I follow: http://wiki.eclipse.org/M2T-JET-FAQ/How_do_I_run_a_JET_template_from_Java%3F thus have the following code:

Bundle bundle = Platform.getBundle("my.bundle.name");
InputStream inputStream = FileLocator.openStream(bundle, new Path("input.xml"), false);
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream);

final JET2Context jet2Context = JET2Helpers.createJETContext(doc, null);
final BufferedJET2Writer jet2Writer = JET2Helpers.createJETWriter();

JET2TemplateManager.run(new String[] {"my.bundle.name"}, new JET2TemplateManager.ITemplateOperation() {

  public void run(ITemplateRunner templateRunner) {
    templateRunner.generate("templates/main.jet", jet2Context, jet2Writer);
  }
});

getConsoleSupport().println(jet2Writer.getContent());



But i couldn't manage generating my classes (only white spaces). If I replace templates/main.jet by templates/part/MyNamePart.java.jet, then I obtain an output (as currPart variable is defined in main.jet, not a very interesting one).

What is missing to invoke a "start template" (as said in the extension point: org.eclipse.jet.transform) from Java?

Thanks a lot for you help.
--andiqo

Re: [JET] invoke a startTemplate (ie a template which use other templates) from Java? [message #723016 is a reply to message #722971] Wed, 07 September 2011 12:47 Go to previous messageGo to next message
Paul Elder is currently offline Paul ElderFriend
Messages: 849
Registered: July 2009
Senior Member
Have a look at:

http://wiki.eclipse.org/JET_FAQ_How_do_I_run_a_JET_transformation_from_Java%3F

Paul
Re: [JET] invoke a startTemplate (ie a template which use other templates) from Java? [message #723030 is a reply to message #723016] Wed, 07 September 2011 13:38 Go to previous message
andiqo  is currently offline andiqo Friend
Messages: 32
Registered: July 2009
Member
Thank you Paul for your answer.

I just manage to get it work adding the following to my code.

At the beginning of the process:
// Set Loader for all templates
final JET2Context context = new JET2Context(modelRoot, copiedVariables);
TransformContextExtender ex = TransformContextExtender.getInstance(context);
ex.setLoader(new _jet_transformation());


At the end of the process:
// Commit
TransformContextExtender ex = (TransformContextExtender) jet2Context.getPrivateData(TransformContextExtender.class.getName());
if (ex != null) {
   ex.commit(new NullProgressMonitor());
}


I'll try using the wiki page you pointed to me.
Thanks again.
++andiqo
Previous Topic:[Acceleo] PDE, Ant build and Acceleo(Compiler)
Next Topic:[acceleo] please see the method named "registerPackages" in the Java launcher of your gene
Goto Forum:
  


Current Time: Sun Nov 23 01:13:34 GMT 2014

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

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