1. /*
  2. * Copyright 2000,2002-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.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.io.OutputStream;
  23. import org.apache.tools.ant.Project;
  24. import org.apache.tools.ant.Task;
  25. /**
  26. * Parses output from jikes and
  27. * passes errors and warnings
  28. * into the right logging channels of Project.
  29. *
  30. * <p><strong>As of Ant 1.2, this class is considered to be dead code
  31. * by the Ant developers and is unmaintained. Don't use
  32. * it.</strong></p>
  33. *
  34. * @deprecated use Jikes' exit value to detect compilation failure.
  35. */
  36. public class JikesOutputParser implements ExecuteStreamHandler {
  37. protected Task task;
  38. protected boolean errorFlag = false; // no errors so far
  39. protected int errors;
  40. protected int warnings;
  41. protected boolean error = false;
  42. protected boolean emacsMode;
  43. protected BufferedReader br;
  44. /**
  45. * Ignore.
  46. */
  47. public void setProcessInputStream(OutputStream os) {
  48. }
  49. /**
  50. * Ignore.
  51. */
  52. public void setProcessErrorStream(InputStream is) {
  53. }
  54. /**
  55. * Set the inputstream
  56. */
  57. public void setProcessOutputStream(InputStream is) throws IOException {
  58. br = new BufferedReader(new InputStreamReader(is));
  59. }
  60. /**
  61. * Invokes parseOutput.
  62. */
  63. public void start() throws IOException {
  64. parseOutput(br);
  65. }
  66. /**
  67. * Ignore.
  68. */
  69. public void stop() {
  70. }
  71. /**
  72. * Construct a new Parser object
  73. * @param task - task in which context we are called
  74. */
  75. protected JikesOutputParser(Task task, boolean emacsMode) {
  76. super();
  77. System.err.println("As of Ant 1.2 released in October 2000, the "
  78. + "JikesOutputParser class");
  79. System.err.println("is considered to be dead code by the Ant "
  80. + "developers and is unmaintained.");
  81. System.err.println("Don\'t use it!");
  82. this.task = task;
  83. this.emacsMode = emacsMode;
  84. }
  85. /**
  86. * Parse the output of a jikes compiler
  87. * @param reader - Reader used to read jikes's output
  88. */
  89. protected void parseOutput(BufferedReader reader) throws IOException {
  90. if (emacsMode) {
  91. parseEmacsOutput(reader);
  92. } else {
  93. parseStandardOutput(reader);
  94. }
  95. }
  96. private void parseStandardOutput(BufferedReader reader) throws IOException {
  97. String line;
  98. String lower;
  99. // We assume, that every output, jikes does, stands for an error/warning
  100. // XXX
  101. // Is this correct?
  102. // TODO:
  103. // A warning line, that shows code, which contains a variable
  104. // error will cause some trouble. The parser should definitely
  105. // be much better.
  106. while ((line = reader.readLine()) != null) {
  107. lower = line.toLowerCase();
  108. if (line.trim().equals("")) {
  109. continue;
  110. }
  111. if (lower.indexOf("error") != -1) {
  112. setError(true);
  113. } else if (lower.indexOf("warning") != -1) {
  114. setError(false);
  115. } else {
  116. // If we don't know the type of the line
  117. // and we are in emacs mode, it will be
  118. // an error, because in this mode, jikes won't
  119. // always print "error", but sometimes other
  120. // keywords like "Syntax". We should look for
  121. // all those keywords.
  122. if (emacsMode) {
  123. setError(true);
  124. }
  125. }
  126. log(line);
  127. }
  128. }
  129. private void parseEmacsOutput(BufferedReader reader) throws IOException {
  130. // This may change, if we add advanced parsing capabilities.
  131. parseStandardOutput(reader);
  132. }
  133. private void setError(boolean err) {
  134. error = err;
  135. if (error) {
  136. errorFlag = true;
  137. }
  138. }
  139. private void log(String line) {
  140. if (!emacsMode) {
  141. task.log("", (error ? Project.MSG_ERR : Project.MSG_WARN));
  142. }
  143. task.log(line, (error ? Project.MSG_ERR : Project.MSG_WARN));
  144. }
  145. /**
  146. * Indicate if there were errors during the compile
  147. * @return if errors occurred
  148. */
  149. protected boolean getErrorFlag() {
  150. return errorFlag;
  151. }
  152. }