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 6.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000,2002 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "Ant" and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import java.io.BufferedReader;
  56. import java.io.IOException;
  57. import java.io.InputStream;
  58. import java.io.InputStreamReader;
  59. import java.io.OutputStream;
  60. import org.apache.tools.ant.Project;
  61. import org.apache.tools.ant.Task;
  62. /**
  63. * Parses output from jikes and
  64. * passes errors and warnings
  65. * into the right logging channels of Project.
  66. *
  67. * <p><strong>As of Ant 1.2, this class is considered to be dead code
  68. * by the Ant developers and is unmaintained. Don't use
  69. * it.</strong></p>
  70. *
  71. * @author skanthak@muehlheim.de
  72. * @deprecated use Jikes' exit value to detect compilation failure.
  73. */
  74. public class JikesOutputParser implements ExecuteStreamHandler {
  75. protected Task task;
  76. protected boolean errorFlag = false; // no errors so far
  77. protected int errors;
  78. protected int warnings;
  79. protected boolean error = false;
  80. protected boolean emacsMode;
  81. protected BufferedReader br;
  82. /**
  83. * Ignore.
  84. */
  85. public void setProcessInputStream(OutputStream os) {}
  86. /**
  87. * Ignore.
  88. */
  89. public void setProcessErrorStream(InputStream is) {}
  90. /**
  91. * Set the inputstream
  92. */
  93. public void setProcessOutputStream(InputStream is) throws IOException {
  94. br = new BufferedReader(new InputStreamReader(is));
  95. }
  96. /**
  97. * Invokes parseOutput.
  98. */
  99. public void start() throws IOException {
  100. parseOutput(br);
  101. }
  102. /**
  103. * Ignore.
  104. */
  105. public void stop() {}
  106. /**
  107. * Construct a new Parser object
  108. * @param task - task in whichs context we are called
  109. */
  110. protected JikesOutputParser(Task task, boolean emacsMode) {
  111. super();
  112. System.err.println("As of Ant 1.2 released in October 2000, the "
  113. + "JikesOutputParser class");
  114. System.err.println("is considered to be dead code by the Ant "
  115. + "developers and is unmaintained.");
  116. System.err.println("Don\'t use it!");
  117. this.task = task;
  118. this.emacsMode = emacsMode;
  119. }
  120. /**
  121. * Parse the output of a jikes compiler
  122. * @param reader - Reader used to read jikes's output
  123. */
  124. protected void parseOutput(BufferedReader reader) throws IOException {
  125. if (emacsMode) {
  126. parseEmacsOutput(reader);
  127. } else {
  128. parseStandardOutput(reader);
  129. }
  130. }
  131. private void parseStandardOutput(BufferedReader reader) throws IOException {
  132. String line;
  133. String lower;
  134. // We assume, that every output, jike does, stands for an error/warning
  135. // XXX
  136. // Is this correct?
  137. // TODO:
  138. // A warning line, that shows code, which contains a variable
  139. // error will cause some trouble. The parser should definitely
  140. // be much better.
  141. while ((line = reader.readLine()) != null) {
  142. lower = line.toLowerCase();
  143. if (line.trim().equals("")) {
  144. continue;
  145. }
  146. if (lower.indexOf("error") != -1) {
  147. setError(true);
  148. } else if (lower.indexOf("warning") != -1) {
  149. setError(false);
  150. } else {
  151. // If we don't know the type of the line
  152. // and we are in emacs mode, it will be
  153. // an error, because in this mode, jikes won't
  154. // always print "error", but sometimes other
  155. // keywords like "Syntax". We should look for
  156. // all those keywords.
  157. if (emacsMode) {
  158. setError(true);
  159. }
  160. }
  161. log(line);
  162. }
  163. }
  164. private void parseEmacsOutput(BufferedReader reader) throws IOException {
  165. // This may change, if we add advanced parsing capabilities.
  166. parseStandardOutput(reader);
  167. }
  168. private void setError(boolean err) {
  169. error = err;
  170. if (error) {
  171. errorFlag = true;
  172. }
  173. }
  174. private void log(String line) {
  175. if (!emacsMode) {
  176. task.log("", (error ? Project.MSG_ERR : Project.MSG_WARN));
  177. }
  178. task.log(line, (error ? Project.MSG_ERR : Project.MSG_WARN));
  179. }
  180. /**
  181. * Indicate if there were errors during the compile
  182. * @return if errors ocured
  183. */
  184. protected boolean getErrorFlag() {
  185. return errorFlag;
  186. }
  187. }