1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.util;
24  
25  import com.liferay.mozilla.javascript.ErrorReporter;
26  import com.liferay.mozilla.javascript.EvaluatorException;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.yahoo.platform.yui.compressor.CssCompressor;
30  import com.liferay.yahoo.platform.yui.compressor.JavaScriptCompressor;
31  
32  import java.io.BufferedReader;
33  import java.io.StringReader;
34  import java.io.StringWriter;
35  
36  /**
37   * <a href="MinifierUtil.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class MinifierUtil {
42  
43      public static String minifyCss(String content) {
44          return _instance._minifyCss(content);
45      }
46  
47      public static String minifyJavaScript(String content) {
48          return _instance._minifyJavaScript(content);
49      }
50  
51      private MinifierUtil() {
52      }
53  
54      private String _minifyCss(String content) {
55          StringWriter stringWriter = new StringWriter();
56  
57          try {
58              CssCompressor cssCompressor = new CssCompressor(
59                  new BufferedReader(new StringReader(content)));
60  
61              cssCompressor.compress(stringWriter, _CSS_LINE_BREAK);
62          }
63          catch (Exception e) {
64              _log.error("CSS Minifier failed for\n" + content);
65  
66              stringWriter.append(content);
67          }
68  
69          return stringWriter.toString();
70      }
71  
72      private String _minifyJavaScript(String content) {
73          StringWriter stringWriter = new StringWriter();
74  
75          try {
76              JavaScriptCompressor javaScriptCompressor =
77                  new JavaScriptCompressor(
78                      new BufferedReader(new StringReader(content)),
79                      new JavaScriptErrorReporter());
80  
81              javaScriptCompressor.compress(
82                      stringWriter, _JS_LINE_BREAK, _JS_MUNGE, _JS_VERBOSE,
83                      _JS_PRESERVE_ALL_SEMICOLONS, _JS_DISABLE_OPTIMIZATIONS);
84          }
85          catch (Exception e) {
86              _log.error("JavaScript Minifier failed for\n" + content);
87  
88              stringWriter.append(content);
89          }
90  
91          return stringWriter.toString();
92      }
93  
94      private static final int _CSS_LINE_BREAK = -1;
95  
96      private static final boolean _JS_DISABLE_OPTIMIZATIONS = false;
97  
98      private static final int _JS_LINE_BREAK = -1;
99  
100     private static final boolean _JS_MUNGE = true;
101 
102     private static final boolean _JS_PRESERVE_ALL_SEMICOLONS = false;
103 
104     private static final boolean _JS_VERBOSE = false;
105 
106     private static Log _log = LogFactoryUtil.getLog(MinifierUtil.class);
107 
108     private static MinifierUtil _instance = new MinifierUtil();
109 
110     private class JavaScriptErrorReporter implements ErrorReporter {
111 
112         public void error(
113             String message, String sourceName, int line, String lineSource,
114             int lineOffset) {
115 
116             if (line < 0) {
117                 _log.error(message);
118             }
119             else {
120                 _log.error(line + ": " + lineOffset + ": " + message);
121             }
122         }
123 
124         public EvaluatorException runtimeError(
125             String message, String sourceName, int line, String lineSource,
126             int lineOffset) {
127 
128             error(message, sourceName, line, lineSource, lineOffset);
129 
130             return new EvaluatorException(message);
131         }
132 
133         public void warning(
134             String message, String sourceName, int line, String lineSource,
135             int lineOffset) {
136 
137             if (!_log.isWarnEnabled()) {
138                 return;
139             }
140 
141             if (line < 0) {
142                 _log.warn(message);
143             }
144             else {
145                 _log.warn(line + ": " + lineOffset + ": " + message);
146             }
147         }
148 
149     }
150 
151 }