AbstractBuilder.java
4.2 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
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.commons.io.IOUtils
* 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.impl.HtmlLibraryBuilder;
import com.adobe.granite.ui.clientlibs.impl.ProcessorConfig;
import com.adobe.granite.ui.clientlibs.impl.ProcessorProvider;
import com.adobe.granite.ui.clientlibs.script.ScriptProcessor;
import com.adobe.granite.ui.clientlibs.script.ScriptResource;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class AbstractBuilder
implements HtmlLibraryBuilder {
private static final Logger log = LoggerFactory.getLogger(AbstractBuilder.class);
protected final String libraryPath;
private ProcessorProvider processorProvider = null;
private List<ProcessorConfig> configs = Collections.emptyList();
private boolean minify;
protected AbstractBuilder(String libraryPath) {
this.libraryPath = libraryPath;
}
@Override
public String build(Collection<ScriptResource> resources) throws IOException {
String script = this.concatenate(resources);
if (script.length() > 0 && this.processorProvider != null) {
String mode = this.minify ? "min" : "default";
for (ProcessorConfig cfg : this.configs) {
ScriptProcessor processor;
if (!cfg.getMode().equals(mode) || (processor = this.processorProvider.getProcessor(cfg.getProcessorName())) == null) continue;
StringWriter out = new StringWriter(script.length());
HashMap<String, String> options = new HashMap<String, String>(cfg.getOptions());
if (this.minify && !options.containsKey("minify")) {
options.put("minify", "true");
}
if (processor.process(this.getType(), new StringScriptResource(script), out, options)) {
log.debug("processed {} with processor {} for mode {}", new Object[]{this.libraryPath, cfg.getProcessorName(), mode});
script = out.toString();
continue;
}
log.debug("processing {} with processor {} for mode {} rejected.", new Object[]{this.libraryPath, cfg.getProcessorName(), mode});
}
}
return script;
}
@Override
public void setDoMinify(boolean doMinify) {
this.minify = doMinify;
}
public void setProcessorProvider(ProcessorProvider processorProvider) {
this.processorProvider = processorProvider;
}
public void setProcessorConfigs(List<ProcessorConfig> configs) {
this.configs = configs;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected String concatenate(Collection<ScriptResource> resources) throws IOException {
StringWriter out = new StringWriter();
for (ScriptResource resource : resources) {
Reader r = null;
try {
r = resource.getReader();
IOUtils.copy((Reader)r, (Writer)out);
}
finally {
IOUtils.closeQuietly((Reader)r);
}
out.append("\n");
}
return out.toString();
}
private class StringScriptResource
implements ScriptResource {
private final String source;
public StringScriptResource(String source) {
this.source = source;
}
@Override
public String getName() {
return AbstractBuilder.this.libraryPath + AbstractBuilder.this.getType().extension;
}
@Override
public Reader getReader() throws IOException {
return new StringReader(this.source);
}
@Override
public long getSize() {
return this.source.length();
}
}
}