1 package com.zmicer.utils;
2
3 import org.apache.log4j.Logger;
4
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.util.ArrayList;
8 import java.util.List;
9 import java.util.PropertyResourceBundle;
10 import java.util.ResourceBundle;
11 import java.util.StringTokenizer;
12 import java.util.regex.Pattern;
13
14
15
16
17
18
19
20 public class FileUtils
21 {
22
23
24
25 final public static Logger LOG = Logger.getLogger(ZmicerUtilsConstants.LoggingCategory.OBTAIN_RESOURCES.toString());
26
27
28
29
30
31
32
33
34
35 public static List<File> findFilesByRE(final String dirName, final Pattern reFileDefinition)
36 {
37 InputArgumentUtils.checkObjects(dirName, reFileDefinition);
38 LoggingUtils.debug(LOG, FileUtils.class, "Start finding the files using string dir [" + dirName + "] using pattern [" +
39 reFileDefinition.pattern() + "]");
40 final File dir = new File(dirName);
41 if (!dir.isDirectory())
42 {
43 final String loggingMessage = "The dir [" + dirName + "] doesn't point to the real dir.";
44 LoggingUtils.debug(LOG, FileUtils.class, loggingMessage);
45 throw new IllegalArgumentException(loggingMessage);
46 }
47 return findFilesByRE(dir, reFileDefinition);
48 }
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public static List<File> findFilesByRE(final File dirFile, final Pattern reFileDefinition)
63 {
64 InputArgumentUtils.checkObjects(dirFile, reFileDefinition);
65 final List<File> result = new ArrayList<File>();
66 if (!dirFile.isDirectory())
67 {
68 final String loggingMessage = "The dir [" + dirFile.getAbsolutePath() + "] doesn't point to the real dir.";
69 LoggingUtils.debug(LOG, FileUtils.class, loggingMessage);
70 throw new IllegalArgumentException(loggingMessage);
71 }
72 final File[] files = dirFile.listFiles();
73 final List<File> dirsToBeChecked = new ArrayList<File>();
74
75 for (final File fileToBeChecked : files)
76 {
77 if (fileToBeChecked.isDirectory())
78 {
79 dirsToBeChecked.add(fileToBeChecked);
80 }
81 else if (fileToBeChecked.isFile())
82 {
83 final String fileName = fileToBeChecked.getName();
84 if (REUtils.matches(fileName, reFileDefinition))
85 {
86 result.add(fileToBeChecked);
87 }
88 }
89 }
90 for (final File dirToBeChecked : dirsToBeChecked)
91 {
92 result.addAll(findFilesByRE(dirToBeChecked, reFileDefinition));
93 }
94 return result;
95 }
96
97
98
99
100
101
102
103
104
105 public static List<File> findFoldersByRE(final String dirName, final Pattern reFolderDefinition)
106 {
107 InputArgumentUtils.checkObjects(dirName, reFolderDefinition);
108 LoggingUtils.debug(LOG, FileUtils.class, "Start finding the folders using string dir [" + dirName + "] and pattern [" +
109 reFolderDefinition.pattern() + "]");
110 final File dir = new File(dirName);
111 if (!dir.isDirectory())
112 {
113 final String loggingMessage = "The dir [" + dirName + "] doesn't point to the real dir.";
114 LoggingUtils.debug(LOG, FileUtils.class, loggingMessage);
115 throw new IllegalArgumentException(loggingMessage);
116 }
117 return findFoldersByRE(dir, reFolderDefinition);
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131
132 public static List<File> findFoldersByRE(final File dirFile, final Pattern reFolderDefinition)
133 {
134 InputArgumentUtils.checkObjects(dirFile, reFolderDefinition);
135 final List<File> result = new ArrayList<File>();
136 if (!dirFile.isDirectory())
137 {
138 final String loggingMessage = "The dir [" + dirFile.getAbsolutePath() + "] doesn't point to the real dir.";
139 LoggingUtils.debug(LOG, FileUtils.class, loggingMessage);
140 throw new IllegalArgumentException(loggingMessage);
141 }
142 if (REUtils.matches(dirFile.getName(), reFolderDefinition))
143 {
144 result.add(dirFile);
145 }
146 final File[] files = dirFile.listFiles();
147 final List<File> dirsToBeChecked = new ArrayList<File>();
148
149 for (final File fileToBeChecked : files)
150 {
151 if (fileToBeChecked.isDirectory())
152 {
153 dirsToBeChecked.add(fileToBeChecked);
154 final String folderName = fileToBeChecked.getName();
155 if (REUtils.matches(folderName, reFolderDefinition))
156 {
157 result.add(fileToBeChecked);
158 }
159 }
160 }
161 for (final File dirToBeChecked : dirsToBeChecked)
162 {
163 result.addAll(findFilesByRE(dirToBeChecked, reFolderDefinition));
164 }
165 return result;
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 public static List<File> findFilesByREInClassPath(final List<String> classPathStrings, final Pattern reFileDefinition)
184 {
185 InputArgumentUtils.checkObjects(classPathStrings, reFileDefinition);
186
187
188 List<String> forLog = new ArrayList<String>();
189 forLog.add("Searching for the files using pattern [" + reFileDefinition.pattern() + "] in the following classpathes dirs");
190 forLog.addAll(classPathStrings);
191 LoggingUtils.debug(LOG, FileUtils.class, StringUtils.joinStringsWithLineFolding(forLog));
192
193 final List<File> result = new ArrayList<File>();
194 final List<String> files = ClassPathUtils.getFileNames(classPathStrings, true);
195 final List<String> dirs = ClassPathUtils.getDirNames(classPathStrings, true);
196 if (null != files)
197 {
198 for (String fileName : files)
199 {
200 int counter = 0;
201 if (REUtils.matches(fileName, reFileDefinition))
202 {
203 File file = new File(fileName);
204
205 result.add(file);
206 counter++;
207 LoggingUtils.debug(LOG, FileUtils.class, "Found file number [" + counter + "] with name [" + fileName +
208 "]matching the regexp using the provided classpath.");
209 }
210 }
211 }
212 if (null != dirs)
213 {
214 for (String dir : dirs)
215 {
216 int counter = 0;
217 final List<File> dirObjects = findFilesByRE(dir, reFileDefinition);
218 if (null != dirObjects && !dirObjects.isEmpty())
219 {
220 counter += dirObjects.size();
221 result.addAll(dirObjects);
222 forLog = new ArrayList<String>();
223 forLog.add("Found [" + counter + "] more files matching the regexp.");
224 for (File fileObject : dirObjects)
225 {
226 forLog.add(fileObject.getAbsolutePath());
227 }
228 LoggingUtils.debug(LOG, FileUtils.class, StringUtils.joinStringsWithLineFolding(forLog));
229 }
230 }
231 }
232 return result;
233 }
234
235
236
237
238
239
240
241
242
243
244
245 public static String getFolderBaseName(final String folderFullName)
246 {
247 InputArgumentUtils.checkStrings(true, folderFullName);
248 final StringTokenizer tokenizer = new StringTokenizer(folderFullName, File.separator);
249 String result = null;
250 while (tokenizer.hasMoreTokens())
251 {
252 result = tokenizer.nextToken();
253 }
254 return result;
255 }
256
257
258
259
260
261
262
263
264
265
266
267
268 public static ResourceBundle getResourceBundleViaFile(final String resourceName)
269 {
270 InputArgumentUtils.checkStrings(true, resourceName);
271
272 final String propertiesExtension = ".properties";
273 String resourceForFile = ((resourceName.contains(File.separator)) ? resourceName : resourceName.replace(".", File.separator)) +
274 (resourceName.contains(propertiesExtension) ? "" : propertiesExtension);
275
276
277 LoggingUtils.debug(LOG, FileUtils.class, "Trying to obtain resource [" + resourceName + "] using File class facility. Name " +
278 "of file [" + resourceForFile + "]");
279 final File resourceFile = new File(resourceForFile);
280 if (resourceFile.isFile())
281 {
282 try
283 {
284 LoggingUtils.debug(LOG, FileUtils.class, "The ResourceBundle with the name [" + resourceForFile +
285 "] was obtained via the FileInputStream (name is anot a base name but file name)");
286 FileInputStream inputStream = new FileInputStream(resourceFile);
287 return new PropertyResourceBundle(inputStream);
288 }
289 catch (Exception ex)
290 {
291
292 }
293 }
294 LoggingUtils.debug(LOG, FileUtils.class, "Can not obtain resource [" + resourceName + "] using File class facility. Name " +
295 "of file [" + resourceForFile + "]");
296 return null;
297 }
298
299
300
301
302
303
304
305
306
307 public static String constructPath(final String... args)
308 {
309 InputArgumentUtils.checkStrings(true, args);
310 String result = "";
311 boolean isEmpty = true;
312 for (final String part : args)
313 {
314 result += ((isEmpty) ? "" : File.separator) + part;
315 isEmpty = false;
316 }
317 return result;
318 }
319 }
320