001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.mozilla.javascript.ErrorReporter;
018    import com.liferay.mozilla.javascript.EvaluatorException;
019    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
020    import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.yahoo.platform.yui.compressor.CssCompressor;
024    import com.liferay.yahoo.platform.yui.compressor.JavaScriptCompressor;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class MinifierUtil {
030    
031            public static String minifyCss(String content) {
032                    return _instance._minifyCss(content);
033            }
034    
035            public static String minifyJavaScript(String content) {
036                    return _instance._minifyJavaScript(content);
037            }
038    
039            private MinifierUtil() {
040            }
041    
042            private String _minifyCss(String content) {
043                    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();
044    
045                    try {
046                            CssCompressor cssCompressor = new CssCompressor(
047                                    new UnsyncStringReader(content));
048    
049                            cssCompressor.compress(unsyncStringWriter, _CSS_LINE_BREAK);
050                    }
051                    catch (Exception e) {
052                            _log.error("CSS Minifier failed for\n" + content);
053    
054                            unsyncStringWriter.append(content);
055                    }
056    
057                    return unsyncStringWriter.toString();
058            }
059    
060            private String _minifyJavaScript(String content) {
061                    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();
062    
063                    try {
064                            JavaScriptCompressor javaScriptCompressor =
065                                    new JavaScriptCompressor(
066                                            new UnsyncStringReader(content),
067                                            new JavaScriptErrorReporter());
068    
069                            javaScriptCompressor.compress(
070                                            unsyncStringWriter, _JS_LINE_BREAK, _JS_MUNGE, _JS_VERBOSE,
071                                            _JS_PRESERVE_ALL_SEMICOLONS, _JS_DISABLE_OPTIMIZATIONS);
072                    }
073                    catch (Exception e) {
074                            _log.error("JavaScript Minifier failed for\n" + content);
075    
076                            unsyncStringWriter.append(content);
077                    }
078    
079                    return unsyncStringWriter.toString();
080            }
081    
082            private static final int _CSS_LINE_BREAK = -1;
083    
084            private static final boolean _JS_DISABLE_OPTIMIZATIONS = false;
085    
086            private static final int _JS_LINE_BREAK = -1;
087    
088            private static final boolean _JS_MUNGE = true;
089    
090            private static final boolean _JS_PRESERVE_ALL_SEMICOLONS = false;
091    
092            private static final boolean _JS_VERBOSE = false;
093    
094            private static Log _log = LogFactoryUtil.getLog(MinifierUtil.class);
095    
096            private static MinifierUtil _instance = new MinifierUtil();
097    
098            private class JavaScriptErrorReporter implements ErrorReporter {
099    
100                    public void error(
101                            String message, String sourceName, int line, String lineSource,
102                            int lineOffset) {
103    
104                            if (line < 0) {
105                                    _log.error(message);
106                            }
107                            else {
108                                    _log.error(line + ": " + lineOffset + ": " + message);
109                            }
110                    }
111    
112                    public EvaluatorException runtimeError(
113                            String message, String sourceName, int line, String lineSource,
114                            int lineOffset) {
115    
116                            error(message, sourceName, line, lineSource, lineOffset);
117    
118                            return new EvaluatorException(message);
119                    }
120    
121                    public void warning(
122                            String message, String sourceName, int line, String lineSource,
123                            int lineOffset) {
124    
125                            if (!_log.isWarnEnabled()) {
126                                    return;
127                            }
128    
129                            if (line < 0) {
130                                    _log.warn(message);
131                            }
132                            else {
133                                    _log.warn(line + ": " + lineOffset + ": " + message);
134                            }
135                    }
136    
137            }
138    
139    }