de.infinityloop.util
Class Utility

java.lang.Object
  extended by de.infinityloop.util.Utility

public class Utility
extends java.lang.Object

Collection of (static) Utility methods.


Field Summary
static java.text.SimpleDateFormat[] rfc822DateFormats
          Parses an RFC-1123 compliant date string into a Calendar object.
 
Constructor Summary
Utility()
           
 
Method Summary
static java.util.List<java.lang.Object> arr2ArrayList(java.lang.Object[] arr)
          Convert an array to a Vector.
static void benchmark(int repetitions)
          Do a quick and dirty benchmark of the system we are running on.
static int coerceObjectToInt(java.lang.Object obj, int defaultValue)
          Try to make an integer from an arbitrary Java object.
static java.lang.String coerceObjectToString(java.lang.Object obj, java.lang.String defaultValue)
          Try to make a String from an arbitrary Java object.
static java.util.ArrayList convertHashMapToArrayList(java.util.HashMap ht)
          Converts a HashMap to a Stack of Pair's.
static java.util.Stack convertHashMapToStack(java.util.HashMap ht)
          Converts a HashMap to a Stack of Pair's.
static java.util.Stack convertHashtableToStack(java.util.Hashtable ht)
          Converts a Hashtable to a Stack of Pair's.
static java.util.Stack convertHashtableToStackSorted(java.util.Hashtable ht)
          Converts a Hashtable to a Stack of Pair's, with sorted keys in natural order.
static java.lang.String convertLineEndings(java.lang.String s, java.lang.String lineEnding)
          Converts the lines in the passed string into a form using the specified line ending string
static java.util.Hashtable convertStackToHashtable(java.util.Vector v)
          Converts a Vector of Pair()s to a Hashtable.
static java.util.Stack convertTreeMapToStack(java.util.TreeMap ht)
          Converts a TreeMap to a Stack of Pair's.
static java.util.Map convertVectorToHashMap(java.util.Vector v)
          Converts a Vector of Pair()s to a Hashtable.
static int countOccurrences(java.lang.String s, char c)
          Returns the number of occurrences of a specified character in a String.
static int dayDifference(java.util.Calendar one, java.util.Calendar two)
          Calculates the difference in days between the two dates passed in (one - two).
static void debugMessage(java.lang.String msg)
          Issue a debug level message to the current active log.
static java.lang.Object deserializeJavaObject(java.lang.String ser)
          Deserializes the passed serialization string and returns the appropriate Java object.
static java.awt.Rectangle ensureVisibleWindowPosition(java.awt.Rectangle desired)
          Returns a possibly modified copy of the passed, desired window bounds rectangle so that the window is actually visible at least 60x60px from the top left corner to be grabbable and draggable.
static void errorMessage(java.lang.String msg)
          Issue a error level message to the current active log.
static java.lang.String escapeCharacters(java.lang.String input, java.lang.String chars, int mode)
          Escapes all characters in input that are listed in chars according to the specified mode.
static int executeCommand(java.util.Vector theCommand, boolean wait, java.lang.StringBuffer stdout, java.lang.StringBuffer stderr)
          executes the passed commandline via Runtime.exec().
static void fatalMessage(java.lang.String msg)
          Issue a fatal level message to the current active log.
static java.lang.String getCurrentMethodName()
          Return the name of the routine that called getCurrentMethodName
static java.lang.String getJREversionString()
          Returns the JRE version as in the system property.
static java.lang.String getMangledComponent(int n, java.lang.String s)
          Retrieves the n-th component of a mangled string, with n starting at 0.
static java.lang.String getTimerInfoString()
          prints current Timer information
static int hourDifference(java.util.Calendar one, java.util.Calendar two)
          Calculates the difference in days between the two dates passed in (one - two). does not take into account leap years, leap seconds etc.
static java.lang.String indentation(int steps, java.lang.String indentationElement)
          Calculates indentation string based on passed values: (steps * indentatationElement) as String
static void infoMessage(java.lang.String msg)
          Issue a info level message to the current active log.
static boolean isDocFile(DiskFile file)
          determines if the passed file is a DOC file
static boolean isHexCharacter(char c)
          Determines whether the passed character is a hex representation character, i.e. if it is within [0-9A-Fa-f].
static boolean isRTFFile(DiskFile file)
          determines if the passed file is an RTF file
static boolean isWordFile(DiskFile file)
          determines if the passed file is a Word document file, i.e.
static boolean isWordFilePlatform(DiskFile file)
          determines if the passed file is a readable Word document file, i.e.
static int makeDTTMFromRFCDate(java.lang.String s)
          translates an RFC date to DTTM.
static java.lang.String makeISODateString(java.util.Calendar cld)
          Makes an ISO-8601 compliant date from a Java Calendar object.
static java.lang.String makeRFCDateFromDTTM(int value)
          translates a DTTM bitfield to a RFC date.
static java.lang.String makeRFCDateString(java.util.Calendar cld)
          Makes an RFC-1123 compliant date from a Java Calendar object.
static java.lang.String makeStringFromList(java.util.List list)
          Renders a List object into a String, by puttin the string value of each item into a new line (0xa)
static int makeVersionInt(java.lang.String versionstring)
          Tranforms the version string (M.m.r) into a int.
static java.lang.String makeVersionString(int version)
          Tranforms the integer version into a human readable version string.
static java.lang.String makeVersionString(int version, java.lang.String build)
          Tranforms the integer version into a human readable version string, including the build number.
static java.lang.String minimizeWhitespace(java.lang.String src)
          Minimizes whitespace in a string, i.e. condenses all whitespace characters to one single WS character.
static java.lang.String parseCSS2Identifier(java.lang.String src)
          Parses a CSS2 (class-)identifier, resolving all hex escapes it might have.
static java.util.Calendar parseISODateString(java.lang.String date)
          Parses an ISO-8601 compliant date string into a Calendar object.
static java.util.Calendar parseRFCDateString(java.lang.String date)
           
static java.lang.String prependLineNumbers(java.lang.String s)
          Prepends line numbers to the passed string, starting with 1
static java.lang.String prettyPrintBytenumber(long n)
          pretty-prints the passed number of bytes.
static byte[] readDataToByteArray(InputProxy ip)
          This reads from an input proxy the data into a byte array and returns that.
static void resetTimer(java.lang.String key)
          resets the timer with name key
static void resetTimers()
          resets all timing info
static java.lang.String[] resolveArgumentString(java.lang.String params, java.util.Vector charsNeedingQuoting)
          Resolves a (commandline-) String into an array of Strings with the single arguments.
static java.lang.String resolveNumericalEntities(java.lang.String s)
          Replaces numerical entities in the passed string (&#decdigits;) into a Unicode character.
static java.util.Stack resolveParameters(java.lang.String params)
          Resolves a string into a Stack of Pair()s of parameter-name, parameter-value.
static java.lang.String serializeExceptionStacktrace(java.lang.Throwable t)
          Serializes the stack trace of the passed Throwable object to a String and returns it.
static java.lang.String serializeJavaObject(java.lang.Object obj)
          Serializes the passed Java object to a String.
static java.lang.String setMangledComponent(java.lang.String dest, int n, java.lang.String val)
          Sets the nth component in the passed string to the indicated value.
static java.util.ArrayList splitTextToLines(java.lang.String source, boolean stripEmptyLines, boolean removeComments)
          Splits the passed text into single lines and returns an ArrayList of Strings, each containing a single line of the original input.
static void startTimer(java.lang.String key)
          start a cumulative timer of key key
static long stopTimer(java.lang.String key)
          stops a cumulative timer of key key and returns the length of the just stopped timing action in milliseconds.
static java.lang.String toHexString(long number, int length, boolean truncate)
          Converts the passed long to a hex string with the specified length, with leading zeros if necessary.
static java.lang.String unquoteCSS2IdentifierOrString(java.lang.String s)
          Converts the passed CSS string to the internal format: unquote, resolve escapes etc.
static void warnMessage(java.lang.String msg)
          Issue a warning level message to the current active log.
static void writeTextLinesStringPlatform(java.lang.String s, java.io.Writer w)
          Writes a string containing Java GUI newline characters (\n) to the specified OutputWriter, replacing Java GUI newlines by platform newlines.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

rfc822DateFormats

public static final java.text.SimpleDateFormat[] rfc822DateFormats
Parses an RFC-1123 compliant date string into a Calendar object.

To support optional components, we try several format string in defined order.

Constructor Detail

Utility

public Utility()
Method Detail

convertTreeMapToStack

public static java.util.Stack convertTreeMapToStack(java.util.TreeMap ht)
Converts a TreeMap to a Stack of Pair's.


convertHashtableToStack

public static java.util.Stack convertHashtableToStack(java.util.Hashtable ht)
Converts a Hashtable to a Stack of Pair's.


convertHashMapToStack

public static java.util.Stack convertHashMapToStack(java.util.HashMap ht)
Converts a HashMap to a Stack of Pair's.


convertHashMapToArrayList

public static java.util.ArrayList convertHashMapToArrayList(java.util.HashMap ht)
Converts a HashMap to a Stack of Pair's.


convertHashtableToStackSorted

public static java.util.Stack convertHashtableToStackSorted(java.util.Hashtable ht)
Converts a Hashtable to a Stack of Pair's, with sorted keys in natural order.


convertStackToHashtable

public static java.util.Hashtable convertStackToHashtable(java.util.Vector v)
Converts a Vector of Pair()s to a Hashtable.


convertVectorToHashMap

public static java.util.Map convertVectorToHashMap(java.util.Vector v)
Converts a Vector of Pair()s to a Hashtable.


countOccurrences

public static int countOccurrences(java.lang.String s,
                                   char c)
Returns the number of occurrences of a specified character in a String.


makeRFCDateFromDTTM

public static java.lang.String makeRFCDateFromDTTM(int value)
translates a DTTM bitfield to a RFC date.
 DTTM format:
 wwwyyyyy yyyymmmm dddddhhh hhmmmmmm

 w day of week (0 Sun - 6 Sat)
 y year-1900
 m month (1-12)
 d day of month (1-31)
 h hour 0-23
 m minute 0-59

   0-5  Minute  0-59
   6-10 Hour    0-23
   11-15        Day of month    1-31
   16-19        Month   1-12
   20-28        Year    = Year-1900
   29-31        Day of week     0 (Sun)-6 (Sat)
 


makeDTTMFromRFCDate

public static int makeDTTMFromRFCDate(java.lang.String s)
translates an RFC date to DTTM.
 DTTM format:
 wwwyyyyy yyyymmmm dddddhhh hhmmmmmm

 w day of week (0 Sun - 6 Sat)
 y year-1900
 m month (1-12)
 d day of month (1-31)
 h hour 0-23
 m minute 0-59

   0-5  Minute  0-59
   6-10 Hour    0-23
   11-15        Day of month    1-31
   16-19        Month   1-12
   20-28        Year    = Year-1900
   29-31        Day of week     0 (Sun)-6 (Sat)
 


unquoteCSS2IdentifierOrString

public static java.lang.String unquoteCSS2IdentifierOrString(java.lang.String s)
Converts the passed CSS string to the internal format: unquote, resolve escapes etc.


parseCSS2Identifier

public static java.lang.String parseCSS2Identifier(java.lang.String src)
Parses a CSS2 (class-)identifier, resolving all hex escapes it might have.


isHexCharacter

public static boolean isHexCharacter(char c)
Determines whether the passed character is a hex representation character, i.e. if it is within [0-9A-Fa-f].


dayDifference

public static int dayDifference(java.util.Calendar one,
                                java.util.Calendar two)
Calculates the difference in days between the two dates passed in (one - two). Does not take into account leap years, leap seconds etc.


hourDifference

public static int hourDifference(java.util.Calendar one,
                                 java.util.Calendar two)
Calculates the difference in days between the two dates passed in (one - two). does not take into account leap years, leap seconds etc.


indentation

public static java.lang.String indentation(int steps,
                                           java.lang.String indentationElement)
Calculates indentation string based on passed values: (steps * indentatationElement) as String

Parameters:
steps - number of steps to indent
indentationElement - for each indentation step, this String is written
Returns:
the full indentation string

resolveParameters

public static java.util.Stack resolveParameters(java.lang.String params)
Resolves a string into a Stack of Pair()s of parameter-name, parameter-value. Syntax: [name ('='|':'|':=') '"' value '"']? [ '[;,]' name '=' '"' value '"' ]*


resolveArgumentString

public static java.lang.String[] resolveArgumentString(java.lang.String params,
                                                       java.util.Vector charsNeedingQuoting)
Resolves a (commandline-) String into an array of Strings with the single arguments. Use double quotes for arguments containing spaces. Whitespace delimits arguments. Use the backslash character for escaping (i.e. \\ for the backslash and \" for double quotes).


splitTextToLines

public static java.util.ArrayList splitTextToLines(java.lang.String source,
                                                   boolean stripEmptyLines,
                                                   boolean removeComments)
Splits the passed text into single lines and returns an ArrayList of Strings, each containing a single line of the original input.

Parameters:
source - the source string
stripEmptyLines - when true, no elements are generated for lines whose trim().length() == 0.
removeComments - when true, all lines starting with '#' or '//' are discarded
Returns:
the lines as an ArrayList of String objects

makeStringFromList

public static java.lang.String makeStringFromList(java.util.List list)
Renders a List object into a String, by puttin the string value of each item into a new line (0xa)

Parameters:
list - the list to render
Returns:

arr2ArrayList

public static java.util.List<java.lang.Object> arr2ArrayList(java.lang.Object[] arr)
Convert an array to a Vector.

Parameters:
arr - the Object array
Returns:
the Vector

getJREversionString

public static java.lang.String getJREversionString()
Returns the JRE version as in the system property.


makeISODateString

public static java.lang.String makeISODateString(java.util.Calendar cld)
Makes an ISO-8601 compliant date from a Java Calendar object.


makeRFCDateString

public static java.lang.String makeRFCDateString(java.util.Calendar cld)
Makes an RFC-1123 compliant date from a Java Calendar object.

Format: "Wkd, dd Mon yyyy hh:mm:ss GMT"


parseISODateString

public static java.util.Calendar parseISODateString(java.lang.String date)
Parses an ISO-8601 compliant date string into a Calendar object. http://www.w3.org/TR/xmlschema-2/#isoformats


parseRFCDateString

public static java.util.Calendar parseRFCDateString(java.lang.String date)

makeVersionString

public static java.lang.String makeVersionString(int version)
Tranforms the integer version into a human readable version string.


makeVersionString

public static java.lang.String makeVersionString(int version,
                                                 java.lang.String build)
Tranforms the integer version into a human readable version string, including the build number.


makeVersionInt

public static int makeVersionInt(java.lang.String versionstring)
Tranforms the version string (M.m.r) into a int.


writeTextLinesStringPlatform

public static void writeTextLinesStringPlatform(java.lang.String s,
                                                java.io.Writer w)
                                         throws java.io.IOException
Writes a string containing Java GUI newline characters (\n) to the specified OutputWriter, replacing Java GUI newlines by platform newlines.

Throws:
java.io.IOException

convertLineEndings

public static java.lang.String convertLineEndings(java.lang.String s,
                                                  java.lang.String lineEnding)
Converts the lines in the passed string into a form using the specified line ending string


prependLineNumbers

public static java.lang.String prependLineNumbers(java.lang.String s)
Prepends line numbers to the passed string, starting with 1


minimizeWhitespace

public static java.lang.String minimizeWhitespace(java.lang.String src)
Minimizes whitespace in a string, i.e. condenses all whitespace characters to one single WS character.


resolveNumericalEntities

public static java.lang.String resolveNumericalEntities(java.lang.String s)
                                                 throws java.lang.Exception
Replaces numerical entities in the passed string (&#decdigits;) into a Unicode character.

Throws:
java.lang.Exception

getMangledComponent

public static java.lang.String getMangledComponent(int n,
                                                   java.lang.String s)
Retrieves the n-th component of a mangled string, with n starting at 0. Returns null if that component is not found.


setMangledComponent

public static java.lang.String setMangledComponent(java.lang.String dest,
                                                   int n,
                                                   java.lang.String val)
Sets the nth component in the passed string to the indicated value. It replaces any value the string might have, or fills with empty components to be able to set the n-th component.

Note: n is 0-based.


toHexString

public static java.lang.String toHexString(long number,
                                           int length,
                                           boolean truncate)
Converts the passed long to a hex string with the specified length, with leading zeros if necessary. The value will be truncated if for representing the number, more than length characters are needed and truncate is true. Otherwise, the needed length for representing the number will be used. If length is negative, leading zeros will be suppressed (and a correspondingly shorter string is returned).


getCurrentMethodName

public static java.lang.String getCurrentMethodName()
Return the name of the routine that called getCurrentMethodName


startTimer

public static void startTimer(java.lang.String key)
start a cumulative timer of key key


stopTimer

public static long stopTimer(java.lang.String key)
stops a cumulative timer of key key and returns the length of the just stopped timing action in milliseconds. Returns 0 if that timer was not running.


resetTimers

public static void resetTimers()
resets all timing info


resetTimer

public static void resetTimer(java.lang.String key)
resets the timer with name key


getTimerInfoString

public static java.lang.String getTimerInfoString()
prints current Timer information


isRTFFile

public static boolean isRTFFile(DiskFile file)
determines if the passed file is an RTF file

Parameters:
file - the file to test
Returns:
true if it is an RTF file, false otherwise

isDocFile

public static boolean isDocFile(DiskFile file)
determines if the passed file is a DOC file

Parameters:
file - the file to test
Returns:
true if it is a Word binary (.doc) file, false otherwise

isWordFile

public static boolean isWordFile(DiskFile file)
determines if the passed file is a Word document file, i.e. either a Word binary (.doc) file or an RTF file.

Parameters:
file - the file to test
Returns:
true if it is an RTF file, false otherwise

isWordFilePlatform

public static boolean isWordFilePlatform(DiskFile file)
determines if the passed file is a readable Word document file, i.e. either a Word binary (.doc, on Windows platform only!) file or an RTF file.

Parameters:
file - the file to test
Returns:
true if it is an RTF file, false otherwise

executeCommand

public static int executeCommand(java.util.Vector theCommand,
                                 boolean wait,
                                 java.lang.StringBuffer stdout,
                                 java.lang.StringBuffer stderr)
                          throws java.lang.Exception
executes the passed commandline via Runtime.exec(). It optionally waits until the command terminates and in that case returns the result code. Otherwise (not waiting) it always returns 0 (=ok). You can pass in StringBuffer() objects for the process's stdout and stderr stream, which will be filled accordingly. You should set wait to true in this case as otherwise, the method will return immediately and the string buffers will not be filled completely. If you do not want to capture the output, pass null. The output will then be written to the log file.

Throws:
java.lang.Exception

serializeJavaObject

public static java.lang.String serializeJavaObject(java.lang.Object obj)
Serializes the passed Java object to a String.

Parameters:
obj - the object to serialize
Returns:
a String representation of the serialization

deserializeJavaObject

public static java.lang.Object deserializeJavaObject(java.lang.String ser)
Deserializes the passed serialization string and returns the appropriate Java object.

Parameters:
obj - the object to serialize
Returns:
a String representation of the serialization

debugMessage

public static void debugMessage(java.lang.String msg)
Issue a debug level message to the current active log.

Parameters:
msg - the message to log
Since:
5.3.3 / 1.6.3

infoMessage

public static void infoMessage(java.lang.String msg)
Issue a info level message to the current active log.

Parameters:
msg - the message to log
Since:
5.3.3 / 1.6.3

errorMessage

public static void errorMessage(java.lang.String msg)
Issue a error level message to the current active log.

Parameters:
msg - the message to log
Since:
5.3.3 / 1.6.3

warnMessage

public static void warnMessage(java.lang.String msg)
Issue a warning level message to the current active log.

Parameters:
msg - the message to log
Since:
5.3.3 / 1.6.3

fatalMessage

public static void fatalMessage(java.lang.String msg)
Issue a fatal level message to the current active log.

Parameters:
msg - the message to log
Since:
5.3.3 / 1.6.3

benchmark

public static void benchmark(int repetitions)
Do a quick and dirty benchmark of the system we are running on.

Parameters:
repetitions - repetitions of the test

escapeCharacters

public static java.lang.String escapeCharacters(java.lang.String input,
                                                java.lang.String chars,
                                                int mode)
Escapes all characters in input that are listed in chars according to the specified mode.

Parameters:
input - the input string
chars - a String of all characters that need to be escaped
mode - the escaping mode
Returns:
the input string with escaping applied

prettyPrintBytenumber

public static java.lang.String prettyPrintBytenumber(long n)
pretty-prints the passed number of bytes.


serializeExceptionStacktrace

public static java.lang.String serializeExceptionStacktrace(java.lang.Throwable t)
Serializes the stack trace of the passed Throwable object to a String and returns it.

Parameters:
t - the Throwable
Returns:
a String with the serialized stacktrace info

readDataToByteArray

public static byte[] readDataToByteArray(InputProxy ip)
This reads from an input proxy the data into a byte array and returns that.

Parameters:
ip -
Returns:

ensureVisibleWindowPosition

public static java.awt.Rectangle ensureVisibleWindowPosition(java.awt.Rectangle desired)
Returns a possibly modified copy of the passed, desired window bounds rectangle so that the window is actually visible at least 60x60px from the top left corner to be grabbable and draggable. Does only support main screen configuration!

Parameters:
desired - the desired bounds
Returns:
the bounds so that the window is visible and draggable

coerceObjectToInt

public static int coerceObjectToInt(java.lang.Object obj,
                                    int defaultValue)
Try to make an integer from an arbitrary Java object. Return the defaultValue passed in when this does not succeed.

Parameters:
obj - the object to handle, may be null
defaultValue - the value to return if the actual object cannot be coerced into an integer
Returns:
the object made into an integer value

coerceObjectToString

public static java.lang.String coerceObjectToString(java.lang.Object obj,
                                                    java.lang.String defaultValue)
Try to make a String from an arbitrary Java object. Return the defaultValue passed in when this does not succeed.

Parameters:
obj - the object to make a string of; implementation uses .toString() method on the object
defaultValue - the default value to return when .toString() does not succeed or the object is null
Returns:
the object made into a String