Java Resource Get getResources(String pkgName)

Here you can find the source of getResources(String pkgName)

Description

get Resources

License

Apache License

Declaration

public static List<URL> getResources(String pkgName) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;

import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Main {
    private static final String PROTOCOL_FILE = "file";
    private static final String PROTOCOL_JAR = "jar";

    public static List<URL> getResources(String pkgName) {
        List<URL> urls = new ArrayList<URL>();
        try {//  www . j  a v a2  s  .com
            ClassLoader cld = Thread.currentThread().getContextClassLoader();
            if (cld == null) {
                throw new ClassNotFoundException("Can't get class loader.");
            }
            String path = pkgName.replace('.', '/');
            // Ask for all resources for the path
            Enumeration<URL> resources = cld.getResources(path);
            while (resources.hasMoreElements()) {
                URL url = resources.nextElement();
                urls.addAll(list(url));
            }
            return urls;
        } catch (Throwable x) {
            throw new RuntimeException(x);
        }
    }

    @SuppressWarnings("deprecation")
    private static List<URL> list(URL url) {
        try {
            List<URL> urls = new ArrayList<URL>();
            if (PROTOCOL_FILE.equals(url.getProtocol())) {
                File dir = new File(URLDecoder.decode(url.getPath(), "UTF-8"));
                String[] files = dir.list();
                for (String f : files) {
                    urls.add(new File(dir.getPath() + '/' + f).toURL());
                }
            } else if (PROTOCOL_JAR.equals(url.getProtocol())) {
                String path = URLDecoder.decode(url.getPath(), "UTF-8");
                int e = path.lastIndexOf(".jar!") + 4;
                String jarUrl = path.substring(0, e);
                String jar = jarUrl;
                if (jar.startsWith("file:/")) {
                    jar = jar.substring(6);
                }
                String name = path.substring(e + 2);

                JarFile jarFile = new JarFile(jar);
                Enumeration<JarEntry> jes = jarFile.entries();
                while (jes.hasMoreElements()) {
                    JarEntry je = jes.nextElement();
                    if (je.getName().startsWith(name)) {
                        URL url2 = new URL("jar:" + jarUrl + "!/" + je.getName());
                        urls.add(url2);
                    }

                }
            }
            return urls;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. getResourceInBundleOrFragments(Bundle bundle, String name)
  2. getResourceListing(ClassLoader cl)
  3. getResourceMessage(Locale locale, String resource, String key, Object... params)
  4. getResourceReader(Class clazz, String name, String charset)
  5. getResources(String archiveName)
  6. getResourcesFromDirectory(File resource, Pattern pattern)
  7. getResourceString(ResourceBundle rb, String key, Object param1)
  8. getResourceString(String key)
  9. getResourceString(String key, Object... args)