Skip to content
Snippets Groups Projects
Commit dfd184c9 authored by Ståle Walderhaug's avatar Ståle Walderhaug
Browse files

The transformation plugin for SmallTest.m2t

parent 18d9a425
No related branches found
No related tags found
No related merge requests found
Showing
with 384 additions and 0 deletions
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.universaal.tools.transformationcommand</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
#Sun May 23 14:28:26 CEST 2010
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Transformation Command
Bundle-SymbolicName: org.universaal.tools.transformationcommand;singleton:=true
Bundle-Version: 0.1.0
Bundle-Vendor: SINTEF
Require-Bundle: org.eclipse.ui,
org.eclipse.core.resources;bundle-version="3.0.0",
org.eclipse.uml2.uml;bundle-version="2.2.0",
org.eclipse.core.expressions;bundle-version="3.4.0",
org.eclipse.mofscript.parser;bundle-version="1.3.0",
org.eclipse.mofscript.runtime;bundle-version="1.3.0",
org.eclipse.mofscript.model;bundle-version="1.3.0",
org.eclipse.core.runtime;bundle-version="3.4.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework;version="1.4.0"
/**
* transformation SmallTest
* date: 24/5/2010
* author: null
* description:
*/
texttransformation SmallTest (in uml:"http://www.eclipse.org/uml2/3.0.0/UML") {
/**
* Main (entry point)
*/
uml.Class::main () {
'Class ' self.name
}
}
source.. = src/
output.. = bin/
bin.includes = plugin.xml,\
META-INF/,\
.,\
icons/,\
SmallTest.m2t
transformations/org.universaal.tools.transformationcommand/icons/sample.gif

983 B

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension
point="org.eclipse.ui.commands">
<category
name="universAAL"
id="org.istmusic.tools.transformationcommand.commands.category">
</category>
<command
name="List UML classes"
categoryId="org.istmusic.tools.transformationcommand.commands.category"
id="org.istmusic.tools.transformationcommand.commands.generateAdaptationModelCommand">
</command>
</extension>
<extension
point="org.eclipse.ui.handlers">
<handler
commandId="org.istmusic.tools.transformationcommand.commands.generateAdaptationModelCommand"
class="org.istmusic.tools.transformationcommand.handlers.TransformToAdaptationModelHandler">
<activeWhen>
<with
variable="selection">
<iterate
ifEmpty="false"
operator="or">
<instanceof
value="org.eclipse.core.resources.IFile">
</instanceof>
<test
property="org.eclipse.core.resources.extension "
value="uml">
</test>
</iterate>
<count
value="1">
</count>
</with>
</activeWhen>
</handler>
</extension>
<extension
point="org.eclipse.ui.menus">
<menuContribution
locationURI="menu:org.eclipse.ui.main.menu?after=additions">
<menu
label="universAAL"
mnemonic="M"
id="org.istmusic.tools.transformationcommand.menus.MUSICMenu">
<command
commandId="org.istmusic.tools.transformationcommand.commands.generateAdaptationModelCommand"
mnemonic="A"
id="org.istmusic.tools.transformationcommand.menus.sampleCommand">
</command>
</menu>
</menuContribution>
<menuContribution
locationURI="popup:org.eclipse.ui.popup.any?after=additions">
<menu
label="universAAL">
<command
commandId="org.istmusic.tools.transformationcommand.commands.generateAdaptationModelCommand">
</command>
</menu>
</menuContribution>
</extension>
<extension
point="org.eclipse.core.expressions.definitions">
<definition
id="org.istmusic.tools.transformationcommand.umlfileselected">
<with
variable="selection">
<iterate
ifEmpty="false"
operator="or">
<instanceof
value="org.eclipse.core.resources.IFile">
</instanceof>
<test
property="org.eclipse.core.resources.extension"
value="uml">
</test>
</iterate>
<count
value="1">
</count>
</with>
</definition>
</extension>
</plugin>
package org.istmusic.tools.transformationcommand.handlers;
public class TransformToAdaptationModelHandler extends TransformationHandler {
static final String TRANSFORMATION_FILENAME = "SmallTest.m2t";
static final String THIS_BUNDLE_NAME = "org.istmusic.tools.transformationcommand";
public TransformToAdaptationModelHandler() {
setFileAndBundleName(TRANSFORMATION_FILENAME, THIS_BUNDLE_NAME);
}
}
package org.istmusic.tools.transformationcommand.handlers;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import java.io.IOException;
import java.net.URL;
import java.io.File;
import java.net.URISyntaxException;
import java.util.Iterator;
import org.osgi.framework.Bundle;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.jface.action.IAction;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.mofscript.MOFScriptModel.MOFScriptSpecification;
import org.eclipse.mofscript.parser.MofScriptParseError;
import org.eclipse.mofscript.parser.ParserUtil;
import org.eclipse.mofscript.runtime.ExecutionManager;
import org.eclipse.mofscript.runtime.ExecutionMessageListener;
import org.eclipse.mofscript.runtime.MofScriptExecutionException;
/**
* Our sample handler extends AbstractHandler, an IHandler base class.
* @see org.eclipse.core.commands.IHandler
* @see org.eclipse.core.commands.AbstractHandler
*/
public abstract class TransformationHandler extends AbstractHandler implements ExecutionMessageListener {
String transformationFileName;
String thisBundleName;
public void setFileAndBundleName(String theTransformationFile, String theBundle) {
transformationFileName = theTransformationFile;
thisBundleName = theBundle;
}
/**
* the command has been executed, so extract extract the needed information
* from the application context.
*/
public Object execute(ExecutionEvent event) throws ExecutionException {
// First, retrieve the current selection and check whether it is a file
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
ISelection selection = window.getSelectionService().getSelection();
if ((selection != null) && (selection instanceof StructuredSelection)) {
Object selectedFile = ((StructuredSelection)selection).getFirstElement();
if (selectedFile instanceof IFile) {
// If the selection is a file, start the transformation
doTransform((IFile)selectedFile, event);
}
}
else {
MessageDialog.openInformation(
window.getShell(),
"Transformation Command",
"The selection is not a valid source for transformation" );
}
return null;
}
public void doTransform(IFile inputFile, ExecutionEvent event) {
IPath path = new Path(transformationFileName);
URL l = FileLocator.find(Platform.getBundle(thisBundleName), path, null);
try {
l = FileLocator.toFileURL(l);
} catch (IOException e) {
System.out.println("Could not locate transformation script");
return;
}
if (l != null) {
System.out.print("Running transformation script: ");
System.out.println(l);
}
ParserUtil parserUtil = new ParserUtil();
ExecutionManager execMgr = ExecutionManager.getExecutionManager();
//
// The parserutil parses and sets the input transformation model
// for the execution manager.
//
File f = null;
try {
f = new File (l.toURI());
} catch (URISyntaxException e) {
System.out.println("Could not find URI for transformation script");
return;
}
MOFScriptSpecification spec = parserUtil.parse(f, true);
// check for errors:
int errorCount = ParserUtil.getModelChecker().getErrorCount();
Iterator errorIt = ParserUtil.getModelChecker().getErrors(); // Iterator of MofScriptParseError objects
System.out.println ("Preparing transformation...");
if (errorCount > 0) {
System.out.println ("Error parsing transformation: " + errorCount + " errors");
for (;errorIt.hasNext();) {
MofScriptParseError parseError = (MofScriptParseError) errorIt.next();
System.out.println("\t \t: Error: " + parseError.toString());
}
return;
}
// load source model
XMIResourceFactoryImpl _xmiFac = new XMIResourceFactoryImpl();
EObject sourceModel = null;
// File sourceModelFile = new File(selectedFile.getLocationURI()); // new File ("SM.ecore");
ResourceSet rSet = new ResourceSetImpl ();
rSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", _xmiFac);
//System.out.println("Converting URI for selected file: " + inputFile.getLocationURI().toString());
URI uri = null;
try {
uri = URI.createURI(inputFile.getLocationURI().toString());
}
catch (Exception ex) {
ex.printStackTrace();
return;
}
System.out.println("Converted URI for selected file");
Resource resource = rSet.getResource(uri, true);
if (resource != null) {
if (resource.getContents().size() > 0) {
sourceModel = (EObject) resource.getContents().get(0);
}
}
if (sourceModel == null) {
System.out.println("Source model could not be located");
return;
}
System.out.println("Adding source model");
// set the source model for the execution manager
execMgr.addSourceModel(sourceModel);
// sets the root output directory, if any is desired (e.g. "c:/temp")
execMgr.setRootDirectory("c:/temp/");
// if true, files are not generated to the file system, but populated into a filemodel
// which can be fetched afterwards. Value false will result in standard file generation
execMgr.setUseFileModel(false);
// Turns on/off system logging
execMgr.setUseLog(false);
// Adds an output listener for the transformation execution.
execMgr.getExecutionStack().addOutputMessageListener(this);
try {
System.out.println("Performing transformation");
execMgr.executeTransformation();
System.out.println("Completed transformation");
} catch (MofScriptExecutionException mex) {
mex.printStackTrace();
}
}
@Override
public void executionMessage(String arg0, String arg1) {
// Ignore messages from MOFscript for now
System.out.println(arg1);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment