Commit | Line | Data |
---|---|---|
ec1f3444 NR |
1 | package be.nikiroo.utils.resources; |
2 | ||
3 | // code copied from from: | |
4 | // http://forums.devx.com/showthread.php?t=153784, | |
5 | // via: | |
6 | // http://stackoverflow.com/questions/3923129/get-a-list-of-resources-from-classpath-directory | |
7 | ||
8 | import java.io.File; | |
9 | import java.io.IOException; | |
10 | import java.util.ArrayList; | |
11 | import java.util.Collection; | |
12 | import java.util.Enumeration; | |
e8aa5bf9 | 13 | import java.util.List; |
ec1f3444 NR |
14 | import java.util.regex.Pattern; |
15 | import java.util.zip.ZipEntry; | |
16 | import java.util.zip.ZipException; | |
17 | import java.util.zip.ZipFile; | |
18 | ||
19 | /** | |
20 | * list resources available from the classpath @ * | |
21 | */ | |
22 | class TransBundle_ResourceList { | |
23 | ||
24 | /** | |
25 | * for all elements of java.class.path get a Collection of resources Pattern | |
26 | * pattern = Pattern.compile(".*"); gets all resources | |
27 | * | |
28 | * @param pattern | |
29 | * the pattern to match | |
30 | * @return the resources in the order they are found | |
31 | */ | |
32 | public static Collection<String> getResources(final Pattern pattern) { | |
33 | final ArrayList<String> retval = new ArrayList<String>(); | |
34 | final String classPath = System.getProperty("java.class.path", "."); | |
35 | final String[] classPathElements = classPath.split(System | |
36 | .getProperty("path.separator")); | |
37 | for (final String element : classPathElements) { | |
38 | retval.addAll(getResources(element, pattern)); | |
39 | } | |
40 | ||
41 | return retval; | |
42 | } | |
43 | ||
44 | private static Collection<String> getResources(final String element, | |
45 | final Pattern pattern) { | |
46 | final ArrayList<String> retval = new ArrayList<String>(); | |
47 | final File file = new File(element); | |
48 | if (file.isDirectory()) { | |
49 | retval.addAll(getResourcesFromDirectory(file, pattern)); | |
50 | } else { | |
51 | retval.addAll(getResourcesFromJarFile(file, pattern)); | |
52 | } | |
53 | ||
54 | return retval; | |
55 | } | |
56 | ||
57 | private static Collection<String> getResourcesFromJarFile(final File file, | |
58 | final Pattern pattern) { | |
59 | final ArrayList<String> retval = new ArrayList<String>(); | |
60 | ZipFile zf; | |
61 | try { | |
62 | zf = new ZipFile(file); | |
63 | } catch (final ZipException e) { | |
64 | throw new Error(e); | |
65 | } catch (final IOException e) { | |
66 | throw new Error(e); | |
67 | } | |
68 | final Enumeration<? extends ZipEntry> e = zf.entries(); | |
69 | while (e.hasMoreElements()) { | |
cd0c27d2 | 70 | final ZipEntry ze = e.nextElement(); |
ec1f3444 NR |
71 | final String fileName = ze.getName(); |
72 | final boolean accept = pattern.matcher(fileName).matches(); | |
73 | if (accept) { | |
74 | retval.add(fileName); | |
75 | } | |
76 | } | |
77 | try { | |
78 | zf.close(); | |
79 | } catch (final IOException e1) { | |
80 | throw new Error(e1); | |
81 | } | |
82 | ||
83 | return retval; | |
84 | } | |
85 | ||
86 | private static Collection<String> getResourcesFromDirectory( | |
87 | final File directory, final Pattern pattern) { | |
e8aa5bf9 NR |
88 | List<String> acc = new ArrayList<String>(); |
89 | List<File> dirs = new ArrayList<File>(); | |
90 | getResourcesFromDirectory(acc, dirs, directory, pattern); | |
91 | ||
92 | List<String> rep = new ArrayList<String>(); | |
93 | for (String value : acc) { | |
94 | if (pattern.matcher(value).matches()) { | |
95 | rep.add(value); | |
96 | } | |
97 | } | |
98 | ||
99 | return rep; | |
100 | } | |
101 | ||
102 | private static void getResourcesFromDirectory(List<String> acc, | |
103 | List<File> dirs, final File directory, final Pattern pattern) { | |
ec1f3444 | 104 | final File[] fileList = directory.listFiles(); |
e8aa5bf9 NR |
105 | if (fileList != null) { |
106 | for (final File file : fileList) { | |
107 | if (!dirs.contains(file)) { | |
108 | try { | |
109 | String key = file.getCanonicalPath(); | |
110 | if (!acc.contains(key)) { | |
111 | if (file.isDirectory()) { | |
112 | dirs.add(file); | |
113 | getResourcesFromDirectory(acc, dirs, file, | |
114 | pattern); | |
115 | } else { | |
116 | acc.add(key); | |
117 | } | |
118 | } | |
119 | } catch (IOException e) { | |
ec1f3444 | 120 | } |
ec1f3444 NR |
121 | } |
122 | } | |
123 | } | |
ec1f3444 NR |
124 | } |
125 | } |