Java Resource Load getResourceListing(URL pathUrl, String prefix, String suffix)

Here you can find the source of getResourceListing(URL pathUrl, String prefix, String suffix)

Description

get Resource Listing

License

Open Source License

Declaration

private static Set<String> getResourceListing(URL pathUrl, String prefix, String suffix)
            throws URISyntaxException, IOException 

Method Source Code

//package com.java2s;
/**/*from w  w  w.jav a2 s  .  c om*/
 *
 * Copyright 2014-2015 Martin Goellnitz
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Main {
    private static Set<String> getResourceListing(URL pathUrl, String prefix, String suffix)
            throws URISyntaxException, IOException {
        Set<String> result = new HashSet<>();
        if ("file".equals(pathUrl.getProtocol())) {
            for (String name : new File(pathUrl.toURI()).list()) {
                if (name.endsWith(suffix)) {
                    result.add(prefix + "/" + name);
                } // if
            } // for
        } // if

        if ("jar".equals(pathUrl.getProtocol())) {
            String jarPath = pathUrl.getPath().substring(5, pathUrl.getPath().indexOf("!")); // only the JAR file
            JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
            for (Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements();) {
                String name = entries.nextElement().getName();
                if (name.startsWith(prefix) && name.endsWith(suffix)) {
                    result.add(name);
                } // if
            } // for
            jar.close();
        } // if

        return result;
    }

    /**
     * Read a set of files names from resources or files in the class path.
     *
     * @param prefix prefix of the names in the result set including path and name prefix
     * @param suffix suffix of the names in the result set including e.g. and extension or naming convention
     * @return set of file names to be used for getResource()
     * @throws URISyntaxException
     * @throws IOException
     */
    public static Set<String> getResourceListing(String prefix, String suffix)
            throws URISyntaxException, IOException {
        Set<String> result = new HashSet<>();
        Enumeration<URL> en = Thread.currentThread().getContextClassLoader().getResources(prefix);
        while (en.hasMoreElements()) {
            URL metaInf = en.nextElement();
            result.addAll(getResourceListing(metaInf, prefix, suffix));
        } // while
        return result;
    }
}

Related

  1. getResourceFile(String resource)
  2. getResourceFile(String resourceName)
  3. getResourceFromJar(File fromFile, String insideFile)
  4. getResourceInDirPackage(String packageName, String packagePath, final boolean recursive, List classes)
  5. getResourceList(File fdir, String extn)
  6. getResourceNameFromFileName(final String pFileName)
  7. getResourceNames(File dir)
  8. getResourceNames(String resName)
  9. getResourceNamesFromDir(File dir, String extension)