1 19 package org.netbeans.lib.jmi.uml2mof; 20 21 import java.io.File ; 22 import java.io.FileOutputStream ; 23 import java.util.Iterator ; 24 import javax.jmi.model.ModelPackage; 25 import javax.jmi.model.MofPackage; 26 import javax.jmi.xmi.XmiReader; 27 import javax.jmi.xmi.XmiWriter; 28 import org.netbeans.api.mdr.MDRManager; 29 import org.netbeans.api.mdr.MDRepository; 30 import org.omg.uml.UmlPackage; 31 import org.openide.ErrorManager; 32 import org.openide.util.Lookup; 33 34 38 public class Main { 39 private static final String MOF_INSTANCE = "MOFInstance"; 41 private static final String UML_INSTANCE = "UMLInstance"; 43 private static final String UML_MM = "UML"; 45 46 private static MDRepository rep; 48 private static UmlPackage uml; 50 private static ModelPackage mof; 52 private static XmiReader reader; 54 55 public static void main(String args[]) { 56 try { 57 rep = MDRManager.getDefault().getDefaultRepository(); 59 String uri = new File (args[0]).toURL().toString(); 62 FileOutputStream out = new FileOutputStream (args[1]); 65 reader = (XmiReader) Lookup.getDefault().lookup(XmiReader.class); 67 XmiWriter writer = (XmiWriter) Lookup.getDefault().lookup(XmiWriter.class); 69 70 init(); 73 74 rep.beginTrans(true); 76 try { 77 reader.read(uri, uml); 79 Transformer.execute(uml, mof); 81 writer.write(out, mof, null); 83 } finally { 84 rep.endTrans(true); 90 MDRManager.getDefault().shutdownAll(); 92 out.close(); 93 } 94 } catch (Exception e) { 95 ErrorManager.getDefault().notify(ErrorManager.ERROR, e); 96 } 97 } 98 99 100 private static void init() throws Exception { 101 mof = (ModelPackage) rep.getExtent(MOF_INSTANCE); 103 uml = (UmlPackage) rep.getExtent(UML_INSTANCE); 104 if (mof == null) { 108 mof = (ModelPackage) rep.createExtent(MOF_INSTANCE); 110 } 111 if (uml == null) { 112 uml = (UmlPackage) rep.createExtent(UML_INSTANCE, getUmlPackage()); 116 } 117 } 118 119 122 private static MofPackage getUmlPackage() throws Exception { 123 ModelPackage umlMM = (ModelPackage) rep.getExtent(UML_MM); 125 if (umlMM == null) { 126 umlMM = (ModelPackage) rep.createExtent(UML_MM); 128 } 129 MofPackage result = getUmlPackage(umlMM); 131 if (result == null) { 132 reader.read(UmlPackage.class.getResource("resources/01-02-15_Diff.xml").toString(), umlMM); 134 result = getUmlPackage(umlMM); 136 } 137 return result; 138 } 139 140 143 private static MofPackage getUmlPackage(ModelPackage umlMM) { 144 for (Iterator it = umlMM.getMofPackage().refAllOfClass().iterator(); it.hasNext();) { 146 MofPackage pkg = (MofPackage) it.next(); 147 if (pkg.getContainer() == null && "UML".equals(pkg.getName())) { 149 return pkg; 151 } 152 } 153 return null; 155 } 156 } 157 | Popular Tags |