Class Resource

java.lang.Object
org.nuiton.util.Resource

public class Resource
extends java.lang.Object
Cette class permet de rechercher un fichier en indiquant son nom avec son chemin. Cette librairie ira ensuite chercher ce fichier sur le système de fichier, et s'il n'est pas trouvé dans le classpath. Le fichier peut donc être dans un fichier .jar ou .zip. Exemple :
 URL image = Resource.getURL("/images/bidulle.png");
 
Created: 5 août 2003
Author:
Benjamin Poussin - poussin@codelutin.com
  • Constructor Summary

    Constructors 
    Modifier Constructor Description
    protected Resource()  
  • Method Summary

    Modifier and Type Method Description
    static boolean containsDirectDirectory​(java.net.URL url, java.lang.String directory)
    Test if an url contains the given directory with no recurse seeking.
    protected static java.util.List<java.net.URL> doFindMatchingFileSystemResources​(java.net.URL rootDirResource, java.lang.String subPattern)
    Find all resources in the file system that match the given location pattern via the Java style matcher.
    protected static java.util.List<java.net.URL> doFindPathMatchingJarResources​(java.net.URL rootDirResource, java.lang.String subPattern)
    Find all resources in jar files that match the given location pattern via the Java Regex style Matcher.
    protected static void doRetrieveMatchingFiles​(java.lang.String fullPattern, java.io.File dir, java.util.List<java.net.URL> result)
    Recursively retrieve files that match the given pattern, adding them to the given result list.
    static java.net.URL[] getClassPathURLsFromJarManifest​(java.net.URL jarURL)  
    static javax.swing.ImageIcon getIcon​(java.lang.String name)
    Retourne l'icone demandee.
    protected static java.util.List<java.net.URL> getPatternRessources​(java.lang.String pattern, java.lang.ClassLoader classLoader)
    Obtain some resources from a pattern using a specific class loader to seel resources.
    static java.util.List<java.net.URL> getResources​(java.lang.String pattern)
    Find pattern resouces in ClassLoader.getSystemClassLoader().
    static java.util.List<java.net.URL> getResources​(java.lang.String pattern, java.lang.ClassLoader classLoader)
    Find pattern resouces in classloader.
    static java.net.URL getURL​(java.lang.String name)
    Recherche la ressource nom.
    static java.net.URL getURLOrNull​(java.lang.String name)
    Recherche la ressource nom.
    static java.util.List<java.net.URL> getURLs​(java.lang.String pattern)
    Retourner la liste des fichiers du classLoader.
    static java.util.List<java.net.URL> getURLs​(java.lang.String pattern, java.net.URL... arrayURL)
    Retourner la liste des fichiers du classLoader.
    static java.util.List<java.net.URL> getURLs​(java.lang.String pattern, java.net.URLClassLoader urlClassLoader)
    Retourner la liste des fichiers du classLoader.
    static java.util.List<java.net.URL> getURLsFromDirectory​(java.io.File repository, java.lang.String pattern)
    Retourne la liste des fichiers correspondant au pattern donne, aucun ordre ne doit être supposé sur les fichiers.
    static java.util.List<java.net.URL> getURLsFromJar​(java.io.File jarfile, java.lang.String pattern)  
    static java.util.List<java.net.URL> getURLsFromZip​(java.io.File zipFile, java.lang.String pattern)  
    static boolean isJar​(java.lang.String name)
    Verifie si le fichier est un fichier jar.
    static boolean isJarUrl​(java.net.URL url)
    Test if an url detnoe a jar file.
    protected static boolean isPattern​(java.lang.String str)
    Return true if str is a pattern (contains * or ?).
    static boolean isZip​(java.lang.String name)
    Verifie si le fichier est un fichier zip
    protected static java.util.List<java.net.URL> retrieveMatchingFiles​(java.io.File rootDir, java.lang.String pattern)
    Retrieve files that match the given path pattern, checking the given directory and its subdirectories.
    protected static java.util.List<java.net.URL> walkThroughClassLoaderAndGetURLs​(java.lang.String pattern)  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • getURL

      public static java.net.URL getURL​(java.lang.String name)
      Recherche la ressource nom.
      Parameters:
      name - nom de la ressource
      Returns:
      l'url de la ressource
      Throws:
      ResourceNotFoundException - si la resource n'a pas ete trouvee
    • getURLOrNull

      public static java.net.URL getURLOrNull​(java.lang.String name)
      Recherche la ressource nom.
      Parameters:
      name - le nom de la ressource
      Returns:
      l'url de la ressource ou null
    • getIcon

      public static javax.swing.ImageIcon getIcon​(java.lang.String name)
      Retourne l'icone demandee.
      Parameters:
      name - le nom de l'icone
      Returns:
      Retourne l'icon demande ou null s'il n'est pas trouvé
    • getURLs

      public static java.util.List<java.net.URL> getURLs​(java.lang.String pattern)
      Retourner la liste des fichiers du classLoader. Ces fichiers doivent correspondre au pattern donne. Utile par defaut ClassLoader.getSystemClassLoader().
      Parameters:
      pattern - le nom du fichier a extraire du fichier compressé ou du repertoire doit correspondre au pattern (repertoire + nom compris).
      Returns:
      la liste des urls correspondant au pattern
    • getURLs

      public static java.util.List<java.net.URL> getURLs​(java.lang.String pattern, java.net.URLClassLoader urlClassLoader)
      Retourner la liste des fichiers du classLoader. Ces fichiers doivent correspondre au pattern donne.
      Parameters:
      urlClassLoader - classloader to use (if null, use ClassLoader.getSystemClassLoader()
      pattern - le nom du fichier a extraire du fichier compressé ou du repertoire doit correspondre au pattern (repertoire + nom compris).
      Returns:
      la liste des urls correspondant au pattern
    • walkThroughClassLoaderAndGetURLs

      protected static java.util.List<java.net.URL> walkThroughClassLoaderAndGetURLs​(java.lang.String pattern)
    • getURLs

      public static java.util.List<java.net.URL> getURLs​(java.lang.String pattern, java.net.URL... arrayURL)
      Retourner la liste des fichiers du classLoader. Ces fichiers doivent correspondre au pattern donne.
      Parameters:
      arrayURL - les urls ou chercher
      pattern - le nom du fichier a extraire du fichier compressé ou dur epertoire doit correspondre au pattern (repertoire + nom compris).
      Returns:
      la liste des urls correspondant au pattern
    • getClassPathURLsFromJarManifest

      public static java.net.URL[] getClassPathURLsFromJarManifest​(java.net.URL jarURL) throws java.io.IOException
      Throws:
      java.io.IOException
    • getURLsFromZip

      public static java.util.List<java.net.URL> getURLsFromZip​(java.io.File zipFile, java.lang.String pattern)
    • getURLsFromJar

      public static java.util.List<java.net.URL> getURLsFromJar​(java.io.File jarfile, java.lang.String pattern)
    • getURLsFromDirectory

      public static java.util.List<java.net.URL> getURLsFromDirectory​(java.io.File repository, java.lang.String pattern)
      Retourne la liste des fichiers correspondant au pattern donne, aucun ordre ne doit être supposé sur les fichiers.
      Parameters:
      repository - repertoire dans lequel on recherche les fichiers
      pattern - le nom du fichier a extraire du fichier du repertoire doit correspondre au pattern (repertoire + nom compris). si le pattern est null, tous les fichiers trouvé sont retourné.
      Returns:
      la liste des urls correspondant au pattern
    • isJar

      public static boolean isJar​(java.lang.String name)
      Verifie si le fichier est un fichier jar.
      Parameters:
      name - nom du fichier a tester
      Returns:
      vrai si le fichier se termine par .jar faux sinon
    • isZip

      public static boolean isZip​(java.lang.String name)
      Verifie si le fichier est un fichier zip
      Parameters:
      name - nom du fichier a tester
      Returns:
      vrai si le fichier se termine par .zip faux sinon
    • containsDirectDirectory

      public static boolean containsDirectDirectory​(java.net.URL url, java.lang.String directory) throws java.io.IOException
      Test if an url contains the given directory with no recurse seeking.
      Parameters:
      url - the url to seek
      directory - the directory to find
      Returns:
      true if directory was found, false otherwise.
      Throws:
      java.io.IOException - if any io pb
    • isPattern

      protected static boolean isPattern​(java.lang.String str)
      Return true if str is a pattern (contains * or ?).
      Parameters:
      str - str to test
      Returns:
      true if str is a pattern, false otherwise
      Since:
      2.2
    • getResources

      public static java.util.List<java.net.URL> getResources​(java.lang.String pattern) throws java.io.IOException
      Find pattern resouces in ClassLoader.getSystemClassLoader(). Usage :
        List<URL> urls = Resources.getResources("META-INF/.*\\.MF");
        List<URL> urls = Resources.getResources("org/nuiton/util/.?esource\\.class");
       
      Parameters:
      pattern - java regex style pattern to find
      Returns:
      url list found
      Throws:
      java.io.IOException - if any IO problem while seeking resources
      Since:
      2.2
    • getResources

      public static java.util.List<java.net.URL> getResources​(java.lang.String pattern, java.lang.ClassLoader classLoader) throws java.io.IOException
      Find pattern resouces in classloader. Usage :
        List<URL> urls = Resources.getResources("META-INF/.*\\.MF");
        List<URL> urls = Resources.getResources("org/nuiton/util/.?esource\\.class");
       
      Parameters:
      pattern - java regex style pattern to find
      classLoader - classLoader
      Returns:
      url list found
      Throws:
      java.io.IOException - if any IO problem while seeking resources
      Since:
      2.2
    • getPatternRessources

      protected static java.util.List<java.net.URL> getPatternRessources​(java.lang.String pattern, java.lang.ClassLoader classLoader) throws java.io.IOException
      Obtain some resources from a pattern using a specific class loader to seel resources.
      Parameters:
      pattern - pattern of searched resources
      classLoader - class loader which responsible to seek resources
      Returns:
      list of resources found
      Throws:
      java.io.IOException - if any IO problem while scanning resources
      Since:
      2.2
    • isJarUrl

      public static boolean isJarUrl​(java.net.URL url)
      Test if an url detnoe a jar file. Code taken from spring source code : org.springframework.core.io.support.PathMatchingResourcePatternResolver
      Parameters:
      url - url to test
      Returns:
      true if url denote a jar file
      Since:
      2.2
    • doFindPathMatchingJarResources

      protected static java.util.List<java.net.URL> doFindPathMatchingJarResources​(java.net.URL rootDirResource, java.lang.String subPattern) throws java.io.IOException
      Find all resources in jar files that match the given location pattern via the Java Regex style Matcher. Code taken from spring source code : org.springframework.core.io.support.PathMatchingResourcePatternResolver
      Parameters:
      rootDirResource - the root directory as Resource
      subPattern - the sub pattern to match (below the root directory)
      Returns:
      the Set of matching Resource instances
      Throws:
      java.io.IOException - in case of I/O errors
      Since:
      2.2
      See Also:
      JarURLConnection
    • doFindMatchingFileSystemResources

      protected static java.util.List<java.net.URL> doFindMatchingFileSystemResources​(java.net.URL rootDirResource, java.lang.String subPattern) throws java.io.IOException
      Find all resources in the file system that match the given location pattern via the Java style matcher. Code taken from spring source code : org.springframework.core.io.support.PathMatchingResourcePatternResolver
      Parameters:
      rootDirResource - the root directory as Resource
      subPattern - the sub pattern to match (below the root directory)
      Returns:
      the Set of matching Resource instances
      Throws:
      java.io.IOException - in case of I/O errors
      Since:
      2.2
      See Also:
      retrieveMatchingFiles(File, String)
    • retrieveMatchingFiles

      protected static java.util.List<java.net.URL> retrieveMatchingFiles​(java.io.File rootDir, java.lang.String pattern) throws java.io.IOException
      Retrieve files that match the given path pattern, checking the given directory and its subdirectories. Code taken from spring source code : org.springframework.core.io.support.PathMatchingResourcePatternResolver
      Parameters:
      rootDir - the directory to start from
      pattern - the pattern to match against, relative to the root directory
      Returns:
      the Set of matching File instances
      Throws:
      java.io.IOException - if directory contents could not be retrieved
      Since:
      2.2
    • doRetrieveMatchingFiles

      protected static void doRetrieveMatchingFiles​(java.lang.String fullPattern, java.io.File dir, java.util.List<java.net.URL> result) throws java.io.IOException
      Recursively retrieve files that match the given pattern, adding them to the given result list. Code taken from spring source code : org.springframework.core.io.support.PathMatchingResourcePatternResolver
      Parameters:
      fullPattern - the pattern to match against, with preprended root directory path
      dir - the current directory
      result - the Set of matching File instances to add to
      Throws:
      java.io.IOException - if directory contents could not be retrieved
      Since:
      2.2