org.lambico.test.spring
Class Utils

java.lang.Object
  extended by org.lambico.test.spring.Utils

public class Utils
extends java.lang.Object

Some utility methods.

Author:
Paolo Dona paolo.dona@seesaw.it, Michele Franzin paolo.dona@seesaw.it, Andrea Nasato

Constructor Summary
protected Utils()
          A default protected constructor.
 
Method Summary
static java.util.List<java.lang.String> convertToNameValueList(java.util.Map map)
          Converts a map in a list of name/value pairs.
static java.util.List<java.lang.String> convertToNameValueList(java.util.Map map, boolean urlEncode)
          Converts a map in a list of name/value pairs.
static boolean hasUTF8preamble(byte[] b)
          Checks if the array starts with the UTF-8 preamble.
static boolean hasUTF8preamble(java.lang.String s)
          Checks if the string starts with the UTF-8 preamble.
static byte[] loadBinary(org.springframework.core.io.Resource classpathResource)
          Loads a binary file from the classpath.
static byte[] loadBinary(java.lang.String classpathResource)
          Loads a binary file from the classpath.
static java.lang.String loadString(org.springframework.core.io.Resource classpathResource)
          Loads a text file from the classpath.
static java.lang.String loadString(java.lang.String classpathResource)
          Loads a text file from the classpath.
static byte[] stripUTF8preamble(byte[] b)
          Strips the UTF8 preamble, if present.
static java.lang.String stripUTF8preamble(java.lang.String s)
          Strips the UTF8 preamble, if present.
static java.lang.String[] uncamelize(java.lang.String camelString)
          Return an array containing all the substrings of camelString, according to this rule: divide camelString into tokens with every token starting from a capital letter to another.
protected static byte[] unsafeLoadBinary(java.io.InputStream stream)
          Loads a binary file from a stream.
protected static java.lang.String unsafeLoadString(java.io.InputStream stream)
          Loads a text file from a stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Utils

protected Utils()
A default protected constructor.

Method Detail

convertToNameValueList

public static java.util.List<java.lang.String> convertToNameValueList(java.util.Map map)
Converts a map in a list of name/value pairs.

Parameters:
map - The map.
Returns:
A list of name/value pairs (name=value).

convertToNameValueList

public static java.util.List<java.lang.String> convertToNameValueList(java.util.Map map,
                                                                      boolean urlEncode)
Converts a map in a list of name/value pairs.

Parameters:
map - The map.
urlEncode - if true, encode the values as URLs.
Returns:
A list of name/value pairs (name=value).

loadBinary

public static byte[] loadBinary(java.lang.String classpathResource)
Loads a binary file from the classpath.

Parameters:
classpathResource - The resource path.
Returns:
The content of the classpath resource.

loadBinary

public static byte[] loadBinary(org.springframework.core.io.Resource classpathResource)
Loads a binary file from the classpath.

Parameters:
classpathResource - The resource path.
Returns:
The content of the classpath resource.

unsafeLoadBinary

protected static byte[] unsafeLoadBinary(java.io.InputStream stream)
                                  throws java.io.IOException
Loads a binary file from a stream.

Parameters:
stream - The stream from where to load. It is open, and it won't be closed by this method.
Returns:
The content of the classpath resource.
Throws:
java.io.IOException - In case of error.

loadString

public static java.lang.String loadString(java.lang.String classpathResource)
Loads a text file from the classpath. NB: UTF-8 header are removed!

Parameters:
classpathResource - The resource path.
Returns:
The content of the classpath resource.

loadString

public static java.lang.String loadString(org.springframework.core.io.Resource classpathResource)
Loads a text file from the classpath. NB: UTF-8 header are removed!

Parameters:
classpathResource - The resource path.
Returns:
The content of the classpath resource.

unsafeLoadString

protected static java.lang.String unsafeLoadString(java.io.InputStream stream)
                                            throws java.io.IOException
Loads a text file from a stream.

Parameters:
stream - The stream from where to load. It is open, and it won't be closed by this method.
Returns:
The content of the classpath resource.
Throws:
java.io.IOException - In case of error.

stripUTF8preamble

public static java.lang.String stripUTF8preamble(java.lang.String s)
                                          throws java.io.UnsupportedEncodingException
Strips the UTF8 preamble, if present. NB: the conversion to byte array, and a following strip won't work. The reason is that: new String(UTF8_PREAMBLE).getBytes() != UTF8_PREAMBLE; Here an explanation.

Parameters:
s - The string to strip.
Returns:
The string without the UTF8 preamble.
Throws:
java.io.UnsupportedEncodingException - If the UTF-8 encoding is not supported.

stripUTF8preamble

public static byte[] stripUTF8preamble(byte[] b)
Strips the UTF8 preamble, if present.

Parameters:
b - The array of bytes (characters).
Returns:
The stripped array.

hasUTF8preamble

public static boolean hasUTF8preamble(byte[] b)
Checks if the array starts with the UTF-8 preamble.

Parameters:
b - The array to inspect.
Returns:
true if the array starts with the UTF-8 preamble.

hasUTF8preamble

public static boolean hasUTF8preamble(java.lang.String s)
Checks if the string starts with the UTF-8 preamble.

Parameters:
s - The string to inspect.
Returns:
true if the string starts with the UTF-8 preamble.

uncamelize

public static java.lang.String[] uncamelize(java.lang.String camelString)
Return an array containing all the substrings of camelString, according to this rule: divide camelString into tokens with every token starting from a capital letter to another. Some examples:
  1. 'MyCamelString' gives: {my,camel,string}
  2. 'myFantasticCamelString' gives: {my,fantastic,camel,string}
  3. 'MYCAMELSTRING' gives: {m,y,c,a,m,e,l,s,t,r,i,n,g}

Parameters:
camelString - The string to uncamelize.
Returns:
The string uncamelized.


Copyright © 2009-2010 Lambico Team. All Rights Reserved.