001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import java.io.File;
022: import java.io.FileWriter;
023: import java.io.IOException;
024: import java.io.Writer;
025: import java.io.BufferedWriter;
026: import java.io.OutputStreamWriter;
027: import java.io.FileOutputStream;
028:
029: import org.apache.tools.ant.BuildException;
030: import org.apache.tools.ant.Project;
031: import org.apache.tools.ant.Task;
032: import org.apache.tools.ant.util.FileUtils;
033: import org.apache.tools.ant.types.LogLevel;
034:
035: /**
036: * Writes a message to the Ant logging facilities.
037: *
038: * @since Ant 1.1
039: *
040: * @ant.task category="utility"
041: */
042: public class Echo extends Task {
043: // CheckStyle:VisibilityModifier OFF - bc
044: protected String message = "";
045: protected File file = null;
046: protected boolean append = false;
047: /** encoding; set to null or empty means 'default' */
048: private String encoding = "";
049:
050: // by default, messages are always displayed
051: protected int logLevel = Project.MSG_WARN;
052:
053: // CheckStyle:VisibilityModifier ON
054:
055: /**
056: * Does the work.
057: *
058: * @exception BuildException if something goes wrong with the build
059: */
060: public void execute() throws BuildException {
061: if (file == null) {
062: log(message, logLevel);
063: } else {
064: Writer out = null;
065: try {
066: String filename = file.getAbsolutePath();
067: if (encoding == null || encoding.length() == 0) {
068: out = new FileWriter(filename, append);
069: } else {
070: out = new BufferedWriter(new OutputStreamWriter(
071: new FileOutputStream(filename, append),
072: encoding));
073: }
074: out.write(message, 0, message.length());
075: } catch (IOException ioe) {
076: throw new BuildException(ioe, getLocation());
077: } finally {
078: FileUtils.close(out);
079: }
080: }
081: }
082:
083: /**
084: * Message to write.
085: *
086: * @param msg Sets the value for the message variable.
087: */
088: public void setMessage(String msg) {
089: this .message = msg;
090: }
091:
092: /**
093: * File to write to.
094: * @param file the file to write to, if not set, echo to
095: * standard output
096: */
097: public void setFile(File file) {
098: this .file = file;
099: }
100:
101: /**
102: * If true, append to existing file.
103: * @param append if true, append to existing file, default
104: * is false.
105: */
106: public void setAppend(boolean append) {
107: this .append = append;
108: }
109:
110: /**
111: * Set a multiline message.
112: * @param msg the CDATA text to append to the output text
113: */
114: public void addText(String msg) {
115: message += getProject().replaceProperties(msg);
116: }
117:
118: /**
119: * Set the logging level. Level should be one of
120: * <ul>
121: * <li>error</li>
122: * <li>warning</li>
123: * <li>info</li>
124: * <li>verbose</li>
125: * <li>debug</li>
126: * </ul>
127: * <p>The default is "warning" to ensure that messages are
128: * displayed by default when using the -quiet command line option.</p>
129: * @param echoLevel the logging level
130: */
131: public void setLevel(EchoLevel echoLevel) {
132: logLevel = echoLevel.getLevel();
133: }
134:
135: /**
136: * Declare the encoding to use when outputting to a file;
137: * Use "" for the platform's default encoding.
138: * @param encoding the character encoding to use.
139: * @since 1.7
140: */
141: public void setEncoding(String encoding) {
142: this .encoding = encoding;
143: }
144:
145: /**
146: * The enumerated values for the level attribute.
147: */
148: public static class EchoLevel extends LogLevel {
149: }
150: }
|