|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.gjt.sp.jedit.MiscUtilities
public class MiscUtilities
Path name manipulation, string manipulation, and more.
The most frequently used members of this class are:
Some path name methods:
String comparison:
A compareStrings(String,String,boolean)
method that unlike
This class also defines several inner classes for use with the sorting features of the Java collections API:
For example, you might call:
Arrays.sort(myListOfStrings,
new MiscUtilities.StringICaseCompare());
Nested Class Summary | |
---|---|
static interface |
MiscUtilities.Compare
Deprecated. |
static class |
MiscUtilities.MenuItemCompare
Compares menu item labels. |
static class |
MiscUtilities.StringCompare
Compares strings. |
static class |
MiscUtilities.StringICaseCompare
Compares strings ignoring case. |
Field Summary | |
---|---|
static java.text.DecimalFormat |
KB_FORMAT
|
static java.text.DecimalFormat |
MB_FORMAT
|
static java.lang.String |
UTF_8_Y
This encoding is not supported by Java, yet it is useful. |
Method Summary | |
---|---|
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)
Returns the canonical form of the specified path name. |
static java.lang.String |
charsToEntities(java.lang.String str)
Converts <, >, & in the string to their HTML entity equivalents. |
static java.lang.String |
charsToEscapes(java.lang.String str)
Escapes newlines, tabs, backslashes, and quotes in the specified string. |
static java.lang.String |
charsToEscapes(java.lang.String str,
java.lang.String toEscape)
Escapes the specified characters in the specified string. |
static java.lang.String |
classToFile(java.lang.String name)
Converts a class name to a file name. |
static void |
closeQuietly(java.io.InputStream in)
Deprecated. use IOUtilities.closeQuietly(java.io.InputStream) |
static void |
closeQuietly(java.io.OutputStream out)
Deprecated. use IOUtilities.closeQuietly(java.io.OutputStream) |
static int |
compareStrings(java.lang.String str1,
java.lang.String str2,
boolean ignoreCase)
Compares two strings. |
static int |
compareVersions(java.lang.String v1,
java.lang.String v2)
Deprecated. Call compareStrings() instead |
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 boolean |
copyStream(int bufferSize,
ProgressObserver progress,
java.io.InputStream in,
java.io.OutputStream out,
boolean canStop)
Deprecated. use IOUtilities.copyStream(int, org.gjt.sp.util.ProgressObserver, java.io.InputStream, java.io.OutputStream, boolean) |
static boolean |
copyStream(ProgressObserver progress,
java.io.InputStream in,
java.io.OutputStream out,
boolean canStop)
Deprecated. use IOUtilities.copyStream(org.gjt.sp.util.ProgressObserver, java.io.InputStream, java.io.OutputStream, boolean) |
static java.lang.String |
createWhiteSpace(int len,
int tabSize)
Deprecated. use StandardUtilities.createWhiteSpace(int, int) |
static java.lang.String |
createWhiteSpace(int len,
int tabSize,
int start)
Deprecated. use StandardUtilities.createWhiteSpace(int, int, int) |
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 |
fileToClass(java.lang.String name)
Converts a file name to a class name. |
static org.xml.sax.InputSource |
findEntity(java.lang.String systemId,
java.lang.String test,
java.lang.Class where)
Tries to find the given systemId in the context of the given class. |
static java.lang.String |
formatFileSize(long length)
Formats the given file size into a nice string (123 bytes, 10.6 KB, 1.2 MB). |
static java.lang.String[] |
getEncodings()
Returns a list of supported character encodings. |
static java.lang.String |
getFileExtension(java.lang.String path)
Returns the extension of the specified filename, or an empty string if there is none. |
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)
Returns the last component of the specified path name without the trailing extension (if there is one). |
static java.lang.String |
getFileParent(java.lang.String path)
Deprecated. Call getParentOfPath() instead |
static java.lang.String |
getFileProtocol(java.lang.String url)
Deprecated. Call getProtocolOfURL() 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 int |
getLeadingWhiteSpace(java.lang.String str)
Deprecated. use StandardUtilities.getLeadingWhiteSpace(String) |
static int |
getLeadingWhiteSpaceWidth(java.lang.String str,
int tabSize)
Deprecated. use StandardUtilities.getLeadingWhiteSpace(String) |
static java.lang.String |
getLongestPrefix(java.util.List 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 int |
getOffsetOfVirtualColumn(javax.swing.text.Segment seg,
int tabSize,
int column,
int[] totalVirtualWidth)
Deprecated. use StandardUtilities.getVirtualWidth(javax.swing.text.Segment, int) |
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 int |
getTrailingWhiteSpace(java.lang.String str)
Deprecated. use StandardUtilities.getTrailingWhiteSpace(String) |
static int |
getVirtualWidth(javax.swing.text.Segment seg,
int tabSize)
Deprecated. use StandardUtilities.getVirtualWidth(javax.swing.text.Segment, int) |
static java.lang.String |
globToRE(java.lang.String glob)
Converts a Unix-style glob to a regular expression. |
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.Reader reader)
Check if a Reader 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 boolean |
moveFile(java.io.File source,
java.io.File dest)
Moves the source file to the destination. |
static boolean |
objectsEqual(java.lang.Object o1,
java.lang.Object o2)
Returns if two strings are equal. |
static int |
parsePermissions(java.lang.String s)
Parse a Unix-style permission string (rwxrwxrwx). |
static boolean |
parseXML(java.io.InputStream in,
org.xml.sax.helpers.DefaultHandler handler)
Convenience method for parsing an XML file. |
static boolean |
pathsEqual(java.lang.String p1,
java.lang.String p2)
|
static void |
quicksort(java.util.List list,
java.util.Comparator compare)
Deprecated. Collections.sort() |
static void |
quicksort(java.lang.Object[] obj,
java.util.Comparator compare)
Deprecated. use Arrays.sort() |
static void |
quicksort(java.lang.Object[] obj,
MiscUtilities.Compare compare)
Deprecated. use Arrays.sort() |
static void |
quicksort(java.util.Vector vector,
java.util.Comparator compare)
Deprecated. Collections.sort() |
static void |
quicksort(java.util.Vector vector,
MiscUtilities.Compare compare)
Deprecated. Collections.sort() |
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,
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 boolean |
stringsEqual(java.lang.String s1,
java.lang.String s2)
Deprecated. Call objectsEqual() instead. |
static java.lang.String |
throwableToString(java.lang.Throwable t)
Returns a string containing the stack trace of the given throwable. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String UTF_8_Y
public static final java.text.DecimalFormat KB_FORMAT
public static final java.text.DecimalFormat MB_FORMAT
Method Detail |
---|
public static java.lang.String canonPath(java.lang.String path)
~
. For local path names
only.
path
- The path namepublic 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..
path
- parent
- public 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 pathpublic static java.lang.String getFileName(java.lang.String path)
path
- The path namepublic static java.lang.String getFileNameNoExtension(java.lang.String path)
path
- The path namepublic static java.lang.String getFileParent(java.lang.String path)
public static java.lang.String getParentOfPath(java.lang.String path)
path
- The path namepublic static java.lang.String getFileProtocol(java.lang.String url)
public static java.lang.String getProtocolOfURL(java.lang.String url)
url
- The URLpublic static boolean isURL(java.lang.String str)
str
- The string to check
public 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 boolean moveFile(java.io.File source, java.io.File dest)
false
. Otherwise, the contents of the
source are copied to the destination, the source is deleted,
and true
is returned.
source
- The source file to move.dest
- The destination where to move the file.
public static boolean copyStream(int bufferSize, ProgressObserver progress, java.io.InputStream in, java.io.OutputStream out, boolean canStop) throws java.io.IOException
IOUtilities.copyStream(int, org.gjt.sp.util.ProgressObserver, java.io.InputStream, java.io.OutputStream, boolean)
bufferSize
- the size of the bufferprogress
- the progress observer it could be nullin
- the input streamout
- the output streamcanStop
- if true, the copy can be stopped by interrupting the thread
true
if the copy was done, false
if it was interrupted
java.io.IOException
- IOException If an I/O error occurspublic static boolean copyStream(ProgressObserver progress, java.io.InputStream in, java.io.OutputStream out, boolean canStop) throws java.io.IOException
IOUtilities.copyStream(org.gjt.sp.util.ProgressObserver, java.io.InputStream, java.io.OutputStream, boolean)
progress
- the progress observer it could be nullin
- the input streamout
- the output streamcanStop
- if true, the copy can be stopped by interrupting the thread
true
if the copy was done, false
if it was interrupted
java.io.IOException
- IOException If an I/O error occurspublic static boolean isBinary(java.io.Reader reader) throws java.io.IOException
reader
- the reader
true
if the Reader was detected as binary
java.io.IOException
- IOException If an I/O error occurspublic static boolean isBackup(java.lang.String filename)
filename
- the filename to check
public 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 file
java.io.IOException
- io exception during readpublic static void closeQuietly(java.io.InputStream in)
IOUtilities.closeQuietly(java.io.InputStream)
InputStream
ignoring it if it is null and ignoring exceptions.
in
- the InputStream to close.public static void closeQuietly(java.io.OutputStream out)
IOUtilities.closeQuietly(java.io.OutputStream)
OutputStream
ignoring it if it is null and ignoring exceptions.
out
- the OutputStream to close.public 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 name
public static int getLeadingWhiteSpace(java.lang.String str)
StandardUtilities.getLeadingWhiteSpace(String)
str
- The stringpublic static int getTrailingWhiteSpace(java.lang.String str)
StandardUtilities.getTrailingWhiteSpace(String)
str
- The stringpublic static int getLeadingWhiteSpaceWidth(java.lang.String str, int tabSize)
StandardUtilities.getLeadingWhiteSpace(String)
str
- The stringtabSize
- The tab sizepublic static int getVirtualWidth(javax.swing.text.Segment seg, int tabSize)
StandardUtilities.getVirtualWidth(javax.swing.text.Segment, int)
seg
- The segmenttabSize
- The tab sizepublic static int getOffsetOfVirtualColumn(javax.swing.text.Segment seg, int tabSize, int column, int[] totalVirtualWidth)
StandardUtilities.getVirtualWidth(javax.swing.text.Segment, int)
seg
- The segmenttabSize
- The tab sizecolumn
- The virtual column numbertotalVirtualWidth
- If this array is non-null, the total
virtual width will be stored in its first location if this method
returns -1.
public static java.lang.String createWhiteSpace(int len, int tabSize)
StandardUtilities.createWhiteSpace(int, int)
To get a whitespace string tuned to the current buffer's settings, call this method as follows:
myWhitespace = MiscUtilities.createWhiteSpace(myLength, (buffer.getBooleanProperty("noTabs") ? 0 : buffer.getTabSize()));
len
- The lengthtabSize
- The tab size, or 0 if tabs are not to be usedpublic static java.lang.String createWhiteSpace(int len, int tabSize, int start)
StandardUtilities.createWhiteSpace(int, int, int)
To get a whitespace string tuned to the current buffer's settings, call this method as follows:
myWhitespace = MiscUtilities.createWhiteSpace(myLength, (buffer.getBooleanProperty("noTabs") ? 0 : buffer.getTabSize()));
len
- The lengthtabSize
- The tab size, or 0 if tabs are not to be usedstart
- The start offset, for tab alignmentpublic static java.lang.String globToRE(java.lang.String glob)
? becomes ., * becomes .*, {aa,bb} becomes (aa|bb).
glob
- The glob patternpublic static java.lang.String escapesToChars(java.lang.String str)
str
- The stringpublic static java.lang.String charsToEscapes(java.lang.String str)
str
- The stringpublic static java.lang.String charsToEscapes(java.lang.String str, java.lang.String toEscape)
str
- The stringtoEscape
- Any characters that require escapingpublic static int compareVersions(java.lang.String v1, java.lang.String v2)
compareStrings()
instead
public static int compareStrings(java.lang.String str1, java.lang.String str2, boolean ignoreCase)
Unlike
str1
- The first stringstr2
- The second stringignoreCase
- If true, case will be ignored
public static boolean stringsEqual(java.lang.String s1, java.lang.String s2)
objectsEqual()
instead.
public static boolean objectsEqual(java.lang.Object o1, java.lang.Object o2)
o1.equals(o2)
.
public static java.lang.String charsToEntities(java.lang.String str)
str
- The stringpublic static java.lang.String formatFileSize(long length)
length
- The sizepublic static java.lang.String getLongestPrefix(java.util.List 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 void quicksort(java.lang.Object[] obj, java.util.Comparator compare)
Arrays.sort()
Arrays.sort()
.
obj
- The arraycompare
- Compares the objectspublic static void quicksort(java.util.Vector vector, java.util.Comparator compare)
Collections.sort()
vector
- The vectorcompare
- Compares the objectspublic static void quicksort(java.util.List list, java.util.Comparator compare)
Collections.sort()
list
- The listcompare
- Compares the objectspublic static void quicksort(java.lang.Object[] obj, MiscUtilities.Compare compare)
Arrays.sort()
Arrays.sort()
.
obj
- The arraycompare
- Compares the objectspublic static void quicksort(java.util.Vector vector, MiscUtilities.Compare compare)
Collections.sort()
vector
- The vectorcompare
- Compares the objectspublic 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()
public static java.lang.String throwableToString(java.lang.Throwable t)
public static boolean parseXML(java.io.InputStream in, org.xml.sax.helpers.DefaultHandler handler) throws java.io.IOException
SAX Errors are caught and are not propagated to the caller; instead, an error message is printed to jEdit's activity log. So, if you need custom error handling, do not use this method.
The given stream is closed before the method returns, regardless whether there were errors or not.
java.io.IOException
public static org.xml.sax.InputSource findEntity(java.lang.String systemId, java.lang.String test, java.lang.Class where)
getResourceAsStream()
method using the test string
as the resource.
This is used a lot internally while parsing XML files used by jEdit, but anyone is free to use the method if it sounds usable.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |