org.varienaja.util
Class StringUtil

java.lang.Object
  extended by org.varienaja.util.StringUtil

public final class StringUtil
extends java.lang.Object

Miscellaneous string utility methods. Copied from Subsonic.

Version:
$Id: StringUtil.java,v 1.1 2010/03/16 18:55:42 varienaja Exp $
Author:
Sindre Mehus

Field Summary
static java.lang.String ENCODING_LATIN
           
static java.lang.String ENCODING_UTF8
           
 
Method Summary
static java.lang.String capitalize(java.lang.String word)
          Changes the first letter into a capital.
static boolean contains(java.util.List<java.lang.String> items, java.lang.String query)
          Scans (case insensitive) if a String is present in the List of Strings.
static java.lang.String formatBytes(long byteCount)
          Converts a byte-count to a formatted string suitable for display to the user.
static java.util.List<java.lang.String> getIndividualWords(java.lang.String words)
          Parses the String of comma separated words and returns a list of the word strings.
static java.lang.String getMimeType(java.lang.String suffix)
          Returns the proper MIME type for the given suffix.
static java.lang.String getSuffix(java.lang.String s)
          Returns the suffix (the substring after the last dot) of the given string.
static boolean isEqual(java.lang.Object a, java.lang.Object b)
          Determines whether a is equal to b, taking null into account.
static java.util.Locale parseLocale(java.lang.String s)
          Parses a locale from the given string.
static java.lang.String[] readLines(java.io.InputStream in)
          Reads lines from the given input stream.
static java.lang.String removeSuffix(java.lang.String s)
          Removes the suffix (the substring after the last dot) of the given string.
static java.lang.String[] split(java.lang.String input)
          Splits the input string.
static java.lang.String toHtml(java.lang.String s)
          Returns the specified string converted to a format suitable for HTML.
static java.lang.String toHttpUrl(java.lang.String url, int port)
          Change protocol from "https" to "http" for the given URL.
static java.lang.String utf8HexDecode(java.lang.String s)
          Decodes the given string by using the hexadecimal representation of its UTF-8 bytes.
static java.lang.String utf8HexEncode(java.lang.String s)
          Encodes the given string by using the hexadecimal representation of its UTF-8 bytes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ENCODING_LATIN

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

ENCODING_UTF8

public static final java.lang.String ENCODING_UTF8
See Also:
Constant Field Values
Method Detail

toHtml

public static java.lang.String toHtml(java.lang.String s)
Returns the specified string converted to a format suitable for HTML. All single-quote, double-quote, greater-than, less-than and ampersand characters are replaces with their corresponding HTML Character Entity code.

Parameters:
s - the string to convert
Returns:
the converted string

getSuffix

public static java.lang.String getSuffix(java.lang.String s)
Returns the suffix (the substring after the last dot) of the given string. The dot is included in the returned suffix.

Parameters:
s - The string in question.
Returns:
The suffix, or an empty string if no suffix is found.

removeSuffix

public static java.lang.String removeSuffix(java.lang.String s)
Removes the suffix (the substring after the last dot) of the given string. The dot is also removed.

Parameters:
s - The string in question, e.g., "foo.mp3".
Returns:
The string without the suffix, e.g., "foo".

getMimeType

public static java.lang.String getMimeType(java.lang.String suffix)
Returns the proper MIME type for the given suffix.

Parameters:
suffix - The suffix, e.g., "mp3" or ".mp3".
Returns:
The corresponding MIME type, e.g., "audio/mpeg". If no MIME type is found, application/octet-stream is returned.

formatBytes

public static java.lang.String formatBytes(long byteCount)
Converts a byte-count to a formatted string suitable for display to the user. For instance: This method assumes that 1 KB is 1024 bytes.

Parameters:
byteCount - The number of bytes.
Returns:
The formatted string.

split

public static java.lang.String[] split(java.lang.String input)
Splits the input string. White space is interpreted as separator token. Double quotes are interpreted as grouping operator.
For instance, the input "u2 rem "greatest hits"" will return an array with three elements: {"u2", "rem", "greatest hits"}

Parameters:
input - The input string.
Returns:
Array of elements.

readLines

public static java.lang.String[] readLines(java.io.InputStream in)
                                    throws java.io.IOException
Reads lines from the given input stream. All lines are trimmed. Empty lines and lines starting with "#" are skipped. The input stream is always closed by this method.

Parameters:
in - The input stream to read from.
Returns:
Array of lines.
Throws:
java.io.IOException - If an I/O error occurs.

toHttpUrl

public static java.lang.String toHttpUrl(java.lang.String url,
                                         int port)
                                  throws java.net.MalformedURLException
Change protocol from "https" to "http" for the given URL. The port number is also changed, but not if the given URL is already "http".

Parameters:
url - The original URL.
port - The port number to use, for instance 443.
Returns:
The transformed URL.
Throws:
java.net.MalformedURLException - If the original URL is invalid.

isEqual

public static boolean isEqual(java.lang.Object a,
                              java.lang.Object b)
Determines whether a is equal to b, taking null into account.

Returns:
Whether a and b are equal, or both null.

parseLocale

public static java.util.Locale parseLocale(java.lang.String s)
Parses a locale from the given string.

Parameters:
s - The locale string. Should be formatted as per the documentation in Locale.toString().
Returns:
The locale.

utf8HexEncode

public static java.lang.String utf8HexEncode(java.lang.String s)
                                      throws java.lang.Exception
Encodes the given string by using the hexadecimal representation of its UTF-8 bytes.

Parameters:
s - The string to encode.
Returns:
The encoded string.
Throws:
java.lang.Exception - If an error occurs.

utf8HexDecode

public static java.lang.String utf8HexDecode(java.lang.String s)
                                      throws java.lang.Exception
Decodes the given string by using the hexadecimal representation of its UTF-8 bytes.

Parameters:
s - The string to decode.
Returns:
The decoded string.
Throws:
java.lang.Exception - If an error occurs.

getIndividualWords

public static java.util.List<java.lang.String> getIndividualWords(java.lang.String words)
Parses the String of comma separated words and returns a list of the word strings. The first character of each word is in uppercase. The resulting list contains each word at most one time.

Parameters:
words - The string of comma separated words.
Returns:
A list of strings of the words in the string.

capitalize

public static java.lang.String capitalize(java.lang.String word)
Changes the first letter into a capital.

Parameters:
word - The word to capitalize.
Returns:
The capitalized string.

contains

public static boolean contains(java.util.List<java.lang.String> items,
                               java.lang.String query)
Scans (case insensitive) if a String is present in the List of Strings.

Parameters:
items - The items
query - The query
Returns:
Whether the query is contained in the items.


Copyright © 2010 A.J.V.. All Rights Reserved.