Changeset 6575
- Timestamp:
- 09/23/15 08:04:09 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vlo/branches/vlo-3.3-oeaw/vlo-importer/src/test/java/eu/clarin/cmdi/vlo/importer/ImporterTestcase.java
r6574 r6575 65 65 MetadataImporter.languageCodeUtils = new LanguageCodeUtils(config); 66 66 67 Pattern pattern = Pattern.compile(".*\\.xml"); 68 69 ResourceList rl = new ResourceList(); 70 final Collection<String> list = rl.getResources(pattern); 71 for(final String name : list){ 72 System.out.println(name); 73 } 67 74 68 75 69 } … … 84 78 85 79 86 public class ResourceList{ 87 88 /** 89 * for all elements of java.class.path get a Collection of resources Pattern 90 * pattern = Pattern.compile(".*"); gets all resources 91 * 92 * @param pattern 93 * the pattern to match 94 * @return the resources in the order they are found 95 */ 96 public Collection<String> getResources( 97 final Pattern pattern){ 98 final ArrayList<String> retval = new ArrayList<String>(); 99 final String classPath = System.getProperty("java.class.path", "."); 100 final String[] classPathElements = classPath.split(":"); 101 for(final String element : classPathElements){ 102 retval.addAll(getResources(element, pattern)); 103 } 104 return retval; 105 } 106 107 private Collection<String> getResources( 108 final String element, 109 final Pattern pattern){ 110 final ArrayList<String> retval = new ArrayList<String>(); 111 final File file = new File(element); 112 if(file.isDirectory()){ 113 retval.addAll(getResourcesFromDirectory(file, pattern)); 114 } else{ 115 retval.addAll(getResourcesFromJarFile(file, pattern)); 116 } 117 return retval; 118 } 119 120 private Collection<String> getResourcesFromJarFile( 121 final File file, 122 final Pattern pattern){ 123 final ArrayList<String> retval = new ArrayList<String>(); 124 ZipFile zf; 125 try{ 126 zf = new ZipFile(file); 127 } catch(final ZipException e){ 128 throw new Error(e); 129 } catch(final IOException e){ 130 throw new Error(e); 131 } 132 final Enumeration e = zf.entries(); 133 while(e.hasMoreElements()){ 134 final ZipEntry ze = (ZipEntry) e.nextElement(); 135 final String fileName = ze.getName(); 136 final boolean accept = pattern.matcher(fileName).matches(); 137 if(accept){ 138 retval.add(fileName); 139 } 140 } 141 try{ 142 zf.close(); 143 } catch(final IOException e1){ 144 throw new Error(e1); 145 } 146 return retval; 147 } 148 149 private Collection<String> getResourcesFromDirectory( 150 final File directory, 151 final Pattern pattern){ 152 final ArrayList<String> retval = new ArrayList<String>(); 153 final File[] fileList = directory.listFiles(); 154 for(final File file : fileList){ 155 if(file.isDirectory()){ 156 retval.addAll(getResourcesFromDirectory(file, pattern)); 157 } else{ 158 try{ 159 final String fileName = file.getCanonicalPath(); 160 final boolean accept = pattern.matcher(fileName).matches(); 161 if(accept){ 162 retval.add(fileName); 163 } 164 } catch(final IOException e){ 165 throw new Error(e); 166 } 167 } 168 } 169 return retval; 170 } 171 } 80 172 81 }
Note: See TracChangeset
for help on using the changeset viewer.