1 | /*
|
---|
2 | * Copyright 2000-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs;
|
---|
19 |
|
---|
20 | import java.io.File;
|
---|
21 | import java.io.FileWriter;
|
---|
22 | import java.io.IOException;
|
---|
23 | import org.apache.tools.ant.BuildException;
|
---|
24 | import org.apache.tools.ant.Project;
|
---|
25 | import org.apache.tools.ant.Task;
|
---|
26 | import org.apache.tools.ant.types.EnumeratedAttribute;
|
---|
27 | /**
|
---|
28 | * Writes a message to the Ant logging facilities.
|
---|
29 | *
|
---|
30 | *
|
---|
31 | * @since Ant 1.1
|
---|
32 | *
|
---|
33 | * @ant.task category="utility"
|
---|
34 | */
|
---|
35 | public class Echo extends Task {
|
---|
36 | protected String message = ""; // required
|
---|
37 | protected File file = null;
|
---|
38 | protected boolean append = false;
|
---|
39 |
|
---|
40 | // by default, messages are always displayed
|
---|
41 | protected int logLevel = Project.MSG_WARN;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Does the work.
|
---|
45 | *
|
---|
46 | * @exception BuildException if something goes wrong with the build
|
---|
47 | */
|
---|
48 | public void execute() throws BuildException {
|
---|
49 | if (file == null) {
|
---|
50 | log(message, logLevel);
|
---|
51 | } else {
|
---|
52 | FileWriter out = null;
|
---|
53 | try {
|
---|
54 | out = new FileWriter(file.getAbsolutePath(), append);
|
---|
55 | out.write(message, 0, message.length());
|
---|
56 | } catch (IOException ioe) {
|
---|
57 | throw new BuildException(ioe, getLocation());
|
---|
58 | } finally {
|
---|
59 | if (out != null) {
|
---|
60 | try {
|
---|
61 | out.close();
|
---|
62 | } catch (IOException ioex) {
|
---|
63 | //ignore
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Message to write.
|
---|
72 | *
|
---|
73 | * @param msg Sets the value for the message variable.
|
---|
74 | */
|
---|
75 | public void setMessage(String msg) {
|
---|
76 | this.message = msg;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * File to write to.
|
---|
81 | * @param file the file to write to, if not set, echo to
|
---|
82 | * standard output
|
---|
83 | */
|
---|
84 | public void setFile(File file) {
|
---|
85 | this.file = file;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * If true, append to existing file.
|
---|
90 | * @param append if true, append to existing file, default
|
---|
91 | * is false.
|
---|
92 | */
|
---|
93 | public void setAppend(boolean append) {
|
---|
94 | this.append = append;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Set a multiline message.
|
---|
99 | * @param msg the CDATA text to append to the output text
|
---|
100 | */
|
---|
101 | public void addText(String msg) {
|
---|
102 | message += getProject().replaceProperties(msg);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Set the logging level. Level should be one of
|
---|
107 | * <ul>
|
---|
108 | * <li>error</li>
|
---|
109 | * <li>warning</li>
|
---|
110 | * <li>info</li>
|
---|
111 | * <li>verbose</li>
|
---|
112 | * <li>debug</li>
|
---|
113 | * </ul>
|
---|
114 | * <p>The default is "warning" to ensure that messages are
|
---|
115 | * displayed by default when using the -quiet command line option.</p>
|
---|
116 | * @param echoLevel the logging level
|
---|
117 | */
|
---|
118 | public void setLevel(EchoLevel echoLevel) {
|
---|
119 | String option = echoLevel.getValue();
|
---|
120 | if (option.equals("error")) {
|
---|
121 | logLevel = Project.MSG_ERR;
|
---|
122 | } else if (option.equals("warning")) {
|
---|
123 | logLevel = Project.MSG_WARN;
|
---|
124 | } else if (option.equals("info")) {
|
---|
125 | logLevel = Project.MSG_INFO;
|
---|
126 | } else if (option.equals("verbose")) {
|
---|
127 | logLevel = Project.MSG_VERBOSE;
|
---|
128 | } else {
|
---|
129 | // must be "debug"
|
---|
130 | logLevel = Project.MSG_DEBUG;
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * The enumerated values for the level attribute.
|
---|
136 | */
|
---|
137 | public static class EchoLevel extends EnumeratedAttribute {
|
---|
138 | /**
|
---|
139 | * @see EnumeratedAttribute#getValues
|
---|
140 | * @return the strings allowed for the level attribute
|
---|
141 | */
|
---|
142 | public String[] getValues() {
|
---|
143 | return new String[] {"error", "warning", "info",
|
---|
144 | "verbose", "debug"};
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|