Home » Modeling » M2T (model-to-text transformation) » Re: invoke template from java with string return?
Re: invoke template from java with string return? [message #25166] |
Thu, 05 July 2007 09:26  |
Eclipse User |
|
|
|
Rick:
Yes, JET 0.8.0 has 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 ...);
// optionally set any context variables via
context.setVariable("name", ... value ...);
final BufferedJET2Writer out = new BodyContentWriter();
JET2TemplateManager.run( jetTransformsToSeach, new
JET2TemplateManager.ITemplateOperation() {
public run(JET2TemplateManager.ITemplateRunner templateRunner) {
String result = 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
(https://bugs.eclipse.org/bugs/show_bug.cgi?id=195523).
Paul
"Rick Wood" <woodri@us.ibm.com> wrote in message
news:f66bjj$39i$1@build.eclipse.org...
> Hi,
>
> 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?
>
> Thanks,
> Rick
|
|
|
Re: invoke template from java with string return? [message #25329 is a reply to message #25166] |
Sun, 08 July 2007 12:17   |
Eclipse User |
|
|
|
Originally posted by: woodri.us.ibm.com
Paul,
Thanks, that worked. I was able to load and run a template.
But there is a classloader problem. Another project that does work on a
model (call it com.xyz.myproject) uses templates to do part of the work.
A JET2 project manages the templates (call this project jet2test) - so
com.xyz.myproject runs a jet2test template.
When com.xyz.myproject passes one of its objects into the template's
generate() method via JET2Context's setVariable(), the template's
classloader can't find the object. The classloader is a
DefaultClassLoader, whose delegate is a BundleLoader. I looked at the
JET2 APIs, and didn't see access to classloader in JET2TemplateManager,
JET2TemplateLoader, or JET2Context.
Here's the setup:
- the context object is part of com.xyz.myproject, which exports package
com.xyz.myproject.stuff.
- the template is in project jet2test, which sees com.xyz.myproject as a
required project on the build path.
I'm debugging com.xyz.myproject, which loads a template from jet2test:
<%@ jet package="library.Writer.name" class="template1"
imports="com.xyz.myproject.stuff.MyClass" %>
<% MyClass instance = (MyClass) context.getVariable("MyClass"); %>
<%= instance.getSomeValue() %>
this compiles to:
...
import com.xyz.myproject.stuff.MyClass;
...
public void generate(final JET2Context context, final JET2Writer
__out) {
JET2Writer out = __out;
MyClass instance = (MyClass) context.getVariable("MyClass");
out.write( instance.getSomeValue() );
out.write(NL);
}
When context.getVariable("MyClass") runs, the classloader throws this
exception:
java.lang.NoClassDefFoundError: com.xyz.myproject.stuff.MyClass
at library.Writer.name.template1.generate(template1.java)
at
org.eclipse.jet.JET2TemplateManager$TemplateRunnerImpl.gener ate(JET2TemplateManager.java)
at com.xyz.myproject.template.Jet2Template$1.run(Jet2Template.j ava:52)
at org.eclipse.jet.JET2TemplateManager.run(JET2TemplateManager. java:147)
at com.xyz.myproject.template.Jet2Template.generate(Jet2Templat e.java:47)
at ...
Paul Elder wrote:
> Rick:
>
> Yes, JET 0.8.0 has 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 ...);
> // optionally set any context variables via
> context.setVariable("name", ... value ...);
> final BufferedJET2Writer out = new BodyContentWriter();
> JET2TemplateManager.run( jetTransformsToSeach, new
> JET2TemplateManager.ITemplateOperation() {
> public run(JET2TemplateManager.ITemplateRunner templateRunner) {
> String result = 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
> (https://bugs.eclipse.org/bugs/show_bug.cgi?id=195523).
>
> Paul
>
>
> "Rick Wood" <woodri@us.ibm.com> wrote in message
> news:f66bjj$39i$1@build.eclipse.org...
>> Hi,
>>
>> 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?
>>
>> Thanks,
>> Rick
>
>
|
|
|
Re: invoke template from java with string return? [message #25369 is a reply to message #25166] |
Sun, 08 July 2007 14:16   |
Eclipse User |
|
|
|
Originally posted by: woodri.us.ibm.com
Paul,
I also found using your example that XPath expressions couldn't be
evaluated, because the JET2Context was missing a TagFactory. I looked
again at the example from "[JET2] JetEmitter" of 4/11/2007, and noticed
similarities with:
public void run(IJETBundleDescriptor, JET2TemplateLoader,
IProgressMonitor)
defined in
JET2Platform.runTransform(String, JET2Context, IProgressMonitor)
as part of the line
bundleManager.run(id, new IJETRunnable() {...
Using the code from run() fixed the problem, the TagFactory is set in
final TransformContextExtender tce
= TransformContextExtender.getInstance(context);
But ... for now, I am just using Java, no tags or XML as input, so it's
not a big deal. All in all, I have to say JET is a great package -
fast, powerful, and easy to use.
Thanks,
Rick
Paul Elder wrote:
> Rick:
>
> Yes, JET 0.8.0 has 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 ...);
> // optionally set any context variables via
> context.setVariable("name", ... value ...);
> final BufferedJET2Writer out = new BodyContentWriter();
> JET2TemplateManager.run( jetTransformsToSeach, new
> JET2TemplateManager.ITemplateOperation() {
> public run(JET2TemplateManager.ITemplateRunner templateRunner) {
> String result = 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
> (https://bugs.eclipse.org/bugs/show_bug.cgi?id=195523).
>
> Paul
>
>
> "Rick Wood" <woodri@us.ibm.com> wrote in message
> news:f66bjj$39i$1@build.eclipse.org...
>> Hi,
>>
>> 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?
>>
>> Thanks,
>> Rick
>
>
|
|
|
Re: invoke template from java with string return? [message #25410 is a reply to message #25329] |
Mon, 09 July 2007 08:14  |
Eclipse User |
|
|
|
Rick:
I'll have to look into this. Class loaders always make my head spin, and so
far OSGi handled everything for me. I'll post back in a day or two.
As an aside, if you are not interested in 'dynamic' loading of templates,
you could dispense with the template manager entirely, and make your main
project (com.xyz.myproject) dependent on the template project (jet2test).
The main project could then simply use the template class directly...
JET2Context context = ...
BufferedJET2Writer out = ...
JET2Template template = new template1();
template.generate(context, out);
....
Paul
"Rick Wood" <woodri@us.ibm.com> wrote in message
news:f6r2mp$to$3@build.eclipse.org...
> Paul,
>
> Thanks, that worked. I was able to load and run a template.
>
> But there is a classloader problem. Another project that does work on a
> model (call it com.xyz.myproject) uses templates to do part of the work. A
> JET2 project manages the templates (call this project jet2test) - so
> com.xyz.myproject runs a jet2test template.
>
> When com.xyz.myproject passes one of its objects into the template's
> generate() method via JET2Context's setVariable(), the template's
> classloader can't find the object. The classloader is a
> DefaultClassLoader, whose delegate is a BundleLoader. I looked at the
> JET2 APIs, and didn't see access to classloader in JET2TemplateManager,
> JET2TemplateLoader, or JET2Context.
>
> Here's the setup:
> - the context object is part of com.xyz.myproject, which exports package
> com.xyz.myproject.stuff.
> - the template is in project jet2test, which sees com.xyz.myproject as a
> required project on the build path.
>
> I'm debugging com.xyz.myproject, which loads a template from jet2test:
>
> <%@ jet package="library.Writer.name" class="template1"
> imports="com.xyz.myproject.stuff.MyClass" %>
> <% MyClass instance = (MyClass) context.getVariable("MyClass"); %>
> <%= instance.getSomeValue() %>
>
> this compiles to:
>
> ...
> import com.xyz.myproject.stuff.MyClass;
> ...
> public void generate(final JET2Context context, final JET2Writer
> __out) {
> JET2Writer out = __out;
> MyClass instance = (MyClass) context.getVariable("MyClass");
> out.write( instance.getSomeValue() );
> out.write(NL);
> }
>
> When context.getVariable("MyClass") runs, the classloader throws this
> exception:
>
> java.lang.NoClassDefFoundError: com.xyz.myproject.stuff.MyClass
> at library.Writer.name.template1.generate(template1.java)
> at
> org.eclipse.jet.JET2TemplateManager$TemplateRunnerImpl.gener ate(JET2TemplateManager.java)
> at com.xyz.myproject.template.Jet2Template$1.run(Jet2Template.j ava:52)
> at org.eclipse.jet.JET2TemplateManager.run(JET2TemplateManager. java:147)
> at com.xyz.myproject.template.Jet2Template.generate(Jet2Templat e.java:47)
> at ...
>
>
> Paul Elder wrote:
>> Rick:
>>
>> Yes, JET 0.8.0 has 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
>> ...);
>> // optionally set any context variables via
>> context.setVariable("name", ... value ...);
>> final BufferedJET2Writer out = new BodyContentWriter();
>> JET2TemplateManager.run( jetTransformsToSeach, new
>> JET2TemplateManager.ITemplateOperation() {
>> public run(JET2TemplateManager.ITemplateRunner templateRunner) {
>> String result = 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
>> (https://bugs.eclipse.org/bugs/show_bug.cgi?id=195523).
>>
>> Paul
>>
>>
>> "Rick Wood" <woodri@us.ibm.com> wrote in message
>> news:f66bjj$39i$1@build.eclipse.org...
>>> Hi,
>>>
>>> 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?
>>>
>>> Thanks,
>>> Rick
>>
|
|
|
Goto Forum:
Current Time: Fri May 09 01:42:49 EDT 2025
Powered by FUDForum. Page generated in 0.05405 seconds
|