1 package be
.nikiroo
.utils
.resources
;
3 // code copied from from:
4 // http://forums.devx.com/showthread.php?t=153784,
6 // http://stackoverflow.com/questions/3923129/get-a-list-of-resources-from-classpath-directory
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
;
19 * list resources available from the classpath @ *
21 class TransBundle_ResourceList
{
24 * for all elements of java.class.path get a Collection of resources Pattern
25 * pattern = Pattern.compile(".*"); gets all resources
28 * the pattern to match
29 * @return the resources in the order they are found
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
));
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
));
50 retval
.addAll(getResourcesFromJarFile(file
, pattern
));
56 private static Collection
<String
> getResourcesFromJarFile(final File file
,
57 final Pattern pattern
) {
58 final ArrayList
<String
> retval
= new ArrayList
<String
>();
61 zf
= new ZipFile(file
);
62 } catch (final ZipException e
) {
64 } catch (final IOException e
) {
67 final Enumeration
<?
extends ZipEntry
> e
= zf
.entries();
68 while (e
.hasMoreElements()) {
69 final ZipEntry ze
= e
.nextElement();
70 final String fileName
= ze
.getName();
71 final boolean accept
= pattern
.matcher(fileName
).matches();
78 } catch (final IOException e1
) {
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
));
94 final String fileName
= file
.getCanonicalPath();
95 final boolean accept
= pattern
.matcher(fileName
).matches();
99 } catch (final IOException e
) {