Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [JET2] JetEmitter
[JET2] JetEmitter [message #11305] Wed, 11 April 2007 14:08 Go to next message
Eclipse User
Originally posted by: markus.herrmannsdoerfer.bmw-carit.de

Hi,

I want to evaluate templates which will be dynamically provided. Right
now I use Velocity, but I think of switching to JET.

With the old JET this was possible via the JETEmitter class.
Unfortunately, this class no longer exists in JET2. How can I have the
same functionality as with JETEmitter in JET2?

Thank you very much in advance.

Markus Herrmannsdörfer
Re: [JET2] JetEmitter [message #11342 is a reply to message #11305] Wed, 11 April 2007 18:09 Go to previous message
Paul Elder is currently offline Paul Elder
Messages: 849
Registered: July 2009
Senior Member
Markus:

Two answers:

1) There is currently no direct JETEmitter equivalent. I will be
implementing something similar by 0.8.0M7 as part of the effort to provide
better compatibility with the old JET (JET1). There are lots of things I
don't like about JETEmitter: including that it recompiles templates on the
fly, uses a secret Eclipse project, and requires you to do some serious
classpath fiddling in your code that calls JETEmitter.

My plan is to implement something simpler, based on how JET2 does its
template overriding. In particular, I think it reasonable to expect the
templates to be statically compiled (by the builder, rather tan JETEmitter),
and that the only dynamic thing that needs to happen is the loading of the
templates. By statically compiling all templates, template developers can
get earlier and better feedback on compile errors. They can also setup any
template projects with the correct classpaths. See answer 2 for how JET2
templates are dynamically loaded.

2) JET2 has a very different world view than JET1. In particular, rather
than writting individual templates, and invoking them from your Java code,
you create a "transformation" than includes a special template (typically
main.jet) which invokes other templates and writes the results to a file. In
the JET2 world, your Java code would invoke a JET2 "transformation", but
specifying an id. A JET2 transformation can extend another by specifying
the other transformation's id in its plugin.xml (org.elcipse.jet.transform
extension, (transform) element, overrides attribute). To switch to a user's
version of a transformation, your code merely has to accept a transformation
id from the user.

The infrastructure for loading JET2 transformations/plug-ins/bundles, and
finding and executing a template (including overrides, etc) is all public
API. So, if you wanted, you could invoke individual JET2 templates from your
Java code, but if you're going to write the results to the workspace,
consider using a JET transformation instead. Here a snippet showing how to
use the JET2 template loading mechanism:

IJETBundleManager jbm = JET2Platform.getJETBundleManager();

String jetTransformationId = ...; // the JET transformation id from which
to load templates
final Object source = ...; // the 'source' object maps to / in XPath
expressions. Can be null if you don't use the XPath root object.
IProgressMonitor monitor = new NullProgressMonitor();
try {
jbm.run(jetTransformationId, new IJETRunnable() {
public void run(IJETBundleDescriptor descriptor,
JET2TemplateLoader templateLoader, IProgressMonitor monitor) {
JET2Template template =
templateLoader.getTemplate("templates/foo.jet");

BodyContentWriter out = new BodyContentWriter();
JET2Context context = new JET2Context(source);
context.setVariable("var", ...); // specify a variable value: String,
Boolean, Number or Object
template.generate(context, out);

// TODO: do something with result
String result = out.getContent();

}}, monitor);
} catch (BundleException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

I'm sure this raises more questions than it answers. Please ask if you think
of some.

Paul
Previous Topic:Re: Jet 2.0?
Next Topic:Questions about XSD transformation
Goto Forum:
  


Current Time: Fri Oct 24 18:13:15 GMT 2014

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

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