001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.theme.util;
017
018import org.apache.log4j.Logger;
019import org.mozilla.javascript.ErrorReporter;
020import org.mozilla.javascript.EvaluatorException;
021
022/**
023 * Reports any error occurring during JavaScript files compression
024 *
025 * @author Kuali Rice Team (rice.collab@kuali.org)
026 */
027public class JavaScriptErrorReporter implements ErrorReporter {
028    private static final Logger LOG = Logger.getLogger(JavaScriptErrorReporter.class);
029
030    private String filename;
031
032    /**
033     * Error reporter constructor
034     *
035     * @param filename JavaScript source filename
036     */
037    public JavaScriptErrorReporter(String filename) {
038        this.filename = filename;
039    }
040
041    /**
042     * Reports a warning
043     *
044     * @param message a String describing the warning
045     * @param sourceName a String describing the JavaScript source where the warning occured; typically a filename or
046     * URL
047     * @param line the line number associated with the warning
048     * @param lineSource the text of the line (may be null)
049     * @param lineOffset the offset into lineSource where problem was detected
050     */
051    @Override
052    public void warning(String message, String sourceName, int line, String lineSource, int lineOffset) {
053        if (line < 0) {
054            LOG.warn(message);
055        } else {
056            LOG.warn("[" + filename + ":" + line + "] " + message);
057        }
058    }
059
060    /**
061     * Reports an error. If execution has not yet begun, the JavaScript engine is free to find additional errors rather
062     * than terminating the translation. However, it will not execute a script that had errors.
063     *
064     * @param message a String describing the warning
065     * @param sourceName a String describing the JavaScript source where the warning occured; typically a filename or
066     * URL
067     * @param line the line number associated with the warning
068     * @param lineSource the text of the line (may be null)
069     * @param lineOffset the offset into lineSource where problem was detected
070     */
071    @Override
072    public void error(String message, String sourceName, int line, String lineSource, int lineOffset) {
073        if (line < 0) {
074            LOG.error(message);
075        } else {
076            LOG.error("[" + filename + ":" + line + "] " + message);
077        }
078    }
079
080    /**
081     * Creates an EvaluatorException that may be thrown. runtimeErrors, unlike errors, will always terminate the
082     * current script
083     *
084     * @param message a String describing the warning
085     * @param sourceName a String describing the JavaScript source where the warning occured; typically a filename or
086     * URL
087     * @param line the line number associated with the warning
088     * @param lineSource the text of the line (may be null)
089     * @param lineOffset the offset into lineSource where problem was detected
090     */
091    @Override
092    public EvaluatorException runtimeError(String message, String sourceName, int line, String lineSource,
093            int lineOffset) {
094        error(message, sourceName, line, lineSource, lineOffset);
095
096        return new EvaluatorException(message);
097    }
098}