JMRI: Portable File AccessJMRI uses files for icons, images, panel files, scripts, and lots of other things. References to those are routinely used by various code, and stored in XML files. Some of the files are distributed with JMRI, and some are created outside the JMRI distribution directory by the local user.
In addition, JMRI is a cross-platform application, and we want to be able to move JMRI XML files from one computer to another, even of a different type, and have them still work.
To do this, we've created a system of pseudo-URLs for accessing file locations. Filenames are used internally and stored in XML files with prefixes that identify where the file is to be found in a system independent manner.
If the name
- starts with "program:", treat the rest as a relative pathname below the program directory
- starts with "preference:", treat the rest as a relative path below the preferences directory
- starts with "home:", treat the rest as a relative path below the directory in the user.home property, Java's idea of the user's home directory
- starts with "resource:", treat the rest as a pathname relative to the program directory (deprecated; see "program:" above)
- starts with "file:", treat the rest as a relative path below the resource directory in the preferences directory (deprecated; see "preference:" above)
ImplementationThe jmri.util.FileUtil class provides translation routines to make this automatic:
- static public String getExternalFilename(String pName)
- Convert an internal name (with prefixes as above) into an absolute pathname for use on the local computer. There's no guarantee that the file or any enclosing directory is actually present, but the pathname will be syntactically valid on the present computer.
- static public String getPortableFilename(File file)
- static public String getPortableFilename(String filename)
- Convert a File or filename string into a string name with the appropriate prefix.