1 17 package org.eclipse.emf.ant.taskdefs.codegen; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.List ; 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 75 public class JETEmitterTask extends EMFTask 76 { 77 public static class Variable 78 { 79 private String name; 80 private String pluginID; 81 82 public String getName() 83 { 84 return name; 85 } 86 87 public void setName(String name) 88 { 89 this.name = name; 90 } 91 92 public String getPluginID() 93 { 94 return pluginID; 95 } 96 97 public void setPluginID(String pluginID) 98 { 99 this.pluginID = pluginID; 100 } 101 } 102 103 private String templateURI; 104 private File templateFile; 105 private File newFile; 106 private String project; 107 private List variables; 108 private Object argument; 109 private Class argumentClass; 110 111 public void setTemplateFile(File templateFile) 112 { 113 this.templateFile = templateFile; 114 } 115 116 public void setTemplateURI(String templateURI) 117 { 118 this.templateURI = templateURI; 119 } 120 121 public void setNewFile(File newFile) 122 { 123 this.newFile = newFile; 124 } 125 126 public void setProject(String 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 (); 137 } 138 variables.add(variable); 139 return variable; 140 } 141 142 public void setArgument(Object argument) 143 { 144 this.argument = argument; 145 } 146 147 public void setArgumentClass(Class argumentClass) 148 { 149 this.argumentClass = argumentClass; 150 } 151 152 protected String 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 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 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 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 , InstantiationException , IllegalAccessException 209 { 210 Object [] arguments = null; 211 if (argument != null) 212 { 213 arguments = argument instanceof Object [] ? (Object [])argument : new Object []{ argument }; 214 } 215 else if (argumentClass != null) 216 { 217 arguments = new Object []{ argumentClass.newInstance() }; 218 } 219 else 220 { 221 arguments = new Object [1]; 222 } 223 224 String result = emitter.generate(getProgressMonitor(), arguments); 225 Util.writeFile(newFile, result); 226 } 227 } 228 | Popular Tags |