1 28 29 package org.objectweb.openccm.uml.transformation.modfact; 30 31 import ispuml.mdaTransformation.model.jmi.JmiModelCreateUtils; 32 33 import java.io.File ; 34 import java.io.FileInputStream ; 35 import java.io.FileOutputStream ; 36 import java.io.InputStream ; 37 import java.util.Collection ; 38 39 import javax.jmi.reflect.RefPackage; 40 41 42 46 47 public class JmiModFactModelCreateUtils extends JmiModelCreateUtils { 48 49 54 public static RefPackage createModel(String metaModelURL, String metamodelJmiClasses) { 55 try { 56 Class metaModelClass = Class.forName(metamodelJmiClasses); 58 Class parameterType [] = { String .class }; 59 java.lang.reflect.Method createMethod = metaModelClass.getMethod("create", parameterType); 60 Object parameter [] = { metaModelURL }; 61 return (RefPackage)createMethod.invoke(null, parameter); 62 } catch (Exception ex) { 66 ex.printStackTrace(); 67 } 68 return null; 69 } 70 71 76 public static RefPackage getModelImpl(RefPackage extent) { 77 return extent.refOutermostPackage(); 78 } 79 80 86 public static void readModel(File file, RefPackage extent, String importer) { 87 try { 88 readModel(new FileInputStream (file), extent, importer); 89 } catch (java.lang.Exception ex) { 90 ex.printStackTrace(); 91 } 92 } 93 94 100 public static void readModel(InputStream input, RefPackage extent, String importer) { 101 try { 102 Class importerClass = Class.forName(importer); 104 Object importerInstance = importerClass.newInstance(); 105 java.lang.reflect.Method [] methods = importerClass.getMethods(); 106 java.lang.reflect.Method initiateMethod = null; 107 for (int i=0 ; i<methods.length ; i++) { 108 if (methods[i].getName().equals("initiate")) { 109 initiateMethod = methods[i]; 110 } 111 } 112 if (initiateMethod == null) 113 throw new Exception ("Method initiate not found in " + importer + " class."); 114 115 Object initParam [] = { extent }; 116 initiateMethod.invoke(importerInstance, initParam); 117 Class parseParamType [] = { InputStream .class }; 118 java.lang.reflect.Method parseMethod = importerClass.getMethod("parse", parseParamType); 119 Object parseParam [] = { input }; 120 parseMethod.invoke(importerInstance, parseParam); 121 122 127 132 } catch (java.lang.Exception ex) { 137 ex.printStackTrace(); 138 } 139 } 140 141 147 public static void writeModel(File file, RefPackage extent, String exporter) { 148 try { 149 org.objectweb.modfact.jmi.logging.ModFactLogger _logger; 155 _logger = org.objectweb.modfact.jmi.logging.ModFactLogger.getLogger("ModelXMIExport"); 156 157 Class exporterClass = Class.forName(exporter); 158 Class [] constructorParamType = { String .class, org.objectweb.modfact.jmi.logging.ModFactLogger.class }; 159 java.lang.reflect.Constructor constructor = exporterClass.getConstructor(constructorParamType); 160 Object [] constructorParam = { new String (), _logger }; 161 Object exporterInstance = constructor.newInstance(constructorParam); 162 164 FileOutputStream fileOutput = new FileOutputStream (file); 165 Class outputParamType [] = { java.io.OutputStream .class }; 166 Object outputParam [] = { fileOutput }; 167 exporterClass.getMethod("setOutput", outputParamType).invoke(exporterInstance, outputParam); 168 Class parseParamType [] = { Object .class }; 169 Object parseParam [] = { extent }; 170 exporterClass.getMethod("parse", parseParamType).invoke(exporterInstance, parseParam); 171 172 } catch (java.lang.Exception ex) { 177 ex.printStackTrace(); 178 } 179 } 180 181 186 public static Collection getRoots(RefPackage extent) { 187 Collection roots = new java.util.ArrayList (); 188 addRoots(roots, extent); 189 return roots; 190 } 191 192 } | Popular Tags |