1 /* 2 * Created on Dec 31, 2004 3 */ 4 package net.sf.smc.ant; 5 6 import java.io.File; 7 import java.util.*; 8 import org.apache.tools.ant.BuildException; 9 import org.apache.tools.ant.Project; 10 import org.apache.tools.ant.Task; 11 import org.apache.tools.ant.taskdefs.Java; 12 import org.apache.tools.ant.types.EnumeratedAttribute; 13 import org.apache.tools.ant.types.Path; 14 import org.apache.tools.ant.types.Reference; 15 16 /** 17 * <smc 18 * target="java|graph|table.." 19 * smfile="Turnstyle.sm" 20 * destdir="${build.classes.dir}" 21 * smcjar="bin/Smc.jar" 22 * suffix="jav" 23 * sync="true|false" 24 * serial="true|false" 25 * g="true|false" 26 * glevel="0|1|2" /> 27 * 28 * @author Eitan Suez 29 */ 30 public class SmcJarWrapper extends Task 31 { 32 private Path _classpath; 33 private File _smfile; 34 private File _destdir; 35 private String _target; 36 private File _smcjar; 37 private String _suffix; 38 private boolean _sync, _serial, _g; 39 private String _glevel; 40 41 private static Map DEFAULT_SUFFIXES = new HashMap(); 42 static 43 { 44 DEFAULT_SUFFIXES.put("c", "c"); 45 DEFAULT_SUFFIXES.put("c++", "cpp"); 46 DEFAULT_SUFFIXES.put("java", "java"); 47 DEFAULT_SUFFIXES.put("perl", "pl"); 48 DEFAULT_SUFFIXES.put("python", "py"); 49 DEFAULT_SUFFIXES.put("ruby", "rb"); 50 DEFAULT_SUFFIXES.put("tcl", "tcl"); 51 DEFAULT_SUFFIXES.put("vb", "vb"); 52 DEFAULT_SUFFIXES.put("csharp", "cs"); 53 DEFAULT_SUFFIXES.put("table", "html"); 54 DEFAULT_SUFFIXES.put("graph", "dot"); 55 } 56 setClasspath(Path classpath)57 public void setClasspath(Path classpath) 58 { 59 _classpath = classpath; 60 } setClasspathRef(Reference ref)61 public void setClasspathRef(Reference ref) 62 { 63 createClasspath().setRefid(ref); 64 } createClasspath()65 public Path createClasspath() 66 { 67 if (_classpath == null) 68 { 69 _classpath = new Path(this.getProject()); 70 } 71 return _classpath.createPath(); 72 } 73 setSmfile(File smfile)74 public void setSmfile(File smfile) 75 { 76 _smfile = smfile; 77 deriveStemname(); 78 } 79 80 private String _stemname = ""; deriveStemname()81 private void deriveStemname() 82 { 83 String smfilename = _smfile.getName(); 84 int idx = smfilename.lastIndexOf(".sm"); 85 _stemname = smfilename.substring(0, idx); 86 } 87 setDestdir(File destdir)88 public void setDestdir(File destdir) 89 { 90 _destdir = destdir; 91 } setTarget(TargetEnum target)92 public void setTarget(TargetEnum target) 93 { 94 _target = target.getValue(); 95 } 96 setSmcjar(File jar)97 public void setSmcjar(File jar) 98 { 99 _smcjar = jar; 100 } 101 setSync(boolean sync)102 public void setSync(boolean sync) { _sync = sync; } setSerial(boolean serial)103 public void setSerial(boolean serial) { _serial = serial; } setG(boolean g)104 public void setG(boolean g) { _g = g; } 105 setGlevel(String glevel)106 public void setGlevel(String glevel) { _glevel = glevel; } 107 setSuffix(String suffix)108 public void setSuffix(String suffix) { _suffix = suffix; } 109 110 111 private static String[] TARGET_OPTIONS = 112 { "c++", "java", "tcl", "vb", 113 "csharp", "table", "graph"}; 114 115 public static class TargetEnum extends EnumeratedAttribute 116 { getValues()117 public String[] getValues() { return TARGET_OPTIONS; } 118 } 119 execute()120 public void execute() 121 { 122 validateParameters(); 123 124 File parent = (_destdir == null) ? 125 _smfile.getParentFile() : _destdir; 126 String suffix = (_suffix == null) ? 127 (String) DEFAULT_SUFFIXES.get(_target) : _suffix; 128 String child = _stemname + "Context." + suffix; 129 File destfile = new File(parent, child); 130 log("Generated filename computed as "+destfile, Project.MSG_DEBUG); 131 132 if (destfile.exists() && _smfile.lastModified() <= destfile.lastModified()) 133 { 134 log("Generation omitted as " + destfile + " is up to date.", Project.MSG_VERBOSE); 135 return; 136 } 137 if (!destfile.exists()) 138 log("Generating file " + destfile + ".."); 139 else 140 log("Updating file " + destfile + ".."); 141 142 Java javaTask = (Java) getProject().createTask("java"); 143 javaTask.setTaskName(getTaskName()); 144 javaTask.setClasspath(_classpath); 145 146 javaTask.setJar(_smcjar); 147 148 javaTask.createArg().setValue("-"+_target); 149 150 if (_destdir != null) 151 { 152 javaTask.createArg().setValue("-d"); 153 javaTask.createArg().setFile(_destdir); 154 } 155 156 if (_suffix != null) 157 { 158 javaTask.createArg().setValue("-suffix"); 159 javaTask.createArg().setValue(_suffix); 160 } 161 162 if (_sync) { javaTask.createArg().setValue("-sync"); } 163 if (_serial) { javaTask.createArg().setValue("-serial"); } 164 if (_g) { javaTask.createArg().setValue("-g"); } 165 166 if (_glevel != null) 167 { 168 javaTask.createArg().setValue("-glevel"); 169 javaTask.createArg().setValue(_glevel); 170 } 171 172 javaTask.createArg().setFile(_smfile); 173 174 javaTask.setFork(true); 175 176 if (javaTask.executeJava() != 0) 177 throw new BuildException("error"); 178 179 } 180 validateParameters()181 private void validateParameters() 182 { 183 if (_target == null) 184 { 185 throw new BuildException("target attribute is required"); 186 } 187 if (_smfile == null) 188 { 189 throw new BuildException(".sm file atrribute is required"); 190 } 191 if (_smcjar == null) 192 { 193 throw new BuildException("smcjar file atrribute is required"); 194 } 195 if (_destdir != null && !_destdir.isDirectory()) 196 { 197 throw new BuildException(_destdir + " is not a valid directory"); 198 } 199 if (_glevel != null && 200 !"0".equals(_glevel) && !"1".equals(_glevel) && !"2".equals(_glevel) ) 201 { 202 throw new BuildException("Invalid value for glevel, should be 0, 1 or 2"); 203 } 204 } 205 206 } 207