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.Set<java.lang.String> extractExtensions
           
protected static java.lang.String LINUX_DEFAULT_CACHE
           
protected static int maxPaths
           
static java.io.FileFilter OSP_CACHE_FILTER
           
protected static java.io.File ospCache
           
protected 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.lang.String SEARCH_CACHE_SUBDIRECTORY
           
protected static java.util.ArrayList<java.lang.String> searchPaths
           
protected static java.lang.String WIN_XP_DEFAULT_CACHE
           
protected 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 java.io.File chooseOSPCache(java.awt.Component parent)
          Uses a JFileChooser to select a cache directory.
static boolean clearOSPCache(java.io.File cache, boolean clearSearchCache)
          Clears an OSP cache.
static boolean clearOSPCacheHost(java.io.File hostDir)
          Clears an OSP cache host directory.
static boolean copyAllFiles(java.io.File inFile, java.io.File outFile)
          Copies a source file to a target file.
static java.io.File copyHTMLToOSPCache(java.lang.String htmlPath)
          Copies an HTML file with associated images and stylesheet to the OSP cache.
static boolean deleteFile(java.io.File file)
          Deletes a file or folder.
static java.io.File download(java.lang.String urlPath, java.io.File target, boolean alwaysOverwrite)
          Downloads a file from the web to a target File.
static java.io.File downloadToOSPCache(java.lang.String urlPath, java.lang.String fileName, boolean alwaysOverwrite)
          Downloads a file from the web to the OSP Cache.
static java.io.File extractFileFromZIP(java.lang.String source, java.io.File target, boolean alwaysOverwrite)
          Extracts a file from a ZIP archive to a target file.
static java.applet.AudioClip getAudioClip(java.lang.String path)
          Gets an audio clip.
static java.awt.image.BufferedImage getBufferedImage(java.lang.String path)
          Gets a buffered image.
static java.awt.image.BufferedImage getBufferedImage(java.lang.String path, int bufferedImageType)
          Gets a buffered image.
static java.io.File getDefaultOSPCache()
          Gets the default directory for cached files.
static java.util.List<java.io.File> getFiles(java.io.File directory, java.io.FileFilter filter)
          Gets the list of files in a directory and its subdirectories that are accepted by a FileFilter.
static java.lang.String getHTMLCode(java.lang.String path)
          Returns the HTML code for a local or web HTML page.
static javax.swing.ImageIcon getIcon(java.lang.String path)
          Gets an image icon.
static java.awt.Image getImage(java.lang.String path)
          Gets an image.
static java.lang.String getNonURIPath(java.lang.String uriPath)
          Removes protocol and "%20" from URI paths.
static java.io.File getOSPCache()
          Gets the directory for cached files.
static java.io.File getOSPCacheFile(java.lang.String urlPath)
          Gets the cache file associated with a URL path.
static java.io.File getOSPCacheFile(java.lang.String urlPath, java.lang.String name)
          Gets the cache file associated with a URL path.
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.io.File getSearchCache()
          Gets the search cache directory.
static java.io.File getSearchCacheFile(java.lang.String urlPath)
          Gets the search cache (XML) file associated with a URL path.
static java.lang.String getString(java.lang.String path)
          Gets a string.
static java.lang.String getStyleSheetFromHTMLCode(java.lang.String code)
          Returns the first stylesheet link, if any, in an HTML page.
static java.lang.String getTitleFromHTMLCode(java.lang.String code)
          Returns the title, if any, of an HTML page.
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)
          Gets the contents of a zip file.
static boolean isCacheEnabled()
          Gets the cacheEnabled property.
static boolean isCanceled()
          Determines if the current operation is canceled.
static boolean isOSPCachePath(java.lang.String path)
          Determines if a path defines a file in the OSP cache.
static boolean isURLAvailable(java.lang.String urlPath)
          Determines if a url path is available (ie both valid and connected).
static java.io.InputStream openInputStream(java.lang.String path)
          Opens and returns an input stream.
static java.io.Reader openReader(java.lang.String path)
          Opens and returns a reader.
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 newCache)
          Sets the directory for cached files.
static java.util.Set<java.io.File> unzip(java.lang.String zipPath, java.io.File targetDir, boolean alwaysOverwrite)
          Unzips a ZIP file into the given directory.
 
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

protected static final java.lang.String WIN_XP_DEFAULT_CACHE
See Also:
Constant Field Values

WINDOWS_DEFAULT_CACHE

protected static final java.lang.String WINDOWS_DEFAULT_CACHE
See Also:
Constant Field Values

OSX_DEFAULT_CACHE

protected static final java.lang.String OSX_DEFAULT_CACHE
See Also:
Constant Field Values

LINUX_DEFAULT_CACHE

protected static final java.lang.String LINUX_DEFAULT_CACHE
See Also:
Constant Field Values

SEARCH_CACHE_SUBDIRECTORY

protected static final java.lang.String SEARCH_CACHE_SUBDIRECTORY
See Also:
Constant Field Values

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.Set<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)
Opens and returns an input stream. May return null.

Parameters:
path - the path
Returns:
the input stream

openReader

public static java.io.Reader openReader(java.lang.String path)
Opens and returns a reader. May return null.

Parameters:
path - the path
Returns:
the reader

getString

public static java.lang.String getString(java.lang.String path)
Gets a string. May return null.

Parameters:
path - the path
Returns:
the string

getIcon

public static javax.swing.ImageIcon getIcon(java.lang.String path)
Gets an image icon. May return null.

Parameters:
path - the path
Returns:
the icon

getImage

public static java.awt.Image getImage(java.lang.String path)
Gets an image. May return null.

Parameters:
path - the path
Returns:
the image

getBufferedImage

public static java.awt.image.BufferedImage getBufferedImage(java.lang.String path)
Gets a buffered image. May return null.

Parameters:
path - the path
Returns:
the image

getBufferedImage

public static java.awt.image.BufferedImage getBufferedImage(java.lang.String path,
                                                            int bufferedImageType)
Gets a buffered image. May return null.

Parameters:
path - the path
bufferedImageType - one of the types defined by the BufferedImage class
Returns:
the image

getAudioClip

public static java.applet.AudioClip getAudioClip(java.lang.String path)
Gets an audio clip. May return null.

Parameters:
path - the path
Returns:
the audio clip

setOSPCache

public static void setOSPCache(java.io.File newCache)
Sets the directory for cached files.

Parameters:
newCache - the desired cache directory

getOSPCache

public static java.io.File getOSPCache()
Gets the directory for cached files.

Returns:
the OSP cache

getDefaultOSPCache

public static java.io.File getDefaultOSPCache()
Gets the default directory for cached files.

Returns:
the default OSP cache

chooseOSPCache

public static java.io.File chooseOSPCache(java.awt.Component parent)
Uses a JFileChooser to select a cache directory.

Parameters:
parent - a component to own the file chooser
Returns:
the chosen file

isOSPCachePath

public static boolean isOSPCachePath(java.lang.String path)
Determines if a path defines a file in the OSP cache.

Parameters:
path - the path
Returns:
true if path is in the OSP cache

getOSPCacheFile

public static java.io.File getOSPCacheFile(java.lang.String urlPath)
Gets the cache file associated with a URL path.

Parameters:
urlPath - the path to the file
Returns:
the cache file

getOSPCacheFile

public static java.io.File getOSPCacheFile(java.lang.String urlPath,
                                           java.lang.String name)
Gets the cache file associated with a URL path.

Parameters:
urlPath - the path to the file
name - name of the file (may be null)
Returns:
the cache file

getSearchCache

public static java.io.File getSearchCache()
Gets the search cache directory.

Returns:
the search cache

getSearchCacheFile

public static java.io.File getSearchCacheFile(java.lang.String urlPath)
Gets the search cache (XML) file associated with a URL path.

Parameters:
urlPath - the path to the file
Returns:
the search cache file

downloadToOSPCache

public static java.io.File downloadToOSPCache(java.lang.String urlPath,
                                              java.lang.String fileName,
                                              boolean alwaysOverwrite)
Downloads a file from the web to the OSP Cache.

Parameters:
urlPath - the path to the file
fileName - the name to assign the downloaded file
alwaysOverwrite - true to overwrite an existing file, if any
Returns:
the downloaded file, or null if failed to download

getHTMLCode

public static java.lang.String getHTMLCode(java.lang.String path)
Returns the HTML code for a local or web HTML page.

Parameters:
path - the path to the HTML page
Returns:
the HTML code, or null if not found or not HTML

getTitleFromHTMLCode

public static java.lang.String getTitleFromHTMLCode(java.lang.String code)
Returns the title, if any, of an HTML page.

Parameters:
code - the HTML code
Returns:
the title, or null if none defined

getStyleSheetFromHTMLCode

public static java.lang.String getStyleSheetFromHTMLCode(java.lang.String code)
Returns the first stylesheet link, if any, in an HTML page.

Parameters:
code - the HTML code
Returns:
the first stylesheet link found, or null if none

copyHTMLToOSPCache

public static java.io.File copyHTMLToOSPCache(java.lang.String htmlPath)
Copies an HTML file with associated images and stylesheet to the OSP cache. Note this does NOT overwrite cache files--to replace, delete them before calling this method

Parameters:
htmlPath - the path to the source HTML file
Returns:
the copied File, or null if failed

copyAllFiles

public static boolean copyAllFiles(java.io.File inFile,
                                   java.io.File outFile)
Copies a source file to a target file. If source file is a directory, copies contents of directory, including subdirectories

Parameters:
inFile - the source
outFile - the target
Returns:
true if all files successfully copied

clearOSPCache

public static boolean clearOSPCache(java.io.File cache,
                                    boolean clearSearchCache)
Clears an OSP cache. Always deletes "osp-host" directories in cache. Deletes search cache if requested.

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

clearOSPCacheHost

public static boolean clearOSPCacheHost(java.io.File hostDir)
Clears an OSP cache host directory.

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

deleteFile

public static boolean deleteFile(java.io.File file)
Deletes a file or folder. In case of a folder, deletes all contents and the folder itself.

Parameters:
file - the file to delete
Returns:
true if deleted

getFiles

public static java.util.List<java.io.File> getFiles(java.io.File directory,
                                                    java.io.FileFilter filter)
Gets the list of files in a directory and its subdirectories that are accepted by a FileFilter.

Parameters:
directory - the directory to search
filter - the FileFilter
Returns:
the list of files

getZipContents

public static java.util.Set<java.lang.String> getZipContents(java.lang.String zipPath)
Gets the contents of a zip file.

Parameters:
zipPath - the path to the zip file
Returns:
a set of file names in alphabetical order

unzip

public static java.util.Set<java.io.File> unzip(java.lang.String zipPath,
                                                java.io.File targetDir,
                                                boolean alwaysOverwrite)
Unzips a ZIP file into the given directory. ZIP file may be on a server. Can be canceled using the static setCanceled(boolean) method. Note this does not warn of possible overwrites.

Parameters:
zipPath - the (url) path to the zip file
targetDir - target directory to save the extracted files
alwaysOverwrite - true to overwrite existing files, if any
Returns:
the Set of extracted files

download

public static java.io.File download(java.lang.String urlPath,
                                    java.io.File target,
                                    boolean alwaysOverwrite)
Downloads a file from the web to a target File.

Parameters:
urlPath - the path to the file
target - the target file
alwaysOverwrite - true to overwrite an existing file, if any
Returns:
the downloaded file, or null if failed

extractFileFromZIP

public static java.io.File extractFileFromZIP(java.lang.String source,
                                              java.io.File target,
                                              boolean alwaysOverwrite)
Extracts a file from a ZIP archive to a target file. ZIP archive may be on a server.

Parameters:
source - the path of the file to be extracted (eg "http:/www.server/folder/images.zip!/image1.png")
target - target file to save
alwaysOverwrite - true to overwrite existing files, if any
Returns:
the extracted file

isURLAvailable

public static boolean isURLAvailable(java.lang.String urlPath)
Determines if a url path is available (ie both valid and connected).

Parameters:
urlPath - the path in URI form
Returns:
true if available

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