Changeset 6571
- Timestamp:
- 09/23/15 07:48:37 (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
r6570 r6571 5 5 import java.net.URISyntaxException; 6 6 import java.net.URL; 7 import java.net.URLClassLoader; 7 import java.util.ArrayList; 8 import java.util.Collection; 9 import java.util.Enumeration; 10 import java.util.regex.Pattern; 11 import java.util.zip.ZipEntry; 12 import java.util.zip.ZipException; 13 import java.util.zip.ZipFile; 8 14 9 15 import org.apache.commons.io.FileUtils; … … 59 65 MetadataImporter.languageCodeUtils = new LanguageCodeUtils(config); 60 66 61 System.out.println("CLASSPATH: " + System.getProperty("java.class.path")); 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 } 62 74 63 URL vocabulary = this.getClass().getClassLoader().getResource(VOC_MAPS_PATH + MetadataImporter.config.getLicenseAvailabilityMapUrl()); 64 File f = new File(vocabulary.toURI()); 65 66 if(f.exists()){ 67 System.out.println(f.getAbsolutePath()); 68 System.out.println(f.getPath()); 69 }else{ 70 throw new RuntimeException("File " + VOC_MAPS_PATH + MetadataImporter.config.getLicenseAvailabilityMapUrl() + " doesnt exist"); 71 } 75 System.exit(0); 72 76 73 77 } … … 80 84 } 81 85 } 86 87 88 public class ResourceList{ 82 89 90 /** 91 * for all elements of java.class.path get a Collection of resources Pattern 92 * pattern = Pattern.compile(".*"); gets all resources 93 * 94 * @param pattern 95 * the pattern to match 96 * @return the resources in the order they are found 97 */ 98 public Collection<String> getResources( 99 final Pattern pattern){ 100 final ArrayList<String> retval = new ArrayList<String>(); 101 final String classPath = System.getProperty("java.class.path", "."); 102 final String[] classPathElements = classPath.split(":"); 103 for(final String element : classPathElements){ 104 retval.addAll(getResources(element, pattern)); 105 } 106 return retval; 107 } 108 109 private Collection<String> getResources( 110 final String element, 111 final Pattern pattern){ 112 final ArrayList<String> retval = new ArrayList<String>(); 113 final File file = new File(element); 114 if(file.isDirectory()){ 115 retval.addAll(getResourcesFromDirectory(file, pattern)); 116 } else{ 117 retval.addAll(getResourcesFromJarFile(file, pattern)); 118 } 119 return retval; 120 } 121 122 private Collection<String> getResourcesFromJarFile( 123 final File file, 124 final Pattern pattern){ 125 final ArrayList<String> retval = new ArrayList<String>(); 126 ZipFile zf; 127 try{ 128 zf = new ZipFile(file); 129 } catch(final ZipException e){ 130 throw new Error(e); 131 } catch(final IOException e){ 132 throw new Error(e); 133 } 134 final Enumeration e = zf.entries(); 135 while(e.hasMoreElements()){ 136 final ZipEntry ze = (ZipEntry) e.nextElement(); 137 final String fileName = ze.getName(); 138 final boolean accept = pattern.matcher(fileName).matches(); 139 if(accept){ 140 retval.add(fileName); 141 } 142 } 143 try{ 144 zf.close(); 145 } catch(final IOException e1){ 146 throw new Error(e1); 147 } 148 return retval; 149 } 150 151 private Collection<String> getResourcesFromDirectory( 152 final File directory, 153 final Pattern pattern){ 154 final ArrayList<String> retval = new ArrayList<String>(); 155 final File[] fileList = directory.listFiles(); 156 for(final File file : fileList){ 157 if(file.isDirectory()){ 158 retval.addAll(getResourcesFromDirectory(file, pattern)); 159 } else{ 160 try{ 161 final String fileName = file.getCanonicalPath(); 162 final boolean accept = pattern.matcher(fileName).matches(); 163 if(accept){ 164 retval.add(fileName); 165 } 166 } catch(final IOException e){ 167 throw new Error(e); 168 } 169 } 170 } 171 return retval; 172 } 173 } 83 174 }
Note: See TracChangeset
for help on using the changeset viewer.