View Javadoc

1   /*
2    * Copyright 2007 scala-tools.org
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,
11   * software 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
14   * and limitations under the License.
15   */
16  package org.scala_tools.maven;
17  
18  import com.sun.org.apache.xalan.internal.xsltc.dom.MatchingIterator;
19  import java.io.BufferedReader;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.InputStreamReader;
23  import java.io.PrintWriter;
24  
25  import java.util.regex.Matcher;
26  import java.util.regex.Pattern;
27  import org.apache.maven.plugin.logging.Log;
28  import org.codehaus.plexus.util.IOUtil;
29  import org.codehaus.plexus.util.StringUtils;
30  
31  public class StreamLogger extends Thread {
32  	private static final String LS = System.getProperty("line.separator");
33  	private static final boolean emacsMode = StringUtils.isNotEmpty(System.getProperty("emacsMode"));
34  	private static final boolean javaMode = StringUtils.isNotEmpty(System.getProperty("javaMode"));
35  
36      private InputStream in_;
37      private Log log_;
38      private boolean isErr_;
39      private PrintWriter out_;
40      private Pattern pattern_ = Pattern.compile("\\s*((/|\\w:).+?):(\\d+):\\s*(?:Error|error|Warning|warning|Caution|caution):\\s(.+)");
41  
42      public StreamLogger(InputStream in, Log log, boolean isErr) {
43          in_ = in;
44          log_ = log;
45          isErr_ = isErr;
46      }
47  
48      @Override
49      public void run() {
50          BufferedReader reader = null;
51          try {
52              reader = new BufferedReader(new InputStreamReader(in_));
53              String line = null;
54              StringBuilder sb = null;
55              while ((line = reader.readLine()) != null) {
56                  if (isErr_) {
57                  	if (!emacsMode && !javaMode) {
58                          log_.warn(line);
59                  	} else {
60                  		if (sb == null) {
61                  			sb = new StringBuilder("Compilation failure"+ LS);
62                  		}
63                          if (javaMode) {
64                              Matcher matcher = pattern_.matcher(line);
65                              if (matcher.matches()) {
66                                  line = matcher.group(1)+":["+matcher.group(3) +",1] " + matcher.group(4);
67                              }
68                          }
69                      	sb.append(LS + line);
70                  	}
71                  } else {
72                      log_.info(line);
73                  }
74              }
75              if (sb != null) {
76              	log_.warn(sb.toString());
77              }
78          } catch(IOException exc) {
79              throw new RuntimeException("wrap: " + exc.getMessage(), exc);
80          } finally {
81              IOUtil.close(reader);
82              IOUtil.close(in_);
83              IOUtil.close(out_);
84          }
85      }
86  }