org.nuiton.util
Class Resource

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

public class Resource
extends 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:
bpoussin $Id: Resource.java 2194 2011-08-26 15:28:26Z echatellier $

Constructor Summary
protected Resource()
           
 
Method Summary
static void addClassLoader(ClassLoader classLoader, URL url)
          Permet d'ajouter dans un classloader une nouvelle URL dans lequel il faut rechercher les fichiers.
static void addDefaultClassLoader(URL url)
          Permet d'ajouter dans le classloader par defaut une nouvelle URL dans lequel il faut rechercher les fichiers.
static boolean containsDirectDirectory(URL url, String directory)
          Test if an url contains the given directory with no recurse seeking.
protected static List<URL> doFindMatchingFileSystemResources(URL rootDirResource, String subPattern)
          Find all resources in the file system that match the given location pattern via the Java style matcher.
protected static List<URL> doFindPathMatchingJarResources(URL rootDirResource, String subPattern)
          Find all resources in jar files that match the given location pattern via the Java Regex style Matcher.
protected static void doRetrieveMatchingFiles(String fullPattern, File dir, List<URL> result)
          Recursively retrieve files that match the given pattern, adding them to the given result list.
static URL[] getClassPathURLsFromJarManifest(URL jarURL)
           
static ImageIcon getIcon(String name)
          Retourne l'icone demandee.
protected static List<URL> getPatternRessources(String pattern, ClassLoader classLoader)
          Obtain some resources from a pattern using a specific class loader to seel resources.
static Class getPrimitiveClass(Class clazz)
          Deprecated. since 2.2, use now the method ObjectUtil.getPrimitiveClass(Class)
static List<URL> getResources(String pattern)
          Find pattern resouces in ClassLoader.getSystemClassLoader().
static List<URL> getResources(String pattern, ClassLoader classLoader)
          Find pattern resouces in classloader.
static URL getURL(String name)
          Recherche la ressource nom.
static URL getURLOrNull(String name)
          Recherche la ressource nom.
static List<URL> getURLs(String pattern)
          Retourner la liste des fichiers du classLoader.
static List<URL> getURLs(String pattern, URL... arrayURL)
          Retourner la liste des fichiers du classLoader.
static List<URL> getURLs(String pattern, URLClassLoader classLoader)
          Retourner la liste des fichiers du classLoader.
static List<URL> getURLsFromDirectory(File repository, String pattern)
          Retourne la liste des fichiers correspondant au pattern donne, aucun ordre ne doit être supposé sur les fichiers.
static List<URL> getURLsFromJar(File jarfile, String pattern)
           
static List<URL> getURLsFromZip(File zipFile, String pattern)
           
static boolean isJar(String name)
          Verifie si le fichier est un fichier jar.
static boolean isJarUrl(URL url)
          Test if an url detnoe a jar file.
protected static boolean isPattern(String str)
          Return true if str is a pattern (contains * or ?).
static boolean isPrimitive(Class clazz)
          Deprecated. since 2.2, use now the method ObjectUtil.isPrimitive(Class)
static boolean isZip(String name)
          Verifie si le fichier est un fichier zip
protected static List<URL> retrieveMatchingFiles(File rootDir, String pattern)
          Retrieve files that match the given path pattern, checking the given directory and its subdirectories.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Resource

protected Resource()
Method Detail

addDefaultClassLoader

public static void addDefaultClassLoader(URL url)
Permet d'ajouter dans le classloader par defaut une nouvelle URL dans lequel il faut rechercher les fichiers.

Parameters:
url - l'url a ajouter

addClassLoader

public static void addClassLoader(ClassLoader classLoader,
                                  URL url)
Permet d'ajouter dans un classloader une nouvelle URL dans lequel il faut rechercher les fichiers.

Parameters:
classLoader - le classloader a modifier
url - l'url a ajouter

getURL

public static URL getURL(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 URL getURLOrNull(String name)
Recherche la ressource nom.

Parameters:
name - le nom de la ressource
Returns:
l'url de la ressource ou null

getIcon

public static ImageIcon getIcon(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 List<URL> getURLs(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 List<URL> getURLs(String pattern,
                                URLClassLoader classLoader)
Retourner la liste des fichiers du classLoader. Ces fichiers doivent correspondre au pattern donne.

Parameters:
classLoader - 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

getURLs

public static List<URL> getURLs(String pattern,
                                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 URL[] getClassPathURLsFromJarManifest(URL jarURL)
                                             throws IOException
Throws:
IOException

getURLsFromZip

public static List<URL> getURLsFromZip(File zipFile,
                                       String pattern)

getURLsFromJar

public static List<URL> getURLsFromJar(File jarfile,
                                       String pattern)

getURLsFromDirectory

public static List<URL> getURLsFromDirectory(File repository,
                                             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(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(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

isPrimitive

@Deprecated
public static boolean isPrimitive(Class clazz)
Deprecated. since 2.2, use now the method ObjectUtil.isPrimitive(Class)

Verifie si la classe est de type primitif.

Parameters:
clazz - nom de la classe a tester
Returns:
vrai si le classe est de type primitif faux sinon

getPrimitiveClass

@Deprecated
public static Class getPrimitiveClass(Class clazz)
Deprecated. since 2.2, use now the method ObjectUtil.getPrimitiveClass(Class)

Retourne la classe du type primitf associé avec la classe de de l'objet passé en parametre. Par exemple si la classe passée en paramètre est Integer alors le resultat sera la classe de int.

Parameters:
clazz - la classe dont on souhaite le type primitf
Returns:
le type primitif associé a la classe en paramètre, ou null s'il n'y a pas de type primitif associé.

containsDirectDirectory

public static boolean containsDirectDirectory(URL url,
                                              String directory)
                                       throws 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:
IOException - if any io pb

isPattern

protected static boolean isPattern(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 List<URL> getResources(String pattern)
                              throws IOException
Find pattern resouces in ClassLoader.getSystemClassLoader(). Usage :
  List urls = Resources.getResources("META-INF/.\*\.MF");
  List urls = Resources.getResources("org/nuiton/util/.?esource\\.class");
 

Parameters:
pattern - java regex style pattern to find
Returns:
url list found
Throws:
IOException - if any IO problem while seeking resources
Since:
2.2

getResources

public static List<URL> getResources(String pattern,
                                     ClassLoader classLoader)
                              throws IOException
Find pattern resouces in classloader. Usage :
  List urls = Resources.getResources("META-INF/.\*\.MF");
  List urls = Resources.getResources("org/nuiton/util/.?esource\\.class");
 

Parameters:
pattern - java regex style pattern to find
classLoader - classLoader
Returns:
url list found
Throws:
IOException - if any IO problem while seeking resources
Since:
2.2

getPatternRessources

protected static List<URL> getPatternRessources(String pattern,
                                                ClassLoader classLoader)
                                         throws 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:
IOException - if any IO problem while scanning resources
Since:
2.2

isJarUrl

public static boolean isJarUrl(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 List<URL> doFindPathMatchingJarResources(URL rootDirResource,
                                                          String subPattern)
                                                   throws 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:
IOException - in case of I/O errors
Since:
2.2
See Also:
JarURLConnection

doFindMatchingFileSystemResources

protected static List<URL> doFindMatchingFileSystemResources(URL rootDirResource,
                                                             String subPattern)
                                                      throws 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:
IOException - in case of I/O errors
Since:
2.2
See Also:
retrieveMatchingFiles(File, String)

retrieveMatchingFiles

protected static List<URL> retrieveMatchingFiles(File rootDir,
                                                 String pattern)
                                          throws 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:
IOException - if directory contents could not be retrieved
Since:
2.2

doRetrieveMatchingFiles

protected static void doRetrieveMatchingFiles(String fullPattern,
                                              File dir,
                                              List<URL> result)
                                       throws 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:
IOException - if directory contents could not be retrieved
Since:
2.2


Copyright © 2004-2012 CodeLutin. All Rights Reserved.