1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.tunyk.mvn.plugins.htmlcompressor;
20
21 import java.io.IOException;
22 import java.nio.charset.Charset;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.util.Map.Entry;
26 import java.util.concurrent.ConcurrentMap;
27
28
29
30
31 public class HtmlCompressor {
32
33
34 private static final String[] FILE_EXT = { "htm", "html" };
35
36
37 private String[] fileExtensions;
38
39
40 private String srcDirPath;
41
42
43 private String targetDirPath;
44
45
46 private Charset fileEncoding;
47
48
49 private boolean createJsonFile;
50
51
52 private String targetJsonFilePath;
53
54
55 private String jsonIntegrationFilePath;
56
57
58 private com.googlecode.htmlcompressor.compressor.HtmlCompressor htmlCompressor;
59
60
61
62
63
64
65
66
67
68 public HtmlCompressor(String srcDirPath, String targetDirPath) {
69 this.srcDirPath = srcDirPath;
70 this.targetDirPath = targetDirPath;
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 public HtmlCompressor(String srcDirPath, String targetDirPath, boolean createJsonFile, String targetJsonFilePath,
88 String jsonIntegrationFilePath) {
89 this.srcDirPath = srcDirPath;
90 this.targetDirPath = targetDirPath;
91 this.createJsonFile = createJsonFile;
92 this.targetJsonFilePath = targetJsonFilePath;
93 this.jsonIntegrationFilePath = jsonIntegrationFilePath;
94 }
95
96
97
98
99
100
101
102 public void compress() throws IOException {
103 if (fileExtensions == null || fileExtensions.length == 0) {
104 fileExtensions = FILE_EXT;
105 }
106
107 FileTool fileTool = new FileTool(srcDirPath, fileExtensions, true);
108 fileTool.setFileEncoding(fileEncoding);
109 ConcurrentMap<String, String> map = fileTool.getFiles();
110
111 if (htmlCompressor == null) {
112 htmlCompressor = new com.googlecode.htmlcompressor.compressor.HtmlCompressor();
113 }
114
115 for (Entry<String, String> key : map.entrySet()) {
116 map.put(key.getKey(), htmlCompressor.compress(key.getValue()));
117 }
118
119 fileTool.writeFiles(map, targetDirPath);
120 if (createJsonFile) {
121 String jsonIntegrationCode = Files.readString(Path.of(jsonIntegrationFilePath),
122 fileEncoding == null ? Charset.defaultCharset() : fileEncoding);
123 fileTool.writeToJsonFile(map, targetJsonFilePath, jsonIntegrationCode);
124 }
125 }
126
127
128
129
130
131
132 public String[] getFileExtensions() {
133 return fileExtensions;
134 }
135
136
137
138
139
140
141
142 public void setFileExtensions(String[] fileExtensions) {
143 this.fileExtensions = fileExtensions;
144 }
145
146
147
148
149
150
151 public String getSrcDirPath() {
152 return srcDirPath;
153 }
154
155
156
157
158
159
160
161 public void setSrcDirPath(String srcDirPath) {
162 this.srcDirPath = srcDirPath;
163 }
164
165
166
167
168
169
170 public String getTargetDirPath() {
171 return targetDirPath;
172 }
173
174
175
176
177
178
179
180 public void setTargetDirPath(String targetDirPath) {
181 this.targetDirPath = targetDirPath;
182 }
183
184
185
186
187
188
189 public Charset getFileEncoding() {
190 return fileEncoding;
191 }
192
193
194
195
196
197
198
199 public void setFileEncoding(Charset fileEncoding) {
200 this.fileEncoding = fileEncoding == null ? Charset.defaultCharset() : fileEncoding;
201 }
202
203
204
205
206
207
208 public boolean isCreateJsonFile() {
209 return createJsonFile;
210 }
211
212
213
214
215
216
217
218 public void setCreateJsonFile(boolean createJsonFile) {
219 this.createJsonFile = createJsonFile;
220 }
221
222
223
224
225
226
227 public String getTargetJsonFilePath() {
228 return targetJsonFilePath;
229 }
230
231
232
233
234
235
236
237 public void setTargetJsonFilePath(String targetJsonFilePath) {
238 this.targetJsonFilePath = targetJsonFilePath;
239 }
240
241
242
243
244
245
246 public String getJsonIntegrationFilePath() {
247 return jsonIntegrationFilePath;
248 }
249
250
251
252
253
254
255
256 public void setJsonIntegrationFilePath(String jsonIntegrationFilePath) {
257 this.jsonIntegrationFilePath = jsonIntegrationFilePath;
258 }
259
260
261
262
263
264
265 public com.googlecode.htmlcompressor.compressor.HtmlCompressor getHtmlCompressor() {
266 return htmlCompressor;
267 }
268
269
270
271
272
273
274
275 public void setHtmlCompressor(com.googlecode.htmlcompressor.compressor.HtmlCompressor htmlCompressor) {
276 this.htmlCompressor = htmlCompressor;
277 }
278 }