View Javadoc

1   /*
2    * Derivative Work
3    * Copyright SOFTEC sa. All rights reserved.
4    *
5    * Original work from yuicompressor-maven-plugin
6    * Copyright David Bernard
7    *
8    * This library is free software; you can redistribute it and/or
9    * modify it under the terms of the GNU Lesser General Public
10   * License as published by the Free Software Foundation; either
11   * version 2.1 of the License, or (at your option) any later version.
12   *
13   * This library is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   * Lesser General Public License for more details.
17   *
18   * You should have received a copy of the GNU Lesser General Public
19   * License along with this library; if not, write to the Free Software
20   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21   */
22  package org.codehaus.mojo.javascript.compress;
23  
24  import org.mozilla.javascript.ErrorReporter;
25  import org.mozilla.javascript.EvaluatorException;
26  
27  public class ErrorReporter4Mojo implements ErrorReporter {
28  
29      private String defaultFilename_;
30      private boolean acceptWarn_;
31      private JSCompressorLogger log_;
32      private int warningCnt_;
33      private int errorCnt_;
34  
35      public ErrorReporter4Mojo(JSCompressorLogger log, boolean jswarn) {
36          log_ = log;
37          acceptWarn_ = jswarn;
38      }
39  
40      public JSCompressorLogger getLogger()
41      {
42          return log_;
43      }
44  
45      public void setDefaultFileName(String v) {
46          if (v.length() == 0) {
47              v = null;
48          }
49          defaultFilename_ = v;
50      }
51  
52      public int getErrorCnt() {
53          return errorCnt_;
54      }
55  
56      public int getWarningCnt() {
57          return warningCnt_;
58      }
59  
60      public void error(String message, String sourceName, int line, String lineSource, int lineOffset) {
61          String fullMessage = newMessage(message, sourceName, line, lineSource, lineOffset);
62          log_.error(fullMessage);
63          errorCnt_++;
64      }
65  
66      public EvaluatorException runtimeError(String message, String sourceName, int line, String lineSource, int lineOffset) {
67          error(message, sourceName, line, lineSource, lineOffset);
68          throw new EvaluatorException(message, sourceName, line, lineSource, lineOffset);
69      }
70  
71      public void warning(String message, String sourceName, int line, String lineSource, int lineOffset) {
72          if( message.indexOf("is declared but is apparently never used.") != -1
73              || message.indexOf("Try to use a single 'var' statement per scope.") != -1) return;
74          
75          if (acceptWarn_) {
76              String fullMessage = newMessage(message, sourceName, line, lineSource, lineOffset);
77              log_.warn(fullMessage);
78              warningCnt_++;
79          }
80      }
81  
82      private String newMessage(String message, String sourceName, int line, String lineSource, int lineOffset) {
83          StringBuilder back = new StringBuilder();
84          if ((sourceName == null) || (sourceName.length() == 0)) {
85              sourceName = defaultFilename_;
86          }
87          if (sourceName != null) {
88              back.append(sourceName)
89                  .append(":line ")
90                  .append(line)
91                  .append(":column ")
92                  .append(lineOffset)
93                  .append(':')
94                  ;
95          }
96          if ((message != null) && (message.length() != 0)) {
97              back.append(message);
98          } else {
99              back.append("unknown error");
100         }
101         if ((lineSource != null) && (lineSource.length() != 0)) {
102             back.append("\n\t")
103                 .append(lineSource)
104                 ;
105         }
106         return back.toString();
107     }
108 
109 }