1 /** 2 * Copyright 2005-2016 The Kuali Foundation 3 * 4 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * 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 and 14 * limitations under the License. 15 */ 16 package org.kuali.rice.krad.theme.util; 17 18 import org.apache.log4j.Logger; 19 import org.mozilla.javascript.ErrorReporter; 20 import org.mozilla.javascript.EvaluatorException; 21 22 /** 23 * Reports any error occurring during JavaScript files compression 24 * 25 * @author Kuali Rice Team (rice.collab@kuali.org) 26 */ 27 public class JavaScriptErrorReporter implements ErrorReporter { 28 private static final Logger LOG = Logger.getLogger(JavaScriptErrorReporter.class); 29 30 private String filename; 31 32 /** 33 * Error reporter constructor 34 * 35 * @param filename JavaScript source filename 36 */ 37 public JavaScriptErrorReporter(String filename) { 38 this.filename = filename; 39 } 40 41 /** 42 * Reports a warning 43 * 44 * @param message a String describing the warning 45 * @param sourceName a String describing the JavaScript source where the warning occured; typically a filename or 46 * URL 47 * @param line the line number associated with the warning 48 * @param lineSource the text of the line (may be null) 49 * @param lineOffset the offset into lineSource where problem was detected 50 */ 51 @Override 52 public void warning(String message, String sourceName, int line, String lineSource, int lineOffset) { 53 if (line < 0) { 54 LOG.warn(message); 55 } else { 56 LOG.warn("[" + filename + ":" + line + "] " + message); 57 } 58 } 59 60 /** 61 * Reports an error. If execution has not yet begun, the JavaScript engine is free to find additional errors rather 62 * than terminating the translation. However, it will not execute a script that had errors. 63 * 64 * @param message a String describing the warning 65 * @param sourceName a String describing the JavaScript source where the warning occured; typically a filename or 66 * URL 67 * @param line the line number associated with the warning 68 * @param lineSource the text of the line (may be null) 69 * @param lineOffset the offset into lineSource where problem was detected 70 */ 71 @Override 72 public void error(String message, String sourceName, int line, String lineSource, int lineOffset) { 73 if (line < 0) { 74 LOG.error(message); 75 } else { 76 LOG.error("[" + filename + ":" + line + "] " + message); 77 } 78 } 79 80 /** 81 * Creates an EvaluatorException that may be thrown. runtimeErrors, unlike errors, will always terminate the 82 * current script 83 * 84 * @param message a String describing the warning 85 * @param sourceName a String describing the JavaScript source where the warning occured; typically a filename or 86 * URL 87 * @param line the line number associated with the warning 88 * @param lineSource the text of the line (may be null) 89 * @param lineOffset the offset into lineSource where problem was detected 90 */ 91 @Override 92 public EvaluatorException runtimeError(String message, String sourceName, int line, String lineSource, 93 int lineOffset) { 94 error(message, sourceName, line, lineSource, lineOffset); 95 96 return new EvaluatorException(message); 97 } 98 }