sdsi.sexp
Class Sexp

java.lang.Object
  |
  +--sdsi.sexp.Sexp
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
SexpList, SexpString

public abstract class Sexp
extends java.lang.Object
implements java.io.Serializable

Abstract class to represent S-expressions. There are two subclasses to represent byte-string or list S-expressions.

Author:
Alexander Morcos, Sameer Ajmani
See Also:
SexpString, SexpList, Serialized Form

Field Summary
static char[] base64Digit
          Provides a mapping from integers to the corresponing base-64 digit.
static char[] hexDigit
          Provides a mapping from integers to the corresponding hexadecimal digit.
 
Constructor Summary
Sexp()
           
 
Method Summary
static int base64ToInt(byte b)
          Converts a base-64 digit to its corresponding integer value.
static byte[] base64ToOctet(byte a, byte b, byte c, byte d)
          Translates four base-64 digits into their corresponding three octet values.
abstract  int getCanonLen()
          Calculates the length of the canonical representation of this S-expression.
abstract  byte[] getCanonRep()
          Generates the canonical representation of this S-expression.
(package private) abstract  int getReadableLen()
           
static boolean isBase64Digit(int charToTest)
          Tests if a character is a base 64 digit.
static boolean isDecimalDigit(int charToTest)
          Tests if a character is a decimal digit.
static boolean isHexDigit(int charToTest)
          Tests if a character is a hexadecimal digit.
static boolean isTokenChar(int charToTest)
          Tests if a character is a token character.
static boolean isWhiteSpace(int charToTest)
          Tests if a character is whitespace.
static Sexp parse(java.io.PushbackInputStream input)
          Parses an S-expression out of the input stream.
abstract  java.lang.String toReadableString(int offset, int width, int last)
          Returns a readable string representation of this S-expression.
 java.lang.String toString()
          Returns a string representation of this S-expression.
static java.lang.String toString(byte[] b)
          Converts an array of bytes to a String.
 java.lang.String toVerbatimString()
          Returns a string representation of the canonical form of this S-expression.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

hexDigit

public static final char[] hexDigit
Provides a mapping from integers to the corresponding hexadecimal digit.


base64Digit

public static final char[] base64Digit
Provides a mapping from integers to the corresponing base-64 digit.

Constructor Detail

Sexp

public Sexp()
Method Detail

isWhiteSpace

public static final boolean isWhiteSpace(int charToTest)
Tests if a character is whitespace.

Parameters:
charToTest - the character to be tested
Returns:
true if the character is whitespace, false otherwise

isDecimalDigit

public static final boolean isDecimalDigit(int charToTest)
Tests if a character is a decimal digit.

Parameters:
charToTest - the character to be tested
Returns:
true if the character is a digit between 0 and 9 inclusive, false otherwise

isHexDigit

public static final boolean isHexDigit(int charToTest)
Tests if a character is a hexadecimal digit.

Parameters:
charToTest - the character to be tested
Returns:
true if the character is a decimal digit or a letter between 'a' and 'f' upper or lowercase, false otherwise

isBase64Digit

public static final boolean isBase64Digit(int charToTest)
Tests if a character is a base 64 digit.

Parameters:
charToTest - the character to be tested
Returns:
true if the character is a decimal digit, a letter of the alphabet or '+' or '/', false otherwise

isTokenChar

public static final boolean isTokenChar(int charToTest)
Tests if a character is a token character.

Parameters:
charToTest - the character to be tested.
Returns:
true if the character is a legal token character for an S-expression, false otherwise

base64ToInt

public static final int base64ToInt(byte b)
Converts a base-64 digit to its corresponding integer value.

Parameters:
b - the byte representing the base-64 digit.
Returns:
the corresponing integer value between 0 and 63 as defined for the S-expression base-64 encoding, or -1 if b is not valid base-64 digit.

base64ToOctet

public static final byte[] base64ToOctet(byte a,
                                         byte b,
                                         byte c,
                                         byte d)
                                  throws SexpException
Translates four base-64 digits into their corresponding three octet values. A base-64 string encodes three bytes as a sequence of four base-64 characters. This function takes four base-64 characters and recovers the three bytes being encoded. If the representation ends with one or two equal signs then the two or one, respectively, encoded bytes are returned.

Returns:
an array of bytes of length one, two, or three, containing the bytes that were encoded
Throws:
SexpException - if the bytes are not valid base-64 characters or do not correctly encode one, two, or three bytes.

parse

public static Sexp parse(java.io.PushbackInputStream input)
                  throws SexpException
Parses an S-expression out of the input stream. The input stream is assumed to contain an S-expression in canonical form. Leaves the input stream at the end of the S-expression that was parsed.

Parameters:
input - an input stream conaining an S-expression.
Returns:
the Sexp parsed.
Throws:
SexpException - if there was a problem in the parse.

toString

public static java.lang.String toString(byte[] b)
Converts an array of bytes to a String. Assumes the byte array is encoded with the 8859_1 character set.

Parameters:
b - the byte array to be decoded
Returns:
the String whose bytes are in b

getCanonLen

public abstract int getCanonLen()
Calculates the length of the canonical representation of this S-expression. @return the length of the canonical representation.


getCanonRep

public abstract byte[] getCanonRep()
Generates the canonical representation of this S-expression.

Returns:
a byte array containing exactly the canonical representation.

toString

public java.lang.String toString()
Returns a string representation of this S-expression. Possibly just the a String which represents the canonical-form.

Overrides:
toString in class java.lang.Object
Returns:
a string representation of this S-expression.

toReadableString

public abstract java.lang.String toReadableString(int offset,
                                                  int width,
                                                  int last)
Returns a readable string representation of this S-expression. Given format parameters, returns a nicely formatted readable representation of this S-expression, using base-64 and hexadecimal representation to handle unprintable characters.

Parameters:
offset - the number of spaces that should appear before every line of the string.
width - the total width that the string should take up in characters.
last - the number of spaces that should be left at the end of every line (for possible closing parens).
Returns:
a nicely formatted string representation of this S-expression.

getReadableLen

abstract int getReadableLen()

toVerbatimString

public java.lang.String toVerbatimString()
Returns a string representation of the canonical form of this S-expression.

Returns:
a string representation of the canoical form of this S-expression.