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