KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ant > taskdefs > codegen > JETEmitterTask


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: JETEmitterTask.java,v 1.4 2005/06/08 06:17:17 nickb Exp $
16  */

17 package org.eclipse.emf.ant.taskdefs.codegen;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.tools.ant.BuildException;
26
27 import org.eclipse.emf.ant.taskdefs.EMFTask;
28 import org.eclipse.emf.ant.util.Util;
29 import org.eclipse.emf.codegen.jet.JETEmitter;
30 import org.eclipse.emf.codegen.jet.JETException;
31 import org.eclipse.emf.common.util.URI;
32
33
34 /**
35  * <p>
36  * Exposes some functionalities available on the
37  * {@link org.eclipse.emf.codegen.jet.JETEmitter JETEmitter} class.
38  * </p>
39  * <p>
40  * This task is supposed to be executed by a Eclipse driver with the
41  * <b>org.eclipse.emf.ant</b> plugin. It is neither necessary to use Ant's task
42  * <tt>TaskDef</tt> to declare this task in a script nor to change the Ant's runtime
43  * classpath.
44  * </p>
45  * <p>
46  * The following command line will start a headless Eclipse instance and run the specified Ant script.
47  * </p>
48  * <p>
49  * java -classpath <i>eclipseDir</i>/startup.jar org.eclipse.core.launcher.Main
50  * -data <i>worspaceDir</i>
51  * -application org.eclipse.ant.core.antRunner
52  * -buildfile <i>antScript</i>
53  * </p>
54  * <p>
55  * Usage examples:
56  * </p>
57  * <pre>
58  * &lt;emf.JETEmitter templateURI=&quot;http://www.example.com/jetTemplate.txtjet&quot;
59  * newFile=&quot;c:\output.txt&quot;/&gt;
60  * </pre>
61  * <pre>
62  * &lt;emf.JETEmitter templateFile=&quot;jetTemplate.txtjet&quot;
63  * newFile=&quot;c:\output.txt&quot;/&gt;
64  * </pre>
65  * <pre>
66  * &lt;emf.JETEmitter templateURI=&quot;d:\templates\jetTemplate.txtjet&quot;
67  * newFile=&quot;c:\output.txt&quot;&gt;
68  * &lt;variable name=&quot;MY_VAR&quot; pluginID=&quot;com.myplugin;/&gt;
69  * &lt;variable name=&quot;JUNIT_HOME&quot; pluginID=&quot;org.eclipse.jdt.junit;/&gt;
70  * &lt;/emf.JETEmitter&gt;
71  * </pre>
72  *
73  * @since 2.1.0
74  */

75 public class JETEmitterTask extends EMFTask
76 {
77   public static class Variable
78   {
79     private String JavaDoc name;
80     private String JavaDoc pluginID;
81
82     public String JavaDoc getName()
83     {
84       return name;
85     }
86
87     public void setName(String JavaDoc name)
88     {
89       this.name = name;
90     }
91
92     public String JavaDoc getPluginID()
93     {
94       return pluginID;
95     }
96
97     public void setPluginID(String JavaDoc pluginID)
98     {
99       this.pluginID = pluginID;
100     }
101   }
102
103   private String JavaDoc templateURI;
104   private File JavaDoc templateFile;
105   private File JavaDoc newFile;
106   private String JavaDoc project;
107   private List JavaDoc variables;
108   private Object JavaDoc argument;
109   private Class JavaDoc argumentClass;
110
111   public void setTemplateFile(File JavaDoc templateFile)
112   {
113     this.templateFile = templateFile;
114   }
115
116   public void setTemplateURI(String JavaDoc templateURI)
117   {
118     this.templateURI = templateURI;
119   }
120
121   public void setNewFile(File JavaDoc newFile)
122   {
123     this.newFile = newFile;
124   }
125
126   public void setProject(String JavaDoc project)
127   {
128     this.project = project;
129   }
130
131   public Variable createVariable()
132   {
133     Variable variable = new Variable();
134     if (variables == null)
135     {
136       variables = new ArrayList JavaDoc();
137     }
138     variables.add(variable);
139     return variable;
140   }
141
142   public void setArgument(Object JavaDoc argument)
143   {
144     this.argument = argument;
145   }
146
147   public void setArgumentClass(Class JavaDoc argumentClass)
148   {
149     this.argumentClass = argumentClass;
150   }
151   
152   protected String JavaDoc getTemplateURIAsString()
153   {
154     if (templateURI != null)
155     {
156       return templateURI.toString();
157     }
158     else if (templateFile != null)
159     {
160       try
161       {
162         templateFile = templateFile.getCanonicalFile();
163       }
164       catch (IOException JavaDoc e)
165       {
166       }
167       URI uri = templateFile.isFile() ? URI.createFileURI(templateFile.toString()) : URI.createURI(templateFile.toString());
168       return uri.toString();
169     }
170     else
171     {
172       return null;
173     }
174   }
175
176   protected void checkAttributes() throws BuildException
177   {
178     assertTrue("Either 'templateURI' or 'templateFile' must be specified.", templateURI != null || templateFile != null);
179     assertTrue("The 'newFile' attribute must be specified.", newFile != null);
180   }
181
182   protected void doExecute() throws Exception JavaDoc
183   {
184     invokeEmitter(createJETEmitter());
185   }
186
187   protected JETEmitter createJETEmitter() throws JETException
188   {
189     JETEmitter emitter = new JETEmitter(getTemplateURIAsString());
190     
191     if (project != null)
192     {
193       emitter.setProjectName(project);
194     }
195
196     if (variables != null)
197     {
198       for (Iterator JavaDoc i = variables.iterator(); i.hasNext();)
199       {
200         Variable variable = (Variable)i.next();
201         emitter.addVariable(variable.getName(), variable.getPluginID());
202       }
203     }
204
205     return emitter;
206   }
207
208   protected void invokeEmitter(JETEmitter emitter) throws JETException, IOException JavaDoc, InstantiationException JavaDoc, IllegalAccessException JavaDoc
209   {
210     Object JavaDoc[] arguments = null;
211     if (argument != null)
212     {
213       arguments = argument instanceof Object JavaDoc[] ? (Object JavaDoc[])argument : new Object JavaDoc []{ argument };
214     }
215     else if (argumentClass != null)
216     {
217       arguments = new Object JavaDoc []{ argumentClass.newInstance() };
218     }
219     else
220     {
221       arguments = new Object JavaDoc [1];
222     }
223
224     String JavaDoc result = emitter.generate(getProgressMonitor(), arguments);
225     Util.writeFile(newFile, result);
226   }
227 }
228
Popular Tags