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
.List
;
14 import java
.util
.regex
.Pattern
;
15 import java
.util
.zip
.ZipEntry
;
16 import java
.util
.zip
.ZipException
;
17 import java
.util
.zip
.ZipFile
;
20 * list resources available from the classpath @ *
22 class TransBundle_ResourceList
{
25 * for all elements of java.class.path get a Collection of resources Pattern
26 * pattern = Pattern.compile(".*"); gets all resources
29 * the pattern to match
30 * @return the resources in the order they are found
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
));
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
));
51 retval
.addAll(getResourcesFromJarFile(file
, pattern
));
57 private static Collection
<String
> getResourcesFromJarFile(final File file
,
58 final Pattern pattern
) {
59 final ArrayList
<String
> retval
= new ArrayList
<String
>();
62 zf
= new ZipFile(file
);
63 } catch (final ZipException e
) {
65 } catch (final IOException e
) {
68 final Enumeration
<?
extends ZipEntry
> e
= zf
.entries();
69 while (e
.hasMoreElements()) {
70 final ZipEntry ze
= e
.nextElement();
71 final String fileName
= ze
.getName();
72 final boolean accept
= pattern
.matcher(fileName
).matches();
79 } catch (final IOException e1
) {
86 private static Collection
<String
> getResourcesFromDirectory(
87 final File directory
, final Pattern pattern
) {
88 List
<String
> acc
= new ArrayList
<String
>();
89 List
<File
> dirs
= new ArrayList
<File
>();
90 getResourcesFromDirectory(acc
, dirs
, directory
, pattern
);
92 List
<String
> rep
= new ArrayList
<String
>();
93 for (String value
: acc
) {
94 if (pattern
.matcher(value
).matches()) {
102 private static void getResourcesFromDirectory(List
<String
> acc
,
103 List
<File
> dirs
, final File directory
, final Pattern pattern
) {
104 final File
[] fileList
= directory
.listFiles();
105 if (fileList
!= null) {
106 for (final File file
: fileList
) {
107 if (!dirs
.contains(file
)) {
109 String key
= file
.getCanonicalPath();
110 if (!acc
.contains(key
)) {
111 if (file
.isDirectory()) {
113 getResourcesFromDirectory(acc
, dirs
, file
,
119 } catch (IOException e
) {