net.sourceforge.jsxe.util
Class MiscUtilities

java.lang.Object
  extended bynet.sourceforge.jsxe.util.MiscUtilities

public class MiscUtilities
extends Object

Path name manipulation, string manipulation, and more.

String comparison:

A compareStrings(String,String,boolean) method that unlike String.compareTo(), correctly recognizes and handles embedded numbers.

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());

Since:
jsXe 0.4 beta
Version:
$Id: MiscUtilities.java 989 2006-07-06 21:26:48Z ian_lewis $
Author:
Slava Pestov, John Gellene (API documentation), Ian Lewis (IanLewis@member.fsf.org)

Nested Class Summary
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 String UTF_8_Y
          This encoding is not supported by Java, yet it is useful.
 
Method Summary
static String buildToVersion(String build)
          Converts an internal version number (build) into a `human-readable' form.
static String canonPath(String path)
          Returns the canonical form of the specified path name.
static String charactersToEntities(String s, Map hash)
          Converts characters to entities.
static String charsToEscapes(String str)
          Escapes newlines, tabs, backslashes, and quotes in the specified string.
static String charsToEscapes(String str, String toEscape)
          Escapes the specified characters in the specified string.
static String classToFile(String name)
          Converts a class name to a file name.
static int compareStrings(String str1, String str2, boolean ignoreCase)
          Compares two strings.
static String concatPath(String parent, String path)
          Like #constructPath, except path will be appended to parent even if it is absolute.
static String createWhiteSpace(int len, int tabSize)
          Creates a string of white space with the specified length.
static String entitiesToCharacters(String s, Map hash)
          Converts entities to characters
static boolean equals(Object a, Object b)
          Determines if object a equals object b.
static String escapesToChars(String str)
          Converts "\n" and "\t" escapes in the specified string to newlines and tabs.
static String fileToClass(String name)
          Converts a file name to a class name.
static String getFileExtension(String name)
          Returns the extension of the specified filename, or an empty string if there is none.
static String getFileName(String path)
          Returns the last component of the specified path.
static int getLeadingWhiteSpace(String str)
          Returns the number of leading white space characters in the specified string.
static int getLeadingWhiteSpaceWidth(String str, int tabSize)
          Returns the width of the leading white space in the specified string.
static String getLocalNameFromQualifiedName(String qualifiedName)
          Extracts the XML local name from a qualified name
static String getNSPrefixFromQualifiedName(String qualifiedName)
          Extracts an XML namespace prefix from a qualified name.
static int getOffsetOfVirtualColumn(Segment seg, int tabSize, int column, int[] totalVirtualWidth)
          Returns the array offset of a virtual column number (taking tabs into account) in the segment.
static String getParentOfPath(String path)
          Returns the parent of the specified path.
static String getProtocolOfURL(String url)
          Returns the protocol specified by a URL.
static String[] getSupportedEncodings()
          Returns a list of supported character encodings.
static int getTrailingWhiteSpace(String str)
          Returns the number of trailing whitespace characters in the specified string.
static int getVirtualWidth(Segment seg, int tabSize)
          Returns the virtual column number (taking tabs into account) of the specified offset in the segment.
static String globToRE(String glob)
          Converts a Unix-style glob to a regular expression.
static boolean isAbsolutePath(String path)
          Returns if the specified path name is an absolute path or URL.
static boolean isSupportedEncoding(String encoding)
          Returns if the given character encoding is supported.
static boolean isTrue(String str)
          Returns true if the value of the string is true
static boolean isURL(String str)
          Checks if the specified string is a URL.
static Properties mergeProperties(Properties props1, Properties props2)
          Merges two Properties sets together into a new Properties object giving precidence to the properties in the first argument.
static int parsePermissions(String s)
          Parse a Unix-style permission string (rwxrwxrwx).
static void quicksort(List list, Comparator compare)
          Sorts the specified list.
static void quicksort(Object[] obj, Comparator compare)
          Sorts the specified array.
static void quicksort(Vector vector, Comparator compare)
          Sorts the specified vector.
static String resolveSymlinks(String path)
           
static void saveBackup(File file, int backups, String backupPrefix, String backupSuffix, String backupDirectory)
          Saves a backup (optionally numbered) of a file.
static boolean stringsEqual(String s1, String s2)
          Returns if two strings are equal.
static String uriToFile(String uri)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UTF_8_Y

public static final String UTF_8_Y
This encoding is not supported by Java, yet it is useful. A UTF-8 file that begins with 0xEFBBBF.

See Also:
Constant Field Values
Method Detail

canonPath

public static String canonPath(String path)
Returns the canonical form of the specified path name. Currently only expands a leading ~. For local path names only.

Parameters:
path - The path name

isAbsolutePath

public static boolean isAbsolutePath(String path)
Returns if the specified path name is an absolute path or URL.


concatPath

public static String concatPath(String parent,
                                String path)
Like #constructPath, except path will be appended to parent even if it is absolute.

Parameters:
path -
parent -

getFileExtension

public static String getFileExtension(String name)
Returns the extension of the specified filename, or an empty string if there is none.

Parameters:
name - The file name

getFileName

public static String getFileName(String path)
Returns the last component of the specified path. This method is VFS-aware.

Parameters:
path - The path name

getParentOfPath

public static String getParentOfPath(String path)
Returns the parent of the specified path.

Parameters:
path - The path name
Since:
jsXe 0.4 pre3

getProtocolOfURL

public static String getProtocolOfURL(String url)
Returns the protocol specified by a URL.

Parameters:
url - The URL

isURL

public static boolean isURL(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

saveBackup

public static void saveBackup(File file,
                              int backups,
                              String backupPrefix,
                              String backupSuffix,
                              String backupDirectory)
Saves a backup (optionally numbered) of a file.

Parameters:
file - A local file
backups - The number of backups. Must be >= 1. If > 1, backup files will be numbered.
backupPrefix - The backup file name prefix
backupSuffix - The backup file name suffix
backupDirectory - The directory where to save backups; if null, they will be saved in the same directory as the file itself.

fileToClass

public static String fileToClass(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 String classToFile(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

resolveSymlinks

public static String resolveSymlinks(String path)

getLeadingWhiteSpace

public static int getLeadingWhiteSpace(String str)
Returns the number of leading white space characters in the specified string.

Parameters:
str - The string

getTrailingWhiteSpace

public static int getTrailingWhiteSpace(String str)
Returns the number of trailing whitespace characters in the specified string.

Parameters:
str - The string

getLeadingWhiteSpaceWidth

public static int getLeadingWhiteSpaceWidth(String str,
                                            int tabSize)
Returns the width of the leading white space in the specified string.

Parameters:
str - The string
tabSize - The tab size

getVirtualWidth

public static int getVirtualWidth(Segment seg,
                                  int tabSize)
Returns the virtual column number (taking tabs into account) of the specified offset in the segment.

Parameters:
seg - The segment
tabSize - The tab size

getOffsetOfVirtualColumn

public static int getOffsetOfVirtualColumn(Segment seg,
                                           int tabSize,
                                           int column,
                                           int[] totalVirtualWidth)
Returns the array offset of a virtual column number (taking tabs into account) in the segment.

Parameters:
seg - The segment
tabSize - The tab size
column - The virtual column number
totalVirtualWidth - If this array is non-null, the total virtual width will be stored in its first location if this method returns -1.
Returns:
-1 if the column is out of bounds

createWhiteSpace

public static String createWhiteSpace(int len,
                                      int tabSize)
Creates a string of white space with the specified length.

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()));

Parameters:
len - The length
tabSize - The tab size, or 0 if tabs are not to be used

globToRE

public static String globToRE(String glob)
Converts a Unix-style glob to a regular expression.

? becomes ., * becomes .*, {aa,bb} becomes (aa|bb).

Parameters:
glob - The glob pattern

escapesToChars

public static String escapesToChars(String str)
Converts "\n" and "\t" escapes in the specified string to newlines and tabs.

Parameters:
str - The string

charsToEscapes

public static String charsToEscapes(String str)
Escapes newlines, tabs, backslashes, and quotes in the specified string.

Parameters:
str - The string

charsToEscapes

public static String charsToEscapes(String str,
                                    String toEscape)
Escapes the specified characters in the specified string.

Parameters:
str - The string

compareStrings

public static int compareStrings(String str1,
                                 String str2,
                                 boolean ignoreCase)
Compares two strings.

Unlike String.compareTo(), this method correctly recognizes and handles embedded numbers. For example, it places "My file 2" before "My file 10".

Parameters:
str1 - The first string
str2 - The second string
ignoreCase - If true, case will be ignored
Returns:
negative If str1 < str2, 0 if both are the same, positive if str1 > str2

stringsEqual

public static boolean stringsEqual(String s1,
                                   String s2)
Returns if two strings are equal. This correctly handles null pointers, as opposed to calling s1.equals(s2).


quicksort

public static void quicksort(Object[] obj,
                             Comparator compare)
Sorts the specified array. Equivalent to calling Arrays.sort().

Parameters:
obj - The array
compare - Compares the objects

quicksort

public static void quicksort(Vector vector,
                             Comparator compare)
Sorts the specified vector.

Parameters:
vector - The vector
compare - Compares the objects

quicksort

public static void quicksort(List list,
                             Comparator compare)
Sorts the specified list.

Parameters:
list - The list
compare - Compares the objects

buildToVersion

public static String buildToVersion(String build)
Converts an internal version number (build) into a `human-readable' form.

Parameters:
build - The build

parsePermissions

public static int parsePermissions(String s)
Parse a Unix-style permission string (rwxrwxrwx).


getLocalNameFromQualifiedName

public static String getLocalNameFromQualifiedName(String qualifiedName)
Extracts the XML local name from a qualified name

Parameters:
qualifiedName - the qualified name
Returns:
the local name
Since:
jsXe 0.4 pre1

getNSPrefixFromQualifiedName

public static String getNSPrefixFromQualifiedName(String qualifiedName)
Extracts an XML namespace prefix from a qualified name. If there is no namespace prefix this method returns null.

Parameters:
qualifiedName - the qualified name
Returns:
the namespace prefix. null if no namespace
Since:
jsXe 0.4 pre1

charactersToEntities

public static String charactersToEntities(String s,
                                          Map hash)
Converts characters to entities.

Parameters:
s - The string in which to convert characters to entities
hash - a Map containing character to entity name mappings

entitiesToCharacters

public static String entitiesToCharacters(String s,
                                          Map hash)
Converts entities to characters

Parameters:
s - The string in which to convert characters to entities
hash - a Map containing entity name to character mappings

uriToFile

public static String uriToFile(String uri)

isSupportedEncoding

public static boolean isSupportedEncoding(String encoding)
Returns if the given character encoding is supported.

Since:
jsXe 0.4pre4

getSupportedEncodings

public static String[] getSupportedEncodings()
Returns a list of supported character encodings.

Since:
jsXe 0.4pre4

mergeProperties

public static Properties mergeProperties(Properties props1,
                                         Properties props2)
Merges two Properties sets together into a new Properties object giving precidence to the properties in the first argument. If either Properties object is null, the other Properties object is returned. If both are null then null is returned.

Parameters:
props1 - the first Properties object whose properties are given precidence.
props2 - the second Properties object whose properties are merged with that of the first.

isTrue

public static boolean isTrue(String str)
Returns true if the value of the string is true

Parameters:
str - the boolean string

equals

public static boolean equals(Object a,
                             Object b)
Determines if object a equals object b. Returns false if one is null and the other is not. This was written mainly because you cannot do a.equals(b) if a could be null.

Returns:
true if the objects are equal based on the equals method of a or b or if both are null.
Since:
jsXe 0.4 pre1