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