Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » Launching JET2 from code
Launching JET2 from code [message #57721] Wed, 25 February 2009 16:04 Go to next message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
What's the best entry point for launching JET2 transforms programatically?

I have an EMF model in memory which I loaded from a repository. However I
will want to output a text ( java source ) file into the "current project".

If I create my own version of WorkspaceContextExtender.public static void
loadResourceAsSource(JET2Context context, IResource resource, String
resourceLoaderId, String resourceType)

which simply sets the JET2Context.setSource up directly to the "Resource"
I have in memory and then set up the other variable appropriately, would
this be a good approach, or do you know of any problems I might expect?

The "Resource" will already have successfully loaded the EMF Model and
therefore all the EObjects will have their metadata set for the purposes
of the XPath's reflection, so that shouldn't be a problem....


Thanks
Re: Launching JET2 from code [message #57770 is a reply to message #57721] Thu, 26 February 2009 13:07 Go to previous messageGo to next message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
I seem to able to do this quite happily, as long as I take care of the
context variables;


import java.io.IOException;

import jet2testproject.Activator;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.jet.CoreJETException;
import org.eclipse.jet.JET2Context;
import org.eclipse.jet.JET2Platform;
import org.eclipse.jet.taglib.JET2TagException;
import org.eclipse.jet.taglib.workspace.WorkspaceContextExtender;
import org.eclipse.jet.transform.IJETBundleDescriptor;

public class Jet2Utils2
{
public static final class WorkspaceResourceSpecification
{
public final String projectName;
public WorkspaceResourceSpecification(String projectName)
{
this.projectName = projectName;
}
}

String transformId;
Resource emfResource;
final IProgressMonitor monitor;
JET2Context context;
WorkspaceResourceSpecification resourceSpecification;

public Jet2Utils2(String transformId, Resource emfResource,
WorkspaceResourceSpecification resourceSpec, IProgressMonitor monitor)
{
this.transformId = transformId;
this.emfResource = emfResource;
this.monitor = monitor;
this.resourceSpecification = resourceSpec;
this.context = new JET2Context(null);
}

public void run()
{
IStatus status;
try
{
final IJETBundleDescriptor descriptor =
JET2Platform.getJETBundleManager()
.getDescriptor(transformId);
if(descriptor != null)
{
String fileType = descriptor.getModelExtension();

loadResourceAsSource(context, emfResource, fileType);

status = JET2Platform.runTransform(transformId, context, monitor);
LoggingSupport.handleStatus(Activator.getDefault(), status);
}
else
{
LoggingSupport.error(Activator.getDefault(), "Transform " +
transformId + " not found");
}
}
catch (IOException e)
{
// didn't work.
status = new Status(IStatus.ERROR, transformId, IStatus.OK,
e.getLocalizedMessage(), e);
LoggingSupport.handleStatus(Activator.getDefault(), status);
}
catch (CoreJETException e)
{
status = new Status(IStatus.ERROR, transformId, IStatus.OK,
e.getLocalizedMessage(), e);
LoggingSupport.handleStatus(Activator.getDefault(), status);
}
}

public void loadResourceAsSource(JET2Context context,
Resource emfResource,
String resourceType) throws CoreJETException,
IOException
{
context.setSource(emfResource);

try
{
//The only context variable I use at the momment
context.setVariable(WorkspaceContextExtender.VAR_RESOURCE_PR OJECT_NAME,
resourceSpecification.projectName);

}
catch (JET2TagException e)
{
LoggingSupport.error(Activator.getDefault(), "Invalid variable name",
e); // $NON-NLS
}
}
}
Re: Launching JET2 from code [message #57820 is a reply to message #57770] Fri, 27 February 2009 14:20 Go to previous messageGo to next message
Paul Elder is currently offline Paul ElderFriend
Messages: 849
Registered: July 2009
Senior Member
JF:

Your code looks like it will work, but I'm curious what you didn't just do
something like:

final Map variables = new HashMap();
variables.put(WorkspaceContextExtender.VAR_RESOURCE_PROJECT_ NAME,
projectName);
final IStatus status = JET2Platform.runTransformOnObject(transformId,
emfResource, variables, monitor);

Paul
Re: Launching JET2 from code [message #57844 is a reply to message #57820] Fri, 27 February 2009 14:24 Go to previous message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
That looks much better, I guess working back from what was going on in a
debugger I missed the obvious API,


Thanks...
Previous Topic:Conditional File Generation
Next Topic:[Xpand] where does it look for EMF models?
Goto Forum:
  


Current Time: Sat Dec 21 16:49:22 GMT 2024

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

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

Back to the top