Java Resource Path Get getChildResources(String path)

Here you can find the source of getChildResources(String path)

Description

get Child Resources

License

Open Source License

Declaration

public static List<URL> getChildResources(String path) throws IOException, ClassNotFoundException 

Method Source Code


//package com.java2s;
/*/*from  w  ww.  j  ava2s .co  m*/
 * This file is part of Hootenanny.
 *
 * Hootenanny is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * --------------------------------------------------------------------
 *
 * The following copyright notices are generated automatically. If you
 * have a new notice to add, please use the format:
 * " * @copyright Copyright ..."
 * This will properly maintain the copyright information. DigitalGlobe
 * copyrights will be updated automatically.
 *
 * @copyright Copyright (C) 2013, 2014 DigitalGlobe (http://www.digitalglobe.com/)
 */

import java.io.File;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;

import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;

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

public class Main {
    public static List<URL> getChildResources(String path) throws IOException, ClassNotFoundException {
        List<URL> result = new LinkedList<URL>();

        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        Enumeration<URL> p = classLoader.getResources(path);
        while (p.hasMoreElements()) {
            URL resource = p.nextElement();
            // System.out.println("resource: " + resource.toString());
            if (resource.getProtocol().equalsIgnoreCase("FILE")) {
                result.addAll(loadDirectory(resource.getFile()));
            } else if (resource.getProtocol().equalsIgnoreCase("JAR")) {
                result.addAll(loadJar(path, resource));
            } else if (resource.getProtocol().equalsIgnoreCase("VFS")) {
                // result.addAll( loadVfs( resource ));
                System.out.println("VFS");
            } else {
                throw new ClassNotFoundException(
                        "Unknown protocol on class resource: " + resource.toExternalForm());
            }
        }

        return result;
    }

    public static List<URL> loadDirectory(String filePath) throws IOException {
        List<URL> result = new LinkedList<URL>();
        File directory = new File(filePath);
        if (!directory.isDirectory()) {
            throw new IOException("Invalid directory " + directory.getAbsolutePath());
        }

        File[] files = directory.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                loadDirectory(file.getAbsolutePath());
            } else {
                result.add(new URL("file", null, file.getAbsolutePath()));
            }
        }
        return result;
    }

    public static List<URL> loadJar(String path, URL resource) throws IOException {
        JarURLConnection conn = (JarURLConnection) resource.openConnection();
        JarFile jarFile = conn.getJarFile();
        Enumeration<JarEntry> entries = jarFile.entries();
        List<URL> result = new LinkedList<URL>();

        String p = path;
        if (p.endsWith("/") == false) {
            p = p + "/";
        }

        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            if ((!entry.getName().equals(p))
                    && (entry.getName().startsWith(p) || entry.getName().startsWith("WEB-INF/classes/" + p))) {
                URL url = new URL("jar:" + new URL("file", null, jarFile.getName() + "!/" + entry.getName()));
                result.add(url);
            }
        }

        return result;
    }
}

Related

  1. existsResource(String pathName)
  2. getAbsolutePathFromResource(Class reference, String resource)
  3. getAbsoluteResource(String path)
  4. getAlternateResourceFile(final String resourcePath)
  5. getBasePath(Class clazz, String resource)
  6. getClassResource(Class clazz, String resPath)
  7. getClassResources(Class clazz, String resPath)
  8. getExistingResourceAsFile(final String resourcePath)
  9. getFileFromBundle(String bundleName, String resourcePath)