Home » Modeling » M2T (model-to-text transformation) » generate JET2 template programatically
generate JET2 template programatically [message #64852] |
Wed, 22 July 2009 05:30  |
Eclipse User |
|
|
|
hello,
I developed templates in a Jet transformation project. I managed to run
them using the run configuration command.
My question is the following:
How to run JET2 templates from code? What methods do I use from the JET2
API and how?
thank's
Gloria
|
|
| | | | |
Re: generate JET2 template programatically [message #65134 is a reply to message #64934] |
Mon, 27 July 2009 07:01   |
Eclipse User |
|
|
|
Hi Paul,
I managed to resolve the problem with the
org.osgi.framework.BundleException importing the
org.osgi.framework.BundleException package.
Then I modify my code to run the main.jet template :
public class TestJetExecutionProgramatically {
public static JET2Context createJETContext(Object modelRoot,
final Map<String, ?> variables) {
Map<String, Object> copiedVariables = new HashMap<String, Object>(
variables != null ? variables :
Collections.<String, Object> emptyMap());
// ensure that c:iterate can set the XPath context object
copiedVariables.put("org.eclipse.jet.taglib.control.iterateSetsContext ",
Boolean.TRUE);
final JET2Context context = new JET2Context(modelRoot,
copiedVariables);
// this statement has the side effect of initializing tag handling
TransformContextExtender.getInstance(context);
return context;}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//The object 'Conteneur' is the emf modelroot.
ModelDesTestCaseFactoryImpl f = new ModelDesTestCaseFactoryImpl();
Conteneur monConteneur = f.createConteneur();//creation du Conteneur
JET2Context context = createJETContext(monConteneur, null);
BufferedJET2Writer writer = new BodyContentWriter();
//MyTemplateClass is the corresponding class to the main.jet template I
want to run
JET2Template template = new MyTemplateClass();
template.generate(context, writer);
}
}
and I got that error:
Exception in thread "main" java.lang.NullPointerException
at
org.eclipse.jet.taglib.TagLibraryManager.getTagLibrary(TagLi braryManager.java:89)
at
org.eclipse.jet.internal.runtime.TagFactoryImpl.createRuntim eTag(TagFactoryImpl.java:61)
at my.template.pkg.MyTemplateClass.generate(MyTemplateClass.jav a:96)
at
main.TestJetExecutionProgramatically.main(TestJetExecutionPr ogramatically.java:63)
I think I'm not far from the goal I'm reaching. Thank's for your help.
Gloria
|
|
|
Re: generate JET2 template programatically [message #65177 is a reply to message #65134] |
Mon, 27 July 2009 08:06   |
Eclipse User |
|
|
|
Gloria:
Your test program may launched from Eclipse, but it is NOT running in an
active Eclipse instance. I can tell by looking at your stack trace - it
starts from a 'main' method in one of your classes - Eclipse plug-ins
don't typically do this:
> Exception in thread "main" java.lang.NullPointerException
> at
>
org.eclipse.jet.taglib.TagLibraryManager.getTagLibrary(TagLi braryManager.java:89)
> at
>
org.eclipse.jet.internal.runtime.TagFactoryImpl.createRuntim eTag(TagFactoryImpl.java:61)
> at my.template.pkg.MyTemplateClass.generate(MyTemplateClass.jav a:96)
> at
>
main.TestJetExecutionProgramatically.main(TestJetExecutionPr ogramatically.java:63)
If you want to do a quick test, I suggest the following:
1) create a new Plug-in project:
a) Click File > New > Project ... Select Plug-in Project
b) Enter a plug-in name (e.g. com.myorg.testjet). Click Next
c) In the second wizard page, ensure 'This plug-in will make
contributions to the UI' is checked. Click Next.
d) On the 'Templates' wizard page, select 'Hello, World Command'. Click
Finish. (Instead of clicking Finish, you can click Next, and change the
names of some of the things generated.)
The wizard will open the Plug-in Manifest editor on your new project.
2) Add required dependencies. You will need at least JET and EMF...
a) In the Plug-in Manifest editor, click the Dependencies tab
b) Under Required Plug-ins, click Add, and add org.eclipse.jet
c) Repeat to add org.eclipse.emf.ecore
d) Just in case, add org.eclipse.emf.ecore.xmi
e) Click File > Save.
3) Code you test code in the generated SampleHandler.java class to run the
JET transformation:
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window =
HandlerUtil.getActiveWorkbenchWindowChecked(event);
URI docURI = URI.createFileURI("... your fully qualified doc
URI...");
ResourceSet rs = new ResourceSetImpl();
Resource resource = rs.createResource(docURI);
try {
resource.load(null);
} catch (IOException e) {
throw new ExecutionException("failed to load resource: " +
docURI.toString(), e);
}
IStatus status =
JET2Platform.runTransformOnObject("your-jet-tx-id", resource, new
NullProgressMonitor());
MessageDialog.openInformation(
window.getShell(),
"Jettest",
"returned status: " + status.getMessage());
return null;
}
4) Run your test code in an Eclipse Application (also known as an Eclipse
Runtime workbench):
a) Click Run > Run Configurations
b) Select 'Eclipse Application' and the 'new' icon
c) I suggest you give the 'configuration' a more meaningful name than
'New_configuration'. Something like: "test programmatic JET invocation'.
d) Click Run. A second instance of Eclipse will start - it will include
your test code.
e) You can invoke your test code by clicking Sample Menu > Sample
Command
Tip. You might want to show the Error Log view (Window > Show View >
Other, then look for General > Error Log). Errors such as failing to open
the EMF Resource will only show up in the error log.
Lastly, I am guessing you are new to Eclipse development. I still find the
Eclipse FAQ very useful:
http://wiki.eclipse.org/index.php/Eclipse_FAQs
You may find the All about Plug-ins particularly interesting:
http://wiki.eclipse.org/index.php/Eclipse_FAQs#All_about_Plu g-ins
Let me know how things work out.
Paul
|
|
| |
Re: generate JET2 template programatically [message #358438 is a reply to message #351890] |
Tue, 28 July 2009 08:53   |
Eclipse User |
|
|
|
Gloria:
The error probably indicates that there was an execution error while the
JET transformation was running.
To get better information on the error, I suggest the following:
1) Use the following code to report on the IStatus returned from
JET2Platform.runTransformOnObject:
StatusManager.getManager.handle(status);
(You may need to open META-INF/MANIFEST.MF, go to the Dependencies tab,
and add org.eclipse.ui.workbench as a dependency to get this to compile.)
This should pop up a dialog box with all the details.
2) You could also write the status to the Eclipse log (which you could
then inspect using Window > Show View > Other, and then selecting General
> Error Log):
if(!status.isOK()) {
Activator.getDefault().getLog().log(status)
}
The Activator class should have been created when you created your
plug-in. You may need to do Source > Organize Imports (CTRL+SHIFT+O) to
get the class imported.
As for recommended reading, the Eclipse books I learned from are all
getting a little old :-(
The Eclipse FAQs web site (there was a book, too), is based on Eclipse
3.0, but I still use it.
I also used the "Java Developer's Guide to Eclipse". It too, is getting
old, but it is still very popular:
http://www.jdg2e.com/
Paul
Paul
|
|
|
Re: generate JET2 template programatically [message #372605 is a reply to message #358438] |
Wed, 29 July 2009 05:53  |
Eclipse User |
|
|
|
Hi Paul,
Using the following code you gave me:
"StatusManager.getManager().handle(status);"
Then, it shew me the error log and I found the mistake. The issue was that
when I was trying to find out, I had added some code everywhere and I had
forgotten to remove something then.
Now, the project runs very well. I thank you so much.
I have just finished my studies and I've been employed by a company as
trainee. My manager wants me to work and earn skills on modeling tool.
They are no people in the company that is really qualified in modeling
project.
Then, thank's for your help.
Gloria
|
|
|
Goto Forum:
Current Time: Thu May 08 10:53:51 EDT 2025
Powered by FUDForum. Page generated in 0.49595 seconds
|