Changeset 6571


Ignore:
Timestamp:
09/23/15 07:48:37 (9 years ago)
Author:
davor.ostojic@oeaw.ac.at
Message:
 
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  
    55import java.net.URISyntaxException;
    66import java.net.URL;
    7 import java.net.URLClassLoader;
     7import java.util.ArrayList;
     8import java.util.Collection;
     9import java.util.Enumeration;
     10import java.util.regex.Pattern;
     11import java.util.zip.ZipEntry;
     12import java.util.zip.ZipException;
     13import java.util.zip.ZipFile;
    814
    915import org.apache.commons.io.FileUtils;
     
    5965        MetadataImporter.languageCodeUtils = new LanguageCodeUtils(config);
    6066       
    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        }
    6274       
    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);
    7276     
    7377    }
     
    8084        }
    8185    }
     86   
     87   
     88    public class ResourceList{
    8289
     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    }
    83174}
Note: See TracChangeset for help on using the changeset viewer.