org.japano.util
Class Util

java.lang.Object
  extended by org.japano.util.Util

public class Util
extends java.lang.Object

Contains various static utility methods usefull for webapplications.

Version:
$Id: Util.java,v 1.27 2005/12/14 06:34:10 fforw Exp $ SourceForge.net Logo
Author:
Sven Helmberger ( sven dot helmberger at gmx dot de )

Nested Class Summary
static class Util.TEST
          Test methods for Util
 
Field Summary
static java.util.List ALLOWED_STRICT
           
 
Method Summary
static java.util.Map asMap(java.lang.Object[] keyValue)
          Returns an unmodifiable map containing the keys and values from the given Object array.
static java.security.SecureRandom createSecureRandom()
          Creates a new secure random number generator.
static void deleteRecursively(java.lang.String dir)
          Recursively deletes a directory and all sub directories and files.
static java.lang.Object deserialized(java.lang.String path)
          Deserializes an object from a file.
static java.lang.String dumpAsHTML(java.lang.Object o)
           
static java.lang.String encode(byte[] data)
           
static java.lang.String getDate(long stamp)
          Returns a textual representation of the given time stamp (Format: MM.dd.yyyy - HH:mm:ss.SSS).
static org.apache.log4j.Logger getLogger()
          Returns the utility class logger.
static java.lang.String getSHADigest(java.lang.String input)
          Returns the hex encoded SHA-digest for a given input.
static java.lang.String htmlEncode(java.lang.String text, boolean convertTags)
          Converts illegal Characters to HTML entities.
static boolean isAbsoluteURL(java.lang.String url)
           
static void main(java.lang.String[] args)
           
static void printThrowable(Buffer out, java.lang.Throwable t)
           
static java.lang.String quoteEscape(java.lang.String s)
          Makes the given String safe to be enclosed in quotes.
static java.lang.String randomWord(int len)
          Generates a secure random word with the given length.
static java.lang.String randomWord(int len, char[] alphabet)
          Generates a secure random word with the given length.
static java.util.Map readMap(java.io.InputStream is)
           
static java.util.Map readMap(java.lang.String path)
          Reads the map from the a given path.
static java.lang.String removeJavadocTags(java.lang.String text)
           
static java.lang.String replace(java.lang.String in, java.lang.String what, java.lang.String with)
           
static java.lang.String safeHTML(java.lang.String value, java.util.List allowedTags)
          Makes safe html out of a given html snippet.
static void serialize(java.lang.String path, java.lang.Object o)
          Serializes the given object into a file.
static void writeCollection(java.util.Collection c, java.lang.String path, java.lang.String comment)
          Writes a collection to a text file. the objects in the collection are seperated by a line separator.
static void writeMap(java.util.Map m, java.lang.String path, java.lang.String comment)
          Writes a Map to a text file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ALLOWED_STRICT

public static final java.util.List ALLOWED_STRICT
Method Detail

htmlEncode

public static java.lang.String htmlEncode(java.lang.String text,
                                          boolean convertTags)
Converts illegal Characters to HTML entities. The method tries to use named entities where possible. otherwise a numerical entity is used.

Parameters:
text - Text to encode
convertTags - if true, HTML tags are readable as text ( "<b>" becomes "&lt;b&gt;" ).
Returns:
encoded text

getSHADigest

public static java.lang.String getSHADigest(java.lang.String input)
Returns the hex encoded SHA-digest for a given input. This is usefull for secure password storage. Passwords are not stored as clear text but as their SHA-digested equivalent. If the password is entered it also encoded as SHA-digest. Then that digest is compared to the orginial digest. if they match, the password was correct.

Parameters:
input - Text input
Returns:
hex encoded SHA-digest

randomWord

public static java.lang.String randomWord(int len)
Generates a secure random word with the given length.

Parameters:
len - Amount of random characters to generate
Returns:
random Word containing letters and numbers.

randomWord

public static java.lang.String randomWord(int len,
                                          char[] alphabet)
Generates a secure random word with the given length.

Parameters:
len - Amount of random characters to generate
alphabet - Alphabet to generate from.
Returns:
random Word containing letters and numbers.

deleteRecursively

public static void deleteRecursively(java.lang.String dir)
Recursively deletes a directory and all sub directories and files.

Parameters:
dir - path of the directory to delete.

getLogger

public static org.apache.log4j.Logger getLogger()
Returns the utility class logger.

Returns:
log4j logger

getDate

public static java.lang.String getDate(long stamp)
Returns a textual representation of the given time stamp (Format: MM.dd.yyyy - HH:mm:ss.SSS).


safeHTML

public static java.lang.String safeHTML(java.lang.String value,
                                        java.util.List allowedTags)
Makes safe html out of a given html snippet.

Parameters:
allowedTags - string list of allowed tags.
value - HTML snippet
Returns:
safe HTML snippet

dumpAsHTML

public static java.lang.String dumpAsHTML(java.lang.Object o)

asMap

public static java.util.Map asMap(java.lang.Object[] keyValue)
                           throws java.lang.IllegalArgumentException
Returns an unmodifiable map containing the keys and values from the given Object array. The key at index n is mapped to the value at index n+1.

Throws:
java.lang.IllegalArgumentException - if the length of the given array is not an even number.

createSecureRandom

public static java.security.SecureRandom createSecureRandom()
Creates a new secure random number generator. The following secure random algorithm names are tried:


printThrowable

public static void printThrowable(Buffer out,
                                  java.lang.Throwable t)

isAbsoluteURL

public static boolean isAbsoluteURL(java.lang.String url)

replace

public static java.lang.String replace(java.lang.String in,
                                       java.lang.String what,
                                       java.lang.String with)

writeCollection

public static void writeCollection(java.util.Collection c,
                                   java.lang.String path,
                                   java.lang.String comment)
Writes a collection to a text file. the objects in the collection are seperated by a line separator. The comment is written with a leading '#'.


writeMap

public static void writeMap(java.util.Map m,
                            java.lang.String path,
                            java.lang.String comment)
Writes a Map to a text file. The textfile contains key value pairs seperated by a space. The comment is written with a leading '#'.


readMap

public static java.util.Map readMap(java.lang.String path)
Reads the map from the a given path. It expects space seperated key value pairs.


readMap

public static java.util.Map readMap(java.io.InputStream is)

deserialized

public static java.lang.Object deserialized(java.lang.String path)
Deserializes an object from a file.

Parameters:
path -
Returns:
deserialized object

serialize

public static void serialize(java.lang.String path,
                             java.lang.Object o)
Serializes the given object into a file.

Parameters:
path - full path of the file to write to
o - object to write

encode

public static java.lang.String encode(byte[] data)

quoteEscape

public static java.lang.String quoteEscape(java.lang.String s)
Makes the given String safe to be enclosed in quotes.


removeJavadocTags

public static java.lang.String removeJavadocTags(java.lang.String text)

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Throws:
java.lang.Exception