1 17 package org.eclipse.emf.importer.ecore; 18 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 import org.eclipse.core.runtime.IProgressMonitor; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.core.runtime.Status; 25 26 import org.eclipse.emf.common.util.URI; 27 import org.eclipse.emf.ecore.EPackage; 28 import org.eclipse.emf.ecore.EcorePackage; 29 import org.eclipse.emf.ecore.resource.Resource; 30 import org.eclipse.emf.ecore.resource.ResourceSet; 31 import org.eclipse.emf.ecore.util.EcoreUtil; 32 import org.eclipse.emf.importer.ModelImporter; 33 34 35 public class EcoreImporter extends ModelImporter 36 { 37 public String getID() 38 { 39 return "org.eclipse.emf.importer.ecore"; 40 } 41 42 protected IStatus doComputeEPackages(IProgressMonitor progressMonitor) throws Exception 43 { 44 IStatus status = Status.OK_STATUS; 45 46 List locationURIs = getModelLocationURIs(); 47 if (locationURIs.isEmpty()) 48 { 49 status = new Status( 50 IStatus.ERROR, 51 EcoreImporterPlugin.getPlugin().getBundle().getSymbolicName(), 52 0, 53 EcoreImporterPlugin.INSTANCE.getString("_UI_SpecifyAValidCoreModel_message"), 54 null); 55 } 56 else 57 { 58 progressMonitor.beginTask("", 2); 59 progressMonitor.subTask(EcoreImporterPlugin.INSTANCE.getString("_UI_Loading_message", new Object []{ locationURIs })); 60 61 ResourceSet ecoreResourceSet = createResourceSet(); 62 for (Iterator i = locationURIs.iterator(); i.hasNext(); ) 63 { 64 URI ecoreModelLocation = (URI)i.next(); 65 ecoreResourceSet.getResource(ecoreModelLocation, true); 66 } 67 EcoreUtil.resolveAll(ecoreResourceSet); 68 69 for (Iterator i = ecoreResourceSet.getResources().iterator(); i.hasNext(); ) 70 { 71 Resource resource = (Resource)i.next(); 72 getEPackages().addAll(EcoreUtil.getObjectsByType(resource.getContents(), EcorePackage.eINSTANCE.getEPackage())); 73 } 74 } 75 return status; 76 } 77 78 public void addToResource(EPackage ePackage, ResourceSet resourceSet) 79 { 80 if (ePackage.eResource() != null && getGenModel().eResource() != null) 81 { 82 URI ePackageURI = ePackage.eResource().getURI(); 83 URI genModelURI = getGenModel().eResource().getURI(); 84 85 if (!ePackageURI.trimSegments(1).equals(genModelURI.trimSegments(1))) 86 { 87 ePackage.eResource().getContents().remove(ePackage); 88 } 89 } 90 super.addToResource(ePackage, resourceSet); 91 } 92 93 protected void adjustGenModel(IProgressMonitor progressMonitor) 94 { 95 super.adjustGenModel(progressMonitor); 96 97 URI genModelURI = createFileURI(getGenModelPath().toString()); 98 for (Iterator i = getModelLocationURIs().iterator(); i.hasNext();) 99 { 100 getGenModel().getForeignModel().add(makeRelative((URI)i.next(), genModelURI).toString()); 101 } 102 } 103 104 protected void loadOriginalGenModel(URI genModelURI) 105 { 106 super.loadOriginalGenModel(genModelURI); 107 108 StringBuffer text = new StringBuffer (); 109 for (Iterator i = getOriginalGenModel().getForeignModel().iterator(); i.hasNext();) 110 { 111 String value = (String )i.next(); 112 if (value.endsWith(".ecore") || value.endsWith(".emof")) 113 { 114 text.append(makeAbsolute(URI.createURI(value), genModelURI).toString()); 115 text.append(" "); 116 } 117 } 118 setModelLocation(text.toString().trim()); 119 } 120 } | Popular Tags |