1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.mozilla.javascript.ErrorReporter;
18  import com.liferay.mozilla.javascript.EvaluatorException;
19  import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
20  import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
21  import com.liferay.portal.kernel.log.Log;
22  import com.liferay.portal.kernel.log.LogFactoryUtil;
23  import com.liferay.yahoo.platform.yui.compressor.CssCompressor;
24  import com.liferay.yahoo.platform.yui.compressor.JavaScriptCompressor;
25  
26  /**
27   * <a href="MinifierUtil.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class MinifierUtil {
32  
33      public static String minifyCss(String content) {
34          return _instance._minifyCss(content);
35      }
36  
37      public static String minifyJavaScript(String content) {
38          return _instance._minifyJavaScript(content);
39      }
40  
41      private MinifierUtil() {
42      }
43  
44      private String _minifyCss(String content) {
45          UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(true);
46  
47          try {
48              CssCompressor cssCompressor = new CssCompressor(
49                  new UnsyncStringReader(content));
50  
51              cssCompressor.compress(unsyncStringWriter, _CSS_LINE_BREAK);
52          }
53          catch (Exception e) {
54              _log.error("CSS Minifier failed for\n" + content);
55  
56              unsyncStringWriter.append(content);
57          }
58  
59          return unsyncStringWriter.toString();
60      }
61  
62      private String _minifyJavaScript(String content) {
63          UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter(true);
64  
65          try {
66              JavaScriptCompressor javaScriptCompressor =
67                  new JavaScriptCompressor(
68                      new UnsyncStringReader(content),
69                      new JavaScriptErrorReporter());
70  
71              javaScriptCompressor.compress(
72                      unsyncStringWriter, _JS_LINE_BREAK, _JS_MUNGE, _JS_VERBOSE,
73                      _JS_PRESERVE_ALL_SEMICOLONS, _JS_DISABLE_OPTIMIZATIONS);
74          }
75          catch (Exception e) {
76              _log.error("JavaScript Minifier failed for\n" + content);
77  
78              unsyncStringWriter.append(content);
79          }
80  
81          return unsyncStringWriter.toString();
82      }
83  
84      private static final int _CSS_LINE_BREAK = -1;
85  
86      private static final boolean _JS_DISABLE_OPTIMIZATIONS = false;
87  
88      private static final int _JS_LINE_BREAK = -1;
89  
90      private static final boolean _JS_MUNGE = true;
91  
92      private static final boolean _JS_PRESERVE_ALL_SEMICOLONS = false;
93  
94      private static final boolean _JS_VERBOSE = false;
95  
96      private static Log _log = LogFactoryUtil.getLog(MinifierUtil.class);
97  
98      private static MinifierUtil _instance = new MinifierUtil();
99  
100     private class JavaScriptErrorReporter implements ErrorReporter {
101 
102         public void error(
103             String message, String sourceName, int line, String lineSource,
104             int lineOffset) {
105 
106             if (line < 0) {
107                 _log.error(message);
108             }
109             else {
110                 _log.error(line + ": " + lineOffset + ": " + message);
111             }
112         }
113 
114         public EvaluatorException runtimeError(
115             String message, String sourceName, int line, String lineSource,
116             int lineOffset) {
117 
118             error(message, sourceName, line, lineSource, lineOffset);
119 
120             return new EvaluatorException(message);
121         }
122 
123         public void warning(
124             String message, String sourceName, int line, String lineSource,
125             int lineOffset) {
126 
127             if (!_log.isWarnEnabled()) {
128                 return;
129             }
130 
131             if (line < 0) {
132                 _log.warn(message);
133             }
134             else {
135                 _log.warn(line + ": " + lineOffset + ": " + message);
136             }
137         }
138 
139     }
140 
141 }