View Javadoc
1   /*
2    * YuiCompressor Maven plugin
3    *
4    * Copyright 2012-2023 Hazendaz.
5    *
6    * Licensed under the GNU Lesser General Public License (LGPL),
7    * version 2.1 or later (the "License").
8    * You may not use this file except in compliance with the License.
9    * You may read the licence in the 'lgpl.txt' file in the root folder of
10   * project or obtain a copy at
11   *
12   *     https://www.gnu.org/licenses/lgpl-2.1.html
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" basis,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package net.alchim31.maven.yuicompressor;
21  
22  import java.io.File;
23  
24  import org.apache.maven.plugin.logging.Log;
25  import org.codehaus.plexus.build.BuildContext;
26  import org.mozilla.javascript.ErrorReporter;
27  import org.mozilla.javascript.EvaluatorException;
28  
29  /**
30   * The Class ErrorReporter4Mojo.
31   */
32  public class ErrorReporter4Mojo implements ErrorReporter {
33  
34      /** The default filename. */
35      private String defaultFilename;
36  
37      /** The accept warn. */
38      private boolean acceptWarn;
39  
40      /** The log. */
41      private Log log;
42  
43      /** The warning cnt. */
44      private int warningCnt;
45  
46      /** The error cnt. */
47      private int errorCnt;
48  
49      /** The build context. */
50      private BuildContext buildContext;
51  
52      /** The source file. */
53      private File sourceFile;
54  
55      /**
56       * Instantiates a new error reporter 4 mojo.
57       *
58       * @param log
59       *            the log
60       * @param jswarn
61       *            the jswarn
62       * @param buildContext
63       *            the build context
64       */
65      public ErrorReporter4Mojo(Log log, boolean jswarn, BuildContext buildContext) {
66          this.log = log;
67          this.acceptWarn = jswarn;
68          this.buildContext = buildContext;
69      }
70  
71      /**
72       * Sets the default file name.
73       *
74       * @param v
75       *            the new default file name
76       */
77      public void setDefaultFileName(String v) {
78          if (v.length() == 0) {
79              v = null;
80          }
81          defaultFilename = v;
82      }
83  
84      /**
85       * Gets the error cnt.
86       *
87       * @return the error cnt
88       */
89      public int getErrorCnt() {
90          return errorCnt;
91      }
92  
93      /**
94       * Gets the warning cnt.
95       *
96       * @return the warning cnt
97       */
98      public int getWarningCnt() {
99          return warningCnt;
100     }
101 
102     @Override
103     public void error(String message, String sourceName, int line, String lineSource, int lineOffset) {
104         String fullMessage = newMessage(message, sourceName, line, lineSource, lineOffset);
105         buildContext.addMessage(sourceFile, line, lineOffset, message, BuildContext.SEVERITY_ERROR, null);
106         log.error(fullMessage);
107         errorCnt++;
108     }
109 
110     @Override
111     public EvaluatorException runtimeError(String message, String sourceName, int line, String lineSource,
112             int lineOffset) {
113         error(message, sourceName, line, lineSource, lineOffset);
114         throw new EvaluatorException(message, sourceName, line, lineSource, lineOffset);
115     }
116 
117     @Override
118     public void warning(String message, String sourceName, int line, String lineSource, int lineOffset) {
119         if (acceptWarn) {
120             String fullMessage = newMessage(message, sourceName, line, lineSource, lineOffset);
121             buildContext.addMessage(sourceFile, line, lineOffset, message, BuildContext.SEVERITY_WARNING, null);
122             log.warn(fullMessage);
123             warningCnt++;
124         }
125     }
126 
127     /**
128      * New message.
129      *
130      * @param message
131      *            the message
132      * @param sourceName
133      *            the source name
134      * @param line
135      *            the line
136      * @param lineSource
137      *            the line source
138      * @param lineOffset
139      *            the line offset
140      *
141      * @return the string
142      */
143     private String newMessage(String message, String sourceName, int line, String lineSource, int lineOffset) {
144         StringBuilder back = new StringBuilder();
145         if (sourceName == null || sourceName.length() == 0) {
146             sourceName = defaultFilename;
147         }
148         if (sourceName != null) {
149             back.append(sourceName).append(":line ").append(line).append(":column ").append(lineOffset).append(':');
150         }
151         if (message != null && message.length() != 0) {
152             back.append(message);
153         } else {
154             back.append("unknown error");
155         }
156         if (lineSource != null && lineSource.length() != 0) {
157             back.append("\n\t").append(lineSource);
158         }
159         return back.toString();
160     }
161 
162     /**
163      * Sets the file.
164      *
165      * @param file
166      *            the new file
167      */
168     public void setFile(File file) {
169         sourceFile = file;
170     }
171 
172 }