Package org.gjt.sp.jedit
Class MiscUtilities
- java.lang.Object
-
- org.gjt.sp.jedit.MiscUtilities
-
public class MiscUtilities extends java.lang.Object
Path, URL name manipulation, string manipulation, and more.The most frequently used members of this class are:
Some path name methods:
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.lang.String
abbreviate(java.lang.String path)
The reverse of expandVariables(), returns a shortened path if possible.static java.lang.String
abbreviateView(java.lang.String path)
Same as abbreviate() but checks a view option which can disable the feature for jEdit UI components.static java.io.Reader
autodetect(java.io.InputStream in, Buffer buffer)
Tries to detect if the stream is gzipped, and if it has an encoding specified with an XML PI.static java.lang.String
buildToVersion(java.lang.String build)
Converts an internal version number (build) into a `human-readable' form.static java.lang.String
canonPath(java.lang.String path)
static java.lang.String
classToFile(java.lang.String name)
Converts a class name to a file name.static java.lang.String
concatPath(java.lang.String parent, java.lang.String path)
LikeconstructPath(java.lang.String, java.lang.String)
, exceptpath
will be appended toparent
even if it is absolute.static java.lang.String
constructPath(java.lang.String parent, java.lang.String path)
Constructs an absolute path name from a directory and another path name.static java.lang.String
constructPath(java.lang.String parent, java.lang.String path1, java.lang.String path2)
Constructs an absolute path name from three path components.static java.lang.String
escapesToChars(java.lang.String str)
Converts "\n" and "\t" escapes in the specified string to newlines and tabs.static java.lang.String
expandVariables(java.lang.String arg)
Accepts a string from the user (or a settings file) which may contain a variable-prefix path of various syntaxes.static java.lang.String
fileToClass(java.lang.String name)
Converts a file name to a class name.static java.lang.String
getBackupDirectory()
Get backup.directory property, or null.static java.lang.String
getBaseName(java.lang.String path)
static java.lang.String
getCompleteBaseName(java.lang.String path)
static java.lang.String[]
getEncodings(boolean getSelected)
Returns a list of supported character encodings.static java.lang.String
getFileExtension(java.lang.String path)
Returns the extension of the specified filename, starting with the last dot.static java.lang.String
getFileName(java.lang.String path)
Returns the last component of the specified path.static java.lang.String
getFileNameNoExtension(java.lang.String path)
Deprecated.use getBaseName() instead.static int
getFirstSeparatorIndex(java.lang.String path)
Return the first index of either / or the OS-specific file separator.static int
getLastSeparatorIndex(java.lang.String path)
Return the last index of either / or the OS-specific file separator.static java.lang.String
getLongestPrefix(java.lang.Object[] str, boolean ignoreCase)
Returns the longest common prefix in the given set of strings.static java.lang.String
getLongestPrefix(java.lang.String[] str, boolean ignoreCase)
Returns the longest common prefix in the given set of strings.static java.lang.String
getLongestPrefix(java.util.List<java.lang.String> str, boolean ignoreCase)
Returns the longest common prefix in the given set of strings.static java.io.File
getNthBackupFile(java.lang.String name, int backup, int backups, java.lang.String backupPrefix, java.lang.String backupSuffix, java.lang.String backupDirectory)
Gets the file to store the Nth backup of the given file.static java.lang.String
getParentOfPath(java.lang.String path)
Returns the parent of the specified path.static java.lang.String
getProtocolOfURL(java.lang.String url)
Returns the protocol specified by a URL.static boolean
isAbsolutePath(java.lang.String path)
Returns if the specified path name is an absolute path or URL.static boolean
isBackup(java.lang.String filename)
Check if the filename is a backup file.static boolean
isBinary(java.io.InputStream in)
Check if an InputStream is binary.static boolean
isToolsJarAvailable()
If on JDK 1.2 or higher, make sure that tools.jar is available.static boolean
isURL(java.lang.String str)
Checks if the specified string is a URL.static void
openInDesktop(java.lang.String path)
Opens a file or URI using the desktop file associations.static int
parsePermissions(java.lang.String s)
Parse a Unix-style permission string (rwxrwxrwx).static boolean
pathsEqual(java.lang.String p1, java.lang.String p2)
static java.io.File
prepareAutosaveDirectory(java.lang.String path)
Prepares the directory to autosave the specified file.static java.io.File
prepareBackupDirectory(java.lang.String path)
Prepares the directory to backup the specified file.static java.io.File
prepareBackupFile(java.lang.String path, int backups, java.lang.String backupPrefix, java.lang.String backupSuffix, java.lang.String backupDirectory, int backupTimeDistance)
Prepares the filename for performing backup of the given file.static java.io.File
prepareBackupFile(java.lang.String path, java.io.File backupDir)
Prepares the filename for performing backup of the given file.static java.lang.String
resolveSymlinks(java.lang.String path)
Resolves any symbolic links in the path name specified usingFile.getCanonicalPath()
.static void
saveBackup(java.io.File file)
Saves a backup (optionally numbered) of a file.static void
saveBackup(java.io.File file, int backups, java.lang.String backupPrefix, java.lang.String backupSuffix, java.lang.String backupDirectory)
Saves a backup (optionally numbered) of a file.static void
saveBackup(java.io.File file, int backups, java.lang.String backupPrefix, java.lang.String backupSuffix, java.lang.String backupDirectory, int backupTimeDistance)
Saves a backup (optionally numbered) of a file.static void
saveBackup(java.io.File file, java.io.File backupFile)
Saves a backup of a local file.static void
storeProperties(java.util.Properties props, java.io.OutputStream out, java.lang.String comments)
Stores properties with sorted keys.static java.lang.String
throwableToString(java.lang.Throwable t)
Returns a string containing the stack trace of the given throwable.
-
-
-
Method Detail
-
canonPath
@Nonnull public static java.lang.String canonPath(@Nonnull java.lang.String path)
- Parameters:
path
- The path name- Returns:
- the canonical form of the specified path name. Currently
only expands a leading
~
. For local path names only. - Since:
- jEdit 4.0pre2
-
expandVariables
public static java.lang.String expandVariables(java.lang.String arg)
Accepts a string from the user (or a settings file) which may contain a variable-prefix path of various syntaxes. Performs the reverse of abbreviate() from any platform. The function supports the following prefix syntaxes: ~/ or ~\ expand to user.home $varname (all platforms) %varname% (all platforms) ${varname} (non-Windows platforms) And expand each of these by looking at the system environment variables for possible expansions.- Returns:
- a string which is either the unchanged input string, or one with expanded variables.
- Since:
- jEdit 4.3
- See Also:
abbreviate(java.lang.String)
-
abbreviate
public static java.lang.String abbreviate(java.lang.String path)
The reverse of expandVariables(), returns a shortened path if possible. Uses platform convention (%varname% on windows, $varname on other platforms)- Returns:
- an abbreviated path, replacing values with variables, if a prefix exists.
- Since:
- jEdit 4.3
- See Also:
expandVariables(java.lang.String)
-
abbreviateView
public static java.lang.String abbreviateView(java.lang.String path)
Same as abbreviate() but checks a view option which can disable the feature for jEdit UI components.- Since:
- jEdit 5.1
-
resolveSymlinks
public static java.lang.String resolveSymlinks(@Nonnull java.lang.String path)
Resolves any symbolic links in the path name specified usingFile.getCanonicalPath()
. For local path names only.- Since:
- jEdit 4.2pre1
-
isAbsolutePath
public static boolean isAbsolutePath(@Nonnull java.lang.String path)
Returns if the specified path name is an absolute path or URL.- Since:
- jEdit 4.1pre11
-
constructPath
public static java.lang.String constructPath(@Nullable java.lang.String parent, @Nonnull java.lang.String path)
Constructs an absolute path name from a directory and another path name. This method is VFS-aware.- Parameters:
parent
- The directorypath
- The path name
-
constructPath
public static java.lang.String constructPath(java.lang.String parent, java.lang.String path1, java.lang.String path2)
Constructs an absolute path name from three path components. This method is VFS-aware.- Parameters:
parent
- The parent directorypath1
- The first pathpath2
- The second path
-
concatPath
public static java.lang.String concatPath(@Nonnull java.lang.String parent, @Nonnull java.lang.String path)
LikeconstructPath(java.lang.String, java.lang.String)
, exceptpath
will be appended toparent
even if it is absolute. For local path names only..- Parameters:
parent
- the parent pathpath
- the path to append to the parent
-
getFirstSeparatorIndex
public static int getFirstSeparatorIndex(java.lang.String path)
Return the first index of either / or the OS-specific file separator.- Parameters:
path
- The path- Returns:
- the index of the first separator or -1 if not found
- Since:
- jEdit 4.3pre3
-
getLastSeparatorIndex
public static int getLastSeparatorIndex(@Nonnull java.lang.String path)
Return the last index of either / or the OS-specific file separator.- Parameters:
path
- The path- Since:
- jEdit 4.3pre3
-
getFileExtension
@Nonnull public static java.lang.String getFileExtension(java.lang.String path)
Returns the extension of the specified filename, starting with the last dot.- Parameters:
path
- The path- Returns:
- the last dot (.) and the text that follows, or an empty string if no dots are found. i.e. if your filename is IP-192.168.1.1-data.tar.gz it will return .gz
- See Also:
getCompleteBaseName(String)
-
getFileName
public static java.lang.String getFileName(java.lang.String path)
Returns the last component of the specified path. This method is VFS-aware.- Parameters:
path
- The path name
-
getCompleteBaseName
public static java.lang.String getCompleteBaseName(java.lang.String path)
- Parameters:
path
- the path name- Returns:
- the complete basename of a fileName (before the last period). i.e. if your filename is /net/log/IP-192.168.1.1-data.tar.gz it will return IP-192.168.1.1-data.tar
- Since:
- jEdit 5.0
- See Also:
getBaseName
,getFileExtension
-
getBaseName
public static java.lang.String getBaseName(java.lang.String path)
- Parameters:
path
- The path name- Returns:
- the base name of a fileName (before the first period). i.e. If your filename is /net/log/IP-192.168.1.1-data.tar.gz it will return IP-192
- Since:
- jEdit 5.0
- See Also:
getCompleteBaseName(String)
-
getFileNameNoExtension
@Deprecated public static java.lang.String getFileNameNoExtension(java.lang.String path)
Deprecated.use getBaseName() instead.- Returns:
- the same thing as getBaseName()
- Since:
- jEdit 4.0
-
getParentOfPath
public static java.lang.String getParentOfPath(java.lang.String path)
Returns the parent of the specified path. This method is VFS-aware.- Parameters:
path
- The path name- Since:
- jEdit 2.6pre5
-
getProtocolOfURL
public static java.lang.String getProtocolOfURL(java.lang.String url)
Returns the protocol specified by a URL.- Parameters:
url
- The URL- Since:
- jEdit 2.6pre5
-
isURL
public static boolean isURL(@Nonnull java.lang.String str)
Checks if the specified string is a URL.- Parameters:
str
- The string to check- Returns:
- True if the string is a URL, false otherwise
-
getNthBackupFile
public static java.io.File getNthBackupFile(java.lang.String name, int backup, int backups, java.lang.String backupPrefix, java.lang.String backupSuffix, java.lang.String backupDirectory)
Gets the file to store the Nth backup of the given file.- Parameters:
name
- The last part of the filename of the file being backed up.backup
- The number of the current backup.backups
- Total number of backup copies.- Since:
- 5.0pre1
-
openInDesktop
public static void openInDesktop(java.lang.String path)
Opens a file or URI using the desktop file associations.Uses native desktop commands for each platform, which ask the user to choose an association for files that do not already have one, using the desktop's dialog, in contrast to Desktop.open() which just throws an IOException for unknown types. If a URI is supplied, use desktop browser.
- Parameters:
path
- or URI of thing to open/browse- Since:
- jEdit 5.0
-
prepareAutosaveDirectory
public static java.io.File prepareAutosaveDirectory(java.lang.String path)
Prepares the directory to autosave the specified file. A jEdit property is used to determine the directory. If there is none specified by props, then the current directory is used, but only for local files. The directory is created if it does not exist.- Parameters:
path
- path to the buffer- Returns:
- Autosave directory.
null
is returned for non-local files if no backup directory is specified in properties. - Since:
- jEdit 5.5
-
getBackupDirectory
public static java.lang.String getBackupDirectory()
Get backup.directory property, or null.- Returns:
- backup.directory property, or null
- Since:
- jEdit 5.5pre1
-
prepareBackupDirectory
public static java.io.File prepareBackupDirectory(java.lang.String path)
Prepares the directory to backup the specified file. A jEdit property is used to determine the directory. If there is no dedicated backup directory specified by props, then the current directory is used, but only for local files. The directory is created if it does not exist.- Parameters:
path
- path to the buffer- Returns:
- Backup directory.
null
is returned for non-local files if no backup directory is specified in properties. - Since:
- 5.0pre1
-
prepareBackupFile
public static java.io.File prepareBackupFile(java.lang.String path, java.io.File backupDir)
Prepares the filename for performing backup of the given file. In case of multiple backups does necessary backup renumbering. Checks whether the last backup was not earlier thanbackup.minTime
(property) ms ago. Uses jedit properties to determine backup parameters, like prefix, suffix.- Parameters:
path
- The file to back up.backupDir
- The directory, usually obtained fromprepareBackupDirectory
.- Returns:
- File suitable for backup of
file
, ornull
if the last backup was less thanbackup.minTime
ms ago. - Since:
- 5.0pre1
-
prepareBackupFile
public static java.io.File prepareBackupFile(java.lang.String path, int backups, java.lang.String backupPrefix, java.lang.String backupSuffix, java.lang.String backupDirectory, int backupTimeDistance)
Prepares the filename for performing backup of the given file. In case of multiple backups does necessary backup renumbering. Checks whether the last backup was not earlier thanbackupTimeDistance
ms ago.- Parameters:
path
- The file to back up.backups
- The number of backups. Must be >= 1. If > 1, backup files will be numbered.backupDirectory
- The directory determined externally or obtained fromprepareBackupDirectory
.- Returns:
- File suitable for backup of
file
, ornull
if the last backup was less thanbackupTimeDistance
ms ago. - Since:
- 5.0pre1
-
saveBackup
public static void saveBackup(java.io.File file)
Saves a backup (optionally numbered) of a file. Reads jedit properties to determine backup parameters, like prefix, suffix, directory.This version calls
prepareBackupDirectory
.- Parameters:
file
- A local file- Since:
- jEdit 5.0pre1
-
saveBackup
public static void saveBackup(java.io.File file, int backups, java.lang.String backupPrefix, java.lang.String backupSuffix, java.lang.String backupDirectory)
Saves a backup (optionally numbered) of a file.- Parameters:
file
- A local filebackups
- The number of backups. Must be >= 1. If > 1, backup files will be numbered.backupPrefix
- The backup file name prefixbackupSuffix
- The backup file name suffixbackupDirectory
- The directory where to save backups; if null, they will be saved in the same directory as the file itself.- Since:
- jEdit 4.0pre1
-
saveBackup
public static void saveBackup(java.io.File file, int backups, java.lang.String backupPrefix, java.lang.String backupSuffix, java.lang.String backupDirectory, int backupTimeDistance)
Saves a backup (optionally numbered) of a file. Requires specifying the backup directory and generates the backup filename.- Parameters:
file
- A local filebackups
- The number of backups. Must be >= 1. If > 1, backup files will be numbered.backupPrefix
- The backup file name prefixbackupSuffix
- The backup file name suffixbackupDirectory
- The directory where to save backups; if null, they will be saved in the same directory as the file itself.backupTimeDistance
- The minimum time in minutes when a backup version 1 shall be moved into version 2; if 0, backups are always moved.- Since:
- jEdit 4.2pre5
-
saveBackup
public static void saveBackup(java.io.File file, java.io.File backupFile)
Saves a backup of a local file. Requires specifying source and destination files.- Parameters:
file
- A local filebackupFile
- A local backup file.- Since:
- jEdit 5.0pre1
-
isBinary
public static boolean isBinary(java.io.InputStream in) throws java.io.IOException
Check if an InputStream is binary. First this tries encoding auto detection. If an encoding is detected, the stream should be a text stream. Otherwise, this will check the first characters 100 (jEdit property vfs.binaryCheck.length) in the system default encoding. If more than 1 (jEdit property vfs.binaryCheck.count) NUL( ) was found, the stream is declared binary. This is not 100% because sometimes the autodetection could fail. This method will not close the stream. You have to do it yourself- Parameters:
in
- the stream- Returns:
true
if the stream was detected as binary- Throws:
java.io.IOException
- IOException If an I/O error occurs- Since:
- jEdit 4.3pre10
-
isBackup
public static boolean isBackup(java.lang.String filename)
Check if the filename is a backup file.- Parameters:
filename
- the filename to check- Returns:
- true if this is a backup file.
- Since:
- jEdit 4.3pre5
-
autodetect
public static java.io.Reader autodetect(java.io.InputStream in, Buffer buffer) throws java.io.IOException
Tries to detect if the stream is gzipped, and if it has an encoding specified with an XML PI.- Parameters:
in
- the input stream reader that must be autodetectedbuffer
- a buffer. It can be null if you only want to autodetect the encoding of a file- Returns:
- a Reader using the detected encoding
- Throws:
java.io.IOException
- io exception during read- Since:
- jEdit 4.3pre5
-
fileToClass
public static java.lang.String fileToClass(java.lang.String name)
Converts a file name to a class name. All slash characters are replaced with periods and the trailing '.class' is removed.- Parameters:
name
- The file name
-
classToFile
public static java.lang.String classToFile(java.lang.String name)
Converts a class name to a file name. All periods are replaced with slashes and the '.class' extension is added.- Parameters:
name
- The class name
-
pathsEqual
public static boolean pathsEqual(java.lang.String p1, java.lang.String p2)
- Parameters:
p1
- A path namep2
- A path name- Returns:
- True if both paths are equal, ignoring trailing slashes, as well as case insensitivity on Windows.
- Since:
- jEdit 4.3pre2
-
escapesToChars
public static java.lang.String escapesToChars(java.lang.String str)
Converts "\n" and "\t" escapes in the specified string to newlines and tabs.- Parameters:
str
- The string- Since:
- jEdit 2.3pre1
-
getLongestPrefix
public static java.lang.String getLongestPrefix(java.util.List<java.lang.String> str, boolean ignoreCase)
Returns the longest common prefix in the given set of strings.- Parameters:
str
- The stringsignoreCase
- If true, case insensitive- Since:
- jEdit 4.2pre2
-
getLongestPrefix
public static java.lang.String getLongestPrefix(java.lang.String[] str, boolean ignoreCase)
Returns the longest common prefix in the given set of strings.- Parameters:
str
- The stringsignoreCase
- If true, case insensitive- Since:
- jEdit 4.2pre2
-
getLongestPrefix
public static java.lang.String getLongestPrefix(java.lang.Object[] str, boolean ignoreCase)
Returns the longest common prefix in the given set of strings.- Parameters:
str
- The strings (callstoString()
on each object)ignoreCase
- If true, case insensitive- Since:
- jEdit 4.2pre6
-
buildToVersion
public static java.lang.String buildToVersion(java.lang.String build)
Converts an internal version number (build) into a `human-readable' form.- Parameters:
build
- The build
-
isToolsJarAvailable
public static boolean isToolsJarAvailable()
If on JDK 1.2 or higher, make sure that tools.jar is available. This method should be called by plugins requiring the classes in this library.tools.jar is searched for in the following places:
- the classpath that was used when jEdit was started,
- jEdit's jars folder in the user's home,
- jEdit's system jars folder,
- java.home/lib/. In this case, tools.jar is added to jEdit's list of known jars using jEdit.addPluginJAR(), so that it gets loaded through JARClassLoader.
On older JDK's this method does not perform any checks, and returns
true
(even though there is no tools.jar).- Returns:
false
if and only if on JDK 1.2 and tools.jar could not be found. In this case it prints some warnings on Log, too, about the places where it was searched for.- Since:
- jEdit 3.2.2
-
parsePermissions
public static int parsePermissions(java.lang.String s)
Parse a Unix-style permission string (rwxrwxrwx).- Parameters:
s
- The string (must be 9 characters long).- Since:
- jEdit 4.1pre8
-
getEncodings
public static java.lang.String[] getEncodings(boolean getSelected)
Returns a list of supported character encodings.- Parameters:
getSelected
- Whether to return just the selected encodings or all.- Since:
- jEdit 4.3
-
throwableToString
public static java.lang.String throwableToString(java.lang.Throwable t)
Returns a string containing the stack trace of the given throwable.- Since:
- jEdit 4.2pre6
-
storeProperties
public static void storeProperties(java.util.Properties props, java.io.OutputStream out, java.lang.String comments) throws java.io.IOException
Stores properties with sorted keys.- Parameters:
props
- Given properties.out
- Output stream.comments
- Description of the property list.- Throws:
java.io.IOException
- Since:
- jEdit 5.3
-
-