001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.minifier;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
018    import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.util.PropsValues;
022    
023    import com.yahoo.platform.yui.compressor.JavaScriptCompressor;
024    import com.yahoo.platform.yui.mozilla.javascript.ErrorReporter;
025    import com.yahoo.platform.yui.mozilla.javascript.EvaluatorException;
026    
027    /**
028     * @author Carlos Sierra Andr??s
029     */
030    public class YahooJavaScriptMinifier implements JavaScriptMinifier {
031    
032            @Override
033            public String compress(String resourceName, String content) {
034                    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();
035    
036                    try {
037                            JavaScriptCompressor javaScriptCompressor =
038                                    new JavaScriptCompressor(
039                                            new UnsyncStringReader(content),
040                                            new JavaScriptErrorReporter());
041    
042                            javaScriptCompressor.compress(
043                                    unsyncStringWriter, PropsValues.YUI_COMPRESSOR_JS_LINE_BREAK,
044                                    PropsValues.YUI_COMPRESSOR_JS_MUNGE,
045                                    PropsValues.YUI_COMPRESSOR_JS_VERBOSE,
046                                    PropsValues.YUI_COMPRESSOR_JS_PRESERVE_ALL_SEMICOLONS,
047                                    PropsValues.YUI_COMPRESSOR_JS_DISABLE_OPTIMIZATIONS);
048                    }
049                    catch (Exception e) {
050                            _log.error("Unable to minify JavaScript:\n" + content);
051    
052                            unsyncStringWriter.append(content);
053                    }
054    
055                    return unsyncStringWriter.toString();
056            }
057    
058            private static final Log _log = LogFactoryUtil.getLog(
059                    YahooJavaScriptMinifier.class);
060    
061            private class JavaScriptErrorReporter implements ErrorReporter {
062    
063                    @Override
064                    public void error(
065                            String message, String sourceName, int line, String lineSource,
066                            int lineOffset) {
067    
068                            if (line < 0) {
069                                    _log.error(message);
070                            }
071                            else {
072                                    _log.error(line + ": " + lineOffset + ": " + message);
073                            }
074                    }
075    
076                    @Override
077                    public EvaluatorException runtimeError(
078                            String message, String sourceName, int line, String lineSource,
079                            int lineOffset) {
080    
081                            error(message, sourceName, line, lineSource, lineOffset);
082    
083                            return new EvaluatorException(message);
084                    }
085    
086                    @Override
087                    public void warning(
088                            String message, String sourceName, int line, String lineSource,
089                            int lineOffset) {
090    
091                            if (!_log.isWarnEnabled()) {
092                                    return;
093                            }
094    
095                            if (line < 0) {
096                                    _log.warn(message);
097                            }
098                            else {
099                                    _log.warn(line + ": " + lineOffset + ": " + message);
100                            }
101                    }
102    
103            }
104    
105    }