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