org.knopflerfish.framework
Class Util

java.lang.Object
  extended by org.knopflerfish.framework.Util

public class Util
extends java.lang.Object


Nested Class Summary
static interface Util.Comparator
           
 
Field Summary
protected static char CITCHAR
          Default citation char for splitwords().
protected static java.lang.String WHITESPACE
          Default whitespace string for splitwords().
 
Constructor Summary
Util()
           
 
Method Summary
static java.lang.String base64Encode(java.lang.String s)
           
static int binarySearch(java.util.List pl, Util.Comparator c, java.lang.Object p)
          Do binary search for a package entry in the list with the same version number add the specifies package entry.
static int compareStringVersion(java.lang.String ver1, java.lang.String ver2)
          Compare to strings formatted as '[.[.]]'.
static java.lang.String encode(byte[] in, int len)
          Encode a raw byte array to a Base64 String.
static void encode(java.io.InputStream in, java.io.OutputStream out, int len)
           
static boolean filterMatch(java.lang.String filter, java.lang.String s)
          Check wildcard filter matches the string
static java.lang.String getContent(java.io.File f)
           
static FileTree getFileStorage(java.lang.String name)
          Check for local file storage directory.
static java.util.Iterator parseEntries(java.lang.String a, java.lang.String s, boolean single, boolean unique, boolean single_entry)
          Parse strings of format: ENTRY (, ENTRY)* ENTRY = key (; key)* (; PARAM)* PARAM = attribute '=' value PARAM = directive ':=' value
static java.util.ArrayList parseEnumeration(java.lang.String d, java.lang.String s)
          Parse strings of format: ENTRY (, ENTRY)*
static void putContent(java.io.File f, java.lang.String content)
           
static void putContent(java.io.File f, java.lang.String content, boolean useUTF8)
           
static java.lang.String replace(java.lang.String s, java.lang.String v1, java.lang.String v2)
          Replace all occurances of a substring with another string.
static void sort(java.util.List a, Util.Comparator cf, boolean bReverse)
          Sort a vector with objects compareble using a comparison function.
static java.lang.String[] splitwords(java.lang.String s)
          Utility method to split a string into words separated by whitespace.
static java.lang.String[] splitwords(java.lang.String s, java.lang.String whiteSpace)
          Utility method to split a string into words separated by whitespace.
static java.lang.String[] splitwords(java.lang.String s, java.lang.String whiteSpace, char citChar)
          Split a string into words separated by whitespace.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

WHITESPACE

protected static java.lang.String WHITESPACE
Default whitespace string for splitwords(). Value is " \t\n\r")


CITCHAR

protected static char CITCHAR
Default citation char for splitwords(). Value is '"'

Constructor Detail

Util

public Util()
Method Detail

getFileStorage

public static FileTree getFileStorage(java.lang.String name)
Check for local file storage directory.

Parameters:
name - local directory name.
Returns:
A FileTree object of directory or null if no storage is available.

compareStringVersion

public static int compareStringVersion(java.lang.String ver1,
                                       java.lang.String ver2)
                                throws java.lang.NumberFormatException
Compare to strings formatted as '[.[.]]'. If string is null, then it counts as ZERO.

Parameters:
ver1 - First version string.
ver2 - Second version string.
Returns:
Return 0 if equals, -1 if ver1 < ver2 and 1 if ver1 > ver2.
Throws:
java.lang.NumberFormatException - on syntax error in input.

parseEnumeration

public static java.util.ArrayList parseEnumeration(java.lang.String d,
                                                   java.lang.String s)
Parse strings of format: ENTRY (, ENTRY)*

Parameters:
d - Directive being parsed
s - String to parse
Returns:
A sorted ArrayList with enumeration or null if enumeration string was null.
Throws:
java.lang.IllegalArgumentException - If syntax error in input string.

parseEntries

public static java.util.Iterator parseEntries(java.lang.String a,
                                              java.lang.String s,
                                              boolean single,
                                              boolean unique,
                                              boolean single_entry)
Parse strings of format: ENTRY (, ENTRY)* ENTRY = key (; key)* (; PARAM)* PARAM = attribute '=' value PARAM = directive ':=' value

Parameters:
a - Attribute being parsed
s - String to parse
single - If true, only allow one key per ENTRY
unique - Only allow unique parameters for each ENTRY.
single_entry - If true, only allow one ENTRY is allowed.
Returns:
Iterator(Map(param -> value)) or null if input string is null.
Throws:
java.lang.IllegalArgumentException - If syntax error in input string.

splitwords

public static java.lang.String[] splitwords(java.lang.String s)
Utility method to split a string into words separated by whitespace.

Equivalent to splitwords(s, WHITESPACE)


splitwords

public static java.lang.String[] splitwords(java.lang.String s,
                                            java.lang.String whiteSpace)
Utility method to split a string into words separated by whitespace.

Equivalent to splitwords(s, WHITESPACE, CITCHAR)


splitwords

public static java.lang.String[] splitwords(java.lang.String s,
                                            java.lang.String whiteSpace,
                                            char citChar)
Split a string into words separated by whitespace.

Citation chars may be used to group words with embedded whitespace.

Parameters:
s - String to split.
whiteSpace - whitespace to use for splitting. Any of the characters in the whiteSpace string are considered whitespace between words and will be removed from the result. If no words are found, return an array of length zero.
citChar - Citation character used for grouping words with embedded whitespace. Typically '"'

replace

public static java.lang.String replace(java.lang.String s,
                                       java.lang.String v1,
                                       java.lang.String v2)
Replace all occurances of a substring with another string.

The returned string will shrink or grow as necessary depending on the lengths of v1 and v2.

Implementation note: This method avoids using the standard String manipulation methods to increase execution speed. Using the replace method does however include two new operations in the case when matches are found.

Parameters:
s - Source string.
v1 - String to be replaced with v2.
v2 - String replacing v1.
Returns:
Modified string. If any of the input strings are null, the source string s will be returned unmodified. If v1.length == 0, v1.equals(v2) or no occurances of v1 is found, also return s unmodified.

getContent

public static java.lang.String getContent(java.io.File f)

putContent

public static void putContent(java.io.File f,
                              java.lang.String content)
                       throws java.io.IOException
Throws:
java.io.IOException

putContent

public static void putContent(java.io.File f,
                              java.lang.String content,
                              boolean useUTF8)
                       throws java.io.IOException
Throws:
java.io.IOException

sort

public static void sort(java.util.List a,
                        Util.Comparator cf,
                        boolean bReverse)
Sort a vector with objects compareble using a comparison function.

Parameters:
a - Vector to sort
cf - comparison function

binarySearch

public static int binarySearch(java.util.List pl,
                               Util.Comparator c,
                               java.lang.Object p)
Do binary search for a package entry in the list with the same version number add the specifies package entry.

Parameters:
pl - Sorted list of package entries to search.
p - Package entry to search for.
Returns:
index of the found entry. If no entry is found, return (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list.

base64Encode

public static java.lang.String base64Encode(java.lang.String s)
                                     throws java.io.IOException
Throws:
java.io.IOException

encode

public static java.lang.String encode(byte[] in,
                                      int len)
                               throws java.io.IOException
Encode a raw byte array to a Base64 String.

Parameters:
in - Byte array to encode.
len - Length of Base64 lines. 0 means no line breaks.
Throws:
java.io.IOException

encode

public static void encode(java.io.InputStream in,
                          java.io.OutputStream out,
                          int len)
                   throws java.io.IOException
Throws:
java.io.IOException

filterMatch

public static boolean filterMatch(java.lang.String filter,
                                  java.lang.String s)
Check wildcard filter matches the string