Package org.opensourcephysics.tools
Class ResourceLoader
java.lang.Object
org.opensourcephysics.tools.ResourceLoader
This defines static methods for loading resources.
- Version:
- 1.0
- Author:
- Douglas Brown
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
static class
A little class just for VideoFileType that -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static boolean
static final int
protected static boolean
protected static String
static boolean
protected static final String
protected static int
static final int
static final int
static final FileFilter
protected static File
protected static final String
protected static final String
static File
static boolean
protected static final int
protected static boolean
protected static final String
protected static final String
protected static URLClassLoader
static final int
static final int
protected static Map<String,
URLClassLoader> -
Method Summary
Modifier and TypeMethodDescriptionstatic void
addAppletSearchPath
(String base) Adds a search path at the beginning of the applet's search path list.static void
addExtractExtension
(String extension) Adds an extension to the end of the extractExtensions list.static void
addSearchPath
(String base) Adds a path at the beginning of the searchPaths list.static boolean
checkExists
(String path) static File
chooseOSPCache
(Component parent) Uses a JFileChooser to select a cache directory.static boolean
clearOSPCache
(File cache, boolean clearSearchCache) Clears an OSP cache.static boolean
clearOSPCacheHost
(File hostDir) Clears an OSP cache host directory.static void
zip contents caching can save time in complex loading.static int
confirmOverwrite
(String filename) static int
confirmOverwrite
(String filename, boolean canCancel) Whether to overwrite an existing file.static boolean
copyAllFiles
(File inFile, Object fileOrZipStream) Copies a source file to a target file.static boolean
static boolean
Copies a source file to a target file.static File
copyHTMLToOSPCache
(String htmlPath) Copies an HTML file with associated images and stylesheet to the OSP cache.static File
copyURLtoFile
(String urlPath, String filePath) static void
static boolean
deleteFile
(File file) Deletes a file or folder.static File
Downloads a file from the web to a target File.static File
downloadResourceFromDialog
(String urlPath, File file) Download a resource via LibraryBrowser "Download" button or VideoIO "failed to read CODEX" events after user approval.static File
downloadToOSPCache
(String urlPath, String fileName, boolean alwaysOverwrite) Downloads a file from the web to the OSP Cache.static File
Extracts a file using the ResourceLoader utilitystatic File
extractFileFromZIP
(String source, File target, boolean alwaysOverwrite) static File
extractFileFromZIP
(String source, File target, boolean alwaysOverwrite, boolean forceFileCreation) Extracts a file from a ZIP archive to a target file.static String
static String
Fix HTML being loaded from remote sources into HTMLDocuments and displayed using JEditorPane.static String
static AudioClip
getAudioClip
(String path) Gets an audio clip.static BufferedImage
getBufferedImage
(String path) Gets a buffered image.static BufferedImage
getBufferedImage
(String path, int bufferedImageType) Gets a buffered image.static ResourceLoader.Bundle
Get a bundle form org.opensourcePhysics.reosurces, for examplestatic URL
getClassResource
(String path, Class<?> cl) Retrieve an asset using Assets or a class loader.static File
Gets the default directory for cached files.getFiles
(File directory, FileFilter filter) Gets the list of files in a directory and its subdirectories that are accepted by a FileFilter.static String
getHTMLCode
(String path) Returns the HTML code for a local or web HTML page.static void
getHTMLCodeAsync
(String path, Function<String, Void> whenDone) static ImageIcon
Deprecated.This method is replaced by getImageIcon(String path)static Image
Gets an image.static ImageIcon
getImageIcon
(String path) Gets an icon.static URL
getJarURLForFile
(String fileName) C:/temp/car.trz!/Car in a loop with friction.trk becomes jar:file:C:/temp/car.trz!/Car in a loop with friction.trkstatic String[]
getJarURLParts
(String source) Divide [jarfile]!/[path] into String[] {jarfile,path} String source can be from a url such as jar:file:/...static byte[]
getLimitedStreamBytes
(InputStream is, long n, OutputStream out, boolean andCloseInput) From javajs.Rdrstatic String
getNonURIPath
(String uriPath) Removes protocol and "%20" from URI paths.static File
Gets the directory for cached files.static File
getOSPCacheFile
(String urlPath) Gets the cache file associated with a URL path.static File
getOSPCacheFile
(String urlPath, String name) Gets the cache file associated with a URL path.static ResizableIcon
getResizableIcon
(String path) Gets a ResizaableIcon.static Resource
getResource
(String name) Gets a non-zip resource specified by name.static Resource
getResource
(String name, Class<?> type) Only called by csm.ch03 DataLoaderApp to load a known text file resource from its directory.static Resource
getResourceZipURLsOK
(String name) Gets a resource specified by name.static File
getSearchCacheFile
(String urlPath) Gets the search cache (XML) file (path only) associated with a URL path.static String
Gets a string.static String
Returns the first stylesheet link, if any, in an HTML page.static String
Only called by ApplicationApplet to get a manifest Gets a resource specified by base path, name and class.static URL
getTextURL
(String name, Class<?> type) Returns a URL for a help frame (via TextFrame) Gets the URL for a resource specified by name and Class.static String
getTitleFromHTMLCode
(String code) Returns the title, if any, of an HTML page.static String
getURIPath
(String path) Converts a path to URI form (spaces replaced by "%20", etc).static byte[]
getURLBytes
(String url) Used only in OSPDesktopstatic byte[]
getURLContents
(URL url) Just get the URL contents as a byte arraystatic byte[]
getURLContents
(URL url, boolean showErr) static void
getURLContentsAsync
(URL url, Function<byte[], Void> whenDone) Ret the URL contents as a stringstatic Image
getVideoImage
(String path) If we are in ImageVideo, we know we already have the image, we just need it in resource form for caching.getZipContents
(String zipPath, boolean useCached) Gets the contents of a zip file.static void
static byte[]
getZipEntryBytes
(String zipFile, String entryPath, File target) A general osp method that can be used to pull bytes or create a File from a zip file, efficiently caching a map of ZipEntry data in JavaScript that can directly seek and extract bytes from that data.static void
getZipEntryBytesAsync
(String source, File target, Function<byte[], Void> whenDone) static boolean
Gets the cacheEnabled property.static boolean
Determines if the current operation is canceled.static boolean
static boolean
isJarZipTrz
(String path, boolean asEntry) Check for .zip, .jar, or .trzstatic boolean
isSearchPath
(String path) static boolean
isURLAvailable
(String urlPath) Determines if a url path is available (ie both valid and connected).static boolean
Return true if there is any internet signal at all.static InputStream
openInputStream
(String path) Opens and returns an input stream.static Reader
openReader
(String path) Opens and returns a reader.static InputStream
openStream
(URL url) static InputStream
openZipEntryStream
(URL url, URL zipURL) From Resource.static String
static BufferedReader
readerForStream
(InputStream stream, String encoding) static void
removeAppletSearchPath
(String base) Removes a path from the applet search path list.static void
removeFromZipCache
(String zipPath) Remove zip contents from the cache.static void
removeSearchPath
(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
static void
setOSPCache
(File newCache) Sets the directory for cached files.static void
setOSPCache
(String cachePath) Unzips a ZIP file into the temp directory.static boolean
wasPDFOpen
(String filename)
-
Field Details
-
tempDirFile
-
WIN_XP_DEFAULT_CACHE
- See Also:
-
WINDOWS_DEFAULT_CACHE
- See Also:
-
OSX_DEFAULT_CACHE
- See Also:
-
LINUX_DEFAULT_CACHE
- See Also:
-
SEARCH_CACHE_SUBDIRECTORY
- See Also:
-
WEB_CONNECTION_RETRY
protected static final int WEB_CONNECTION_RETRY- See Also:
-
searchPaths
-
appletSearchPaths
-
maxPaths
protected static int maxPaths -
resources
-
cacheEnabled
protected static boolean cacheEnabled -
canceled
protected static boolean canceled -
zipLoaders
-
xsetZipLoader
-
extractExtensions
-
pathsNotFound
-
ospCache
-
warningShown
public static boolean warningShown -
OSP_CACHE_FILTER
-
webConnected
protected static boolean webConnected -
ignoreMissingWebConnection
public static boolean ignoreMissingWebConnection -
downloadURL
-
YES
public static final int YES- See Also:
-
NO
public static final int NO- See Also:
-
YES_TO_ALL
public static final int YES_TO_ALL- See Also:
-
NO_TO_ALL
public static final int NO_TO_ALL- See Also:
-
CANCEL
public static final int CANCEL- See Also:
-
-
Method Details
-
getResource
Gets a non-zip 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
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
Only called by csm.ch03 DataLoaderApp to load a known text file resource from its directory. Gets a resource specified by name and Class. If no resource is found using the name alone, the searchPaths are NOT searched.- Parameters:
name
- the file or URL nametype
- the Class providing default ClassLoader resource loading- Returns:
- the Resource, or null if none found
-
getTextURL
Returns a URL for a help frame (via TextFrame) Gets the URL for 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 nametype
- the Class providing default ClassLoader resource loading- Returns:
- the URL for this resource, or null if none found
-
getText
public static String getText(String basePath, String name, Class<Resource> type, boolean searchFiles) Only called by ApplicationApplet to get a manifest 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 pathname
- the file or URL nametype
- the Class providing ClassLoader resource loadingsearchFiles
- true to search files- Returns:
- the Resource text, or null if none found
-
addSearchPath
Adds a path at the beginning of the searchPaths list.- Parameters:
base
- the base path to add
-
removeSearchPath
Removes a path from the searchPaths list.- Parameters:
base
- the base path to remove
-
addAppletSearchPath
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
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
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
Opens and returns an input stream. May return null.- Parameters:
path
- the path- Returns:
- the input stream
-
openReader
Opens and returns a reader. May return null.- Parameters:
path
- the path- Returns:
- the reader
-
getString
Gets a string. May return null.- Parameters:
path
- the path- Returns:
- the string
-
getIcon
Deprecated.This method is replaced by getImageIcon(String path)Gets an icon. May return null.- Parameters:
path
- the path- Returns:
- the icon. May return null.
-
getImageIcon
Gets an icon. May return null.- Parameters:
path
- the path- Returns:
- the icon. May return null.
-
getResizableIcon
Gets a ResizaableIcon.- Parameters:
path
- the path- Returns:
- the icon. May return null.
-
getBufferedImage
Gets a buffered image. May return null.- Parameters:
path
- the path- Returns:
- the image
-
getBufferedImage
Gets a buffered image. May return null.- Parameters:
path
- the pathbufferedImageType
- one of the types defined by the BufferedImage class- Returns:
- the image
-
getAudioClip
Gets an audio clip. May return null.- Parameters:
path
- the path- Returns:
- the audio clip
-
setOSPCache
-
setOSPCache
Sets the directory for cached files.- Parameters:
newCache
- the desired cache directory
-
getOSPCache
Gets the directory for cached files.- Returns:
- the OSP cache
-
getDefaultOSPCache
Gets the default directory for cached files.- Returns:
- the default OSP cache or null, conceivably
-
chooseOSPCache
Uses a JFileChooser to select a cache directory.- Parameters:
parent
- a component to own the file chooser- Returns:
- the chosen file
-
getOSPCacheFile
Gets the cache file associated with a URL path.- Parameters:
urlPath
- the path to the file- Returns:
- the cache file
-
getOSPCacheFile
Gets the cache file associated with a URL path.- Parameters:
urlPath
- the path to the filename
- name of the file (may be null)- Returns:
- the cache file
-
isSearchPath
-
getSearchFileList
-
getSearchCacheFile
Gets the search cache (XML) file (path only) associated with a URL path.- Parameters:
urlPath
- the path to the file- Returns:
- the search cache file
-
downloadToOSPCache
Downloads a file from the web to the OSP Cache.- Parameters:
urlPath
- the path to the filefileName
- the name to assign the downloaded filealwaysOverwrite
- true to overwrite an existing file, if any
-
getHTMLCode
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
-
getHTMLCodeAsync
-
getTitleFromHTMLCode
Returns the title, if any, of an HTML page.- Parameters:
code
- the HTML code- Returns:
- the title, or null if none defined
-
getStyleSheetFromHTMLCode
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
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
Copies a source file to a target file. If source file is a directory, copies contents of directory, including subdirectories- Parameters:
inFile
- the sourceoutFile
- the target- Returns:
- true if all files successfully copied
-
clearOSPCache
Clears an OSP cache. Always deletes "osp-host" directories in cache. Deletes search cache if requested.- Parameters:
cache
- the cache to clearclearSearchCache
- true to clear the search cache- Returns:
- true if successfully cleared
-
clearOSPCacheHost
Clears an OSP cache host directory.- Parameters:
hostDir
- the cache host directory to clear- Returns:
- true if successfully cleared
-
deleteFile
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
Gets the list of files in a directory and its subdirectories that are accepted by a FileFilter.- Parameters:
directory
- the directory to searchfilter
- the FileFilter- Returns:
- the list of files
-
clearZipCache
public static void clearZipCache()zip contents caching can save time in complex loading. -
removeFromZipCache
Remove zip contents from the cache. -
checkExists
-
getZipContentsAsync
-
getZipContents
Gets the contents of a zip file.- Parameters:
zipPath
- the path to the zip fileuseCache
- true to used cached bytes if available- Returns:
- a set of file names in alphabetical order
-
unzip
Unzips a ZIP file into the temp 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- Returns:
- the Set of extracted files
-
wasPDFOpen
-
downloadResourceFromDialog
Download a resource via LibraryBrowser "Download" button or VideoIO "failed to read CODEX" events after user approval.- Parameters:
urlPath
-file
-- Returns:
- the copied file or null if there is any problem
-
download
Downloads a file from the web to a target File.- Parameters:
urlPath
- the path to the filetarget
- the target file or null to assign a cache file namealwaysOverwrite
- true to overwrite an existing file, if any- Returns:
- the downloaded file, or null if failed
-
extractFileFromZIP
-
extractFileFromZIP
public static File extractFileFromZIP(String source, File target, boolean alwaysOverwrite, boolean forceFileCreation) 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 savealwaysOverwrite
- true to overwrite existing files, if any- Returns:
- the extracted file
-
getZipEntryBytesAsync
-
getZipEntryBytes
public static byte[] getZipEntryBytes(String zipFile, String entryPath, File target) throws IOException A general osp method that can be used to pull bytes or create a File from a zip file, efficiently caching a map of ZipEntry data in JavaScript that can directly seek and extract bytes from that data. The ZipEntry is cached automatically.- Parameters:
zipFile
-entryPath
-target
- the File object to used, or null if only byte[] return is desired- Returns:
- In JavaScript, all bytes are always returned; in Java, all bytes are returned only when target is null -- otherwise, only one buffer's worth of bytes are returned. In either case, a null byte[] return means something went wrong, as of course also does IOException.
- Throws:
IOException
-
getJarURLParts
Divide [jarfile]!/[path] into String[] {jarfile,path} String source can be from a url such as jar:file:/... or file:/... or from File.absolutePath().- Returns:
- null if "!/" is not found, { jarfile, null } if path would be "", and { jarfile, path } otherwise.
-
isWebConnected
public static boolean isWebConnected()Return true if there is any internet signal at all. The exact URL is negotiable, but it must be CORS allowed.- Returns:
-
isURLAvailable
Determines if a url path is available (ie both valid and connected). Guarded by webTestOK == FALSE; to overrided, use ResourceLoader.clearWebTest();- Parameters:
urlPath
- the path in URI form- Returns:
- true if available
-
getNonURIPath
Removes protocol and "%20" from URI paths.- Parameters:
uriPath
- the path in URI form- Returns:
- the path
-
getURIPath
Converts a path to URI form (spaces replaced by "%20", etc).- Parameters:
path
- the path- Returns:
- the path in URI form
-
getJarURLForFile
C:/temp/car.trz!/Car in a loop with friction.trk becomes jar:file:C:/temp/car.trz!/Car in a loop with friction.trk- Parameters:
fileName
-- Returns:
- jar URL
-
isJarZipTrz
Check for .zip, .jar, or .trz- Parameters:
path
-asEntry
- true if we allow "!" jar entry- Returns:
-
fixHTTPS
Fix HTML being loaded from remote sources into HTMLDocuments and displayed using JEditorPane.- Parameters:
htmlStr
-url
-- Returns:
-
extractFiles
-
extract
Extracts a file using the ResourceLoader utility- Parameters:
filename
- String The path of the file to extracttarget
- File The target file for the extracted file- Returns:
- File The extracted file, null if failed
-
confirmOverwrite
-
getBundle
Get a bundle form org.opensourcePhysics.reosurces, for example- Parameters:
bundleName
- defaults to org.opensourcephysics.resources.tools.toolsresourceLocale
- defaults to Locale.getDefault()- Returns:
- the appropriate Bundle
-
fixLang
-
setLocale
-
confirmOverwrite
Whether to overwrite an existing file.- Parameters:
file
- File- Returns:
- boolean
-
isHTTP
-
openZipEntryStream
From Resource.- Parameters:
url
-- Returns:
- Throws:
IOException
-
openStream
- Throws:
IOException
-
getImage
Gets an image. May return null.- Parameters:
path
- the path- Returns:
- the image
-
getURLContents
Just get the URL contents as a byte array- Parameters:
url
-- Returns:
- byte[] or null
-
getURLContents
-
getURLBytes
Used only in OSPDesktop- Parameters:
url
-- Returns:
- Throws:
IOException
-
getURLContentsAsync
Ret the URL contents as a string- Parameters:
url
-
-
readAllAsString
- Throws:
IOException
-
getLimitedStreamBytes
public static byte[] getLimitedStreamBytes(InputStream is, long n, OutputStream out, boolean andCloseInput) throws IOException From javajs.Rdr- Throws:
IOException
-
copyURLtoFile
- Throws:
IOException
-
copyURLtoFileAsync
-
readerForStream
-
getVideoImage
If we are in ImageVideo, we know we already have the image, we just need it in resource form for caching.- Parameters:
path
-- Returns:
-
getClassResource
Retrieve an asset using Assets or a class loader. ResourceLoader.class.getClassLoader() might be used first. Used in Tracker only.- Parameters:
path
- full asset path org/... or relative to the classcl
- the class - only used for Java- Returns:
-
copyFile
-
copyFile
Copies a source file to a target file.- Parameters:
inFile
- the sourceoutFile
- the targetbufLen
- buffer length- Returns:
- true if successfully copied
-