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