org.avis.util
Class Text

java.lang.Object
  extended by org.avis.util.Text

public final class Text
extends Object

General text formatting utilities.

Author:
Matthew Phillips

Method Summary
static void appendEscaped(StringBuilder builder, String string, char charToEscape)
          Append a string to a builder, escaping (with '\') any instances of a special character.
static void appendEscaped(StringBuilder builder, String string, String charsToEscape)
          Append a string to a builder, escaping (with '\') any instances of a set of special characters.
static void appendHex(StringBuilder str, byte b)
          Append the hex form of a byte to a builder.
static void appendHexBytes(StringBuilder str, byte[] bytes)
          Append a byte array to a builder in form: 01 e2 fe ff ...
static String bytesToHex(byte[] bytes)
          Turn an array of bytes into a hex-encoded string e.g.
static String className(Class<?> type)
          Return just the name (minus the package) of a class.
static String className(Object object)
          Return just the name (minus the package) of an object's class.
static byte[] dataToBytes(byte[] expr)
          Turn a data block expression into a block of bytes.
static String expandBackslashes(String text)
          Expand C-like backslash codes such as \n \x90 etc into their literal values.
static int findFirstNonEscaped(String str, char toFind)
          Find the first index of the given character, skipping instances that are escaped by '\'.
static int findFirstNonEscaped(String str, int start, char toFind)
          Find the first index of the given character, skipping instances that are escaped by '\'.
static String formatNotification(Map<String,Object> attributes)
          Generate a string value of the notification.
static byte hexToByte(String byteExpr)
          Parse a string expression as a hex-coded unsigned byte.
static byte[] hexToBytes(String string)
          Parse a series of hex pairs as a sequence of unsigned bytes.
static String idFor(Object instance)
          Generate a hex ID for an object.
static String join(List<?> items, String separator)
          Join a list of objects into a string.
static String join(Object[] items)
          Join a list of objects into a string.
static String join(Object[] items, String separator)
          Join a list of objects into a string.
static String mapToString(Map<?,?> map)
          Generate human friendly string dump of a Map.
static String quotedStringToString(String valueExpr)
          Parse a string value in the format "string", allowing escaped "'s inside the string.
static String shortException(Throwable ex)
          Generate a short exception message without package name and message (if null).
static byte[] slice(byte[] bytes, int start, int end)
           
static String[] split(String text)
          Shortcut to execute split on any whitespace character.
static String[] split(String text, String regex)
          String.split ("") returns {""} rather than {} like you might expect: this returns empty array on "".
static Number stringToNumber(String valueExpr)
          Parse a numeric int, long or double value.
static byte[] stringToOpaque(String valueExpr)
          Parse an opaque value expression e.g.
static Object stringToValue(String expr)
          Parse a string expression as a value.
static String stripBackslashes(String text)
          Remove any \'s from a string.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

className

public static String className(Object object)
Return just the name (minus the package) of an object's class.


className

public static String className(Class<?> type)
Return just the name (minus the package) of a class.


shortException

public static String shortException(Throwable ex)
Generate a short exception message without package name and message (if null).


idFor

public static String idFor(Object instance)
Generate a hex ID for an object.


formatNotification

public static String formatNotification(Map<String,Object> attributes)
Generate a string value of the notification.

Parameters:
attributes - The attribute name/value pairs.
Returns:
The string formatted version of the notification attributes.

appendEscaped

public static void appendEscaped(StringBuilder builder,
                                 String string,
                                 char charToEscape)
Append a string to a builder, escaping (with '\') any instances of a special character.


appendEscaped

public static void appendEscaped(StringBuilder builder,
                                 String string,
                                 String charsToEscape)
Append a string to a builder, escaping (with '\') any instances of a set of special characters.


appendHexBytes

public static void appendHexBytes(StringBuilder str,
                                  byte[] bytes)
Append a byte array to a builder in form: 01 e2 fe ff ...


appendHex

public static void appendHex(StringBuilder str,
                             byte b)
Append the hex form of a byte to a builder.


hexToByte

public static byte hexToByte(String byteExpr)
                      throws InvalidFormatException
Parse a string expression as a hex-coded unsigned byte.

Returns:
A byte in the range 0 - 255 if sign is ignored.
Throws:
InvalidFormatException

stringToValue

public static Object stringToValue(String expr)
                            throws InvalidFormatException
Parse a string expression as a value. Values may be quoted strings ("string"), numbers (0.1, 3, 123456789L), or byte arrays ([0a ff de ad]).

Parameters:
expr - The string expression.
Returns:
The value.
Throws:
InvalidFormatException - if expr is not parseable.
See Also:
stringToNumber(String), stringToOpaque(String), quotedStringToString(String)

stringToNumber

public static Number stringToNumber(String valueExpr)
                             throws InvalidFormatException
Parse a numeric int, long or double value. e.g. 32L, 3.14, 42.

Throws:
InvalidFormatException

quotedStringToString

public static String quotedStringToString(String valueExpr)
                                   throws InvalidFormatException
Parse a string value in the format "string", allowing escaped "'s inside the string.

Throws:
InvalidFormatException

stringToOpaque

public static byte[] stringToOpaque(String valueExpr)
                             throws InvalidFormatException
Parse an opaque value expression e.g. [00 0f 01].

Throws:
InvalidFormatException

hexToBytes

public static byte[] hexToBytes(String string)
                         throws InvalidFormatException
Parse a series of hex pairs as a sequence of unsigned bytes. Pairs may be separated by optional whitespace. e.g. "0A FF 00 01" or "deadbeef".

Throws:
InvalidFormatException

bytesToHex

public static String bytesToHex(byte[] bytes)
Turn an array of bytes into a hex-encoded string e.g. "00 01 aa de".


dataToBytes

public static byte[] dataToBytes(byte[] expr)
                          throws InvalidFormatException
Turn a data block expression into a block of bytes. Formats:
   Hex pairs: [0a 02 ff 31]
   String:    "hello"
   Raw data:  #data
 

Parameters:
expr - The data block expression
Returns:
The data.
Throws:
InvalidFormatException - if the expression was not valid.

slice

public static byte[] slice(byte[] bytes,
                           int start,
                           int end)

findFirstNonEscaped

public static int findFirstNonEscaped(String str,
                                      char toFind)
Find the first index of the given character, skipping instances that are escaped by '\'.


findFirstNonEscaped

public static int findFirstNonEscaped(String str,
                                      int start,
                                      char toFind)
Find the first index of the given character, skipping instances that are escaped by '\'.


stripBackslashes

public static String stripBackslashes(String text)
                               throws InvalidFormatException
Remove any \'s from a string.

Throws:
InvalidFormatException

split

public static String[] split(String text)
Shortcut to execute split on any whitespace character.


split

public static String[] split(String text,
                             String regex)
String.split ("") returns {""} rather than {} like you might expect: this returns empty array on "".


join

public static String join(Object[] items)
Join a list of objects into a string.

Parameters:
items - The items to stringify.
Returns:
The stringified list.

join

public static String join(Object[] items,
                          String separator)
Join a list of objects into a string.

Parameters:
items - The items to stringify.
separator - The separator between items.
Returns:
The stringified list.

join

public static String join(List<?> items,
                          String separator)
Join a list of objects into a string.

Parameters:
items - The items to stringify.
separator - The separator between items.
Returns:
The stringified list.

mapToString

public static String mapToString(Map<?,?> map)
Generate human friendly string dump of a Map.


expandBackslashes

public static String expandBackslashes(String text)
                                throws InvalidFormatException
Expand C-like backslash codes such as \n \x90 etc into their literal values.

Throws:
InvalidFormatException


Copyright © 2008 Matthew Phillips. All Rights Reserved.