Change build scripts
[jvcard.git] / src / be / nikiroo / jvcard / resources / ResourceList.java
CommitLineData
e27d1404
NR
1package be.nikiroo.jvcard.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
8import java.io.File;
9import java.io.IOException;
10import java.util.ArrayList;
11import java.util.Collection;
12import java.util.Enumeration;
13import java.util.regex.Pattern;
14import java.util.zip.ZipEntry;
15import java.util.zip.ZipException;
16import java.util.zip.ZipFile;
17
18/**
19 * list resources available from the classpath @ *
20 */
21public class 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 return retval;
40 }
41
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));
48 } else {
49 retval.addAll(getResourcesFromJarFile(file, pattern));
50 }
51 return retval;
52 }
53
54 private static Collection<String> getResourcesFromJarFile(final File file,
55 final Pattern pattern) {
56 final ArrayList<String> retval = new ArrayList<String>();
57 ZipFile zf;
58 try {
59 zf = new ZipFile(file);
60 } catch (final ZipException e) {
61 throw new Error(e);
62 } catch (final IOException e) {
63 throw new Error(e);
64 }
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();
70 if (accept) {
71 retval.add(fileName);
72 }
73 }
74 try {
75 zf.close();
76 } catch (final IOException e1) {
77 throw new Error(e1);
78 }
79 return retval;
80 }
81
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));
89 } else {
90 try {
91 final String fileName = file.getCanonicalPath();
92 final boolean accept = pattern.matcher(fileName).matches();
93 if (accept) {
94 retval.add(fileName);
95 }
96 } catch (final IOException e) {
97 throw new Error(e);
98 }
99 }
100 }
101 return retval;
102 }
103
104 /**
105 * list the resources that match args[0]
106 *
107 * @param args
108 * args[0] is the pattern to match, or list all resources if
109 * there are no args
110 */
111 public static void main(final String[] args) {
112 Pattern pattern;
113 if (args.length < 1) {
114 pattern = Pattern.compile(".*");
115 } else {
116 pattern = Pattern.compile(args[0]);
117 }
118 final Collection<String> list = ResourceList.getResources(pattern);
119 for (final String name : list) {
120 System.out.println(name);
121 }
122 }
123}