Guest User

addsjunior

a guest
Oct 25th, 2009
240
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.Iterator;
  4. import java.util.List;
  5.  
  6. import org.eclipse.emf.common.util.EList;
  7. import org.eclipse.emf.common.util.URI;
  8. import org.eclipse.emf.ecore.EObject;
  9. import org.eclipse.emf.ecore.resource.Resource;
  10. import org.eclipse.emf.ecore.resource.ResourceSet;
  11. import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
  12. import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
  13. import org.eclipse.uml2.uml.UMLPackage;
  14. import org.eclipse.mofscript.MOFScriptModel.MOFScriptSpecification;
  15. import org.eclipse.mofscript.MOFScriptModel.MOFScriptTransformation;
  16. import org.eclipse.mofscript.fileresourcemodel.frm.FileResource;
  17. import org.eclipse.mofscript.fileresourcemodel.frm.FileResourcesType;
  18. import org.eclipse.mofscript.parser.MofScriptParseError;
  19. import org.eclipse.mofscript.parser.ParserUtil;
  20. import org.eclipse.mofscript.runtime.ExecutionManager;
  21. import org.eclipse.mofscript.runtime.ExecutionMessageListener;
  22. import org.eclipse.mofscript.runtime.MofScriptExecutionException;
  23. import org.w3c.dom.events.UIEvent;
  24.  
  25. import com.sun.corba.se.impl.resolver.FileResolverImpl;
  26.  
  27.  
  28. public class TestAPI implements ExecutionMessageListener {
  29.  
  30. public TestAPI () {
  31. UMLPackage lePackage = UMLPackage.eINSTANCE;
  32. }
  33.  
  34. public void test () {
  35. ParserUtil parserUtil = new ParserUtil();
  36. ExecutionManager execMgr = ExecutionManager.getExecutionManager();
  37. //
  38. // The parserutil parses and sets the input transformation model
  39. // for the execution manager.
  40. //
  41. File f = new File ("UMLTest.m2t");
  42. MOFScriptSpecification spec = parserUtil.parse(f, true);
  43. // check for errors:
  44. int errorCount = ParserUtil.getModelChecker().getErrorCount();
  45. Iterator errorIt = ParserUtil.getModelChecker().getErrors(); // Iterator
  46. of MofScriptParseError objects
  47.  
  48. System.out.println ("Parsing result: " + errorCount + " errors");
  49. if (errorCount > 0) {
  50.  
  51. for (;errorIt.hasNext();) {
  52. MofScriptParseError parseError = (MofScriptParseError) errorIt.next();
  53. System.out.println("\t \t: Error: " + parseError.toString());
  54. }
  55. return;
  56. }
  57.  
  58. // load source model
  59. XMIResourceFactoryImpl _xmiFac = new XMIResourceFactoryImpl();
  60. EObject sourceModel = null;
  61. //File sourceModelFile = new File ("SM.ecore");
  62. File sourceModelFile = new File ("ServiceModel.uml");
  63. ResourceSet rSet = new ResourceSetImpl ();
  64. rSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*",
  65. _xmiFac);
  66. URI uri = URI.createFileURI(sourceModelFile.getAbsolutePath());
  67. Resource resource = rSet.getResource(uri, true);
  68.  
  69. if (resource != null) {
  70. if (resource.getContents().size() > 0) {
  71. sourceModel = (EObject) resource.getContents().get(0);
  72. }
  73. }
  74.  
  75. // set the source model for the exeution manager
  76. execMgr.addSourceModel(sourceModel);
  77. // sets the root output directory, if any is desired (e.g. "c:/temp")
  78. execMgr.setRootDirectory("");
  79. // if true, files are not generated to the file systsm, but populated into
  80. a filemodel
  81. // which can be fetched afterwards. Value false will result in standard
  82. file generation
  83. execMgr.setUseFileModel(false);
  84. // Turns on/off system logging
  85. execMgr.setUseLog(false);
  86. // Adds an output listener for the transformation execution.
  87. execMgr.getExecutionStack().addOutputMessageListener(this);
  88. try {
  89.  
  90. execMgr.executeTransformation();
  91. } catch (MofScriptExecutionException mex) {
  92. mex.printStackTrace();
  93. }
  94.  
  95. //
  96. // Alternatively, we can use the file model,
  97. // in which case no files will be stored, only added to
  98. // an file model (org.eclipse.mofscript.fileresourcemodel)
  99. //
  100. //
  101. execMgr.setUseFileModel(true);
  102. try {
  103. execMgr.executeTransformation();
  104. FileResourcesType fileResource = execMgr.getFileModel();
  105. EList<FileResource> fileResources=fileResource.getFileResource();
  106. for (Iterator<FileResource> fileit = fileResources.iterator();
  107. fileit.hasNext();) {
  108. FileResource fResource = fileit.next();
  109. byte[] contents = fResource.getContent();
  110. // do something with the contents
  111. }
  112. } catch (MofScriptExecutionException mfex) {
  113. }
  114. }
  115.  
  116. /**
  117. * Executing a transformation model directly (no parsing)
  118. */
  119. public void executeModelDirectly() {
  120. ResourceSet rSet = new ResourceSetImpl ();
  121. URI fileUri = URI.createFileURI("someTransformationFile.mofscript");
  122. Resource res = rSet.getResource(fileUri, true);
  123. // get the transformation specification from some resource...
  124. MOFScriptSpecification trSpec = (MOFScriptSpecification)
  125. res.getContents().get(0);
  126. // extract (one) transformation from the specification (it could in theory
  127. be several)
  128. MOFScriptTransformation transformation = (MOFScriptTransformation)
  129. trSpec.getTransformation().get(0);
  130. // set the transformation on the execution manager
  131. ExecutionManager execMgr = ExecutionManager.getExecutionManager();
  132. execMgr.setTransformationModel(transformation);
  133.  
  134. // load source model(s)
  135. EObject src = null;
  136. // add source model(s)
  137. execMgr.addSourceModel(src);
  138. // execute transformation
  139. try {
  140. execMgr.executeTransformation();
  141. } catch(MofScriptExecutionException mex) {
  142. }
  143.  
  144. // ...
  145. }
  146.  
  147. /**
  148. * ExecutionMessageListener interface operations
  149. */
  150. public void executionMessage (String type, String description) {
  151. System.out.println (type + " - " + description);
  152. }
  153.  
  154.  
  155. public static void main (String[] args){
  156. TestAPI api = new TestAPI ();
  157. api.test();
  158. }
  159. }
RAW Paste Data