1 18 package org.apache.tools.ant.taskdefs; 19 20 import java.util.Hashtable ; 21 import org.apache.tools.ant.BuildEvent; 22 import org.apache.tools.ant.BuildException; 23 import org.apache.tools.ant.Project; 24 import org.apache.tools.ant.SubBuildListener; 25 import org.apache.tools.ant.Task; 26 import org.apache.tools.ant.types.EnumeratedAttribute; 27 import org.apache.tools.ant.types.LogLevel; 28 29 47 public class Recorder extends Task implements SubBuildListener { 48 49 52 53 private String filename = null; 54 57 private Boolean append = null; 58 62 private Boolean start = null; 63 64 private int loglevel = -1; 65 66 private boolean emacsMode = false; 67 68 private static Hashtable recorderEntries = new Hashtable (); 69 70 73 78 public void init() { 79 getProject().addBuildListener(this); 80 } 81 82 85 91 public void setName(String fname) { 92 filename = fname; 93 } 94 95 96 101 public void setAction(ActionChoices action) { 102 if (action.getValue().equalsIgnoreCase("start")) { 103 start = Boolean.TRUE; 104 } else { 105 start = Boolean.FALSE; 106 } 107 } 108 109 110 114 public void setAppend(boolean append) { 115 this.append = (append ? Boolean.TRUE : Boolean.FALSE); 116 } 117 118 119 123 public void setEmacsMode(boolean emacsMode) { 124 this.emacsMode = emacsMode; 125 } 126 127 128 133 public void setLoglevel(VerbosityLevelChoices level) { 134 loglevel = level.getLevel(); 135 } 136 137 140 144 public void execute() throws BuildException { 145 if (filename == null) { 146 throw new BuildException("No filename specified"); 147 } 148 149 getProject().log("setting a recorder for name " + filename, 150 Project.MSG_DEBUG); 151 152 RecorderEntry recorder = getRecorder(filename, getProject()); 154 recorder.setMessageOutputLevel(loglevel); 156 recorder.setEmacsMode(emacsMode); 157 if (start != null) { 158 if (start.booleanValue()) { 159 recorder.reopenFile(); 160 recorder.setRecordState(start); 161 } else { 162 recorder.setRecordState(start); 163 recorder.closeFile(); 164 } 165 } 166 } 167 168 171 175 public static class ActionChoices extends EnumeratedAttribute { 176 private static final String [] VALUES = {"start", "stop"}; 177 178 181 182 public String [] getValues() { 183 return VALUES; 184 } 185 } 186 187 188 192 public static class VerbosityLevelChoices extends LogLevel { 193 } 194 195 196 204 protected RecorderEntry getRecorder(String name, Project proj) 205 throws BuildException { 206 Object o = recorderEntries.get(name); 207 RecorderEntry entry; 208 209 if (o == null) { 210 entry = new RecorderEntry(name); 212 213 if (append == null) { 214 entry.openFile(false); 215 } else { 216 entry.openFile(append.booleanValue()); 217 } 218 entry.setProject(proj); 219 recorderEntries.put(name, entry); 220 } else { 221 entry = (RecorderEntry) o; 222 } 223 return entry; 224 } 225 226 231 public void buildStarted(BuildEvent event) { 232 } 233 234 239 public void subBuildStarted(BuildEvent event) { 240 } 241 242 247 public void targetStarted(BuildEvent event) { 248 } 249 250 255 public void targetFinished(BuildEvent event) { 256 } 257 258 263 public void taskStarted(BuildEvent event) { 264 } 265 266 271 public void taskFinished(BuildEvent event) { 272 } 273 274 279 public void messageLogged(BuildEvent event) { 280 } 281 282 287 public void buildFinished(BuildEvent event) { 288 cleanup(); 289 } 290 291 297 public void subBuildFinished(BuildEvent event) { 298 if (event.getProject() == getProject()) { 299 cleanup(); 300 } 301 } 302 303 308 private void cleanup() { 309 recorderEntries.clear(); 310 getProject().removeBuildListener(this); 311 } 312 } 313 314 | Popular Tags |