org.opensourcephysics.tools
Class ResourceLoader

java.lang.Object
  extended by org.opensourcephysics.tools.ResourceLoader

public class ResourceLoader
extends java.lang.Object

This defines static methods for loading resources.

Version:
1.0
Author:
Douglas Brown

Field Summary
protected static java.util.ArrayList<java.lang.String> appletSearchPaths
           
protected static boolean cacheEnabled
           
protected static boolean canceled
           
protected static java.util.ArrayList<java.lang.String> extractExtensions
           
static java.lang.String LINUX_DEFAULT_CACHE
           
protected static int maxPaths
           
static java.io.FileFilter OSP_CACHE_FILTER
           
protected static java.io.File ospCache
           
static java.lang.String OSX_DEFAULT_CACHE
           
protected static java.util.ArrayList<java.lang.String> pathsNotFound
           
protected static java.util.Hashtable<java.lang.String,Resource> resources
           
protected static java.util.ArrayList<java.lang.String> searchPaths
           
static java.lang.String WIN_XP_DEFAULT_CACHE
           
static java.lang.String WINDOWS_DEFAULT_CACHE
           
protected static java.net.URLClassLoader xsetZipLoader
           
protected static java.util.Map<java.lang.String,java.net.URLClassLoader> zipLoaders
           
protected static boolean zipURLsOK
           
 
Method Summary
static void addAppletSearchPath(java.lang.String base)
          Adds a search path at the beginning of the applet's search path list.
static void addExtractExtension(java.lang.String extension)
          Adds an extension to the end of the extractExtensions list.
static void addSearchPath(java.lang.String base)
          Adds a path at the beginning of the searchPaths list.
static boolean clearOSPCache(java.io.File cache)
          Clears an OSP cache directory.
static java.io.File download(java.lang.String urlPath, java.lang.String fileName, java.io.File targetDir, boolean alwaysOverwrite)
           
static java.io.File downloadToOSPCache(java.lang.String urlPath, java.lang.String fileName, java.lang.String subdirectory, boolean alwaysOverwrite)
           
static java.applet.AudioClip getAudioClip(java.lang.String path)
           
static java.awt.image.BufferedImage getBufferedImage(java.lang.String path)
           
static javax.swing.ImageIcon getIcon(java.lang.String path)
           
static java.awt.Image getImage(java.lang.String path)
           
static java.lang.String getNonURIPath(java.lang.String uriPath)
          Removes protocol and "%20" from URI paths.
static java.io.File getOSPCache()
           
static Resource getResource(java.lang.String name)
          Gets a resource specified by name.
static Resource getResource(java.lang.String name, boolean searchFiles)
          Gets a resource specified by name.
static Resource getResource(java.lang.String name, java.lang.Class<?> type)
          Gets a resource specified by name and Class.
static Resource getResource(java.lang.String name, java.lang.Class<?> type, boolean searchFiles)
          Gets a resource specified by name and Class.
static Resource getResource(java.lang.String basePath, java.lang.String name)
          Gets a resource specified by base path and name.
static Resource getResource(java.lang.String basePath, java.lang.String name, boolean searchFiles)
          Gets a resource specified by base path and name.
static Resource getResource(java.lang.String basePath, java.lang.String name, java.lang.Class<Resource> type)
          Gets a resource specified by base path, name and class.
static Resource getResource(java.lang.String basePath, java.lang.String name, java.lang.Class<Resource> type, boolean searchFiles)
          Gets a resource specified by base path, name and class.
static Resource getResourceZipURLsOK(java.lang.String name)
          Gets a resource specified by name.
static java.lang.String getString(java.lang.String path)
           
static java.lang.String getURIPath(java.lang.String path)
          Converts a path to URI form (spaces replaced by "%20", etc).
static java.util.Set<java.lang.String> getZipContents(java.lang.String zipPath)
           
static boolean isCacheEnabled()
          Gets the cacheEnabled property.
static boolean isCanceled()
          Determines if the current operation is canceled.
static boolean isURLAvailable(java.lang.String urlPath)
           
static java.io.InputStream openInputStream(java.lang.String path)
           
static java.io.Reader openReader(java.lang.String path)
           
static void removeAppletSearchPath(java.lang.String base)
          Removes a path from the applet search path list.
static void removeSearchPath(java.lang.String base)
          Removes a path from the searchPaths list.
static void setCacheEnabled(boolean enabled)
          Sets the cacheEnabled property.
static void setCanceled(boolean cancel)
          Cancels the current operation when true.
static void setOSPCache(java.io.File cache)
           
static java.util.Set<java.io.File> unzip(java.lang.String zipPath, java.io.File targetDir, boolean alwaysOverwrite)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OSP_CACHE_FILTER

public static final java.io.FileFilter OSP_CACHE_FILTER

WIN_XP_DEFAULT_CACHE

public static final java.lang.String WIN_XP_DEFAULT_CACHE

WINDOWS_DEFAULT_CACHE

public static final java.lang.String WINDOWS_DEFAULT_CACHE

OSX_DEFAULT_CACHE

public static final java.lang.String OSX_DEFAULT_CACHE

LINUX_DEFAULT_CACHE

public static final java.lang.String LINUX_DEFAULT_CACHE

searchPaths

protected static java.util.ArrayList<java.lang.String> searchPaths

appletSearchPaths

protected static java.util.ArrayList<java.lang.String> appletSearchPaths

maxPaths

protected static int maxPaths

resources

protected static java.util.Hashtable<java.lang.String,Resource> resources

cacheEnabled

protected static boolean cacheEnabled

canceled

protected static boolean canceled

zipLoaders

protected static java.util.Map<java.lang.String,java.net.URLClassLoader> zipLoaders

xsetZipLoader

protected static java.net.URLClassLoader xsetZipLoader

extractExtensions

protected static java.util.ArrayList<java.lang.String> extractExtensions

pathsNotFound

protected static java.util.ArrayList<java.lang.String> pathsNotFound

ospCache

protected static java.io.File ospCache

zipURLsOK

protected static boolean zipURLsOK
Method Detail

getResource

public static Resource getResource(java.lang.String name)
Gets a resource specified by name. If no resource is found using the name alone, the searchPaths are searched.

Parameters:
name - the file or URL name
Returns:
the Resource, or null if none found

getResourceZipURLsOK

public static Resource getResourceZipURLsOK(java.lang.String name)
Gets a resource specified by name. If no resource is found using the name alone, the searchPaths are searched. This will find a zip file as a URL resource, unlike the getResource(String) method.

Parameters:
name - the file or URL name
Returns:
the Resource, or null if none found

getResource

public static Resource getResource(java.lang.String name,
                                   boolean searchFiles)
Gets a resource specified by name. If no resource is found using the name alone, the searchPaths are searched. Files are searched only if searchFile is true.

Parameters:
name - the file or URL name
searchFiles - true to search files
Returns:
the Resource, or null if none found

getResource

public static Resource getResource(java.lang.String name,
                                   java.lang.Class<?> type)
Gets a resource specified by name and Class. If no resource is found using the name alone, the searchPaths are searched.

Parameters:
name - the file or URL name
type - the Class providing default ClassLoader resource loading
Returns:
the Resource, or null if none found

getResource

public static Resource getResource(java.lang.String name,
                                   java.lang.Class<?> type,
                                   boolean searchFiles)
Gets a resource specified by name and Class. If no resource is found using the name alone, the searchPaths are searched. Files are searched only if searchFile is true.

Parameters:
name - the file or URL name
type - the Class providing default ClassLoader resource loading
searchFiles - true to search files
Returns:
the Resource, or null if none found

getResource

public static Resource getResource(java.lang.String basePath,
                                   java.lang.String name)
Gets a resource specified by base path and name. If base path is relative and no resource is found using the base alone, the searchPaths are searched.

Parameters:
basePath - the base path
name - the file or URL name
Returns:
the Resource, or null if none found

getResource

public static Resource getResource(java.lang.String basePath,
                                   java.lang.String name,
                                   boolean searchFiles)
Gets a resource specified by base path and name. If base path is relative and no resource is found using the base alone, the searchPaths are searched. Files are searched only if searchFile is true.

Parameters:
basePath - the base path
name - the file or URL name
searchFiles - true to search files
Returns:
the Resource, or null if none found

getResource

public static Resource getResource(java.lang.String basePath,
                                   java.lang.String name,
                                   java.lang.Class<Resource> type)
Gets a resource specified by base path, name and class. If base path is relative and no resource is found using the base alone, the searchPaths are searched.

Parameters:
basePath - the base path
name - the file or URL name
type - the Class providing ClassLoader resource loading
Returns:
the Resource, or null if none found

getResource

public static Resource getResource(java.lang.String basePath,
                                   java.lang.String name,
                                   java.lang.Class<Resource> type,
                                   boolean searchFiles)
Gets a resource specified by base path, name and class. If base path is relative and no resource is found using the base alone, the searchPaths are searched. Files are searched only if searchFile is true.

Parameters:
basePath - the base path
name - the file or URL name
type - the Class providing ClassLoader resource loading
searchFiles - true to search files
Returns:
the Resource, or null if none found

addSearchPath

public static void addSearchPath(java.lang.String base)
Adds a path at the beginning of the searchPaths list.

Parameters:
base - the base path to add

removeSearchPath

public static void removeSearchPath(java.lang.String base)
Removes a path from the searchPaths list.

Parameters:
base - the base path to remove

addAppletSearchPath

public static void addAppletSearchPath(java.lang.String base)
Adds a search path at the beginning of the applet's search path list. Added by Wolfgang Christian.

Parameters:
base - the base path to add

removeAppletSearchPath

public static void removeAppletSearchPath(java.lang.String base)
Removes a path from the applet search path list. Added by Wolfgang Christian.

Parameters:
base - the base path to remove

setCacheEnabled

public static void setCacheEnabled(boolean enabled)
Sets the cacheEnabled property.

Parameters:
enabled - true to enable the cache

isCacheEnabled

public static boolean isCacheEnabled()
Gets the cacheEnabled property.

Returns:
true if the cache is enabled

addExtractExtension

public static void addExtractExtension(java.lang.String extension)
Adds an extension to the end of the extractExtensions list. Files with this extension found inside jars are extracted before loading.

Parameters:
extension - the extension to add

setCanceled

public static void setCanceled(boolean cancel)
Cancels the current operation when true.

Parameters:
cancel - true to cancel

isCanceled

public static boolean isCanceled()
Determines if the current operation is canceled.

Returns:
true if canceled

openInputStream

public static java.io.InputStream openInputStream(java.lang.String path)

openReader

public static java.io.Reader openReader(java.lang.String path)

getString

public static java.lang.String getString(java.lang.String path)

getIcon

public static javax.swing.ImageIcon getIcon(java.lang.String path)

getImage

public static java.awt.Image getImage(java.lang.String path)

getBufferedImage

public static java.awt.image.BufferedImage getBufferedImage(java.lang.String path)

getAudioClip

public static java.applet.AudioClip getAudioClip(java.lang.String path)

setOSPCache

public static void setOSPCache(java.io.File cache)

getOSPCache

public static java.io.File getOSPCache()

downloadToOSPCache

public static java.io.File downloadToOSPCache(java.lang.String urlPath,
                                              java.lang.String fileName,
                                              java.lang.String subdirectory,
                                              boolean alwaysOverwrite)

clearOSPCache

public static boolean clearOSPCache(java.io.File cache)
Clears an OSP cache directory.

Parameters:
cache - the cache to clear
Returns:
true if successfully cleared

getZipContents

public static java.util.Set<java.lang.String> getZipContents(java.lang.String zipPath)

unzip

public static java.util.Set<java.io.File> unzip(java.lang.String zipPath,
                                                java.io.File targetDir,
                                                boolean alwaysOverwrite)

download

public static java.io.File download(java.lang.String urlPath,
                                    java.lang.String fileName,
                                    java.io.File targetDir,
                                    boolean alwaysOverwrite)

isURLAvailable

public static boolean isURLAvailable(java.lang.String urlPath)

getNonURIPath

public static java.lang.String getNonURIPath(java.lang.String uriPath)
Removes protocol and "%20" from URI paths.

Parameters:
uriPath - the path in URI form
Returns:
the path

getURIPath

public static java.lang.String getURIPath(java.lang.String path)
Converts a path to URI form (spaces replaced by "%20", etc).

Parameters:
path - the path
Returns:
the path in URI form