1 17 package org.eclipse.emf.ant.taskdefs.codegen.ecore; 18 19 import java.io.File ; 20 import java.util.ArrayList ; 21 import java.util.List ; 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 94 public abstract class GeneratorTask extends EMFTask 95 { 96 public static class ReconcileGenModelType extends EnumeratedAttribute 97 { 98 public String [] getValues() 99 { 100 return new String []{ "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 model; 109 private File genModel; 110 private File modelProject; 111 private String modelPluginID; 112 private String modelProjectFragmentPath; 113 private File templatePath; 114 private String 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 model) 127 { 128 this.model = model; 129 } 130 131 public void setGenModel(File genModel) 132 { 133 this.genModel = genModel; 134 } 135 136 public void setModelProject(File modelProject) 137 { 138 this.modelProject = modelProject; 139 } 140 141 public void setModelProjectFragmentPath(String modelProjectFragmentPath) 142 { 143 this.modelProjectFragmentPath = modelProjectFragmentPath; 144 } 145 146 public void setModelPluginID(String 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 templatePath) 157 { 158 this.templatePath = templatePath; 159 } 160 161 public void setCopyright(String 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 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 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 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 arguments = getGeneratorArguments(); 255 generateCodeFromGenModel((String [])arguments.toArray(new String [arguments.size()])); 256 } 257 } 258 259 abstract protected void createGenModel(String [] arguments) throws Exception ; 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 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 getGeneratorArguments() 321 { 322 List arguments = new ArrayList (); 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 [] arguments) 336 { 337 new Generator().run(arguments); 338 } 339 } | Popular Tags |