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.io.unsync.UnsyncStringReader;
28  import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
29  import com.liferay.portal.kernel.log.Log;
30  import com.liferay.portal.kernel.log.LogFactoryUtil;
31  import com.liferay.yahoo.platform.yui.compressor.CssCompressor;
32  import com.liferay.yahoo.platform.yui.compressor.JavaScriptCompressor;
33  
34  /**
35   * <a href="MinifierUtil.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   */
39  public class MinifierUtil {
40  
41      public static String minifyCss(String content) {
42          return _instance._minifyCss(content);
43      }
44  
45      public static String minifyJavaScript(String content) {
46          return _instance._minifyJavaScript(content);
47      }
48  
49      private MinifierUtil() {
50      }
51  
52      private String _minifyCss(String content) {
53          UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(true);
54  
55          try {
56              CssCompressor cssCompressor = new CssCompressor(
57                  new UnsyncStringReader(content));
58  
59              cssCompressor.compress(unsyncStringWriter, _CSS_LINE_BREAK);
60          }
61          catch (Exception e) {
62              _log.error("CSS Minifier failed for\n" + content);
63  
64              unsyncStringWriter.append(content);
65          }
66  
67          return unsyncStringWriter.toString();
68      }
69  
70      private String _minifyJavaScript(String content) {
71          UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(true);
72  
73          try {
74              JavaScriptCompressor javaScriptCompressor =
75                  new JavaScriptCompressor(
76                      new UnsyncStringReader(content),
77                      new JavaScriptErrorReporter());
78  
79              javaScriptCompressor.compress(
80                      unsyncStringWriter, _JS_LINE_BREAK, _JS_MUNGE, _JS_VERBOSE,
81                      _JS_PRESERVE_ALL_SEMICOLONS, _JS_DISABLE_OPTIMIZATIONS);
82          }
83          catch (Exception e) {
84              _log.error("JavaScript Minifier failed for\n" + content);
85  
86              unsyncStringWriter.append(content);
87          }
88  
89          return unsyncStringWriter.toString();
90      }
91  
92      private static final int _CSS_LINE_BREAK = -1;
93  
94      private static final boolean _JS_DISABLE_OPTIMIZATIONS = false;
95  
96      private static final int _JS_LINE_BREAK = -1;
97  
98      private static final boolean _JS_MUNGE = true;
99  
100     private static final boolean _JS_PRESERVE_ALL_SEMICOLONS = false;
101 
102     private static final boolean _JS_VERBOSE = false;
103 
104     private static Log _log = LogFactoryUtil.getLog(MinifierUtil.class);
105 
106     private static MinifierUtil _instance = new MinifierUtil();
107 
108     private class JavaScriptErrorReporter implements ErrorReporter {
109 
110         public void error(
111             String message, String sourceName, int line, String lineSource,
112             int lineOffset) {
113 
114             if (line < 0) {
115                 _log.error(message);
116             }
117             else {
118                 _log.error(line + ": " + lineOffset + ": " + message);
119             }
120         }
121 
122         public EvaluatorException runtimeError(
123             String message, String sourceName, int line, String lineSource,
124             int lineOffset) {
125 
126             error(message, sourceName, line, lineSource, lineOffset);
127 
128             return new EvaluatorException(message);
129         }
130 
131         public void warning(
132             String message, String sourceName, int line, String lineSource,
133             int lineOffset) {
134 
135             if (!_log.isWarnEnabled()) {
136                 return;
137             }
138 
139             if (line < 0) {
140                 _log.warn(message);
141             }
142             else {
143                 _log.warn(line + ": " + lineOffset + ": " + message);
144             }
145         }
146 
147     }
148 
149 }