|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--jcs.util.StringUtil
A class with miscellaneous methods:
doCommandArgs: Validates an array of command-line arguments and tags, and merges the arguments into an array of defaults. validateArg: Checks a keyword arg against permissible values. markupString: Returns the argument string with XML markup replaced with entity symbols (such as "<" for "<"). isNothing: Determines if a string is null or whitespace. sqlSubstring: A substring method that uses the SQL rules for out-of-range args. stackTrace2String: Returns a String with the exception stack trace. stream2String: Returns the argumentInputStream contents as a String. string2Stream: Returns an InputStream on the argument String. multiply: Returns a given String replicated a given number of times. genString: Returns a given string replicated to a given length.
Method Summary | |
static java.lang.String |
deEntityize(java.lang.String val)
Translates a String containing the five special XML entities (>, <, &, ' and "). |
static void |
doCommandArgs(java.lang.String[] args,
java.lang.String[] argTags,
java.lang.String[] argVals)
Checks the command line arguments and merges them with default values for any omitted arguments. |
static java.lang.String |
entityize(java.lang.String val)
Translates a String containing XML markup characters, (>, <, &, ', and ") to a String with the corresponding entities (>, <, &, ', and "). |
static java.lang.String |
genString(int len,
java.lang.String seed)
Generates a string of given length (mainly for test generation). |
static java.lang.String |
genString(java.lang.String seed,
int len)
Generates a string of given length (mainly for test generation). |
static boolean |
isNothing(java.lang.String val)
Tests whether the argument is null, empty, or all whitespace. |
static java.lang.String |
multiply(int times,
java.lang.String seed)
Repeats a given string a given number of times (mainly for test generation). |
static java.lang.String |
multiply(java.lang.String seed,
int times)
Repeats a given string a given number of times (mainly for test generation). |
static java.lang.String |
sqlSubstring(java.lang.String C,
int startArg,
int lengthArg)
Returns a substring of the given string. |
static java.lang.String |
stackTrace2String(java.lang.Exception e)
Returns a String with the stack trace of the given Exception. This is an alternative to using e.printStackTrace() ,
for cases where you want to save the exception info rather than
print it. |
static java.lang.String |
stream2String(java.io.InputStream iStream)
Returns the contents of the argument InputStream as a String. |
static java.io.InputStream |
string2Stream(java.lang.String iString)
Returns the stream on the argument string. |
static java.lang.String |
validateArg(java.lang.String arg,
java.lang.String permissible,
java.lang.String message)
Verifies that the arg is in the permissible values. |
Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
Method Detail |
public static void doCommandArgs(java.lang.String[] args, java.lang.String[] argTags, java.lang.String[] argVals) throws java.lang.Exception
Example Java code:
public static void main (String args[]) { String[] argTags = {"-I", "-O", "-ROW", "-CODE", "-NAMES", "-UPDATE"}; String[] argVals = {"", "", "0", "0", "", "no"}; StringUtil.doCommandArgs(args, argTags, argVals);
args
array is the command line String[]
.
argTags
are the permissible "tags" for command line
arguments.
argVals
is an array of the default values for the
command line arguments, in the same order as the argTags
.
doCommandArgs
method merges the supplied values
in the args
array into the default values in the argVals
array, using the argTags
array to determine the permissible tags
and the order of arguments in argVals
.
For example, assume that the above main
method is in a class named
Sample
.
The following shows a series of calls of Sample
, followed by
the values that would be in argVals
after the call
StringUtil.doCommandArgs(args, argTags, argVals)
.
Call: java Sample -ROW 7 -NAMES yes -I /usr/u/smith/input.txt Resulting argVals: {"/usr/u/smith/input.txt", "", "7", "0", "yes", "no"} Call: java Sample -O "result.txt" -UPDATE no -CODE "K 7" Resulting argVals: {"", "result.txt", "0", "K 7", "", "no"}
args
may be specified in any order.
doCommandArgs
method does no folding of tags or parameters,
and no checking of parameter values or combinations.
args
- the string array of user-supplied arguments,
as provided in command-line invocation of
main
argTags
- an array of the legal argument tags,
e.g. "-F". (case sensitive)argVals
- an array of the default values of the arguments
(or empty strings for arguments with no default)argTags
, or the argTags
and argVals
arrays are not the same length.public static boolean isNothing(java.lang.String val)
val
- a string to be tested for vacuityval
public static java.lang.String validateArg(java.lang.String arg, java.lang.String permissible, java.lang.String message) throws java.lang.IllegalArgumentException
arg
is in the permissible
values.
The "permissible" string contains the permissible keyword values.
Example Java code:
validateArg("yes", "yes,no,maybe", "Arg must be yse, no, or maybe")
arg
- a string with the value to be validatedpermissible
- a string with the valid values. The method
simply checks whether the index of arg
in permissible
is -1.message
- a string that will be included in the exception messagearg
is not in the permissible
valuespublic static java.lang.String sqlSubstring(java.lang.String C, int startArg, int lengthArg) throws java.lang.Exception
sqlSubstring("abc", S, L)
This method is a literal transcription of the rules in the SQL standard, and the variable names are the symbols used in the SQL standard (in caps).
C
- a string that is the source for the substring valuesstartArg
- a string that is the initial position of the substringlengthArg
- an int that is the length of the substringpublic static java.lang.String stackTrace2String(java.lang.Exception e)
e.printStackTrace()
,
for cases where you want to save the exception info rather than
print it.e
- an exception whose stack trace is to be returnedpublic static java.lang.String entityize(java.lang.String val)
Call | Result |
---|---|
entityize("A>B && C<D") | "A>B && C<D" |
deEntityize("A>B && C<D") | "A>B && C<D" |
val
- a string that is to be examined for XML markupval
with XML
markup characters suitably substitutuedpublic static java.lang.String deEntityize(java.lang.String val) throws java.lang.Exception
Call | Result |
---|---|
entityize("A>B && C<D") | "A>B && C<D" |
deEntityize("A>B && C<D") | "A>B && C<D" |
val
- a string that is to be examined for XML entities.val
with XML
entity characters suitably substitutedpublic static java.lang.String stream2String(java.io.InputStream iStream) throws java.lang.Exception
iStream
- an InputStream to be returned as a Stringpublic static java.io.InputStream string2Stream(java.lang.String iString) throws java.lang.Exception
iString
- a String to be returned as a streampublic static java.lang.String multiply(int times, java.lang.String seed)
times
- the number of replicationsseed
- the string that will be replicated to generate
the returned stringpublic static java.lang.String multiply(java.lang.String seed, int times)
multiply(String, int)
is simply
multiply(int, String)
, for convenience.
public static java.lang.String genString(int len, java.lang.String seed)
len
- the length of the returned stringseed
- the string that will be replicated to generate
the returned stringpublic static java.lang.String genString(java.lang.String seed, int len)
genString(String, int)
is simply
genString(int, String)
, for convenience.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |