KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > importer > rose > RoseImporter


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: RoseImporter.java,v 1.9 2005/06/08 06:20:36 nickb Exp $
16  */

17 package org.eclipse.emf.importer.rose;
18
19 import java.io.File JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.IStatus;
28 import org.eclipse.core.runtime.Path;
29 import org.eclipse.core.runtime.Status;
30
31 import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
32 import org.eclipse.emf.codegen.ecore.genmodel.GenPackage;
33 import org.eclipse.emf.common.util.URI;
34 import org.eclipse.emf.ecore.EPackage;
35 import org.eclipse.emf.importer.ImporterPlugin;
36 import org.eclipse.emf.importer.ModelImporter;
37 import org.eclipse.emf.importer.rose.builder.RoseUtil;
38 import org.eclipse.emf.importer.rose.builder.UnitTreeNode;
39 import org.eclipse.emf.importer.util.ImporterUtil;
40
41
42 /**
43  * @since 2.1.0
44  */

45 public class RoseImporter extends ModelImporter
46 {
47   protected Map JavaDoc pathMap;
48   protected boolean noQualify = false;
49   protected boolean unsettablePrimitive = false;
50   
51   protected RoseUtil roseUtil;
52   protected UnitTreeNode unitTreeNode;
53   
54   protected Map JavaDoc roseEPackageInformationMap;
55
56   public void dispose()
57   {
58     if (pathMap != null)
59     {
60       pathMap.clear();
61       pathMap = null;
62     }
63     if (roseEPackageInformationMap != null)
64     {
65       roseEPackageInformationMap.clear();
66       roseEPackageInformationMap = null;
67     }
68
69     super.dispose();
70   }
71
72   public String JavaDoc getID()
73   {
74     return "org.eclipse.emf.importer.rose";
75   }
76
77   public Map JavaDoc getPathMap()
78   {
79     if (pathMap == null)
80     {
81       pathMap = new HashMap JavaDoc();
82     }
83     return pathMap;
84   }
85   
86   public void setModelLocation(String JavaDoc location)
87   {
88     boolean isEqual = location == null ?
89       getModelLocation() == null
90       : location.equals(getModelLocation());
91     if (!isEqual)
92     {
93       getPathMap().clear();
94     }
95     super.setModelLocation(location);
96   }
97
98   public boolean isNoQualify()
99   {
100     return noQualify;
101   }
102
103   public void setNoQualify(boolean noQualify)
104   {
105     this.noQualify = noQualify;
106   }
107
108   public boolean isUnsettablePrimitive()
109   {
110     return unsettablePrimitive;
111   }
112
113   public void setUnsettablePrimitive(boolean unsettablePrimitive)
114   {
115     this.unsettablePrimitive = unsettablePrimitive;
116   }
117
118   public File JavaDoc computeRoseModelFile()
119   {
120     URI modelURI = getFirstModelLocationURI(true);
121     if (modelURI != null)
122     {
123       File JavaDoc modelFile = new File JavaDoc(modelURI.isFile() ? modelURI.toFileString() : modelURI.toString());
124       if (modelFile.isFile())
125       {
126         return modelFile;
127       }
128     }
129     return null;
130   }
131   
132   public void clearEPackagesCollections()
133   {
134     super.clearEPackagesCollections();
135     roseEPackageInformationMap = null;
136   }
137   
138   public IStatus loadPathMap(IProgressMonitor progressMonitor) throws Exception JavaDoc
139   {
140     clearEPackagesCollections();
141    
142     IStatus status = null;
143     
144     File JavaDoc roseModelFile = computeRoseModelFile();
145     if (roseModelFile == null)
146     {
147       status = new Status(IStatus.ERROR,
148         ImporterPlugin.ID, ImporterUtil.ACTION_DIALOG_NONE | ImporterUtil.ACTION_MESSAGE_SET_ERROR,
149         RoseImporterPlugin.INSTANCE.getString("_UI_SpecifyAValidRoseModel_message"), null);
150     }
151     else
152     {
153       String JavaDoc roseModelAbsolutePath = roseModelFile.getAbsolutePath();
154
155       progressMonitor.beginTask("", 2);
156       progressMonitor.subTask(RoseImporterPlugin.INSTANCE.getString("_UI_Loading_message", new Object JavaDoc []{ roseModelAbsolutePath }));
157
158       Map JavaDoc pathMap = getPathMap();
159       for (;;)
160       {
161         roseUtil = createRoseUtil();
162         roseUtil.getRoseEcoreBuilder().noQualify = noQualify;
163         roseUtil.getRoseEcoreBuilder().unsettablePrimitive = unsettablePrimitive;
164
165         roseUtil.getVariableToDirectoryMap().putAll(pathMap);
166         unitTreeNode = roseUtil.createRoseUnitTreeAndTable(roseModelAbsolutePath, null);
167         if (unitTreeNode == null)
168         {
169           status = new Status(IStatus.ERROR,
170             ImporterPlugin.ID, ImporterUtil.ACTION_DIALOG_NONE | ImporterUtil.ACTION_MESSAGE_SET_ERROR,
171             RoseImporterPlugin.INSTANCE.getString("_UI_SpecifyAValidRoseModel_message"), null);
172         }
173         else
174         {
175           boolean hasSymbolWithoutValue = adjustPathMap(roseUtil);
176           if (adjustKnownPathMapSymbols(roseModelAbsolutePath))
177           {
178             continue;
179           }
180           if (!hasSymbolWithoutValue)
181           {
182             status = ImporterUtil.mergeStatus(status, roseUtil.getStatus());
183           }
184           
185           if (getGenModelFileName() == null)
186           {
187             String JavaDoc fileName = unitTreeNode.getNodes().size() != 1
188               ? roseModelFile.getName() : ((UnitTreeNode)unitTreeNode.getNodes().get(0)).getRoseFileName();
189
190             int index = fileName.lastIndexOf(File.separatorChar);
191             if (index >= 0)
192             {
193               fileName = fileName.substring(index + 1);
194             }
195             index = fileName.lastIndexOf(".");
196             if (index >= 0)
197             {
198               fileName = fileName.substring(0, index);
199             }
200             setGenModelFileName(fileName + ".genmodel");
201           }
202         }
203         break;
204       }
205     }
206
207     return status != null ? status : Status.OK_STATUS;
208   }
209   
210   protected boolean adjustPathMap(RoseUtil roseUtil)
211   {
212     boolean hasSymbolWithoutValue = false;
213     Map JavaDoc currentPathMap = getPathMap();
214     Map JavaDoc pathMap = new HashMap JavaDoc();
215     for (Iterator JavaDoc i = roseUtil.getVariableToDirectoryMap().entrySet().iterator(); i.hasNext();)
216     {
217       Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
218       String JavaDoc symbol = (String JavaDoc)entry.getKey();
219       if (symbol != null)
220       {
221         String JavaDoc value = (String JavaDoc)entry.getValue();
222         if (value == null)
223         {
224           value = (String JavaDoc)currentPathMap.get(symbol);
225         }
226         pathMap.put(symbol, value);
227         hasSymbolWithoutValue = hasSymbolWithoutValue || (value == null);
228       }
229     }
230     currentPathMap.clear();
231     currentPathMap.putAll(pathMap);
232     return hasSymbolWithoutValue;
233   }
234   
235   protected boolean adjustKnownPathMapSymbols(String JavaDoc roseModelAbsolutePath)
236   {
237     return false;
238   }
239   
240   /**
241    * Returns whether the Rose model properties includes genmodel information about a given ePackage.
242    * This method should only be used after invoking
243    * {@link ModelImporter#computeEPackages(IProgressMonitor)}.
244    * @param ePackage
245    * @return boolean
246    */

247   public boolean hasRoseGenPackageProperties(EPackage ePackage)
248   {
249     return roseEPackageInformationMap != null && roseEPackageInformationMap.get(ePackage) != null;
250   }
251
252   protected IStatus doComputeEPackages(IProgressMonitor progressMonitor) throws Exception JavaDoc
253   {
254     progressMonitor.beginTask("", 2);
255     IStatus status = loadPathMap(progressMonitor);
256     if (status.isOK())
257     {
258       if (getPathMap().values().contains(null) && !roseUtil.getStatus().isOK())
259       {
260         status = new Status(IStatus.ERROR,
261           ImporterPlugin.ID, ImporterUtil.ACTION_DIALOG_NONE | ImporterUtil.ACTION_MESSAGE_SET_ERROR,
262           RoseImporterPlugin.INSTANCE.getString("_UI_SpecifyTheSymbolLocations_message"), null);
263       }
264       else
265       {
266         roseUtil.createExtent4RoseUnitTree(unitTreeNode);
267         roseUtil.processUnitTree(unitTreeNode);
268         roseEPackageInformationMap = roseUtil.getEPackageToInformationMap();
269         status = ImporterUtil.createStatus(roseUtil.getStatus(), ImporterPlugin.ID, ImporterUtil.ACTION_MESSAGE_NONE);
270         
271         for (Iterator JavaDoc i = roseUtil.getEPackageToInformationMap().entrySet().iterator(); i.hasNext();)
272         {
273           Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
274           List JavaDoc information = (List JavaDoc)entry.getValue();
275           if (information != null)
276           {
277             EPackageInfo ePackageInfo = getEPackageInfo((EPackage)entry.getKey());
278             ePackageInfo.setBasePackage((String JavaDoc)information.get(0));
279             ePackageInfo.setPrefix((String JavaDoc)information.get(1));
280           }
281         }
282   
283         traverseEPackages(unitTreeNode);
284       }
285     }
286     progressMonitor.done();
287     return status;
288   }
289
290   protected void traverseEPackages(UnitTreeNode subNode)
291   {
292     getEPackages().addAll(subNode.getExtent());
293
294     for (Iterator JavaDoc i = subNode.getExtent().iterator(); i.hasNext();)
295     {
296       EPackage ePackage = (EPackage)i.next();
297       EPackageInfo ePackageInfo = getEPackageInfo(ePackage);
298
299       String JavaDoc ecoreFileName = null;
300       String JavaDoc roseFileName = subNode.getRoseFileName();
301       int indexOfSlash = roseFileName.lastIndexOf(File.separator);
302
303       GenPackage genPackage = getGenPackage(ePackage);
304       if (genPackage != null)
305       {
306         String JavaDoc ePackagePath = genPackage.getEcorePackage().eResource().getURI().lastSegment();
307         ecoreFileName = URI.decode(ePackagePath);
308       }
309       else if (roseFileName.endsWith(".cat") && indexOfSlash != -1)
310       {
311         String JavaDoc baseName = roseFileName.substring(indexOfSlash + 1, roseFileName.length() - 4);
312         int indexOfDot = baseName.lastIndexOf(".");
313         if (indexOfDot != -1)
314         {
315           baseName = baseName.substring(indexOfDot + 1);
316         }
317         ecoreFileName = baseName + ".ecore";
318       }
319       else if (subNode.getExtent().size() == 1)
320       {
321         ecoreFileName = subNode.getName() + ".ecore";
322       }
323       else
324       {
325         ecoreFileName = ePackage.getName() + ".ecore";
326       }
327       ePackageInfo.setEcoreFileName(ecoreFileName);
328     }
329
330     for (Iterator JavaDoc i = subNode.getNodes().iterator(); i.hasNext();)
331     {
332       UnitTreeNode childTree = (UnitTreeNode)i.next();
333       traverseEPackages(childTree);
334     }
335   }
336   
337   protected void adjustGenModel(IProgressMonitor progressMonitor)
338   {
339     super.adjustGenModel(progressMonitor);
340
341     GenModel genModel = getGenModel();
342     String JavaDoc projectName = getModelProjectName();
343     IPath genModelPath = getGenModelPath();
344     URI modelURI = (URI)getModelLocationURIs().get(0);
345     URI genModelURI = getGenModel().eResource().getURI();
346
347     genModel.getForeignModel().add(makeRelative(modelURI, genModelURI).toFileString());
348     IPath projectLocation = getWorkspaceRoot().getProject(projectName).getLocation();
349     if (projectLocation != null)
350     {
351       projectLocation = projectLocation.removeLastSegments(1);
352     }
353     else
354     {
355       projectLocation = getGenModelProjectLocation();
356     }
357
358     for (Iterator JavaDoc i = getPathMap().entrySet().iterator(); i.hasNext();)
359     {
360       Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
361       String JavaDoc value = (String JavaDoc)entry.getValue();
362       if (value != null)
363       {
364         genModel.getForeignModel().add(entry.getKey());
365         if (new Path(value).equals(projectLocation))
366         {
367           value = "..";
368           for (int depth = genModelPath.segmentCount(); depth > 2; --depth)
369           {
370             value += "/..";
371           }
372           genModel.getForeignModel().add(value);
373         }
374         else
375         {
376           genModel.getForeignModel().add(value);
377         }
378       }
379     }
380   }
381
382   protected void loadOriginalGenModel(URI genModelURI)
383   {
384     super.loadOriginalGenModel(genModelURI);
385
386     try
387     {
388       Iterator JavaDoc values = getOriginalGenModel().getForeignModel().iterator();
389       if (values.hasNext())
390       {
391         URI modelURI = null;
392         String JavaDoc modelFile = (String JavaDoc)values.next();
393         modelFile = modelFile.replace('\\', '/');
394         if (modelFile.endsWith(".mdl"))
395         {
396           URI genModelFileURI = createFileURI(getOriginalGenModelPath().toString());
397           modelURI = makeAbsolute(URI.createFileURI(modelFile), genModelFileURI);
398           setModelLocation(modelURI.toString());
399           
400           while (values.hasNext())
401           {
402             String JavaDoc variable = (String JavaDoc)values.next();
403             String JavaDoc value = (String JavaDoc)values.next();
404             getPathMap().put(variable, value);
405           }
406         }
407       }
408     }
409     catch (Exception JavaDoc exception)
410     {
411       RoseImporterPlugin.INSTANCE.log(exception);
412     }
413   }
414
415   protected RoseUtil createRoseUtil()
416   {
417     RoseUtil roseUtil = new RoseUtil(createResourceSet().getURIConverter());
418     roseUtil.getRoseEcoreBuilder().noQualify = noQualify;
419     roseUtil.getRoseEcoreBuilder().unsettablePrimitive = unsettablePrimitive;
420     return roseUtil;
421   }
422 }
Popular Tags