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.Arrays;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.ConcurrentMap;
20 import java.util.regex.Matcher;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
23
24 import org.json.JSONException;
25 import org.json.JSONObject;
26
27
28
29
30 public class FileTool {
31
32
33 private String rootDirPath;
34
35
36 private String[] fileExtensions;
37
38
39 private boolean recursive;
40
41
42 private Charset fileEncoding;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public FileTool(String rootDir, String[] fileExtensions, boolean recursive) throws IOException {
58 this.setRootDirPath(rootDir);
59 this.fileExtensions = fileExtensions;
60 this.recursive = recursive;
61 }
62
63
64
65
66
67
68
69
70
71 public ConcurrentMap<String, String> getFiles() throws IOException {
72 ConcurrentMap<String, String> map = new ConcurrentHashMap<>();
73 Path rootDir = Path.of(rootDirPath);
74 List<Path> paths;
75 try (Stream<Path> walk = Files.walk(rootDir)) {
76 paths = walk.map(Path::normalize).filter(Files::isRegularFile)
77 .filter(path -> Arrays.stream(fileExtensions).anyMatch(path.getFileName().toString()::endsWith))
78 .collect(Collectors.toList());
79 }
80 int truncationIndex = 0;
81 for (Path path : paths) {
82 String normalizedFilePath = path.toFile().getCanonicalPath().replace("\\", "/");
83 if (truncationIndex == 0) {
84 truncationIndex = normalizedFilePath.indexOf(rootDirPath) + rootDirPath.length() + 1;
85 }
86 String key = normalizedFilePath.substring(truncationIndex);
87 String value = Files.readString(path, getFileEncoding());
88 map.put(key, value);
89 }
90 return map;
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104 public void writeFiles(Map<String, String> map, String targetDir) throws IOException {
105 for (Entry<String, String> entry : map.entrySet()) {
106 Path path = Path.of(targetDir + '/' + entry.getKey());
107 Files.createDirectories(path.getParent());
108 Files.writeString(path, entry.getValue(), getFileEncoding());
109 }
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 public void writeToJsonFile(Map<String, String> map, String targetFile, String integrationCode)
128 throws IOException, JSONException {
129 String replacePattern = "\"%s\"";
130 Path path = Path.of(targetFile);
131 JSONObject json = new JSONObject();
132 for (Entry<String, String> entry : map.entrySet()) {
133 json.put(entry.getKey(), entry.getValue());
134 }
135 if (integrationCode == null) {
136 integrationCode = replacePattern;
137 }
138 if (integrationCode.indexOf(replacePattern) == -1) {
139 integrationCode += replacePattern;
140 }
141 String contents = integrationCode.replaceFirst(replacePattern, Matcher.quoteReplacement(json.toString()));
142 Files.createDirectories(path.getParent());
143 Files.writeString(path, contents, getFileEncoding());
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158 @SuppressWarnings("LongDoubleConversion")
159 public static String humanReadableByteCount(long bytes, boolean systemOfUnits) {
160 int unit = systemOfUnits ? 1000 : 1024;
161 if (bytes < unit) {
162 return bytes + " B";
163 }
164 int exp = (int) (Math.log(bytes) / Math.log(unit));
165 String pre = (systemOfUnits ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (systemOfUnits ? "" : "i");
166 return "%.1f %sB".formatted(bytes / Math.pow(unit, exp), pre);
167 }
168
169
170
171
172
173
174
175
176
177 public static String getElapsedHMSTime(long elapsedTime) {
178 String format = "%%0%dd".formatted(2);
179 elapsedTime = elapsedTime / 1000;
180 String seconds = format.formatted(elapsedTime % 60);
181 String minutes = format.formatted((elapsedTime % 3600) / 60);
182 String hours = format.formatted(elapsedTime / 3600);
183 return hours + ":" + minutes + ":" + seconds;
184 }
185
186
187
188
189
190
191 public String getRootDirPath() {
192 return rootDirPath;
193 }
194
195
196
197
198
199
200
201
202
203
204 public void setRootDirPath(String rootDirPath) throws IOException {
205 Path path = Path.of(rootDirPath);
206 this.rootDirPath = path.toFile().getCanonicalPath().replace("\\", "/").replaceAll("/$", "");
207 }
208
209
210
211
212
213
214 public String[] getFileExtensions() {
215 return fileExtensions;
216 }
217
218
219
220
221
222
223
224 public void setFileExtensions(String[] fileExtensions) {
225 this.fileExtensions = fileExtensions;
226 }
227
228
229
230
231
232
233 public boolean isRecursive() {
234 return recursive;
235 }
236
237
238
239
240
241
242
243 public void setRecursive(boolean recursive) {
244 this.recursive = recursive;
245 }
246
247
248
249
250
251
252 public Charset getFileEncoding() {
253 return fileEncoding == null ? Charset.defaultCharset() : fileEncoding;
254 }
255
256
257
258
259
260
261
262 public void setFileEncoding(Charset fileEncoding) {
263 this.fileEncoding = fileEncoding == null ? Charset.defaultCharset() : fileEncoding;
264 }
265 }