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; | |
13 | import java.util.regex.Pattern; | |
14 | import java.util.zip.ZipEntry; | |
15 | import java.util.zip.ZipException; | |
16 | import java.util.zip.ZipFile; | |
17 | ||
18 | /** | |
19 | * list resources available from the classpath @ * | |
20 | */ | |
21 | class TransBundle_ResourceList { | |
22 | ||
23 | /** | |
24 | * for all elements of java.class.path get a Collection of resources Pattern | |
25 | * pattern = Pattern.compile(".*"); gets all resources | |
26 | * | |
27 | * @param pattern | |
28 | * the pattern to match | |
29 | * @return the resources in the order they are found | |
30 | */ | |
31 | public static Collection<String> getResources(final Pattern pattern) { | |
32 | final ArrayList<String> retval = new ArrayList<String>(); | |
33 | final String classPath = System.getProperty("java.class.path", "."); | |
34 | final String[] classPathElements = classPath.split(System | |
35 | .getProperty("path.separator")); | |
36 | for (final String element : classPathElements) { | |
37 | retval.addAll(getResources(element, pattern)); | |
38 | } | |
39 | ||
40 | return retval; | |
41 | } | |
42 | ||
43 | private static Collection<String> getResources(final String element, | |
44 | final Pattern pattern) { | |
45 | final ArrayList<String> retval = new ArrayList<String>(); | |
46 | final File file = new File(element); | |
47 | if (file.isDirectory()) { | |
48 | retval.addAll(getResourcesFromDirectory(file, pattern)); | |
49 | } else { | |
50 | retval.addAll(getResourcesFromJarFile(file, pattern)); | |
51 | } | |
52 | ||
53 | return retval; | |
54 | } | |
55 | ||
56 | private static Collection<String> getResourcesFromJarFile(final File file, | |
57 | final Pattern pattern) { | |
58 | final ArrayList<String> retval = new ArrayList<String>(); | |
59 | ZipFile zf; | |
60 | try { | |
61 | zf = new ZipFile(file); | |
62 | } catch (final ZipException e) { | |
63 | throw new Error(e); | |
64 | } catch (final IOException e) { | |
65 | throw new Error(e); | |
66 | } | |
67 | final Enumeration<? extends ZipEntry> e = zf.entries(); | |
68 | while (e.hasMoreElements()) { | |
cd0c27d2 | 69 | final ZipEntry ze = e.nextElement(); |
ec1f3444 NR |
70 | final String fileName = ze.getName(); |
71 | final boolean accept = pattern.matcher(fileName).matches(); | |
72 | if (accept) { | |
73 | retval.add(fileName); | |
74 | } | |
75 | } | |
76 | try { | |
77 | zf.close(); | |
78 | } catch (final IOException e1) { | |
79 | throw new Error(e1); | |
80 | } | |
81 | ||
82 | return retval; | |
83 | } | |
84 | ||
85 | private static Collection<String> getResourcesFromDirectory( | |
86 | final File directory, final Pattern pattern) { | |
87 | final ArrayList<String> retval = new ArrayList<String>(); | |
88 | final File[] fileList = directory.listFiles(); | |
89 | for (final File file : fileList) { | |
90 | if (file.isDirectory()) { | |
91 | retval.addAll(getResourcesFromDirectory(file, pattern)); | |
92 | } else { | |
93 | try { | |
94 | final String fileName = file.getCanonicalPath(); | |
95 | final boolean accept = pattern.matcher(fileName).matches(); | |
96 | if (accept) { | |
97 | retval.add(fileName); | |
98 | } | |
99 | } catch (final IOException e) { | |
100 | throw new Error(e); | |
101 | } | |
102 | } | |
103 | } | |
104 | ||
105 | return retval; | |
106 | } | |
107 | } |