KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > importer > ModelImporterApplication


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: ModelImporterApplication.java,v 1.10 2005/06/08 06:11:55 nickb Exp $
16  */

17 package org.eclipse.emf.importer;
18
19 import java.io.File JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import org.eclipse.core.resources.IWorkspaceRunnable;
29 import org.eclipse.core.resources.ResourcesPlugin;
30 import org.eclipse.core.runtime.CoreException;
31 import org.eclipse.core.runtime.IPath;
32 import org.eclipse.core.runtime.IPlatformRunnable;
33 import org.eclipse.core.runtime.IProgressMonitor;
34 import org.eclipse.core.runtime.IStatus;
35 import org.eclipse.core.runtime.NullProgressMonitor;
36 import org.eclipse.core.runtime.Path;
37 import org.eclipse.core.runtime.Status;
38 import org.eclipse.core.runtime.SubProgressMonitor;
39
40 import org.eclipse.emf.codegen.ecore.Generator;
41 import org.eclipse.emf.codegen.ecore.genmodel.GenModel;
42 import org.eclipse.emf.codegen.ecore.genmodel.GenModelFactory;
43 import org.eclipse.emf.codegen.ecore.genmodel.GenPackage;
44 import org.eclipse.emf.codegen.util.CodeGenUtil.StreamProgressMonitor;
45 import org.eclipse.emf.common.util.URI;
46 import org.eclipse.emf.ecore.EPackage;
47 import org.eclipse.emf.ecore.EcorePackage;
48 import org.eclipse.emf.ecore.resource.Resource;
49 import org.eclipse.emf.ecore.resource.ResourceSet;
50
51
52 /**
53  * @since 2.1.0
54  */

55 public abstract class ModelImporterApplication implements IPlatformRunnable
56 {
57   protected ModelImporter modelImporter;
58
59   protected boolean reload = false;
60   protected IPath modelProjectLocationPath;
61   protected IPath modelFragmentPath;
62   protected IPath editProjectLocationPath;
63   protected IPath editFragmentPath;
64   protected IPath editorProjectLocationPath;
65   protected IPath editorFragmentPath;
66   protected IPath testsProjectLocationPath;
67   protected IPath testsFragmentPath;
68   protected String JavaDoc templatePath;
69   protected String JavaDoc copyright;
70   protected boolean sdo = false;
71   protected String JavaDoc modelPluginID;
72
73   protected List JavaDoc referencedEPackages;
74   protected Map JavaDoc referencedGenModelPathToEPackageNSURIs;
75
76   protected boolean quiet = false;
77
78   public ModelImporter getModelImporter()
79   {
80     if (modelImporter == null)
81     {
82       modelImporter = createModelImporter();
83     }
84     return modelImporter;
85   }
86
87   protected abstract ModelImporter createModelImporter();
88
89   public Object JavaDoc run(final Object JavaDoc args) throws Exception JavaDoc
90   {
91     try
92     {
93       IWorkspaceRunnable runnable = new IWorkspaceRunnable()
94         {
95           public void run(IProgressMonitor progressMonitor) throws CoreException
96           {
97             try
98             {
99               ModelImporterApplication.this.run(progressMonitor, (String JavaDoc[])args);
100             }
101             catch (Exception JavaDoc exception)
102             {
103               throw new CoreException(new Status(
104                 IStatus.ERROR,
105                 ImporterPlugin.getPlugin().getBundle().getSymbolicName(),
106                 0,
107                 "Error",
108                 exception));
109             }
110             finally
111             {
112               progressMonitor.done();
113             }
114           }
115         };
116
117       ResourcesPlugin.getWorkspace().run(runnable, getProgressMonitor());
118       return new Integer JavaDoc(0);
119     }
120     catch (Exception JavaDoc exception)
121     {
122       if (!quiet)
123       {
124         exception.printStackTrace();
125       }
126       ImporterPlugin.INSTANCE.log(exception);
127       return new Integer JavaDoc(1);
128     }
129   }
130
131   public void run(IProgressMonitor progressMonitor, String JavaDoc[] arguments) throws Exception JavaDoc
132   {
133     try
134     {
135       progressMonitor.beginTask("", 1);
136
137       if (arguments.length == 0 || "-help".equalsIgnoreCase(arguments[0].toString()))
138       {
139         printUsage();
140       }
141       else
142       {
143         processArguments(arguments, 0);
144         execute(new SubProgressMonitor(progressMonitor, 1));
145       }
146     }
147     finally
148     {
149       progressMonitor.done();
150     }
151   }
152
153   protected IProgressMonitor getProgressMonitor()
154   {
155     return quiet ? new NullProgressMonitor() : new StreamProgressMonitor(System.out);
156   }
157
158   public void printUsage()
159   {
160     System.out.println(getUsage());
161   }
162
163   protected abstract StringBuffer JavaDoc getUsage();
164
165   protected static final String JavaDoc NL = System.getProperties().getProperty("line.separator");
166
167   protected StringBuffer JavaDoc appendLine(StringBuffer JavaDoc buffer, String JavaDoc line)
168   {
169     if (line != null)
170     {
171       buffer.append(line);
172     }
173     buffer.append(NL);
174     return buffer;
175   }
176
177   public void execute(IProgressMonitor progressMonitor) throws Exception JavaDoc
178   {
179     try
180     {
181       progressMonitor.beginTask("", 6);
182
183       adjustAttributes(new SubProgressMonitor(progressMonitor, 1));
184       adjustModelImporter(new SubProgressMonitor(progressMonitor, 1));
185   
186       computeEPackages(new SubProgressMonitor(progressMonitor, 1));
187       adjustEPackages(new SubProgressMonitor(progressMonitor, 1));
188       adjustGenModel(new SubProgressMonitor(progressMonitor, 1));
189   
190       doExecute(new SubProgressMonitor(progressMonitor, 1));
191     }
192     finally
193     {
194       progressMonitor.done();
195     }
196   }
197
198   protected void processArguments(String JavaDoc[] arguments, int index)
199   {
200     while (index < arguments.length)
201     {
202       index = processArgument(arguments, index);
203     }
204   }
205
206   protected int processArgument(String JavaDoc[] arguments, int index)
207   {
208     if (arguments[index].equalsIgnoreCase("-reload"))
209     {
210       reload = true;
211     }
212     else if (arguments[index].equalsIgnoreCase("-modelProject"))
213     {
214       modelProjectLocationPath = new Path(new File JavaDoc(arguments[++index]).getAbsolutePath());
215       modelFragmentPath = new Path(arguments[++index]);
216     }
217     else if (arguments[index].equalsIgnoreCase("-editProject"))
218     {
219       editProjectLocationPath = new Path(new File JavaDoc(arguments[++index]).getAbsolutePath());
220       editFragmentPath = new Path(arguments[++index]);
221     }
222     else if (arguments[index].equalsIgnoreCase("-editorProject"))
223     {
224       editorProjectLocationPath = new Path(new File JavaDoc(arguments[++index]).getAbsolutePath());
225       editorFragmentPath = new Path(arguments[++index]);
226     }
227     else if (arguments[index].equalsIgnoreCase("-testsProject"))
228     {
229       testsProjectLocationPath = new Path(new File JavaDoc(arguments[++index]).getAbsolutePath());
230       testsFragmentPath = new Path(arguments[++index]);
231     }
232     else if (arguments[index].equalsIgnoreCase("-modelPluginID"))
233     {
234       modelPluginID = arguments[++index];
235     }
236     else if (arguments[index].equalsIgnoreCase("-copyright"))
237     {
238       copyright = arguments[++index];
239     }
240     else if (arguments[index].equalsIgnoreCase("-sdo"))
241     {
242       sdo = true;
243     }
244     else if (arguments[index].equalsIgnoreCase("-quiet"))
245     {
246       quiet = true;
247     }
248     else if (arguments[index].equalsIgnoreCase("-templatePath"))
249     {
250       templatePath = URI.createFileURI(new File JavaDoc(arguments[++index]).getAbsolutePath()).toString();
251     }
252     else if (arguments[index].equalsIgnoreCase("-refGenModel"))
253     {
254       if (referencedGenModelPathToEPackageNSURIs == null)
255       {
256         referencedGenModelPathToEPackageNSURIs = new HashMap JavaDoc();
257       }
258
259       IPath genModelPath = new Path(new File JavaDoc(arguments[++index]).getAbsolutePath());
260       Set JavaDoc ePackageNSURIs = (Set JavaDoc)referencedGenModelPathToEPackageNSURIs.get(genModelPath);
261       if (ePackageNSURIs == null)
262       {
263         ePackageNSURIs = new HashSet JavaDoc();
264         referencedGenModelPathToEPackageNSURIs.put(genModelPath, ePackageNSURIs);
265       }
266
267       do
268       {
269         ePackageNSURIs.add(arguments[++index]);
270       }
271       while (index + 1 < arguments.length && !arguments[index + 1].startsWith("-"));
272     }
273     else
274     {
275       handleUnrecognizedArgument(arguments[index]);
276     }
277     return index + 1;
278   }
279
280   protected void handleUnrecognizedArgument(String JavaDoc argument)
281   {
282     throw new IllegalArgumentException JavaDoc("Unrecognized argument: '" + argument + "'");
283   }
284
285   protected void adjustAttributes(IProgressMonitor progressMonitor)
286   {
287     try
288     {
289       progressMonitor.beginTask("", 1);
290
291       if (modelProjectLocationPath == null)
292       {
293         modelProjectLocationPath = new Path(new File JavaDoc(".").getAbsolutePath());
294       }
295   
296       if (modelFragmentPath == null)
297       {
298         modelFragmentPath = new Path(".");
299       }
300     }
301     finally
302     {
303       progressMonitor.done();
304     }
305   }
306
307   protected void adjustModelImporter(IProgressMonitor progressMonitor)
308   {
309     try
310     {
311       progressMonitor.beginTask("", 1);
312
313       ModelImporter modelImporter = getModelImporter();
314       modelImporter.setUsePlatformURI(false);
315       modelImporter.setGenModelProjectLocation(modelProjectLocationPath);
316       if (modelPluginID != null)
317       {
318         modelImporter.setModelPluginID(modelPluginID);
319       }
320       if (modelProjectLocationPath != null)
321       {
322         modelImporter.setModelPluginDirectory(modelProjectLocationPath + "/./" + modelFragmentPath + "/.");
323       }
324     }
325     finally
326     {
327       progressMonitor.done();
328     }
329   }
330
331   protected void handleGenModelPath(IPath genModelFullPath)
332   {
333     ModelImporter modelImporter = getModelImporter();
334     if (reload)
335     {
336       modelImporter.defineOriginalGenModelPath(genModelFullPath);
337     }
338     else
339     {
340       modelImporter.setGenModelContainerPath(genModelFullPath.removeLastSegments(1));
341       modelImporter.setGenModelFileName(genModelFullPath.lastSegment());
342     }
343   }
344
345   protected final void computeEPackages(IProgressMonitor progressMonitor) throws Exception JavaDoc
346   {
347     try
348     {
349       progressMonitor.beginTask("", 1);
350       getModelImporter().computeEPackages(new SubProgressMonitor(progressMonitor, 1));
351     }
352     finally
353     {
354       progressMonitor.done();
355     }
356   }
357
358   protected void adjustEPackages(IProgressMonitor progressMonitor)
359   {
360     try
361     {
362       progressMonitor.beginTask("", 1);
363
364       if (referencedGenModelPathToEPackageNSURIs != null)
365       {
366         for (Iterator JavaDoc i = referencedGenModelPathToEPackageNSURIs.entrySet().iterator(); i.hasNext();)
367         {
368           Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
369           IPath genModelPath = (IPath)entry.getKey();
370           Set JavaDoc ePackageNSURIs = (Set JavaDoc)entry.getValue();
371   
372           ResourceSet resourceSet = getModelImporter().createResourceSet();
373           Resource resource = resourceSet.getResource(URI.createFileURI(genModelPath.toOSString()), true);
374           GenModel referencedGenModel = (GenModel)resource.getContents().get(0);
375           for (Iterator JavaDoc j = referencedGenModel.getGenPackages().iterator(); j.hasNext();)
376           {
377             GenPackage genPackage = (GenPackage)j.next();
378             if (ePackageNSURIs.contains(genPackage.getEcorePackage().getNsURI()))
379             {
380               getModelImporter().getReferencedGenPackages().add(genPackage);
381             }
382           }
383         }
384       }
385     }
386     finally
387     {
388       progressMonitor.done();
389     }
390   }
391   
392   protected void handleQualifiedEPackageName(EPackage ePackage)
393   {
394     String JavaDoc packageName = ePackage.getName();
395     int index = packageName.lastIndexOf(".");
396     if (index != -1)
397     {
398       getModelImporter().getEPackageInfo(ePackage).setBasePackage(packageName.substring(0, index));
399       ePackage.setName(packageName.substring(index + 1));
400     }
401   }
402
403   protected void handleEPackage(EPackage ePackage, boolean generate)
404   {
405     getModelImporter().getEPackageInfo(ePackage).setGenerate(generate);
406     if (!generate)
407     {
408       if (referencedEPackages == null)
409       {
410         referencedEPackages = new ArrayList JavaDoc();
411       }
412       referencedEPackages.add(ePackage);
413     }
414   }
415
416   protected void adjustGenModel(IProgressMonitor progressMonitor)
417   {
418     try
419     {
420       progressMonitor.beginTask("", 1);
421
422       GenModel genModel = getModelImporter().getGenModel();
423       if (editProjectLocationPath != null)
424       {
425         genModel.setEditDirectory(editProjectLocationPath + "/./" + editFragmentPath + "/.");
426       }
427       if (editorProjectLocationPath != null)
428       {
429         genModel.setEditorDirectory(editorProjectLocationPath + "/./" + editorFragmentPath + "/.");
430       }
431       if (templatePath != null)
432       {
433         genModel.setTemplateDirectory(templatePath);
434         genModel.setDynamicTemplates(true);
435       }
436       if (copyright != null)
437       {
438         genModel.setCopyrightText(copyright);
439       }
440       if (sdo)
441       {
442         setSDODefaults(genModel);
443       }
444     }
445     finally
446     {
447       progressMonitor.done();
448     }
449   }
450
451   protected void setSDODefaults(GenModel genModel)
452   {
453     Generator.setSDODefaults(genModel);
454   }
455
456   protected void doExecute(IProgressMonitor progressMonitor) throws Exception JavaDoc
457   {
458     try
459     {
460       progressMonitor.beginTask("", 3);
461       getModelImporter().prepareGenModelAndEPackages(new SubProgressMonitor(progressMonitor, 1));
462       handleReferencedEPackages();
463       getModelImporter().saveGenModelAndEPackages(new SubProgressMonitor(progressMonitor, 1));
464     }
465     finally
466     {
467       progressMonitor.done();
468     }
469   }
470   
471   /**
472    * Handles the referencedEPackages contributed by the -refPackage argument option.
473    */

474   protected void handleReferencedEPackages()
475   {
476     // Add a dummy GenModel for referenced packages.
477
//
478
if (referencedEPackages != null && !referencedEPackages.isEmpty())
479     {
480       ModelImporter modelImporter = getModelImporter();
481       GenModel genModel = modelImporter.getGenModel();
482       Resource genModelResource = genModel.eResource();
483
484       GenModel referencedGenModel = GenModelFactory.eINSTANCE.createGenModel();
485       genModelResource.getContents().add(referencedGenModel);
486       referencedGenModel.initialize(referencedEPackages);
487       genModel.getUsedGenPackages().addAll(referencedGenModel.getGenPackages());
488       referencedGenModel.getForeignModel().addAll(genModel.getForeignModel());
489       modelImporter.traverseGenPackages(referencedGenModel.getGenPackages());
490
491       for (Iterator JavaDoc i = referencedGenModel.getGenPackages().iterator(); i.hasNext();)
492       {
493         GenPackage genPackage = (GenPackage)i.next();
494         EPackage ePackage = genPackage.getEcorePackage();
495         if (ePackage.eResource() == null)
496         {
497            modelImporter.addToResource(ePackage, genModelResource.getResourceSet());
498         }
499         
500         // Special case for a reference to Ecore to ensure that flag settings are respected and are set only for Ecore itself.
501
//
502
if (EcorePackage.eNS_URI.equals(ePackage.getNsURI()))
503         {
504           if (referencedGenModel.getGenPackages().size() == 1)
505           {
506             referencedGenModel.setModelName("ecore");
507             referencedGenModel.setBooleanFlagsField("eFlags");
508             referencedGenModel.setBooleanFlagsReservedBits(8);
509           }
510           else
511           {
512             i.remove();
513             GenModel ecoreGenModel = GenModelFactory.eINSTANCE.createGenModel();
514             genModel.eResource().getContents().add(ecoreGenModel);
515             ecoreGenModel.getGenPackages().add(genPackage);
516             ecoreGenModel.setBooleanFlagsField("eFlags");
517             ecoreGenModel.setBooleanFlagsReservedBits(8);
518             ecoreGenModel.getForeignModel().addAll(genModel.getForeignModel());
519             ecoreGenModel.setModelName("ecore");
520           }
521         }
522         else if (referencedGenModel.getModelName() == null)
523         {
524           referencedGenModel.setModelName(genPackage.getPackageName());
525         }
526       }
527     }
528   }
529 }
530
Popular Tags