Purple Code

com.purpletech.util
Class Utils

java.lang.Object
  |
  +--com.purpletech.util.Utils

public class Utils
extends java.lang.Object


Nested Class Summary
static class Utils.ExecInfo
          Class encapsulating information from an exec call -- slightly easier than the standard API
 
Constructor Summary
Utils()
           
 
Method Summary
static java.lang.String abbreviate(java.lang.String s, int max)
          Turn "Now is the time for all good men" into "Now is the time for..."
static java.lang.String capitalize(java.lang.String s)
          capitalize the first character of s
static java.lang.String commaList(java.util.Collection c)
          Deprecated. use org.apache.commons.lang.StringUtils.join()
static java.lang.String commaList(java.util.Iterator i)
          Deprecated. use org.apache.commons.lang.StringUtils.join()
static java.lang.String commaList(java.lang.Object[] a)
          Deprecated. use org.apache.commons.lang.StringUtils.join()
static int count(java.lang.String s, char ch)
          Deprecated. use org.apache.commons.lang.StringUtils.countMatches instead
static Utils.ExecInfo exec(java.lang.String command, java.lang.String input, long sleep)
          Wrapper for Runtime.exec.
static int getMaxLength(java.util.Iterator i)
          calculate the maximum length of all strings in i.
static java.lang.String getStackTrace(java.lang.Throwable t)
           
static java.lang.String htmlescape(java.lang.String s1)
          Turns funky characters into HTML entity equivalents
static java.lang.String htmlunescape(java.lang.String s1)
          Given a string containing entity escapes, returns a string containing the actual Unicode characters corresponding to the escapes.
static java.lang.String indent(int indent)
           
static boolean isWhitespace(java.lang.String s)
           
static java.lang.String javaEscape(java.lang.String source)
           
static void javaEscape(java.lang.String source, java.io.Writer out)
          Prepares a string for output inside a Java string, Example:
static java.lang.String jsEscape(java.lang.String source)
          Prepares a string for output inside a JavaScript string, e.g.
static void jsEscape(java.lang.String source, java.io.Writer out)
          Prepares a string for output inside a JavaScript string, e.g.
static java.lang.String lowerize(java.lang.String s)
          lowercase the first character of s
static java.lang.String ltrim(java.lang.String orig)
           Trim the left spacing off of a String.
static boolean ok(java.lang.String s)
           
static java.lang.String pad(java.lang.String s, int length)
          pad or truncate
static java.lang.String pluralize(java.lang.String s)
          turn String s into a plural noun (doing the right thing with "story" -> "stories" and "mess" -> "messes")
static void printCommaList(java.io.PrintWriter out, java.util.Iterator i)
          Given an iterator, prints it as a comma-delimited list (actually a comma-and-space delimited list).
static void printIndent(java.io.PrintWriter out, int indent)
           
static java.lang.String replace(java.lang.String source, java.lang.String target, java.lang.String replacement)
          Replace all occurences of target inside source with replacement.
static java.lang.String rtrim(java.lang.String orig)
           Trim the whitespace off the right side of a String.
static void sleep(long msec)
           
static java.lang.String strdiff(java.lang.String s1, java.lang.String s2)
          Compare two strings, and return the portion where they differ.
static int strdiffat(java.lang.String s1, java.lang.String s2)
          Compare two strings, and return the index at which the strings begin to diverge
static java.lang.String strdiffVerbose(java.lang.String expected, java.lang.String actual)
          Compare two strings, and return a verbose description of how they differ.
static java.lang.String stripWhitespace(java.lang.String s)
          Deprecated. use org.apache.commons.lang.StringUtils deleteSpaces instead
static java.lang.String toUnderscore(java.lang.String s)
          Converts camelCaseVersusC to camel_case_versus_c
static java.lang.String uncurlQuotes(java.lang.String input)
          Filter out Windows and Mac curly quotes, replacing them with the non-curly versions.
static java.lang.String zerofill(int x, int desiredWidth)
          fills the left side of a number with zeros
e.g.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Utils

public Utils()
Method Detail

zerofill

public static java.lang.String zerofill(int x,
                                        int desiredWidth)
fills the left side of a number with zeros
e.g. zerofill(14, 3) -> "014"
e.g. zerofill(187, 6) -> "000014"
e.g. zerofill(-33, 4) -> "-033"


printIndent

public static void printIndent(java.io.PrintWriter out,
                               int indent)

indent

public static java.lang.String indent(int indent)

commaList

public static java.lang.String commaList(java.lang.Object[] a)
Deprecated. use org.apache.commons.lang.StringUtils.join()


commaList

public static java.lang.String commaList(java.util.Collection c)
Deprecated. use org.apache.commons.lang.StringUtils.join()


commaList

public static java.lang.String commaList(java.util.Iterator i)
Deprecated. use org.apache.commons.lang.StringUtils.join()


printCommaList

public static void printCommaList(java.io.PrintWriter out,
                                  java.util.Iterator i)
Given an iterator, prints it as a comma-delimited list (actually a comma-and-space delimited list). E.g. If the iterator contains the strings { "my", "dog", "has fleas" } it will print "my, dog, has fleas".

Parameters:
out - the stream to write to
i - an iterator containing printable (toString) objects, e.g. strings

isWhitespace

public static boolean isWhitespace(java.lang.String s)
Returns:
true if all characters in the string are whitespace characters, or the string is empty

exec

public static Utils.ExecInfo exec(java.lang.String command,
                                  java.lang.String input,
                                  long sleep)
                           throws java.io.IOException
Wrapper for Runtime.exec. Takes input as a String. Times out after sleep msec. Returns an object containing exit value, standard output, and error output.

Parameters:
command - the command-line to execute
input - a string to pass to the process as standard input
sleep - msec to wait before terminating process (if <= 0, waits forever)
java.io.IOException

abbreviate

public static java.lang.String abbreviate(java.lang.String s,
                                          int max)
Turn "Now is the time for all good men" into "Now is the time for..."

Specifically:

If str is less than max characters long, return it. Else abbreviate it to (substring(str, 0, max-3) + "..."). If max is less than 3, throw an IllegalArgumentException. In no case will it return a string of length greater than max.

Parameters:
max - maximum length of result string

pad

public static java.lang.String pad(java.lang.String s,
                                   int length)
pad or truncate


strdiff

public static java.lang.String strdiff(java.lang.String s1,
                                       java.lang.String s2)
Compare two strings, and return the portion where they differ. (More precisely, return the remainder of the second string, starting from where it's different from the first.)

E.g. strdiff("i am a machine", "i am a robot") -> "robot"


strdiffat

public static int strdiffat(java.lang.String s1,
                            java.lang.String s2)
Compare two strings, and return the index at which the strings begin to diverge

E.g. strdiff("i am a machine", "i am a robot") -> 7

Returns:
-1 if they are the same

strdiffVerbose

public static java.lang.String strdiffVerbose(java.lang.String expected,
                                              java.lang.String actual)
Compare two strings, and return a verbose description of how they differ. Shows a window around the location to provide context. E.g. strdiffVerbose("i am a robot", "i am a machine") might return a string containing
 strings differ at character 7
 Expected: ...am a robot
   Actual: ...am a machine
This was developed in order to provide some sanity to JUnit's assertEquals routine.


count

public static int count(java.lang.String s,
                        char ch)
Deprecated. use org.apache.commons.lang.StringUtils.countMatches instead

count the number of occurences of ch inside s


replace

public static java.lang.String replace(java.lang.String source,
                                       java.lang.String target,
                                       java.lang.String replacement)
Replace all occurences of target inside source with replacement. E.g. replace("fee fie fo fum", "f", "gr") -> "gree grie gro grum"


rtrim

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

Trim the whitespace off the right side of a String.

Parameters:
orig - String to rtrim.
Returns:
String - orig with no right spaces

ltrim

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

Trim the left spacing off of a String.

Parameters:
orig - String to rtrim.
Returns:
String - orig with no left spaces

getMaxLength

public static int getMaxLength(java.util.Iterator i)
calculate the maximum length of all strings in i. If i contains other than strings, uses toString() value.


htmlescape

public static java.lang.String htmlescape(java.lang.String s1)
Turns funky characters into HTML entity equivalents

e.g. "bread" & "butter" => &quot;bread&quot; &amp; &quot;butter&quot;. Update: supports nearly all HTML entities, including funky accents. See the source code for more detail.

See Also:
htmlunescape(String)

htmlunescape

public static java.lang.String htmlunescape(java.lang.String s1)
Given a string containing entity escapes, returns a string containing the actual Unicode characters corresponding to the escapes. Note: nasty bug fixed by Helge Tesgaard (and, in parallel, by Alex, but Helge deserves major props for emailing me the fix). 15-Feb-2002 Another bug fixed by Sean Brown

See Also:
htmlescape(String)

jsEscape

public static java.lang.String jsEscape(java.lang.String source)
Prepares a string for output inside a JavaScript string, e.g. for use inside a document.write("") command. Example:
 input string: He didn't say, "Stop!"
 output string: He didn\'t say, \"Stop!\"
 
Deals with quotes and control-chars (tab, backslash, cr, ff, etc.) Bug: does not yet properly escape Unicode / high-bit characters.

See Also:
jsEscape(String, Writer)

javaEscape

public static java.lang.String javaEscape(java.lang.String source)
See Also:
javaEscape(String, Writer)

jsEscape

public static void jsEscape(java.lang.String source,
                            java.io.Writer out)
                     throws java.io.IOException
Prepares a string for output inside a JavaScript string, e.g. for use inside a document.write("") command. Example:
 input string: He didn't say, "stop!"
 output string: He didn\'t say, \"stop!\"
 
Deals with quotes and control-chars (tab, backslash, cr, ff, etc.)

java.io.IOException
See Also:
jsEscape(String)

javaEscape

public static void javaEscape(java.lang.String source,
                              java.io.Writer out)
                       throws java.io.IOException
Prepares a string for output inside a Java string, Example:
 input string: He didn't say, "stop!"
 output string: He didn't say, \"stop!\"
 
Deals with quotes and control-chars (tab, backslash, cr, ff, etc.)

java.io.IOException
See Also:
jsEscape(String,Writer)

uncurlQuotes

public static java.lang.String uncurlQuotes(java.lang.String input)
Filter out Windows and Mac curly quotes, replacing them with the non-curly versions. Note that this doesn't actually do any checking to verify the input codepage. Instead it just converts the more common code points used on the two platforms to their equivalent ASCII values. As such, this method should not be used on ISO-8859-1 input that includes high-bit-set characters, and some text which uses other codepoints may be rendered incorrectly.


capitalize

public static java.lang.String capitalize(java.lang.String s)
capitalize the first character of s


lowerize

public static java.lang.String lowerize(java.lang.String s)
lowercase the first character of s


pluralize

public static java.lang.String pluralize(java.lang.String s)
turn String s into a plural noun (doing the right thing with "story" -> "stories" and "mess" -> "messes")


ok

public static boolean ok(java.lang.String s)

toUnderscore

public static java.lang.String toUnderscore(java.lang.String s)
Converts camelCaseVersusC to camel_case_versus_c


stripWhitespace

public static java.lang.String stripWhitespace(java.lang.String s)
Deprecated. use org.apache.commons.lang.StringUtils deleteSpaces instead


getStackTrace

public static java.lang.String getStackTrace(java.lang.Throwable t)

sleep

public static void sleep(long msec)

Documentation generated March 5 2003