View Javadoc

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