org.mailster.util
Class StringUtilities

java.lang.Object
  extended by org.mailster.util.StringUtilities

public class StringUtilities
extends java.lang.Object

---
Mailster (C) 2007-2009 De Oliveira Edouard

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

See  Mailster Web Site
---

StringUtilities.java - Various methods to handle strings.

Version:
$Revision: 1.7 $, $Date: 2008/12/06 13:57:17 $
Author:
Edouard De Oliveira

Field Summary
static java.lang.String EMPTY_STRING
          Constant for the empty String
 
Constructor Summary
StringUtilities()
           
 
Method Summary
private static void extractDirective(java.util.HashMap<java.lang.String,java.lang.String> map, java.lang.String key, java.lang.String value)
          Processes directive/value pairs from the digest-challenge and fill out the provided map.
static java.lang.String intToFixedLengthHex(int value, int length)
          Takes value and return its HEX value prefixed with zeros .
static boolean isEmpty(java.lang.String str)
          Tests if the given String is empty.
static boolean isLws(byte b)
          Is character a linear white space ?
static int occ(java.lang.String s, char c, int start, int length)
          Returns the occurence count of char c in a substring of the String s starting at position start and containing length chars.
static java.util.HashMap<java.lang.String,java.lang.String> parseDirectives(byte[] buf)
          Parses digest-challenge string, extracting each token and value(s)
static java.lang.String replace(java.lang.String text, java.lang.String repl, java.lang.String with)
          Replaces all occurrences of a String within another String.
static java.lang.String replace(java.lang.String text, java.lang.String repl, java.lang.String with, int max)
          Replaces a String with another String inside a larger String, for the first max values of the search String.
private static int skipLws(byte[] buf, int start)
          Skip all linear white spaces
static java.lang.String[] split(java.lang.String s)
          Split a String into an array of String[] using the space character as token.
static java.lang.String[] split(java.lang.String s, java.lang.String token)
          Split a String into an array of String[].
static java.lang.String stringTo_8859_1(java.lang.String str, boolean useUTF8)
          Used to convert username-value, passwd or realm to 8859_1 encoding if all chars in string are within the 8859_1 (Latin 1) encoding range.
static java.lang.String strip(java.lang.String str)
          Strips whitespace from the start and end of a String.
static java.lang.String strip(java.lang.String str, java.lang.String stripChars)
          Strips any of a set of characters from the start and end of a String.
static java.lang.String stripEnd(java.lang.String str, java.lang.String stripChars)
          Strips any of a set of characters from the end of a String.
static java.lang.String stripStart(java.lang.String str, java.lang.String stripChars)
          Strips any of a set of characters from the start of a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_STRING

public static final java.lang.String EMPTY_STRING
Constant for the empty String

See Also:
Constant Field Values
Constructor Detail

StringUtilities

public StringUtilities()
Method Detail

isEmpty

public static boolean isEmpty(java.lang.String str)
Tests if the given String is empty. A String object is considered empty if it is either null or its trimmed length is zero.

Parameters:
str - the String to test
Returns:
true if the given String is empty; false otherwise

split

public static java.lang.String[] split(java.lang.String s)
Split a String into an array of String[] using the space character as token.

Parameters:
s - the string to split
Returns:
the splitted string array

split

public static java.lang.String[] split(java.lang.String s,
                                       java.lang.String token)
Split a String into an array of String[].

Parameters:
s - the string to split
token - the character token
Returns:
the splitted string array

intToFixedLengthHex

public static java.lang.String intToFixedLengthHex(int value,
                                                   int length)
Takes value and return its HEX value prefixed with zeros .

Parameters:
value - the value to be converted
length - the length to pad to.
Returns:
a non-null String fixed width representing the HEX of value

stringTo_8859_1

public static java.lang.String stringTo_8859_1(java.lang.String str,
                                               boolean useUTF8)
                                        throws AuthException
Used to convert username-value, passwd or realm to 8859_1 encoding if all chars in string are within the 8859_1 (Latin 1) encoding range.

Parameters:
str - a non-null String
useUTF8 - a boolean value set to true if UT8 is in use.
Returns:
a non-null String containing the correct character encoding for username, paswd or realm.
Throws:
AuthException

occ

public static int occ(java.lang.String s,
                      char c,
                      int start,
                      int length)
Returns the occurence count of char c in a substring of the String s starting at position start and containing length chars.

Parameters:
s - the string to parse
c - the character to search for
Returns:
the occurence count

parseDirectives

public static java.util.HashMap<java.lang.String,java.lang.String> parseDirectives(byte[] buf)
                                                                            throws javax.security.sasl.SaslException
Parses digest-challenge string, extracting each token and value(s)

Parameters:
buf - A non-null digest-challenge string.
Throws:
java.io.UnsupportedEncodingException
javax.security.sasl.SaslException - if the String cannot be parsed according to RFC 2831

extractDirective

private static void extractDirective(java.util.HashMap<java.lang.String,java.lang.String> map,
                                     java.lang.String key,
                                     java.lang.String value)
                              throws javax.security.sasl.SaslException
Processes directive/value pairs from the digest-challenge and fill out the provided map.

Parameters:
key - A non-null String challenge token name.
value - A non-null String token value.
Throws:
javax.security.sasl.SaslException - if either the key or the value is null or if the key already has a value.

isLws

public static boolean isLws(byte b)
Is character a linear white space ? LWS = [CRLF] 1*( SP | HT ) Note that we're checking individual bytes instead of CRLF

Parameters:
b - the byte to check
Returns:
true if it's a linear white space

skipLws

private static int skipLws(byte[] buf,
                           int start)
Skip all linear white spaces


replace

public static java.lang.String replace(java.lang.String text,
                                       java.lang.String repl,
                                       java.lang.String with)

Replaces all occurrences of a String within another String.

A null reference passed to this method is a no-op.

 StringUtils.replace(null, *, *)        = null
 StringUtils.replace("", *, *)          = ""
 StringUtils.replace("any", null, *)    = "any"
 StringUtils.replace("any", *, null)    = "any"
 StringUtils.replace("any", "", *)      = "any"
 StringUtils.replace("aba", "a", null)  = "aba"
 StringUtils.replace("aba", "a", "")    = "b"
 StringUtils.replace("aba", "a", "z")   = "zbz"
 

Parameters:
text - text to search and replace in, may be null
repl - the String to search for, may be null
with - the String to replace with, may be null
Returns:
the text with any replacements processed, null if null String input
See Also:
replace(String text, String repl, String with, int max)

replace

public static java.lang.String replace(java.lang.String text,
                                       java.lang.String repl,
                                       java.lang.String with,
                                       int max)

Replaces a String with another String inside a larger String, for the first max values of the search String.

A null reference passed to this method is a no-op.

 StringUtils.replace(null, *, *, *)         = null
 StringUtils.replace("", *, *, *)           = ""
 StringUtils.replace("any", null, *, *)     = "any"
 StringUtils.replace("any", *, null, *)     = "any"
 StringUtils.replace("any", "", *, *)       = "any"
 StringUtils.replace("any", *, *, 0)        = "any"
 StringUtils.replace("abaa", "a", null, -1) = "abaa"
 StringUtils.replace("abaa", "a", "", -1)   = "b"
 StringUtils.replace("abaa", "a", "z", 0)   = "abaa"
 StringUtils.replace("abaa", "a", "z", 1)   = "zbaa"
 StringUtils.replace("abaa", "a", "z", 2)   = "zbza"
 StringUtils.replace("abaa", "a", "z", -1)  = "zbzz"
 

Parameters:
text - text to search and replace in, may be null
repl - the String to search for, may be null
with - the String to replace with, may be null
max - maximum number of values to replace, or -1 if no maximum
Returns:
the text with any replacements processed, null if null String input

strip

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

Strips whitespace from the start and end of a String.

This is similar to String.trim() but removes whitespace. Whitespace is defined by Character.isWhitespace(char).

A null input String returns null.

 StringUtils.strip(null)     = null
 StringUtils.strip("")       = ""
 StringUtils.strip("   ")    = ""
 StringUtils.strip("abc")    = "abc"
 StringUtils.strip("  abc")  = "abc"
 StringUtils.strip("abc  ")  = "abc"
 StringUtils.strip(" abc ")  = "abc"
 StringUtils.strip(" ab c ") = "ab c"
 

Parameters:
str - the String to remove whitespace from, may be null
Returns:
the stripped String, null if null String input

strip

public static java.lang.String strip(java.lang.String str,
                                     java.lang.String stripChars)

Strips any of a set of characters from the start and end of a String. This is similar to String.trim() but allows the characters to be stripped to be controlled.

A null input String returns null. An empty string ("") input returns the empty string.

If the stripChars String is null, whitespace is stripped as defined by Character.isWhitespace(char). Alternatively use strip(String).

 StringUtils.strip(null, *)          = null
 StringUtils.strip("", *)            = ""
 StringUtils.strip("abc", null)      = "abc"
 StringUtils.strip("  abc", null)    = "abc"
 StringUtils.strip("abc  ", null)    = "abc"
 StringUtils.strip(" abc ", null)    = "abc"
 StringUtils.strip("  abcyx", "xyz") = "  abc"
 

Parameters:
str - the String to remove characters from, may be null
stripChars - the characters to remove, null treated as whitespace
Returns:
the stripped String, null if null String input

stripStart

public static java.lang.String stripStart(java.lang.String str,
                                          java.lang.String stripChars)

Strips any of a set of characters from the start of a String.

A null input String returns null. An empty string ("") input returns the empty string.

If the stripChars String is null, whitespace is stripped as defined by Character.isWhitespace(char).

 StringUtils.stripStart(null, *)          = null
 StringUtils.stripStart("", *)            = ""
 StringUtils.stripStart("abc", "")        = "abc"
 StringUtils.stripStart("abc", null)      = "abc"
 StringUtils.stripStart("  abc", null)    = "abc"
 StringUtils.stripStart("abc  ", null)    = "abc  "
 StringUtils.stripStart(" abc ", null)    = "abc "
 StringUtils.stripStart("yxabc  ", "xyz") = "abc  "
 

Parameters:
str - the String to remove characters from, may be null
stripChars - the characters to remove, null treated as whitespace
Returns:
the stripped String, null if null String input

stripEnd

public static java.lang.String stripEnd(java.lang.String str,
                                        java.lang.String stripChars)

Strips any of a set of characters from the end of a String.

A null input String returns null. An empty string ("") input returns the empty string.

If the stripChars String is null, whitespace is stripped as defined by Character.isWhitespace(char).

 StringUtils.stripEnd(null, *)          = null
 StringUtils.stripEnd("", *)            = ""
 StringUtils.stripEnd("abc", "")        = "abc"
 StringUtils.stripEnd("abc", null)      = "abc"
 StringUtils.stripEnd("  abc", null)    = "  abc"
 StringUtils.stripEnd("abc  ", null)    = "abc"
 StringUtils.stripEnd(" abc ", null)    = " abc"
 StringUtils.stripEnd("  abcyx", "xyz") = "  abc"
 

Parameters:
str - the String to remove characters from, may be null
stripChars - the characters to remove, null treated as whitespace
Returns:
the stripped String, null if null String input