6f87c937d07488fb8f6c37e23f10f6ab25569a21
1 package be
.nikiroo
.jvcard
.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 public class 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
));
42 private static Collection
<String
> getResources(final String element
,
43 final Pattern pattern
) {
44 final ArrayList
<String
> retval
= new ArrayList
<String
>();
45 final File file
= new File(element
);
46 if (file
.isDirectory()) {
47 retval
.addAll(getResourcesFromDirectory(file
, pattern
));
49 retval
.addAll(getResourcesFromJarFile(file
, pattern
));
54 private static Collection
<String
> getResourcesFromJarFile(final File file
,
55 final Pattern pattern
) {
56 final ArrayList
<String
> retval
= new ArrayList
<String
>();
59 zf
= new ZipFile(file
);
60 } catch (final ZipException e
) {
62 } catch (final IOException e
) {
65 final Enumeration
<?
extends ZipEntry
> e
= zf
.entries();
66 while (e
.hasMoreElements()) {
67 final ZipEntry ze
= (ZipEntry
) e
.nextElement();
68 final String fileName
= ze
.getName();
69 final boolean accept
= pattern
.matcher(fileName
).matches();
76 } catch (final IOException e1
) {
82 private static Collection
<String
> getResourcesFromDirectory(
83 final File directory
, final Pattern pattern
) {
84 final ArrayList
<String
> retval
= new ArrayList
<String
>();
85 final File
[] fileList
= directory
.listFiles();
86 for (final File file
: fileList
) {
87 if (file
.isDirectory()) {
88 retval
.addAll(getResourcesFromDirectory(file
, pattern
));
91 final String fileName
= file
.getCanonicalPath();
92 final boolean accept
= pattern
.matcher(fileName
).matches();
96 } catch (final IOException e
) {
105 * list the resources that match args[0]
108 * args[0] is the pattern to match, or list all resources if
111 public static void main(final String
[] args
) {
113 if (args
.length
< 1) {
114 pattern
= Pattern
.compile(".*");
116 pattern
= Pattern
.compile(args
[0]);
118 final Collection
<String
> list
= ResourceList
.getResources(pattern
);
119 for (final String name
: list
) {
120 System
.out
.println(name
);