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   */
42  public class MinifierUtil {
43  
44      public static String minifyCss(String content) {
45          return _instance._minifyCss(content);
46      }
47  
48      public static String minifyJavaScript(String content) {
49          return _instance._minifyJavaScript(content);
50      }
51  
52      private MinifierUtil() {
53      }
54  
55      private String _minifyCss(String content) {
56          StringWriter stringWriter = new StringWriter();
57  
58          try {
59              CssCompressor cssCompressor = new CssCompressor(
60                  new BufferedReader(new StringReader(content)));
61  
62              cssCompressor.compress(stringWriter, _CSS_LINE_BREAK);
63          }
64          catch (Exception e) {
65              _log.error("CSS Minifier failed for\n" + content);
66  
67              stringWriter.append(content);
68          }
69  
70          return stringWriter.toString();
71      }
72  
73      private String _minifyJavaScript(String content) {
74          StringWriter stringWriter = new StringWriter();
75  
76          try {
77              JavaScriptCompressor javaScriptCompressor =
78                  new JavaScriptCompressor(
79                      new BufferedReader(new StringReader(content)),
80                      new JavaScriptErrorReporter());
81  
82              javaScriptCompressor.compress(
83                      stringWriter, _JS_LINE_BREAK, _JS_MUNGE, _JS_VERBOSE,
84                      _JS_PRESERVE_ALL_SEMICOLONS, _JS_DISABLE_OPTIMIZATIONS);
85          }
86          catch (Exception e) {
87              _log.error("JavaScript Minifier failed for\n" + content);
88  
89              stringWriter.append(content);
90          }
91  
92          return stringWriter.toString();
93      }
94  
95      private static final int _CSS_LINE_BREAK = -1;
96  
97      private static final boolean _JS_DISABLE_OPTIMIZATIONS = false;
98  
99      private static final int _JS_LINE_BREAK = -1;
100 
101     private static final boolean _JS_MUNGE = true;
102 
103     private static final boolean _JS_PRESERVE_ALL_SEMICOLONS = false;
104 
105     private static final boolean _JS_VERBOSE = false;
106 
107     private static Log _log = LogFactoryUtil.getLog(MinifierUtil.class);
108 
109     private static MinifierUtil _instance = new MinifierUtil();
110 
111     private class JavaScriptErrorReporter implements ErrorReporter {
112 
113         public void error(
114             String message, String sourceName, int line, String lineSource,
115             int lineOffset) {
116 
117             if (line < 0) {
118                 _log.error(message);
119             }
120             else {
121                 _log.error(line + ": " + lineOffset + ": " + message);
122             }
123         }
124 
125         public EvaluatorException runtimeError(
126             String message, String sourceName, int line, String lineSource,
127             int lineOffset) {
128 
129             error(message, sourceName, line, lineSource, lineOffset);
130 
131             return new EvaluatorException(message);
132         }
133 
134         public void warning(
135             String message, String sourceName, int line, String lineSource,
136             int lineOffset) {
137 
138             if (!_log.isWarnEnabled()) {
139                 return;
140             }
141 
142             if (line < 0) {
143                 _log.warn(message);
144             }
145             else {
146                 _log.warn(line + ": " + lineOffset + ": " + message);
147             }
148         }
149 
150     }
151 
152 }