org.safs
Class StringUtils

java.lang.Object
  extended by org.safs.StringUtils

public class StringUtils
extends java.lang.Object


Purpose: string utilities

Since:
JUN 04, 2003
JUN 04, 2003 (DBauman) Original Release
JUN 26, 2003 (CANAGL) Added convertWildcardsToRegularExpression
DEC 19, 2006 (CANAGL) Added readUTF8File support
OCT 29, 2008 (LeiWang)Add method getTokenList().
NOV 25, 2008 (LeiWang)Add method containsSepcialKeys(): See defect S0546329
JAN 26, 2009 (CANAGL)Fixed getTrimmedUnquotedString to trim appropriate whitespace

Field Summary
static int DEFAULT_BUFFER_SIZE
           
static int maxBytesPerRead
           
static java.lang.String REGEX_CHARACTER_ANY
           
static java.lang.String REGEX_CHARACTER_MATCH_MANY
           
static java.lang.String REGEX_CHARACTER_MATCH_ONE
           
static java.lang.String[] specialKeys
           
 
Constructor Summary
StringUtils()
           
 
Method Summary
static java.lang.String arrayToString(java.lang.String[] values, java.lang.String sep)
           
static java.lang.String concat(java.lang.String s1, java.lang.String s2, java.lang.String s3)
          
Purpose: concatenate strings
static boolean containsSepcialKeys(java.lang.String value)
           
static java.lang.String convertWildcardsToRegularExpression(java.lang.String input)
          convert ? and * wildcarding to regular expression wildcarding .? and .*
static int findExactMatchIndex(java.util.ListIterator iter, java.lang.String match)
          
Purpose: find an exact match based on the parameter by walking the list, finding a string which equals the matching string, and returning the index into the list if it matched.
static int findMatchIndex(java.util.ListIterator iter, java.lang.String match)
          
Purpose: find a match based on the parameter by walking the list, finding a string which contains the matching substring, and returning the index into the list if it matched.
static java.lang.String getChars(java.lang.Integer num, java.lang.String chars)
          
Purpose: get a string with the number of specified sequenced 'chars'
static java.lang.String getDBVal(java.lang.Object m)
          Deprecated. for DatabaseUtils.getDBVal(Object)
static java.lang.String getDelimitedString(java.util.List<java.lang.String> list, java.lang.String delimiter)
          Purpose: Get text strung delimited by delimiter from a list, delimiter will be considered as a whole string
Example: If delimiter is "->", if the list contains 3 items, "parent" "child" "grandChild"
the returned text is "parent->child->grandChild"
static int getFieldCount(java.lang.String input, int startindex, java.lang.String seps)
          
Purpose: Finds the count of all fields within the inputRecord found from startindex to the end of the inputRecord.
static java.lang.String getInputToken(java.lang.String input, int n, java.lang.String sep)
          
Purpose: get 0-based field N of input by tokenizing with sep.
static java.lang.String[] getSortArray(java.lang.String[] inp)
           
static java.lang.String getSpaces(java.lang.Integer num)
          
Purpose: get a string with the number of specified spaces
static java.util.List getTokenList(java.lang.String text, java.lang.String delimiter)
          Purpose: Get tokens of text delimited by delimiter, delimiter will be considered as a whole string
Example: If delimiter is "->", text is "parent->child->grandChild", then the returned list will contains
3 items, "parent" "child" "grandChild".
static java.lang.String getTrimmedUnquotedStr(java.lang.String str)
          
Purpose: This method takes a string trims it of leading and trailing spaces, non-breaking spaces, and tabs, and then strips one leading and/or trailing quotation mark(#34)--if they exist.
static boolean isCaseContainsMatch(java.lang.String source, java.lang.String target, boolean exactMatch)
          Purpose: evaluate 2 string values to see if they match.
static java.lang.String leftTrimSpace(java.lang.String text)
          Trims all chars below char (int) 32 EXCEPT for TAB char (int) 9 from the left side of the provided text string.
static boolean matchRegex(java.lang.String expression, java.lang.String value)
          
Purpose: match a regular expression to a value.
static java.lang.StringBuffer readBinaryFile(java.lang.String filename)
          
Purpose: read Binary file based on 'filename', returns String
static java.util.Collection readEncodingfile(java.lang.String filename, java.lang.String encoding)
          read file based on 'filename', returns collection of lines with no CR or LF.
static java.util.Collection readfile(java.lang.String filename)
          read file based on 'filename', returns collection of lines with no CR or LF.
static java.util.Collection readstring(java.lang.String str)
          read string, returns collection of lines with no CR or LF.
static java.util.Collection readUTF8file(java.lang.String filename)
          read file based on 'filename', returns collection of lines with no CR or LF.
static java.lang.String rightTrimSpace(java.lang.String text)
          Trims all chars below char (int) 32 EXCEPT for TAB characters (int) 9 from the right side of the provided text string.
static void writeEncodingfile(java.lang.String filename, java.util.Collection list, java.lang.String encoding)
          Note: The file will be opened as OutputStreamWriter with encoding write to file 'filename' the toString() values contained in list.
static void writefile(java.lang.String filename, java.util.Collection list)
          write to file 'filename' the toString() values contained in list.
static void writefile(java.lang.String filename, java.util.Collection list, java.lang.String delim)
          Deprecated. for DatabaseUtils.writefile(String, Collection, String)
static void writeUTF8file(java.lang.String filename, java.util.Collection list)
          Note: The file will be opened as UTF-8 OutputStreamWriter write to file 'filename' the toString() values contained in list.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REGEX_CHARACTER_ANY

public static final java.lang.String REGEX_CHARACTER_ANY
See Also:
Constant Field Values

REGEX_CHARACTER_MATCH_MANY

public static final java.lang.String REGEX_CHARACTER_MATCH_MANY
See Also:
Constant Field Values

REGEX_CHARACTER_MATCH_ONE

public static final java.lang.String REGEX_CHARACTER_MATCH_ONE
See Also:
Constant Field Values

maxBytesPerRead

public static final int maxBytesPerRead
See Also:
Constant Field Values

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
See Also:
Constant Field Values

specialKeys

public static final java.lang.String[] specialKeys
Constructor Detail

StringUtils

public StringUtils()
Method Detail

findMatchIndex

public static int findMatchIndex(java.util.ListIterator iter,
                                 java.lang.String match)

Purpose: find a match based on the parameter by walking the list, finding a string which contains the matching substring, and returning the index into the list if it matched.
Side Effects: The iterator will be moved to the appropriate item (one past), therefore, the value of the item can also be fetched with iter.previous()
Assumptions: match is not null; All elements on the supporting List are of type String; first match will be considered the match, subsequent matches would therefore be ignored.

Parameters:
iter, - ListIterator
match, - String
Returns:
index if match, -1 if no match

findExactMatchIndex

public static int findExactMatchIndex(java.util.ListIterator iter,
                                      java.lang.String match)

Purpose: find an exact match based on the parameter by walking the list, finding a string which equals the matching string, and returning the index into the list if it matched.
Side Effects: The iterator will be moved to the appropriate item (one past), therefore, the value of the item can also be fetched with iter.previous()
Assumptions: match is not null; All elements on the supporting List are of type String; first match will be considered the match, subsequent matches would therefore be ignored.

Parameters:
iter, - ListIterator
match, - String
Returns:
index if match, -1 if no match

isCaseContainsMatch

public static boolean isCaseContainsMatch(java.lang.String source,
                                          java.lang.String target,
                                          boolean exactMatch)
Purpose: evaluate 2 string values to see if they match. If exactMatch is true, then the 2 strings must match exactly. If exactMatch is false, then we will not only ignore case, but the source string can merely be a substring appearing anywhere in the target.

Parameters:
source, - String to match against target. Can be a case-insensitive substring of target if exactMatch is false.

target, - String to compare with source.

exactMatch, - false allows a case-insensitive comparison for a source substring in target. Otherwise, the source and target must match exactly.

Returns:
true for a match as described above or if *both* source and target are null. false if no match as described above or if either source or target are null.

getSpaces

public static java.lang.String getSpaces(java.lang.Integer num)

Purpose: get a string with the number of specified spaces

Parameters:
num - Integer
Returns:
String with spaces

getChars

public static java.lang.String getChars(java.lang.Integer num,
                                        java.lang.String chars)

Purpose: get a string with the number of specified sequenced 'chars'

Parameters:
num - Integer
chars - String
Returns:
String with spaces

readfile

public static java.util.Collection readfile(java.lang.String filename)
                                     throws java.io.IOException
read file based on 'filename', returns collection of lines with no CR or LF. The file is assumed to be in the default System character encoding.

Parameters:
filename, - String
Returns:
Collection (instance of type ArrayList)
Throws:
java.io.IOException
See Also:
FileUtilities.getSystemBufferedFileReader(String), readBuffer(BufferedReader)

readUTF8file

public static java.util.Collection readUTF8file(java.lang.String filename)
                                         throws java.io.IOException
read file based on 'filename', returns collection of lines with no CR or LF. The file is assumed to be using UTF-8 character encoding.

Parameters:
filename, - String
Returns:
Collection (instance of type ArrayList)
Throws:
java.io.IOException
See Also:
FileUtilities.getUTF8BufferedFileReader(String), readBuffer(BufferedReader)

readEncodingfile

public static java.util.Collection readEncodingfile(java.lang.String filename,
                                                    java.lang.String encoding)
                                             throws java.io.IOException
read file based on 'filename', returns collection of lines with no CR or LF. The file is assumed to be using character encoding indicated by parameter.

Parameters:
filename, - String
encoding, - the character encoding used to read a file.
Returns:
Collection (instance of type ArrayList)
Throws:
java.io.IOException
See Also:
FileUtilities.getUTF8BufferedFileReader(String), readBuffer(BufferedReader)

readstring

public static java.util.Collection readstring(java.lang.String str)
                                       throws java.io.IOException
read string, returns collection of lines with no CR or LF. This essentially splits the String at line separators into an array of Strings.

Parameters:
str, - String,
Returns:
Collection (instance of type ArrayList)
Throws:
java.io.IOException
See Also:
StringReader, BufferedReader.readLine()

readBinaryFile

public static java.lang.StringBuffer readBinaryFile(java.lang.String filename)
                                             throws java.io.IOException

Purpose: read Binary file based on 'filename', returns String

Parameters:
filename, - String
Returns:
String NOTE: I believe that there is a bug in the java class(es) BufferedReader/FileReader when reading certain characters from a binary file. Therefore, the implementation here is with the older BufferedInputStream/FileInputStream classes, which, through exhaustive testing, I find to do a better job of reading in binary characters.
Throws:
java.io.IOException

writefile

public static void writefile(java.lang.String filename,
                             java.util.Collection list)
                      throws java.io.IOException
write to file 'filename' the toString() values contained in list. Each item is written as a separate line using '\n' as the line separator regardless of operating system. Values are written in the system default character encoding.

Parameters:
filename - String full absolute path filename of file to write.
list - Collection of lines to write.
Throws:
java.io.FileNotFoundException - if file cannot be created for any reason.
java.io.IOException - if an error occurs during write operations.
See Also:
FileUtilities.getSystemBufferedFileWriter(String), FileUtilities.writeCollectionToFile(BufferedWriter, Collection, String)

writeUTF8file

public static void writeUTF8file(java.lang.String filename,
                                 java.util.Collection list)
                          throws java.io.IOException
Note: The file will be opened as UTF-8 OutputStreamWriter write to file 'filename' the toString() values contained in list. Each item is written as a separate line using '\n' as the line separator regardless of operating system. Values are written in the UTF-8 encoding.

Parameters:
filename - String full absolute path filename of file to write.
list - Collection of lines to write.
Throws:
java.io.FileNotFoundException - if file cannot be created for any reason.
java.io.IOException - if an error occurs during write operations.
See Also:
FileUtilities.getSystemBufferedFileWriter(String), FileUtilities.writeCollectionToFile(BufferedWriter, Collection, String)

writeEncodingfile

public static void writeEncodingfile(java.lang.String filename,
                                     java.util.Collection list,
                                     java.lang.String encoding)
                              throws java.io.IOException
Note: The file will be opened as OutputStreamWriter with encoding write to file 'filename' the toString() values contained in list. Each item is written as a separate line using '\n' as the line separator regardless of operating system. Values are written in the encoding indicated by parameter

Parameters:
filename - String full absolute path filename of file to write.
list - Collection of lines to write.
encoding - Encoding is used to write a file.
Throws:
java.io.FileNotFoundException - if file cannot be created for any reason.
java.io.IOException - if an error occurs during write operations.
See Also:
FileUtilities.getSystemBufferedFileWriter(String), FileUtilities.writeCollectionToFile(BufferedWriter, Collection, String)

writefile

public static void writefile(java.lang.String filename,
                             java.util.Collection list,
                             java.lang.String delim)
                      throws java.io.IOException
Deprecated. for DatabaseUtils.writefile(String, Collection, String)

Throws:
java.io.IOException
See Also:
DatabaseUtils.writefile(String, Collection, String)

getDBVal

public static java.lang.String getDBVal(java.lang.Object m)
Deprecated. for DatabaseUtils.getDBVal(Object)

See Also:
DatabaseUtils.getDBVal(Object)

leftTrimSpace

public static java.lang.String leftTrimSpace(java.lang.String text)
Trims all chars below char (int) 32 EXCEPT for TAB char (int) 9 from the left side of the provided text string. Does not modify the input text string.

Returns:
a new trimmed string if changes were made, or the text string passed as input if no changes were made, or null if the provided text was null.

rightTrimSpace

public static java.lang.String rightTrimSpace(java.lang.String text)
Trims all chars below char (int) 32 EXCEPT for TAB characters (int) 9 from the right side of the provided text string. Does not modify the input text string.

Returns:
a new trimmed string if changes were made, or the text string passed as input if no changes were made, or null if the provided text was null.

getTrimmedUnquotedStr

public static java.lang.String getTrimmedUnquotedStr(java.lang.String str)

Purpose: This method takes a string trims it of leading and trailing spaces, non-breaking spaces, and tabs, and then strips one leading and/or trailing quotation mark(#34)--if they exist.

Parameters:
str, - String to unquote
Returns:
String result, null if str was null

concat

public static java.lang.String concat(java.lang.String s1,
                                      java.lang.String s2,
                                      java.lang.String s3)

Purpose: concatenate strings

Parameters:
s1, - String
s2, - String
s3, - String
Returns:
s1+s2+s3

convertWildcardsToRegularExpression

public static java.lang.String convertWildcardsToRegularExpression(java.lang.String input)
convert ? and * wildcarding to regular expression wildcarding .? and .*


matchRegex

public static boolean matchRegex(java.lang.String expression,
                                 java.lang.String value)
                          throws SAFSException

Purpose: match a regular expression to a value.
Assumptions: tries first jdk 1.4 built in java.util.regex.Pattern class, then the jakarta org.apache.regexp.RE class. If either are found, it tries to keep static methods and/or instances around so that reflection is minimized the next time around.

Parameters:
expression, - String
value, - String
Returns:
boolean, true if match, false otherwise
Throws:
SAFSRegExException - if an error occurs.
SAFSRegExNotFoundException - if no RegEx support class can be found.
SAFSException

getSortArray

public static java.lang.String[] getSortArray(java.lang.String[] inp)

getInputToken

public static java.lang.String getInputToken(java.lang.String input,
                                             int n,
                                             java.lang.String sep)
                                      throws SAFSNullPointerException

Purpose: get 0-based field N of input by tokenizing with sep. We use a SAFSStringTokenizer. The first field is field #0.

Parameters:
input, - String
n, - int 0-based index
sep, - String
Returns:
token
Throws:
SAFSNullPointerException - if input or sep are null
java.lang.StringIndexOutOfBoundsException - if not found for index n

getFieldCount

public static int getFieldCount(java.lang.String input,
                                int startindex,
                                java.lang.String seps)
                         throws SAFSNullPointerException

Purpose: Finds the count of all fields within the inputRecord found from startindex to the end of the inputRecord.

Parameters:
input, - String
startindex, - int
seps, - String
Returns:
count
Throws:
SAFSNullPointerException - if input or sep are null

getTokenList

public static java.util.List getTokenList(java.lang.String text,
                                          java.lang.String delimiter)
Purpose: Get tokens of text delimited by delimiter, delimiter will be considered as a whole string
Example: If delimiter is "->", text is "parent->child->grandChild", then the returned list will contains
3 items, "parent" "child" "grandChild".

Parameters:
text -
delimiter -
Returns:

getDelimitedString

public static java.lang.String getDelimitedString(java.util.List<java.lang.String> list,
                                                  java.lang.String delimiter)
Purpose: Get text strung delimited by delimiter from a list, delimiter will be considered as a whole string
Example: If delimiter is "->", if the list contains 3 items, "parent" "child" "grandChild"
the returned text is "parent->child->grandChild"

Parameters:
list -
delimiter -
Returns:

containsSepcialKeys

public static boolean containsSepcialKeys(java.lang.String value)

arrayToString

public static java.lang.String arrayToString(java.lang.String[] values,
                                             java.lang.String sep)