KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > Echo


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs;
20
21 import java.io.File JavaDoc;
22 import java.io.FileWriter JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.Writer JavaDoc;
25 import java.io.BufferedWriter JavaDoc;
26 import java.io.OutputStreamWriter JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
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 /**
36  * Writes a message to the Ant logging facilities.
37  *
38  * @since Ant 1.1
39  *
40  * @ant.task category="utility"
41  */

42 public class Echo extends Task {
43     // CheckStyle:VisibilityModifier OFF - bc
44
protected String JavaDoc message = "";
45     protected File JavaDoc file = null;
46     protected boolean append = false;
47     /** encoding; set to null or empty means 'default' */
48     private String JavaDoc encoding = "";
49
50     // by default, messages are always displayed
51
protected int logLevel = Project.MSG_WARN;
52     // CheckStyle:VisibilityModifier ON
53

54     /**
55      * Does the work.
56      *
57      * @exception BuildException if something goes wrong with the build
58      */

59     public void execute() throws BuildException {
60         if (file == null) {
61             log(message, logLevel);
62         } else {
63             Writer JavaDoc out = null;
64             try {
65                 String JavaDoc filename = file.getAbsolutePath();
66                 if (encoding == null || encoding.length() == 0) {
67                     out = new FileWriter JavaDoc(filename, append);
68                 } else {
69                     out = new BufferedWriter JavaDoc(
70                             new OutputStreamWriter JavaDoc(
71                                 new FileOutputStream JavaDoc(filename, append), encoding));
72                 }
73                 out.write(message, 0, message.length());
74             } catch (IOException JavaDoc ioe) {
75                 throw new BuildException(ioe, getLocation());
76             } finally {
77                 FileUtils.close(out);
78             }
79         }
80     }
81
82     /**
83      * Message to write.
84      *
85      * @param msg Sets the value for the message variable.
86      */

87     public void setMessage(String JavaDoc msg) {
88         this.message = msg;
89     }
90
91     /**
92      * File to write to.
93      * @param file the file to write to, if not set, echo to
94      * standard output
95      */

96     public void setFile(File JavaDoc file) {
97         this.file = file;
98     }
99
100     /**
101      * If true, append to existing file.
102      * @param append if true, append to existing file, default
103      * is false.
104      */

105     public void setAppend(boolean append) {
106         this.append = append;
107     }
108
109     /**
110      * Set a multiline message.
111      * @param msg the CDATA text to append to the output text
112      */

113     public void addText(String JavaDoc msg) {
114         message += getProject().replaceProperties(msg);
115     }
116
117     /**
118      * Set the logging level. Level should be one of
119      * <ul>
120      * <li>error</li>
121      * <li>warning</li>
122      * <li>info</li>
123      * <li>verbose</li>
124      * <li>debug</li>
125      * </ul>
126      * <p>The default is &quot;warning&quot; to ensure that messages are
127      * displayed by default when using the -quiet command line option.</p>
128      * @param echoLevel the logging level
129      */

130     public void setLevel(EchoLevel echoLevel) {
131         logLevel = echoLevel.getLevel();
132     }
133
134     /**
135      * Declare the encoding to use when outputting to a file;
136      * Use "" for the platform's default encoding.
137      * @param encoding the character encoding to use.
138      * @since 1.7
139      */

140     public void setEncoding(String JavaDoc encoding) {
141         this.encoding = encoding;
142     }
143
144     /**
145      * The enumerated values for the level attribute.
146      */

147     public static class EchoLevel extends LogLevel {
148     }
149 }
150
Popular Tags