Class ResourceLoader

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

public class ResourceLoader extends Object
This defines static methods for loading resources.
Version:
1.0
Author:
Douglas Brown
  • Field Details

  • Method Details

    • getResource

      public static Resource getResource(String name)
      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

      public static Resource getResourceZipURLsOK(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(String name, Class<?> type)
      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 name
      type - the Class providing default ClassLoader resource loading
      Returns:
      the Resource, or null if none found
    • getTextURL

      public 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. 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 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 path
      name - the file or URL name
      type - the Class providing ClassLoader resource loading
      searchFiles - true to search files
      Returns:
      the Resource text, or null if none found
    • addSearchPath

      public static void addSearchPath(String base)
      Adds a path at the beginning of the searchPaths list.
      Parameters:
      base - the base path to add
    • removeSearchPath

      public static void removeSearchPath(String base)
      Removes a path from the searchPaths list.
      Parameters:
      base - the base path to remove
    • addAppletSearchPath

      public static void addAppletSearchPath(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(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(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 InputStream openInputStream(String path)
      Opens and returns an input stream. May return null.
      Parameters:
      path - the path
      Returns:
      the input stream
    • openReader

      public static Reader openReader(String path)
      Opens and returns a reader. May return null.
      Parameters:
      path - the path
      Returns:
      the reader
    • getString

      public static String getString(String path)
      Gets a string. May return null.
      Parameters:
      path - the path
      Returns:
      the string
    • getIcon

      @Deprecated public static ImageIcon getIcon(String path)
      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

      public static ImageIcon getImageIcon(String path)
      Gets an icon. May return null.
      Parameters:
      path - the path
      Returns:
      the icon. May return null.
    • getResizableIcon

      public static ResizableIcon getResizableIcon(String path)
      Gets a ResizaableIcon.
      Parameters:
      path - the path
      Returns:
      the icon. May return null.
    • getBufferedImage

      public static BufferedImage getBufferedImage(String path)
      Gets a buffered image. May return null.
      Parameters:
      path - the path
      Returns:
      the image
    • getBufferedImage

      public static BufferedImage getBufferedImage(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 AudioClip getAudioClip(String path)
      Gets an audio clip. May return null.
      Parameters:
      path - the path
      Returns:
      the audio clip
    • setOSPCache

      public static void setOSPCache(String cachePath)
    • setOSPCache

      public static void setOSPCache(File newCache)
      Sets the directory for cached files.
      Parameters:
      newCache - the desired cache directory
    • getOSPCache

      public static File getOSPCache()
      Gets the directory for cached files.
      Returns:
      the OSP cache
    • getDefaultOSPCache

      public static File getDefaultOSPCache()
      Gets the default directory for cached files.
      Returns:
      the default OSP cache or null, conceivably
    • chooseOSPCache

      public static File chooseOSPCache(Component parent)
      Uses a JFileChooser to select a cache directory.
      Parameters:
      parent - a component to own the file chooser
      Returns:
      the chosen file
    • getOSPCacheFile

      public static File getOSPCacheFile(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 File getOSPCacheFile(String urlPath, 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
    • isSearchPath

      public static boolean isSearchPath(String path)
    • getSearchFileList

      public static List<File> getSearchFileList()
    • getSearchCacheFile

      public static File getSearchCacheFile(String urlPath)
      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

      public static File downloadToOSPCache(String urlPath, 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
    • getHTMLCode

      public static String getHTMLCode(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
    • getHTMLCodeAsync

      public static void getHTMLCodeAsync(String path, Function<String,Void> whenDone)
    • getTitleFromHTMLCode

      public static String getTitleFromHTMLCode(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 String getStyleSheetFromHTMLCode(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 File copyHTMLToOSPCache(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(File inFile, Object fileOrZipStream)
      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(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(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(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 List<File> getFiles(File directory, 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
    • clearZipCache

      public static void clearZipCache()
      zip contents caching can save time in complex loading.
    • removeFromZipCache

      public static void removeFromZipCache(String zipPath)
      Remove zip contents from the cache.
    • checkExists

      public static boolean checkExists(String path)
    • getZipContentsAsync

      public static void getZipContentsAsync(String zipPath, Function<Map<String,ZipEntry>,Void> whenDone)
    • getZipContents

      public static Map<String,ZipEntry> getZipContents(String zipPath, boolean useCached)
      Gets the contents of a zip file.
      Parameters:
      zipPath - the path to the zip file
      useCache - true to used cached bytes if available
      Returns:
      a set of file names in alphabetical order
    • unzip

      public static Set<File> unzip(String zipPath)
      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

      public static boolean wasPDFOpen(String filename)
    • downloadResourceFromDialog

      public static File downloadResourceFromDialog(String urlPath, File file)
      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

      public static File download(String urlPath, 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 or null to assign a cache file name
      alwaysOverwrite - true to overwrite an existing file, if any
      Returns:
      the downloaded file, or null if failed
    • extractFileFromZIP

      public static File extractFileFromZIP(String source, File target, boolean alwaysOverwrite)
    • 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 save
      alwaysOverwrite - true to overwrite existing files, if any
      Returns:
      the extracted file
    • getZipEntryBytesAsync

      public static void getZipEntryBytesAsync(String source, File target, Function<byte[],Void> whenDone)
    • 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

      public static String[] getJarURLParts(String source)
      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

      public static boolean isURLAvailable(String urlPath)
      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

      public static String getNonURIPath(String uriPath)
      Removes protocol and "%20" from URI paths.
      Parameters:
      uriPath - the path in URI form
      Returns:
      the path
    • getURIPath

      public static String getURIPath(String path)
      Converts a path to URI form (spaces replaced by "%20", etc).
      Parameters:
      path - the path
      Returns:
      the path in URI form
    • getJarURLForFile

      public 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.trk
      Parameters:
      fileName -
      Returns:
      jar URL
    • isJarZipTrz

      public static boolean isJarZipTrz(String path, boolean asEntry)
      Check for .zip, .jar, or .trz
      Parameters:
      path -
      asEntry - true if we allow "!" jar entry
      Returns:
    • fixHTTPS

      public static String fixHTTPS(String htmlStr, URL url)
      Fix HTML being loaded from remote sources into HTMLDocuments and displayed using JEditorPane.
      Parameters:
      htmlStr -
      url -
      Returns:
    • extractFiles

      public static String extractFiles(String modelPath, File sourceDir, List<Object> finalList, File destinationDirectory)
    • extract

      public static File extract(String filename, File target)
      Extracts a file using the ResourceLoader utility
      Parameters:
      filename - String The path of the file to extract
      target - File The target file for the extracted file
      Returns:
      File The extracted file, null if failed
    • confirmOverwrite

      public static int confirmOverwrite(String filename)
    • getBundle

      public static ResourceLoader.Bundle getBundle(String bundleName, Locale resourceLocale)
      Get a bundle form org.opensourcePhysics.reosurces, for example
      Parameters:
      bundleName - defaults to org.opensourcephysics.resources.tools.tools
      resourceLocale - defaults to Locale.getDefault()
      Returns:
      the appropriate Bundle
    • fixLang

      public static String fixLang(String ret)
    • setLocale

      public static void setLocale(Locale locale)
    • confirmOverwrite

      public static int confirmOverwrite(String filename, boolean canCancel)
      Whether to overwrite an existing file.
      Parameters:
      file - File
      Returns:
      boolean
    • isHTTP

      public static boolean isHTTP(String path)
    • openZipEntryStream

      public static InputStream openZipEntryStream(URL url, URL zipURL) throws IOException
      From Resource.
      Parameters:
      url -
      Returns:
      Throws:
      IOException
    • openStream

      public static InputStream openStream(URL url) throws IOException
      Throws:
      IOException
    • getImage

      public static Image getImage(String path)
      Gets an image. May return null.
      Parameters:
      path - the path
      Returns:
      the image
    • getURLContents

      public static byte[] getURLContents(URL url)
      Just get the URL contents as a byte array
      Parameters:
      url -
      Returns:
      byte[] or null
    • getURLContents

      public static byte[] getURLContents(URL url, boolean showErr)
    • getURLBytes

      public static byte[] getURLBytes(String url) throws IOException
      Used only in OSPDesktop
      Parameters:
      url -
      Returns:
      Throws:
      IOException
    • getURLContentsAsync

      public static void getURLContentsAsync(URL url, Function<byte[],Void> whenDone)
      Ret the URL contents as a string
      Parameters:
      url -
    • readAllAsString

      public static String readAllAsString(InputStream is) throws IOException
      Throws:
      IOException
    • getLimitedStreamBytes

      public static byte[] getLimitedStreamBytes(InputStream is, long n, OutputStream out, boolean andCloseInput) throws IOException
      From javajs.Rdr
      Throws:
      IOException
    • copyURLtoFile

      public static File copyURLtoFile(String urlPath, String filePath) throws IOException
      Throws:
      IOException
    • copyURLtoFileAsync

      public static void copyURLtoFileAsync(String webPath, String filePath, Function<File,Void> whenDone)
    • readerForStream

      public static BufferedReader readerForStream(InputStream stream, String encoding)
    • getVideoImage

      public static 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.
      Parameters:
      path -
      Returns:
    • getClassResource

      public static URL getClassResource(String path, Class<?> cl)
      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 class
      cl - the class - only used for Java
      Returns:
    • copyFile

      public static boolean copyFile(File inFile, File outFile)
    • copyFile

      public static boolean copyFile(File inFile, File outFile, int bufLen)
      Copies a source file to a target file.
      Parameters:
      inFile - the source
      outFile - the target
      bufLen - buffer length
      Returns:
      true if successfully copied