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. package org.apache.tools.ant.taskdefs;
  18. import java.io.File;
  19. import java.io.FileWriter;
  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.EnumeratedAttribute;
  25. /**
  26. * Writes a message to the Ant logging facilities.
  27. *
  28. *
  29. * @since Ant 1.1
  30. *
  31. * @ant.task category="utility"
  32. */
  33. public class Echo extends Task {
  34. protected String message = ""; // required
  35. protected File file = null;
  36. protected boolean append = false;
  37. // by default, messages are always displayed
  38. protected int logLevel = Project.MSG_WARN;
  39. /**
  40. * Does the work.
  41. *
  42. * @exception BuildException if something goes wrong with the build
  43. */
  44. public void execute() throws BuildException {
  45. if (file == null) {
  46. log(message, logLevel);
  47. } else {
  48. FileWriter out = null;
  49. try {
  50. out = new FileWriter(file.getAbsolutePath(), append);
  51. out.write(message, 0, message.length());
  52. } catch (IOException ioe) {
  53. throw new BuildException(ioe, getLocation());
  54. } finally {
  55. if (out != null) {
  56. try {
  57. out.close();
  58. } catch (IOException ioex) {
  59. //ignore
  60. }
  61. }
  62. }
  63. }
  64. }
  65. /**
  66. * Message to write.
  67. *
  68. * @param msg Sets the value for the message variable.
  69. */
  70. public void setMessage(String msg) {
  71. this.message = msg;
  72. }
  73. /**
  74. * File to write to.
  75. * @param file the file to write to, if not set, echo to
  76. * standard output
  77. */
  78. public void setFile(File file) {
  79. this.file = file;
  80. }
  81. /**
  82. * If true, append to existing file.
  83. * @param append if true, append to existing file, default
  84. * is false.
  85. */
  86. public void setAppend(boolean append) {
  87. this.append = append;
  88. }
  89. /**
  90. * Set a multiline message.
  91. * @param msg the CDATA text to append to the output text
  92. */
  93. public void addText(String msg) {
  94. message += getProject().replaceProperties(msg);
  95. }
  96. /**
  97. * Set the logging level. Level should be one of
  98. * <ul>
  99. * <li>error</li>
  100. * <li>warning</li>
  101. * <li>info</li>
  102. * <li>verbose</li>
  103. * <li>debug</li>
  104. * </ul>
  105. * <p>The default is "warning" to ensure that messages are
  106. * displayed by default when using the -quiet command line option.</p>
  107. * @param echoLevel the logging level
  108. */
  109. public void setLevel(EchoLevel echoLevel) {
  110. String option = echoLevel.getValue();
  111. if (option.equals("error")) {
  112. logLevel = Project.MSG_ERR;
  113. } else if (option.equals("warning")) {
  114. logLevel = Project.MSG_WARN;
  115. } else if (option.equals("info")) {
  116. logLevel = Project.MSG_INFO;
  117. } else if (option.equals("verbose")) {
  118. logLevel = Project.MSG_VERBOSE;
  119. } else {
  120. // must be "debug"
  121. logLevel = Project.MSG_DEBUG;
  122. }
  123. }
  124. /**
  125. * The enumerated values for the level attribute.
  126. */
  127. public static class EchoLevel extends EnumeratedAttribute {
  128. /**
  129. * @see EnumeratedAttribute#getValues
  130. * @return the strings allowed for the level attribute
  131. */
  132. public String[] getValues() {
  133. return new String[] {"error", "warning", "info",
  134. "verbose", "debug"};
  135. }
  136. }
  137. }