You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Echo.java 5.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. package org.apache.tools.ant.taskdefs;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.io.Writer;
  23. import java.io.BufferedWriter;
  24. import java.io.OutputStreamWriter;
  25. import java.io.FileOutputStream;
  26. import org.apache.tools.ant.BuildException;
  27. import org.apache.tools.ant.Project;
  28. import org.apache.tools.ant.Task;
  29. import org.apache.tools.ant.util.FileUtils;
  30. import org.apache.tools.ant.types.LogLevel;
  31. import org.apache.tools.ant.types.Resource;
  32. import org.apache.tools.ant.types.resources.FileProvider;
  33. import org.apache.tools.ant.types.resources.FileResource;
  34. /**
  35. * Writes a message to the Ant logging facilities.
  36. *
  37. * @since Ant 1.1
  38. *
  39. * @ant.task category="utility"
  40. */
  41. public class Echo extends Task {
  42. // CheckStyle:VisibilityModifier OFF - bc
  43. protected String message = "";
  44. protected File file = null;
  45. protected boolean append = false;
  46. /** encoding; set to null or empty means 'default' */
  47. private String encoding = "";
  48. // by default, messages are always displayed
  49. protected int logLevel = Project.MSG_WARN;
  50. // CheckStyle:VisibilityModifier ON
  51. private Resource output;
  52. /**
  53. * Does the work.
  54. *
  55. * @exception BuildException if something goes wrong with the build
  56. */
  57. public void execute() throws BuildException {
  58. if (output == null) {
  59. log(message, logLevel);
  60. } else {
  61. Writer out = null;
  62. try {
  63. OutputStream os = output instanceof FileProvider ? os = new FileOutputStream(
  64. ((FileProvider) output).getFile(), append) : output.getOutputStream();
  65. OutputStreamWriter osw = (encoding == null || "".equals(encoding)) ? new OutputStreamWriter(
  66. os)
  67. : new OutputStreamWriter(os, encoding);
  68. out = new BufferedWriter(osw);
  69. out.write(message, 0, message.length());
  70. } catch (IOException ioe) {
  71. throw new BuildException(ioe, getLocation());
  72. } finally {
  73. FileUtils.close(out);
  74. }
  75. }
  76. }
  77. /**
  78. * Message to write.
  79. *
  80. * @param msg Sets the value for the message variable.
  81. */
  82. public void setMessage(String msg) {
  83. this.message = msg;
  84. }
  85. /**
  86. * File to write to.
  87. * @param file the file to write to, if not set, echo to
  88. * standard output
  89. */
  90. public void setFile(File file) {
  91. this.file = file;
  92. setOutput(new FileResource(getProject(), file));
  93. }
  94. /**
  95. * Resource to write to.
  96. * @param output the Resource to write to.
  97. * @since Ant 1.8
  98. */
  99. public void setOutput(Resource output) {
  100. if (this.output != null) {
  101. throw new BuildException("Cannot set > 1 output target");
  102. }
  103. this.output = output;
  104. this.file = output instanceof FileProvider ? ((FileProvider) output).getFile() : null;
  105. }
  106. /**
  107. * If true, append to existing file.
  108. * @param append if true, append to existing file, default
  109. * is false.
  110. */
  111. public void setAppend(boolean append) {
  112. this.append = append;
  113. }
  114. /**
  115. * Set a multiline message.
  116. * @param msg the CDATA text to append to the output text
  117. */
  118. public void addText(String msg) {
  119. message += getProject().replaceProperties(msg);
  120. }
  121. /**
  122. * Set the logging level. Level should be one of
  123. * <ul>
  124. * <li>error</li>
  125. * <li>warning</li>
  126. * <li>info</li>
  127. * <li>verbose</li>
  128. * <li>debug</li>
  129. * </ul>
  130. * <p>The default is &quot;warning&quot; to ensure that messages are
  131. * displayed by default when using the -quiet command line option.</p>
  132. * @param echoLevel the logging level
  133. */
  134. public void setLevel(EchoLevel echoLevel) {
  135. logLevel = echoLevel.getLevel();
  136. }
  137. /**
  138. * Declare the encoding to use when outputting to a file;
  139. * Use "" for the platform's default encoding.
  140. * @param encoding the character encoding to use.
  141. * @since 1.7
  142. */
  143. public void setEncoding(String encoding) {
  144. this.encoding = encoding;
  145. }
  146. /**
  147. * The enumerated values for the level attribute.
  148. */
  149. public static class EchoLevel extends LogLevel {
  150. }
  151. }