/** * This method is similar to {@link #getResourceAsURL(String)} except that it looks for a resource with a given * name in a specific package. * * @param ppackage package serving as a base folder for the resource to retrieve * @param name name of the resource in the package. This is a filename only, not a path. * @return a URL pointing to the resource, or <code>null</code> if the resource couldn't be located */ public static URL getPackageResourceAsURL(Package ppackage, String name) { return getPackageResourceAsURL(ppackage, name, getDefaultClassLoader(), null); }
/** * Shorthand for {@link #getResourceAsStream(String, ClassLoader, AbstractFile)} called with the * {@link #getDefaultClassLoader() default class loader} and a <code>null</code> root package file. * * @param path forward slash-separated path to the resource to look for, relative to the parent classpath * location (directory or JAR file) that contains it. * @return an InputStream that allows to read the resource, or <code>null</code> if the resource couldn't be located */ public static InputStream getResourceAsStream(String path) { return getResourceAsStream(path, getDefaultClassLoader(), null); }
/** * Shorthand for {@link #getResourceAsURL(String, ClassLoader, AbstractFile)} called with the * {@link #getDefaultClassLoader() default class loader} and a <code>null</code> root package file. * * @param path forward slash-separated path to the resource to look for, relative to the parent classpath * location (directory or JAR file) that contains it. * @return a URL pointing to the resource, or <code>null</code> if the resource couldn't be located */ public static URL getResourceAsURL(String path) { return getResourceAsURL(path, getDefaultClassLoader(), null); }
/** * This method is similar to {@link #getResourceAsStream(String)} except that it looks for a resource with a given * name in a specific package. * * @param ppackage package serving as a base folder for the resource to retrieve * @param name name of the resource in the package. This is a filename only, not a path. * @return an InputStream that allows to read the resource, or <code>null</code> if the resource couldn't be located */ public static InputStream getPackageResourceAsStream(Package ppackage, String name) { return getPackageResourceAsStream(ppackage, name, getDefaultClassLoader(), null); }
/** * This method is similar to {@link #getResourceAsFile(String)} except that it looks for a resource with a given * name in a specific package. * * @param ppackage package serving as a base folder for the resource to retrieve * @param name name of the resource in the package. This is a filename only, not a path. * @return an AbstractFile that represents the resource, or <code>null</code> if the resource couldn't be located */ public static AbstractFile getPackageResourceAsFile(Package ppackage, String name) { return getPackageResourceAsFile(ppackage, name, getDefaultClassLoader(), null); }
/** * Tests {@link ResourceLoader#getDefaultClassLoader()}. */ @Test public void testDefaultClassLoader() { assert ResourceLoader.getDefaultClassLoader() != null; }
/** * Shorthand for {@link #getResourceAsFile(String, ClassLoader, AbstractFile)} called with the * {@link #getDefaultClassLoader() default class loader} and a <code>null</code> root package file. * * @param path forward slash-separated path to the resource to look for, relative to the parent classpath * location (directory or JAR file) that contains it. * @return an AbstractFile that represents the resource, or <code>null</code> if the resource couldn't be located */ public static AbstractFile getResourceAsFile(String path) { return getResourceAsFile(removeLeadingSlash(path), getDefaultClassLoader(), null); }
/** * Returns an {@link AbstractFile} to the root package of the given <code>Class</code>. For example, if the * specified <code>Class</code> is <code>java.lang.Object</code>'s, the returned file will be the Java runtime * JAR file, which on most platforms is <code>$JAVA_HOME/lib/jre/rt.jar</code>.<br> * The returned file can be used to list or manipulate all resource files contained in a particular classpath's * location, including the .class files. * * @param aClass the class for which to locate the root package. * @return an AbstractFile to the root package of the given <code>Class</code> */ public static AbstractFile getRootPackageAsFile(Class<?> aClass) { ClassLoader classLoader = aClass.getClassLoader(); if(classLoader==null) classLoader = getDefaultClassLoader(); String aClassRelPath = getRelativeClassPath(aClass); URL aClassURL = getResourceAsURL(aClassRelPath, classLoader, null); if(aClassURL==null) return null; // no resource under that path if("jar".equals(aClassURL.getProtocol())) return FileFactory.getFile(getJarFilePath(aClassURL)); String aClassPath = getLocalFilePath(aClassURL); return FileFactory.getFile(aClassPath.substring(0, aClassPath.length()-aClassRelPath.length())); }
classLoader = getDefaultClassLoader();