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