KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > importer > ecore > EcoreImporter


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2005 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: EcoreImporter.java,v 1.5 2005/06/29 00:21:39 marcelop Exp $
16  */

17 package org.eclipse.emf.importer.ecore;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
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 JavaDoc getID()
38   {
39     return "org.eclipse.emf.importer.ecore";
40   }
41
42   protected IStatus doComputeEPackages(IProgressMonitor progressMonitor) throws Exception JavaDoc
43   {
44     IStatus status = Status.OK_STATUS;
45
46     List JavaDoc 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 JavaDoc []{ locationURIs }));
60
61       ResourceSet ecoreResourceSet = createResourceSet();
62       for (Iterator JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc text = new StringBuffer JavaDoc();
109     for (Iterator JavaDoc i = getOriginalGenModel().getForeignModel().iterator(); i.hasNext();)
110     {
111       String JavaDoc value = (String JavaDoc)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