1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.File ; 22 import java.io.FileWriter ; 23 import java.io.IOException ; 24 import java.io.Writer ; 25 import java.io.BufferedWriter ; 26 import java.io.OutputStreamWriter ; 27 import java.io.FileOutputStream ; 28 29 import org.apache.tools.ant.BuildException; 30 import org.apache.tools.ant.Project; 31 import org.apache.tools.ant.Task; 32 import org.apache.tools.ant.util.FileUtils; 33 import org.apache.tools.ant.types.LogLevel; 34 35 42 public class Echo extends Task { 43 protected String message = ""; 45 protected File file = null; 46 protected boolean append = false; 47 48 private String encoding = ""; 49 50 protected int logLevel = Project.MSG_WARN; 52 54 59 public void execute() throws BuildException { 60 if (file == null) { 61 log(message, logLevel); 62 } else { 63 Writer out = null; 64 try { 65 String filename = file.getAbsolutePath(); 66 if (encoding == null || encoding.length() == 0) { 67 out = new FileWriter (filename, append); 68 } else { 69 out = new BufferedWriter ( 70 new OutputStreamWriter ( 71 new FileOutputStream (filename, append), encoding)); 72 } 73 out.write(message, 0, message.length()); 74 } catch (IOException ioe) { 75 throw new BuildException(ioe, getLocation()); 76 } finally { 77 FileUtils.close(out); 78 } 79 } 80 } 81 82 87 public void setMessage(String msg) { 88 this.message = msg; 89 } 90 91 96 public void setFile(File file) { 97 this.file = file; 98 } 99 100 105 public void setAppend(boolean append) { 106 this.append = append; 107 } 108 109 113 public void addText(String msg) { 114 message += getProject().replaceProperties(msg); 115 } 116 117 130 public void setLevel(EchoLevel echoLevel) { 131 logLevel = echoLevel.getLevel(); 132 } 133 134 140 public void setEncoding(String encoding) { 141 this.encoding = encoding; 142 } 143 144 147 public static class EchoLevel extends LogLevel { 148 } 149 } 150 | Popular Tags |