YUIScriptProcessor.java
4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.yahoo.platform.yui.compressor.CssCompressor
* com.yahoo.platform.yui.compressor.JavaScriptCompressor
* javax.annotation.Nonnull
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Service
* org.mozilla.javascript.ErrorReporter
* org.mozilla.javascript.EvaluatorException
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.ui.clientlibs.impl;
import com.adobe.granite.ui.clientlibs.LibraryType;
import com.adobe.granite.ui.clientlibs.script.ScriptProcessor;
import com.adobe.granite.ui.clientlibs.script.ScriptResource;
import com.yahoo.platform.yui.compressor.CssCompressor;
import com.yahoo.platform.yui.compressor.JavaScriptCompressor;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Map;
import javax.annotation.Nonnull;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service(value={ScriptProcessor.class})
public class YUIScriptProcessor
implements ScriptProcessor {
private static final Logger log = LoggerFactory.getLogger(YUIScriptProcessor.class);
private static final int lineBreakPos = 0;
private static final boolean warn = false;
private static final boolean munge = true;
private static final boolean preserveAllSemiColons = false;
private static final boolean disableOptimizationsOpt = false;
@Nonnull
@Override
public String getName() {
return "yui";
}
@Override
public boolean handles(@Nonnull LibraryType type) {
return type == LibraryType.CSS || type == LibraryType.JS;
}
@Override
public boolean process(@Nonnull LibraryType type, @Nonnull ScriptResource source, @Nonnull Writer output, @Nonnull Map<String, String> options) throws IOException {
if (!this.handles(type)) {
log.warn("invalid process call with given type {} and options {}", (Object)type, options);
return false;
}
String minify = options.get("minify");
if (!"true".equals(minify)) {
return false;
}
if (type == LibraryType.CSS) {
CssCompressor compressor = new CssCompressor(source.getReader());
compressor.compress(output, 0);
return true;
}
return this.minifyJS(source.getReader(), output);
}
protected boolean minifyJS(@Nonnull Reader source, @Nonnull Writer output) throws IOException {
JavaScriptCompressor compressor = new JavaScriptCompressor(source, new ErrorReporter(){
public void warning(String message, String sourceName, int line, String lineSource, int lineOffset) {
if (line < 0) {
log.debug("\n[WARNING] " + message);
} else {
log.debug("\n[WARNING] " + line + ':' + lineOffset + ':' + message);
log.debug("\n[WARNING] " + lineSource);
}
}
public void error(String message, String sourceName, int line, String lineSource, int lineOffset) {
if (line < 0) {
log.error("\n[ERROR] " + message);
} else {
log.error("\n[ERROR] " + line + ':' + lineOffset + ':' + message);
log.error("\n[ERROR] " + lineSource);
}
}
public EvaluatorException runtimeError(String message, String sourceName, int line, String lineSource, int lineOffset) {
this.error(message, sourceName, line, lineSource, lineOffset);
return new EvaluatorException(message);
}
});
try {
compressor.compress(output, null, 0, true, false, false, false);
return true;
}
catch (IndexOutOfBoundsException e) {
log.warn("Internal error while compressing script. Most probably caused by empty input: {}", (Object)e.toString());
return false;
}
}
}