com.ca.commons.cbutil
Class CBUtility

java.lang.Object
  |
  +--com.ca.commons.cbutil.CBUtility

public class CBUtility
extends java.lang.Object

This is a grab bag of useful classes and static functions that are not important enough to merit being top level entities. Most of them are concerned with string handling, file handling, and i18n issues.


Nested Class Summary
static class CBUtility.BasicWindowMonitor
          A utility ftn used to make a closing window shut down the current application.
 
Method Summary
static java.lang.String byte2Hex(byte b)
           
static java.lang.String bytes2Hex(byte[] bytes)
           
static java.lang.String bytes2HexSplit(byte[] in, int wordlength)
          From Van Bui - prints out a hex string formatted with spaces between each hex word of length wordlength.
static java.lang.String bytes2HexSplit(byte[] in, int wordlength, int linelength)
          From Van Bui - prints out a hex string formatted with spaces between each hex word of length wordlength, and new lines every linelength.
static void center(java.awt.Component centerMe, java.awt.Component positioner)
          This positions a component to the center of another component.
static java.lang.String char2Hex(char c)
           
static void closeLog()
          Closes the log file.
static void confirm(java.lang.String Msg)
          Utility function.
static java.lang.String convertPathToUnix(java.lang.String dosPath)
          Converts a 'dos' style file path to a unix style file path by exchanging '\' characters for for '/' characters.
static boolean error(java.awt.Component owner, java.lang.String Msg)
          utility ftn; prints error message to user, and echos to the log ftn.
static boolean error(java.awt.Component owner, java.lang.String Msg, java.lang.Exception e)
          wrapper for the JFrame version of error.
static boolean error(java.awt.Frame owner, java.lang.String Msg)
          utility ftn; prints error message to user, and echos to the log ftn.
static boolean error(java.awt.Frame owner, java.lang.String Msg, java.lang.Exception e)
          utility ftn; prints error message and the error to the user, and echos to the log ftn.
static boolean error(java.lang.String Msg)
          utility ftn; prints error message to user, and echos to the log ftn.
static boolean error(java.lang.String Msg, java.lang.Exception e)
          wrapper for the JFrame version of error.
static java.awt.Frame getDefaultDisplay()
           
static int getLogDebugLevel()
          Returns the global debug level.
static java.awt.Frame getParentFrame(java.awt.Component c)
          Iterates through a components parents until it finds the root frame.
static byte hex2Byte(char hex1, char hex2)
           
static byte hexChar2Byte(char hex)
          Convert a single character to a byte...
static void initDefaultDisplay(java.awt.Frame owner)
           
static boolean isNonAsciiUTF8(byte[] sequence)
          Determines whether a given byte sequence is a valid utf-8 encoding, encoding (at least in part) something *other* than normal Ascii (i.e.
static boolean isUnicode(byte[] sequence)
          This uses the implicit 'unicode marker' at the start of a Unicode file to determine whether a file is a unicode file.
static boolean isUTF8(byte[] sequence)
          Determines whether a given byte sequence is a valid utf-8 encoding.
static void log(java.lang.String S)
          Simple logging utility.
static void log(java.lang.String S, int level)
          logs if the global debug level equal to or greater than the passed int value.
static int nextCharIn(int pos, java.lang.String searchMe, java.lang.String compare, boolean match)
          Utility for micro-parser.
static void printEnumeration(java.util.Enumeration e)
          prints an enumeration...
static java.lang.String[] readFilteredDirectory(java.lang.String dirPath, java.lang.String extension)
          Reads a directory, returning all file names of the given extension.
static java.lang.String[] readFilteredDirectory(java.lang.String dirPath, java.lang.String[] fileExtensions)
          Reads a directory, returning all file names of the given extensions
static java.lang.String readI18NByteArray(byte[] data)
           
static java.lang.String readI18NFile(java.io.File file)
          Reads a text file, and returns the result as a StringBuffer.
static java.util.Properties readPropertyFile(java.lang.String fileName)
          Reads a java Properties list from a file.
static byte[] readStream(java.io.InputStream is)
          Reads an input stream into a byte array.
static java.lang.String[] readStringArrayFile(java.lang.String fileName)
          Reads an array of strings from a file (via a property file, 'cause I'm lazy).
static java.lang.String readTextFile(java.io.File file)
          Reads a text file, and returns the result as a StringBuffer.
static java.lang.StringBuffer readURLText(java.net.URL url)
          Returns the raw text (i.e. with tags as "\<...\>" strings) of a web page
static java.lang.StringBuffer readURLText(java.net.URL url, java.lang.StringBuffer errorText)
          Returns the raw text (i.e. with tags as "\<...\>" strings) of a web page
static java.lang.StringBuffer replaceAllBufferChar(java.lang.StringBuffer text, char c, java.lang.String replacement)
          Deletes all characters c in text replaces it with the string replacement.
static java.lang.StringBuffer replaceAllBufferString(java.lang.StringBuffer text, java.lang.String orig, java.lang.String replacement)
          Deletes all characters c in text replaces it with the string replacement.
static java.lang.String replaceAllChar(java.lang.StringBuffer text, char c, java.lang.String replacement)
          Deletes all characters c in text replaces it with the string replacement.
static java.lang.String replaceAllString(java.lang.StringBuffer text, java.lang.String orig, java.lang.String replacement)
          Deletes all characters orig in text and replaces it with the string replacement.
static int replaceChar(java.lang.StringBuffer text, int pos, java.lang.String replacement)
          Deletes a character in text at position pos and replaces it with the string replacement.
static int replaceString(java.lang.StringBuffer text, int pos, int len, java.lang.String replacement)
          Deletes a substring in text at position pos, of length len and replaces it with the string replacement.
static void restoreCursor(java.awt.Component C)
          Gets the cursor back that you just saved.
static void saveCursor(java.awt.Component C)
          Saves a cursor.
static void setHandCursor(java.awt.Component C)
          Sets the cursor to the hand cursor.
static void setLogDebugLevel(int L)
          Sets the level of logging on a scale of 0 (none) to 10 (everything).
static void setLogType(java.lang.String logType)
          Sets the type of logging, using the strings 'none', 'console' or 'file'.
static void setLogType(java.lang.String logType, java.lang.String fileName)
          Sets the type of logging, using the strings 'none', 'console' or 'file'.
static void setNormalCursor(java.awt.Component C)
          Sets the cursor to the normal cursor.
static void setWaitCursor(java.awt.Component C)
          Sets the cursor to the wait cursor.
static java.lang.String string2Hex(java.lang.String orig)
           
static java.lang.String toHTML(java.lang.String rawText)
          Turns a string into HTML displayable text by escaping special characters ('<','&' etc...). ... add new ones as required; or see if an existing ftn somewhere does this already...
static boolean warning(java.awt.Component caller, java.lang.String Msg, java.lang.String Title)
          utility ftn; prints warning dialog message to the user, *without* echoing to the log ftn.
static boolean warning(java.lang.String Msg)
          Short version of warning method - uses default frame, and has the title 'Warning'.
static void writePropertyFile(java.lang.String fileName, java.util.Properties propertyList, java.lang.String comments)
          Writes a java Properties list to a file.
static void writeStringArrayFile(java.lang.String fileName, java.lang.String[] strings)
          Writes an array of strings into a file (via a property file, 'cause I'm lazy).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

readURLText

public static java.lang.StringBuffer readURLText(java.net.URL url)
Returns the raw text (i.e. with tags as "\<...\>" strings) of a web page

Parameters:
url - the url of the web age to read as plain text.
Returns:
a StringBuffer containing the raw html text

readURLText

public static java.lang.StringBuffer readURLText(java.net.URL url,
                                                 java.lang.StringBuffer errorText)
Returns the raw text (i.e. with tags as "\<...\>" strings) of a web page

Parameters:
url - the url of the web age to read as plain text.
errorText - a custom message to return if something goes wrong.
Returns:
a StringBuffer containing the raw html text

readStream

public static byte[] readStream(java.io.InputStream is)
                         throws java.io.IOException
Reads an input stream into a byte array.

java.io.IOException

readTextFile

public static java.lang.String readTextFile(java.io.File file)
                                     throws java.io.IOException
Reads a text file, and returns the result as a StringBuffer. Not Recommended for large (say > 100k) files.

Parameters:
file - the ascii file to read from.
java.io.IOException

readI18NFile

public static java.lang.String readI18NFile(java.io.File file)
                                     throws java.io.IOException
Reads a text file, and returns the result as a StringBuffer. Not Recommended for large (say > 100k) files.

This function attempts to automatically determine the encoding of the file it is to read, as either UTF-16, UTF-8, or default locale encoding, based on 1) whether the first two bytes are Unicode byte-ordering markers ('FFFE' or 'FEFF'), UTF-8 (based on whether the file is a valid UTF8 string) or, failing this, the default locale encoding.

Parameters:
file - the local encoding/unicode/utf8 file to read from.
java.io.IOException

readI18NByteArray

public static java.lang.String readI18NByteArray(byte[] data)

readStringArrayFile

public static java.lang.String[] readStringArrayFile(java.lang.String fileName)
Reads an array of strings from a file (via a property file, 'cause I'm lazy).

Parameters:
fileName - the file to read from

readPropertyFile

public static java.util.Properties readPropertyFile(java.lang.String fileName)
Reads a java Properties list from a file.

Parameters:
fileName - the full path and file name of the properties file to read in.

writeStringArrayFile

public static void writeStringArrayFile(java.lang.String fileName,
                                        java.lang.String[] strings)
Writes an array of strings into a file (via a property file, 'cause I'm lazy). (XXX Warning - will only write unique values; doubles will be lost).

Parameters:
fileName - the file to read to
strings - the array of strings

writePropertyFile

public static void writePropertyFile(java.lang.String fileName,
                                     java.util.Properties propertyList,
                                     java.lang.String comments)
Writes a java Properties list to a file.

Parameters:
fileName - the full path and file name of the properties file to read in.

toHTML

public static java.lang.String toHTML(java.lang.String rawText)
Turns a string into HTML displayable text by escaping special characters ('<','&' etc...). ... add new ones as required; or see if an existing ftn somewhere does this already...


replaceChar

public static int replaceChar(java.lang.StringBuffer text,
                              int pos,
                              java.lang.String replacement)
Deletes a character in text at position pos and replaces it with the string replacement.

Parameters:
text - the text to be modified
pos - the position of the character to be deleted
replacement - the string the character is to be replaced with.

replaceAllChar

public static java.lang.String replaceAllChar(java.lang.StringBuffer text,
                                              char c,
                                              java.lang.String replacement)
Deletes all characters c in text replaces it with the string replacement.

Parameters:
text - the text to be modified
replacement - the string the character is to be replaced with.

replaceAllBufferChar

public static java.lang.StringBuffer replaceAllBufferChar(java.lang.StringBuffer text,
                                                          char c,
                                                          java.lang.String replacement)
Deletes all characters c in text replaces it with the string replacement.

Parameters:
text - the text to be modified
replacement - the string the character is to be replaced with.

replaceString

public static int replaceString(java.lang.StringBuffer text,
                                int pos,
                                int len,
                                java.lang.String replacement)
Deletes a substring in text at position pos, of length len and replaces it with the string replacement.

Parameters:
text - the text to be modified
pos - the position of the character to be deleted
replacement - the string the character is to be replaced with.

replaceAllString

public static java.lang.String replaceAllString(java.lang.StringBuffer text,
                                                java.lang.String orig,
                                                java.lang.String replacement)
Deletes all characters orig in text and replaces it with the string replacement.

Parameters:
text - the text to be modified
replacement - the string the original substring is to be replaced with.

replaceAllBufferString

public static java.lang.StringBuffer replaceAllBufferString(java.lang.StringBuffer text,
                                                            java.lang.String orig,
                                                            java.lang.String replacement)
Deletes all characters c in text replaces it with the string replacement.

Parameters:
text - the text to be modified
replacement - the string the character is to be replaced with.

nextCharIn

public static int nextCharIn(int pos,
                             java.lang.String searchMe,
                             java.lang.String compare,
                             boolean match)
Utility for micro-parser. Gets the next character pos in a string after an initial offset that either matches, or does not match, any of a set of comparison characters.

Parameters:
pos - the position to start searching from
searchMe - the string to search
compare - a string containing characters to compare against
Returns:
the position found, or -1 if no position is found.

readFilteredDirectory

public static java.lang.String[] readFilteredDirectory(java.lang.String dirPath,
                                                       java.lang.String extension)
Reads a directory, returning all file names of the given extension.

Parameters:
dirPath - directory to read
extension - the file extension to filter files with.
Returns:
list of full file names

readFilteredDirectory

public static java.lang.String[] readFilteredDirectory(java.lang.String dirPath,
                                                       java.lang.String[] fileExtensions)
Reads a directory, returning all file names of the given extensions

Parameters:
dirPath - directory to read
Returns:
list of full file names

setWaitCursor

public static void setWaitCursor(java.awt.Component C)
Sets the cursor to the wait cursor.


setNormalCursor

public static void setNormalCursor(java.awt.Component C)
Sets the cursor to the normal cursor.


setHandCursor

public static void setHandCursor(java.awt.Component C)
Sets the cursor to the hand cursor.


saveCursor

public static void saveCursor(java.awt.Component C)
Saves a cursor. One cursor. That's all. Just one. Try to save another one, it'll overwrite this one. So don't.


restoreCursor

public static void restoreCursor(java.awt.Component C)
Gets the cursor back that you just saved. Probably better make sure it's the same component you saved it from. Wouldn't care to guess what happens if it isn't...


setLogDebugLevel

public static void setLogDebugLevel(int L)
Sets the level of logging on a scale of 0 (none) to 10 (everything).

Parameters:
L - the log level.

getLogDebugLevel

public static int getLogDebugLevel()
Returns the global debug level.


setLogType

public static void setLogType(java.lang.String logType)
Sets the type of logging, using the strings 'none', 'console' or 'file'.

Parameters:
logType - the type of logging to use.

setLogType

public static void setLogType(java.lang.String logType,
                              java.lang.String fileName)
Sets the type of logging, using the strings 'none', 'console' or 'file'.

Parameters:
logType - the type of logging to use.

closeLog

public static void closeLog()
Closes the log file.


log

public static void log(java.lang.String S,
                       int level)
logs if the global debug level equal to or greater than the passed int value.

Log Levels

  • 0 - error logging only
  • 1
  • 2
  • 3
  • 4 - entry level logging of all delete/copy/move operations
  • 5
  • 6
  • 7
  • 8
  • 9 - full BER logging

Parameters:
S - the string to log
level - the debug level at which the string starts being printed

log

public static void log(java.lang.String S)
Simple logging utility. Writes log data to a file or console, or ignores it, depending on the value of the logging and logfile property (defaults set in JXplorer.java, user sets in dxconfig.txt)


initDefaultDisplay

public static void initDefaultDisplay(java.awt.Frame owner)

getDefaultDisplay

public static java.awt.Frame getDefaultDisplay()

error

public static boolean error(java.awt.Component owner,
                            java.lang.String Msg)
utility ftn; prints error message to user, and echos to the log ftn.

Returns:
returns false for easy chaining.

error

public static boolean error(java.awt.Frame owner,
                            java.lang.String Msg)
utility ftn; prints error message to user, and echos to the log ftn.

Returns:
returns false for easy chaining.

error

public static boolean error(java.lang.String Msg)
utility ftn; prints error message to user, and echos to the log ftn.

Returns:
returns false for easy chaining.

error

public static boolean error(java.lang.String Msg,
                            java.lang.Exception e)
wrapper for the JFrame version of error.

Parameters:
Msg - a short one line message to display to the user
e - the exception to log
Returns:
returns false for easy chaining.

error

public static boolean error(java.awt.Component owner,
                            java.lang.String Msg,
                            java.lang.Exception e)
wrapper for the JFrame version of error.

Parameters:
Msg - a short one line message to display to the user
e - the exception to log
Returns:
returns false for easy chaining.

error

public static boolean error(java.awt.Frame owner,
                            java.lang.String Msg,
                            java.lang.Exception e)
utility ftn; prints error message and the error to the user, and echos to the log ftn.

Parameters:
Msg - a short one line message to display to the user
e - the exception to log
Returns:
returns false for easy chaining.

confirm

public static void confirm(java.lang.String Msg)
Utility function. Opens a dialog with a confirmation message.

Parameters:
Msg - the confirmation message to be displayed.

warning

public static boolean warning(java.awt.Component caller,
                              java.lang.String Msg,
                              java.lang.String Title)
utility ftn; prints warning dialog message to the user, *without* echoing to the log ftn. Basically wrapper to JOptionPane

Parameters:
caller - the GUI component calling (required for dialog box drawing)
Msg - a short one line message to display to the user
Returns:
returns false for easy chaining.

warning

public static boolean warning(java.lang.String Msg)
Short version of warning method - uses default frame, and has the title 'Warning'.

Parameters:
Msg - the warning message to display.

printEnumeration

public static void printEnumeration(java.util.Enumeration e)
prints an enumeration...


getParentFrame

public static java.awt.Frame getParentFrame(java.awt.Component c)
Iterates through a components parents until it finds the root frame. Useful for initing JDialogs etc. that require a root frame to work properly.


convertPathToUnix

public static java.lang.String convertPathToUnix(java.lang.String dosPath)
Converts a 'dos' style file path to a unix style file path by exchanging '\' characters for for '/' characters.


center

public static void center(java.awt.Component centerMe,
                          java.awt.Component positioner)
This positions a component to the center of another component. If both components are showing on the sceen, it uses absolute screen co-ordinates, otherwise if only the positioner component is showing, it uses relative co-ordinates (since it is unable to obtain screen co-ords). If the components share a reference frame, these two actions are equivalent (i.e. if they both have the same parent). If nothing is showing, the component is unchanged.

Parameters:
positioner - the component used as the reference center. If null, the component will be centered on the screen

bytes2Hex

public static java.lang.String bytes2Hex(byte[] bytes)

string2Hex

public static java.lang.String string2Hex(java.lang.String orig)

byte2Hex

public static java.lang.String byte2Hex(byte b)

char2Hex

public static java.lang.String char2Hex(char c)

hex2Byte

public static byte hex2Byte(char hex1,
                            char hex2)

hexChar2Byte

public static byte hexChar2Byte(char hex)
Convert a single character to a byte...


bytes2HexSplit

public static java.lang.String bytes2HexSplit(byte[] in,
                                              int wordlength)
From Van Bui - prints out a hex string formatted with spaces between each hex word of length wordlength.

Parameters:
in - input array of bytes to convert
wordlength - the length of hex words to print otu.

bytes2HexSplit

public static java.lang.String bytes2HexSplit(byte[] in,
                                              int wordlength,
                                              int linelength)
From Van Bui - prints out a hex string formatted with spaces between each hex word of length wordlength, and new lines every linelength.

Parameters:
in - input array of bytes to convert
wordlength - the length of hex words to print otu.
linelength - the length of a line to print before inserting a line feed.

isUTF8

public static boolean isUTF8(byte[] sequence)
Determines whether a given byte sequence is a valid utf-8 encoding. While this does not mean that the byte *is* a utf-8 encoded string, the chance of a random byte sequence happening to be utf8 is roughly (1/2 ** (byte array length)).

Note that '7 bit ascii' is *always* a valid utf-8 string...

see rfc 2279


isNonAsciiUTF8

public static boolean isNonAsciiUTF8(byte[] sequence)
Determines whether a given byte sequence is a valid utf-8 encoding, encoding (at least in part) something *other* than normal Ascii (i.e. it is utf-8 encoding something that is not just 7-bit ascii, which in utf-8 is indistinguishable from the original text).

While this does not mean that the bytes *are* a utf-8 encoded string, the chance of a random byte sequence (containing bytes with the high-bit set) happening to be utf8 is roughly (1/2 ** (byte array length)).

see rfc 2279


isUnicode

public static boolean isUnicode(byte[] sequence)
This uses the implicit 'unicode marker' at the start of a Unicode file to determine whether a file is a unicode file. At the beginning of every unicode file is a two byte code indicating the endien-ness of the file (either FFFE or FEFF). If either of these sequences is found, this function returns true, otherwise it returns false. Technically this isn't a sure test, since a) something else could have this signiture, and b) unicode files are not absolutely required to have this signiture (but most do).