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.

JikesOutputParser.java 5.5 kB

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