KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ant > taskdefs > codegen > ecore > GeneratorTask


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2004-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: GeneratorTask.java,v 1.9 2005/06/08 06:17:17 nickb Exp $
16  */

17 package org.eclipse.emf.ant.taskdefs.codegen.ecore;
18
19 import java.io.File JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.types.Commandline;
25 import org.apache.tools.ant.types.EnumeratedAttribute;
26
27 import org.eclipse.emf.ant.taskdefs.EMFTask;
28 import org.eclipse.emf.codegen.ecore.Generator;
29
30
31 /**
32  * <p>
33  * Base class for all the &quot;generator tasks&quot; that provides the common attributes
34  * and behavior. A &quot;generator task&quot; is an Ant task that encompasses the
35  * two operations performed when generating code with EMF: the creation of the EMF files
36  * (genmodel and ecore) from a given model specification and the code generation.
37  * </p>
38  * <p>
39  * These are the common attributes provided by this class:
40  * </p>
41  * <table order="1" cellpadding="2" cellspacing="0">
42  * <tr><td><b>Attribute</b></td><td><b>Description</b></td></tr>
43  * <tr>
44  * <td valign="top">model</td>
45  * <td>The file that defines the model.</td>
46  * </tr>
47  * <tr>
48  * <td valign="top">genModel</td>
49  * <td>The .genmodel file.</td>
50  * </tr>
51  * <tr>
52  * <td valign="top">reconcileGenModel</td>
53  * <td>Specifies how the genmodel file is to be supposed to be handled. The possible values are:
54  * <ul>
55  * <li>overwrite - <i>(default)</i>Replaces the genmodel file if it exists;</li>
56  * <li>keep - Doesn't generate the genmodel if one is provided; and</li>
57  * <li>reload - Reloads the genmodel to reflect model changes.</li>
58  * </ul>
59  * </td>
60  * </tr>
61  * <tr>
62  * <td valign="top">generateJavaCode</td>
63  * <td>Boolean value indicating whether the java code should be generated. Should be set to
64  * <tt>false</tt> when you want to generate only the .ecore and .genmodel files</td>
65  * </tr>
66  * <tr>
67  * <td valign="top">templatePath</td>
68  * <td>The directory where your customized JET templates are located.</td>
69  * </tr>
70  * <tr>
71  * <td valign="top">modelProject</td>
72  * <td>The directory where the files generated for the model will be placed into.</td>
73  * </tr>
74  * <tr>
75  * <td valign="top">modelProjectFragmentPath</td>
76  * <td>The model project relative path of the source folder.</td>
77  * </tr>
78  * <tr>
79  * <td valign="top">modelPluginID</td>
80  * <td>The ID of the generated model plugin.</td>
81  * </tr>
82  * <tr>
83  * <td valign="top">sdo</td>
84  * <td>Boolean value indicating whether the SDO API should be generated.</td>
85  * </tr>
86  * <tr>
87  * <td valign="top">copyright</td>
88  * <td>The copyright text.</td>
89  * </tr>
90  * </table>
91  *
92  * @since 2.1.0
93  */

94 public abstract class GeneratorTask extends EMFTask
95 {
96   public static class ReconcileGenModelType extends EnumeratedAttribute
97   {
98     public String JavaDoc[] getValues()
99     {
100       return new String JavaDoc []{ "overwrite", "keep", "reload" };
101     }
102   }
103
104   private static final int GENMODEL_OVERWRITE = 0;
105   private static final int GENMODEL_KEEP = 1;
106   private static final int GENMODEL_RELOAD = 2;
107
108   private File JavaDoc model;
109   private File JavaDoc genModel;
110   private File JavaDoc modelProject;
111   private String JavaDoc modelPluginID;
112   private String JavaDoc modelProjectFragmentPath;
113   private File JavaDoc templatePath;
114   private String JavaDoc copyright;
115   private boolean sdo = false;
116
117   private int reconcileGenModel = GENMODEL_OVERWRITE;
118   private boolean generateJavaCode = true;
119
120   protected Commandline commandline;
121   protected boolean useModelAttribute = true;
122   protected boolean generateModelProject = true;
123   protected boolean generateEditProject = true;
124   protected boolean generateEditorProject = true;
125
126   public void setModel(File JavaDoc model)
127   {
128     this.model = model;
129   }
130
131   public void setGenModel(File JavaDoc genModel)
132   {
133     this.genModel = genModel;
134   }
135
136   public void setModelProject(File JavaDoc modelProject)
137   {
138     this.modelProject = modelProject;
139   }
140
141   public void setModelProjectFragmentPath(String JavaDoc modelProjectFragmentPath)
142   {
143     this.modelProjectFragmentPath = modelProjectFragmentPath;
144   }
145
146   public void setModelPluginID(String JavaDoc modelPluginID)
147   {
148     this.modelPluginID = modelPluginID;
149   }
150
151   public Commandline.Argument createArg()
152   {
153     return getCommandline().createArgument();
154   }
155
156   public void setTemplatePath(File JavaDoc templatePath)
157   {
158     this.templatePath = templatePath;
159   }
160
161   public void setCopyright(String JavaDoc copyright)
162   {
163     this.copyright = copyright;
164   }
165
166   public void setSDO(boolean sdo)
167   {
168     this.sdo = sdo;
169   }
170
171   public void setReconcileGenModel(ReconcileGenModelType type)
172   {
173     String JavaDoc value = type.getValue();
174     if ("overwrite".equals(value))
175     {
176       reconcileGenModel = GENMODEL_OVERWRITE;
177     }
178     else if ("keep".equals(value))
179     {
180       reconcileGenModel = GENMODEL_KEEP;
181     }
182     else if ("reload".equals(value))
183     {
184       reconcileGenModel = GENMODEL_RELOAD;
185     }
186   }
187
188   public void setReconcileGenModel(String JavaDoc type)
189   {
190     if ("overwrite".equals(type))
191     {
192       reconcileGenModel = GENMODEL_OVERWRITE;
193     }
194     else if ("keep".equals(type))
195     {
196       reconcileGenModel = GENMODEL_KEEP;
197     }
198     else if ("reload".equals(type))
199     {
200       reconcileGenModel = GENMODEL_RELOAD;
201     }
202   }
203
204   public void setGenerateJavaCode(boolean generateJavaCode)
205   {
206     this.generateJavaCode = generateJavaCode;
207   }
208
209   protected Commandline getCommandline()
210   {
211     if (commandline == null)
212     {
213       commandline = new Commandline();
214     }
215     return commandline;
216   }
217
218   protected void checkAttributes() throws BuildException
219   {
220     if (useModelAttribute)
221     {
222       assertTrue("The 'model' attribute must be specified.", model != null);
223     }
224     
225     if (reconcileGenModel != GENMODEL_RELOAD)
226     {
227       assertTrue("The 'modelProject' attribute must be specified.", modelProject != null);
228     }
229
230     assertTrue("The 'genModel' attribute must be specified.", genModel != null);
231     assertTrue("The specifed 'templatePath' attribute is not a valid directory.", templatePath == null || templatePath.isDirectory());
232   }
233
234   protected void doExecute() throws Exception JavaDoc
235   {
236     switch (reconcileGenModel)
237     {
238       case GENMODEL_KEEP:
239       {
240         if (genModel.exists()) break;
241       }
242       case GENMODEL_RELOAD:
243       case GENMODEL_OVERWRITE:
244       {
245         addGenModelArguments();
246         adjustEditAndEditorProjects();
247         createGenModel(getCommandline().getArguments());
248         break;
249       }
250     }
251
252     if (generateJavaCode)
253     {
254       List JavaDoc arguments = getGeneratorArguments();
255       generateCodeFromGenModel((String JavaDoc[])arguments.toArray(new String JavaDoc [arguments.size()]));
256     }
257   }
258
259   abstract protected void createGenModel(String JavaDoc[] arguments) throws Exception JavaDoc;
260
261   protected void addGenModelArguments()
262   {
263     if (genModel != null)
264     {
265       getCommandline().createArgument(true).setValue(genModel.getAbsolutePath());
266       if (reconcileGenModel == GENMODEL_RELOAD)
267       {
268         getCommandline().createArgument().setValue("-reload");
269       }
270     }
271     
272     if (model != null)
273     {
274       getCommandline().createArgument(true).setValue(model.getAbsolutePath());
275     }
276
277     if (modelProject != null)
278     {
279       getCommandline().createArgument().setValue("-modelProject");
280       getCommandline().createArgument().setValue(modelProject.getAbsolutePath());
281     }
282     
283     if (modelProjectFragmentPath != null)
284     {
285       getCommandline().createArgument().setValue(modelProjectFragmentPath);
286     }
287
288     if (templatePath != null)
289     {
290       getCommandline().createArgument().setValue("-templatePath");
291       getCommandline().createArgument().setValue(templatePath.getAbsolutePath());
292     }
293
294     if (modelPluginID != null)
295     {
296       getCommandline().createArgument().setValue("-modelPluginID");
297       getCommandline().createArgument().setValue(modelPluginID);
298     }
299
300     if (copyright != null)
301     {
302       getCommandline().createArgument().setValue("-copyright");
303       getCommandline().createArgument().setValue(copyright);
304     }
305
306     if (sdo)
307     {
308       getCommandline().createArgument().setValue("-sdo");
309     }
310   }
311
312   protected void adjustEditAndEditorProjects()
313   {
314     String JavaDoc arguments = getCommandline().toString();
315     generateModelProject = arguments.indexOf("-modelProject") >= 0;
316     generateEditProject = arguments.indexOf("-editProject") >= 0;
317     generateEditorProject = arguments.indexOf("-editorProject") >= 0;
318   }
319
320   protected List JavaDoc getGeneratorArguments()
321   {
322     List JavaDoc arguments = new ArrayList JavaDoc();
323
324     if (generateModelProject)
325       arguments.add("-model");
326     if (generateEditProject)
327       arguments.add("-edit");
328     if (generateEditorProject)
329       arguments.add("-editor");
330
331     arguments.add(genModel.getAbsolutePath());
332     return arguments;
333   }
334
335   protected void generateCodeFromGenModel(String JavaDoc[] arguments)
336   {
337     new Generator().run(arguments);
338   }
339 }
Popular Tags