1
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
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 }