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.3 kB

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