public class MiscUtilities
extends java.lang.Object
The most frequently used members of this class are:
Some path name methods:
Modifier and Type | Method and 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)
Like
constructPath(java.lang.String, java.lang.String) , except path will be
appended to parent 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.util.List<java.lang.String> str,
boolean ignoreCase)
Returns the longest common prefix in the given set of strings.
|
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.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 |
prepareBackupDirectory(java.lang.String path)
Prepares the directory to backup the specified 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.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.lang.String |
resolveSymlinks(java.lang.String path)
Resolves any symbolic links in the path name specified
using
File.getCanonicalPath() . |
static void |
saveBackup(java.io.File file)
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 |
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 |
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.
|
public static java.lang.String canonPath(java.lang.String path)
path
- The path name~
. For local path names
only.public static java.lang.String expandVariables(java.lang.String arg)
abbreviate(java.lang.String)
public static java.lang.String abbreviate(java.lang.String path)
expandVariables(java.lang.String)
public static java.lang.String abbreviateView(java.lang.String path)
public static java.lang.String resolveSymlinks(java.lang.String path)
File.getCanonicalPath()
. For local path
names only.public static boolean isAbsolutePath(java.lang.String path)
public static java.lang.String constructPath(java.lang.String parent, java.lang.String path)
parent
- The directorypath
- The path namepublic static java.lang.String constructPath(java.lang.String parent, java.lang.String path1, java.lang.String path2)
parent
- The parent directorypath1
- The first pathpath2
- The second pathpublic static java.lang.String concatPath(java.lang.String parent, java.lang.String path)
constructPath(java.lang.String, java.lang.String)
, except path
will be
appended to parent
even if it is absolute.
For local path names only..parent
- the parent pathpath
- the path to append to the parentpublic static int getFirstSeparatorIndex(java.lang.String path)
path
- The pathpublic static int getLastSeparatorIndex(java.lang.String path)
path
- The pathpublic static java.lang.String getFileExtension(java.lang.String path)
path
- The pathgetCompleteBaseName(String)
public static java.lang.String getFileName(java.lang.String path)
path
- The path namepublic static java.lang.String getCompleteBaseName(java.lang.String path)
path
- the path namegetBaseName
,
getFileExtension
public static java.lang.String getBaseName(java.lang.String path)
path
- The path namegetCompleteBaseName(String)
@Deprecated public static java.lang.String getFileNameNoExtension(java.lang.String path)
public static java.lang.String getParentOfPath(java.lang.String path)
path
- The path namepublic static java.lang.String getProtocolOfURL(java.lang.String url)
url
- The URLpublic static boolean isURL(java.lang.String str)
str
- The string to checkpublic 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)
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.public static void openInDesktop(java.lang.String path)
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.
path
- or URI of thing to open/browsepublic static java.lang.String getBackupDirectory()
public static java.io.File prepareBackupDirectory(java.lang.String path)
path
- path to the buffernull
is returned for
non-local files if no backup directory is specified in properties.public static java.io.File prepareBackupFile(java.lang.String path, java.io.File backupDir)
backup.minTime
(property) ms ago.
Uses jedit properties to determine backup parameters,
like prefix, suffix.path
- The file to back up.backupDir
- The directory, usually obtained from
prepareBackupDirectory
.file
,
or null
if the last backup was
less than backup.minTime
ms ago.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)
backupTimeDistance
ms ago.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 from prepareBackupDirectory
.file
,
or null
if the last backup was
less than backupTimeDistance
ms ago.public static void saveBackup(java.io.File file)
This version calls
prepareBackupDirectory
.
file
- A local filepublic static void saveBackup(java.io.File file, int backups, java.lang.String backupPrefix, java.lang.String backupSuffix, java.lang.String backupDirectory)
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.public static void saveBackup(java.io.File file, int backups, java.lang.String backupPrefix, java.lang.String backupSuffix, java.lang.String backupDirectory, int backupTimeDistance)
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.public static void saveBackup(java.io.File file, java.io.File backupFile)
file
- A local filebackupFile
- A local backup file.public static boolean isBinary(java.io.InputStream in) throws java.io.IOException
in
- the streamtrue
if the stream was detected as binaryjava.io.IOException
- IOException If an I/O error occurspublic static boolean isBackup(java.lang.String filename)
filename
- the filename to checkpublic static java.io.Reader autodetect(java.io.InputStream in, Buffer buffer) throws java.io.IOException
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 filejava.io.IOException
- io exception during readpublic static java.lang.String fileToClass(java.lang.String name)
name
- The file namepublic static java.lang.String classToFile(java.lang.String name)
name
- The class namepublic static boolean pathsEqual(java.lang.String p1, java.lang.String p2)
p1
- A path namep2
- A path namepublic static java.lang.String escapesToChars(java.lang.String str)
str
- The stringpublic static java.lang.String getLongestPrefix(java.util.List<java.lang.String> str, boolean ignoreCase)
str
- The stringsignoreCase
- If true, case insensitivepublic static java.lang.String getLongestPrefix(java.lang.String[] str, boolean ignoreCase)
str
- The stringsignoreCase
- If true, case insensitivepublic static java.lang.String getLongestPrefix(java.lang.Object[] str, boolean ignoreCase)
str
- The strings (calls toString()
on each object)ignoreCase
- If true, case insensitivepublic static java.lang.String buildToVersion(java.lang.String build)
build
- The buildpublic static boolean isToolsJarAvailable()
tools.jar is searched for in the following places:
On older JDK's this method does not perform any checks, and returns
true
(even though there is no tools.jar).
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.public static int parsePermissions(java.lang.String s)
s
- The string (must be 9 characters long).public static java.lang.String[] getEncodings(boolean getSelected)
getSelected
- Whether to return just the selected encodings or all.public static java.lang.String throwableToString(java.lang.Throwable t)
public static void storeProperties(java.util.Properties props, java.io.OutputStream out, java.lang.String comments) throws java.io.IOException
props
- Given properties.out
- Output stream.comments
- Description of the property list.java.io.IOException