be.destin.util
Class Util

java.lang.Object
  extended by be.destin.util.Util

public class Util
extends java.lang.Object

Functions for different small treatments (mainly starting from a String)

Author:
Christophe Dupriez

Constructor Summary
Util()
           
 
Method Summary
static java.lang.String escapeDot(java.lang.String buf)
          Escapes characters for safe labels in DOT language (GraphWiz.org)
static java.lang.String escapeHTMLEntities(java.lang.String buf)
          Escapes XML entities in a HTML-compatible way (i.e. does not escape entities that are already escaped).
static java.lang.String escapeJavaScript(java.lang.String buf)
          Escapes characters for safe JavaScript strings
static java.lang.String fileIdentificationNormalize(java.lang.String aValue)
          Normalize file names so they don't differ too much from one OS to another Windows "\" become "/" TODO: Other rules?
static java.lang.String firstUp(java.lang.String name)
          If a letter is the very first character, puts it in Upper case.
static java.lang.String fixColumns(java.lang.String aValue, int columns)
          Left align a String in a fixed number of columns for HTML output (  used as a filler)
static java.lang.String formatLong(long sortKey)
          Makes a long number sortable as a string of characters.
static java.lang.String glue(java.lang.String[] toGlue, java.lang.String delim)
          Glue an array of strings (lines) into one string
static java.util.LinkedHashSet<java.lang.String> hashStrings(java.lang.String[] anArray)
          Convert an array of String in a LinkedHashSet of String
static java.lang.String highlight(java.lang.String text, java.lang.String[] QUERIES)
          Highlight a text based on the words from a query.
static int magnitude(long num, int basis)
          Number of digits for a number written in the given basis
static void melt(java.lang.StringBuffer buf, java.lang.String toMelt)
          Ensures the last non alphanum character in buf is not repeated when adding toMelt For instance, to put a dot at the end of a sentence without possible repetition: met(title,". ") will add ". " but without repeating a dot which may already ends the current title.
static java.lang.String noAccent(java.lang.String input)
          Removes all accent over (under) letters
static java.lang.String noDoubleSpace(java.lang.String aValue)
          Removes double (and more) spaces within a String + spaces at beginning or end of the String.
static java.lang.String noHTMLLink(java.lang.String input)
          Removes HTML links from a String (...
static java.lang.String noLuceneJoker(java.lang.String input)
          Removes Lucene joker characters when doing an exact match search
static java.lang.String noPlural(java.lang.String word)
          Brings a word with more than 3 letters to its singular form.
static char noSingleAccent(char c)
          This (incorrect) version takes ONE character and removes its accents.
static java.lang.String noSingleAccent(java.lang.String input)
          Use noSingleAccent for each character in a String.
static java.lang.String[] prepareHighlight(java.lang.String query, boolean byPieces)
          Prepares an array of queried words to help later highlight of text displayed
static void printStack(java.lang.Exception e)
          Prints in System.out (Tomcat stdout logs) the current stack when an exception was thrown
static java.lang.String replaceAllNoRegExp(java.lang.String aInput, java.lang.String aOldPattern, java.lang.String aNewPattern)
          Equivalent to string.replaceAll but without regular expressions
static int searchBuffer(byte[] buffer, int len, byte[] pattern, int base)
          Search a byte array buffer for a byte array pattern, from a base position.
static java.util.LinkedList<java.lang.String> sentenceSplitter(java.lang.String input)
          Splits a String in a list of sentences, based on the punctuation.
static java.util.LinkedList<java.lang.String> singularWordSplitter(java.lang.String input)
          Creates a sequential list of words (brought to singular form)
static boolean string2bool(java.lang.String aString, boolean defBool)
          Converts a String to boolean: "true" = true, "false" = false.
static int string2int(java.lang.String aString, int defInt)
          Converts a String to int.
static java.util.LinkedList<java.lang.String> wordSplitter(java.lang.String input)
          Creates a sequential list of words
static java.lang.String[] wordWrap(java.lang.String input, int maxWidth)
          Wraps a string into a string array representing lines of about maxWidth characters (words are not cut)
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Util

public Util()
Method Detail

string2bool

public static boolean string2bool(java.lang.String aString,
                                  boolean defBool)
Converts a String to boolean: "true" = true, "false" = false.

Parameters:
aString - string to be encoded in bool
defBool - default value if string is null, empty or invalid
Returns:
the boolean in the string

string2int

public static int string2int(java.lang.String aString,
                             int defInt)
Converts a String to int.

Parameters:
aString - string to be parsed in
defInt - default value if string is null, empty or invalid
Returns:
int number represented by the input string

escapeHTMLEntities

public static java.lang.String escapeHTMLEntities(java.lang.String buf)
Escapes XML entities in a HTML-compatible way (i.e. does not escape entities that are already escaped). Discards non XML control characters.

Parameters:
buf - Input string
Returns:
Clean and protected XML string

escapeJavaScript

public static java.lang.String escapeJavaScript(java.lang.String buf)
Escapes characters for safe JavaScript strings

Parameters:
buf - Input string
Returns:
JavaScript compatible String

escapeDot

public static java.lang.String escapeDot(java.lang.String buf)
Escapes characters for safe labels in DOT language (GraphWiz.org)

Parameters:
buf - Input string
Returns:
GraphWiz DOT language compatible String

replaceAllNoRegExp

public static java.lang.String replaceAllNoRegExp(java.lang.String aInput,
                                                  java.lang.String aOldPattern,
                                                  java.lang.String aNewPattern)
Equivalent to string.replaceAll but without regular expressions

Parameters:
aInput - Input string
aOldPattern - String to be replaced. Cannot be empty.
aNewPattern - Replacement string
Returns:
Input string with aOldPattern replaced by aNewPattern

printStack

public static void printStack(java.lang.Exception e)
Prints in System.out (Tomcat stdout logs) the current stack when an exception was thrown

Parameters:
e - thrown exception

noDoubleSpace

public static java.lang.String noDoubleSpace(java.lang.String aValue)
Removes double (and more) spaces within a String + spaces at beginning or end of the String.

Parameters:
aValue - Input String
Returns:
Input string with extraneous, beginning and ending spaces removed.

fileIdentificationNormalize

public static java.lang.String fileIdentificationNormalize(java.lang.String aValue)
Normalize file names so they don't differ too much from one OS to another Windows "\" become "/" TODO: Other rules? to be implemented...

Parameters:
aValue - File name to be normalized
Returns:
Normalized file name

fixColumns

public static java.lang.String fixColumns(java.lang.String aValue,
                                          int columns)
Left align a String in a fixed number of columns for HTML output (&nbsp; used as a filler)

Parameters:
aValue - Input String
columns - Number of columns
Returns:
String filled with &nbsp;

prepareHighlight

public static java.lang.String[] prepareHighlight(java.lang.String query,
                                                  boolean byPieces)
Prepares an array of queried words to help later highlight of text displayed

Parameters:
query - String with the current Query words
byPieces - true if each word is searched independently
Returns:
Array of Searched words for the "highlight" function

highlight

public static java.lang.String highlight(java.lang.String text,
                                         java.lang.String[] QUERIES)
Highlight a text based on the words from a query.

Parameters:
text - Text to analyze
QUERIES - String array prepared by method prepareHighlight
Returns:
Text String with <font color="red"> added around highlighted text TODO: parameterize the highlight markup

noAccent

public static java.lang.String noAccent(java.lang.String input)
Removes all accent over (under) letters

Parameters:
input - Input String
Returns:
"cleaned" string

noLuceneJoker

public static java.lang.String noLuceneJoker(java.lang.String input)
Removes Lucene joker characters when doing an exact match search

Parameters:
input - Input String
Returns:
Cleaned string

noPlural

public static java.lang.String noPlural(java.lang.String word)
Brings a word with more than 3 letters to its singular form. CHEVALS becomes CHEVAL, CHEVEAUX remains CHEVEAUX ! DES remains DES. Only ending "S" (upper or lower case) is removed except if it is duplicated: LESS remains LESS. LES remains LES. LEURS becomes LEUR...

Parameters:
word - Input String
Returns:
Word without S at the end.

noSingleAccent

public static java.lang.String noSingleAccent(java.lang.String input)
Use noSingleAccent for each character in a String. Used for highlighting only.

Parameters:
input - Input String to process one character at a time
Returns:
String with accent removed. OE character may become a single O...

noSingleAccent

public static char noSingleAccent(char c)
This (incorrect) version takes ONE character and removes its accents. The result is only the first resulting character instead of the whole unaccented equivalent (which may be more than one character like OE single character becoming O...)

Parameters:
c - character to process
Returns:
first character of the unaccented equivalent to c

wordSplitter

public static java.util.LinkedList<java.lang.String> wordSplitter(java.lang.String input)
Creates a sequential list of words

Parameters:
input - Input String
Returns:
LinkedList of the words in the input string

sentenceSplitter

public static java.util.LinkedList<java.lang.String> sentenceSplitter(java.lang.String input)
Splits a String in a list of sentences, based on the punctuation.

Parameters:
input - InputString
Returns:
LinkedList with one String for each sentence.

singularWordSplitter

public static java.util.LinkedList<java.lang.String> singularWordSplitter(java.lang.String input)
Creates a sequential list of words (brought to singular form)

Parameters:
input - Input String
Returns:
LinkedList of the words in the input string

noHTMLLink

public static java.lang.String noHTMLLink(java.lang.String input)
Removes HTML links from a String (... and ...) keeping the link text.

Parameters:
input - HTML Input String
Returns:
HTML String but without links

formatLong

public static java.lang.String formatLong(long sortKey)
Makes a long number sortable as a string of characters. Useful to mix numbers and names within an alphabetical sort key.

Parameters:
sortKey - long number to convert
Returns:
22 characters. The first one is '-' for negative numbers, '=' for positive ones ('=' is greater than '-'). The number itself is right aligned in 21 columns (space used as filler).

magnitude

public static int magnitude(long num,
                            int basis)
Number of digits for a number written in the given basis

Parameters:
num - Number to be sized
basis - (1 or less is treated as 10)
Returns:
Number of digits

melt

public static void melt(java.lang.StringBuffer buf,
                        java.lang.String toMelt)
Ensures the last non alphanum character in buf is not repeated when adding toMelt For instance, to put a dot at the end of a sentence without possible repetition: met(title,". ") will add ". " but without repeating a dot which may already ends the current title.

Parameters:
buf - StringBuffer receiving new pieces of text
toMelt - String to add

glue

public static java.lang.String glue(java.lang.String[] toGlue,
                                    java.lang.String delim)
Glue an array of strings (lines) into one string

Parameters:
toGlue - String Array of the lines to "glue"
delim - String inserted between each line (array element)
Returns:
Glued string

wordWrap

public static java.lang.String[] wordWrap(java.lang.String input,
                                          int maxWidth)
Wraps a string into a string array representing lines of about maxWidth characters (words are not cut)

Parameters:
input - Input String
maxWidth - Approximate width of a line
Returns:
Array of Strings (each element is one line)

hashStrings

public static java.util.LinkedHashSet<java.lang.String> hashStrings(java.lang.String[] anArray)
Convert an array of String in a LinkedHashSet of String

Parameters:
anArray - String Array to convert
Returns:
LinkedHashSet with those strings.

firstUp

public static java.lang.String firstUp(java.lang.String name)
If a letter is the very first character, puts it in Upper case.

Parameters:
name - Input String
Returns:
Input string with very first character in Upper case

searchBuffer

public static int searchBuffer(byte[] buffer,
                               int len,
                               byte[] pattern,
                               int base)
Search a byte array buffer for a byte array pattern, from a base position. Beware, UTF-8 characters can be composed of multiple bytes.

Parameters:
buffer -
len - End of the buffer part to search
pattern - Byte array with pattern to search. (if empty, search fails)
base - Position in the buffer where the search starts (0 is the first buffer position)
Returns:
Position of the first byte of pattern in buffer