com.ccg.util
Class JavaString

java.lang.Object
  extended by com.ccg.util.JavaString

public class JavaString
extends Object

Used to encode/decode strings using the Java language escape rules.

This class is used to encode/decode string objects to/from the format used in the Java programming language.

The following table describes the recognized escape codes:

Escape Seq Unicode Value Description
\b\u0008Backspace
\f\u000cForm feed
\n\u000aLine feed (new line)
\r\u000dCarriage return
\t\u0009Tab
\'\u0027Single Quote
\"\u0022Double Quote
\\\u005cBackslash
\(\u0040Begin Parenthesis
\)\u0041End Parenthesis
\uXXXX\uXXXXAny unicode character - must use 4 hex digits

The following sample program may help show how this class works:


import com.ccg.util.JavaString;

public class Foo {

  public static void main(String[] args) {

                                // notice how the escaped double quote
                                // is optional
    try {
      System.out.println(JavaString.decode("\\\"Good-bye\nCruel\tWorld!\""));

                                // build a funky set of characters
      char[] buf = new char[80];
      int i = 0;
      for (; (char) i != '1'; i++) buf[i] = (char) i;
      for (int j = 120; j < 135; j++) buf[i++] = (char) j;
      for (; i < buf.length; i++) buf[i] = (char) (2000 + i);

      JavaString js = new JavaString();
      js.encodeAppend(buf);

      System.out.println();
      System.out.println("Escapes inserted into following:");
      System.out.println(js);

      for (i = 0; i < args.length; i++) {
        System.out.println();
        System.out.println(" Original:"+args[i]);
        js.clear();
        js.encodeAppend(args[i]);
        String encoded = js.toString();
        System.out.println("  Encoded:"+encoded);
        js.clear();
        js.decodeAppend(args[i]);
        System.out.println("  Decoded:"+js);
        js.clear();
        js.decodeAppend(encoded);
        System.out.println("En/Decode:"+js);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

  }

}

 

Try running the above as follows:

 java Foo '"Hello\tWorld!\"\n"Good-bye."'
 

Since:
1.0
Version:
$Revision: 1.1.1.1 $
Author:
$Author: pkb $
See Also:
decode(java.lang.String), encode(java.lang.String)

Constructor Summary
JavaString()
          Initializes object for encoding/decoding
 
Method Summary
 void clear()
          Resets/clears the object to its initial state.
static String decode(String from)
          Replaces standard Java escape codes with actual character values.
 void decodeAppend(char[] buf)
          Appends an array of characters looking for escape codes.
 void decodeAppend(char c, int i)
          Process a single character looking for escape codes.
 void decodeAppend(String str)
          Appends a string looking for escape codes.
static String encode(String from)
          Escapes the necessary characters in the passed string.
 void encodeAppend(char c)
          Process a single and escape it if necessary.
 void encodeAppend(char[] buf)
          Appends a string escaping the necessary characters.
 void encodeAppend(String str)
          Appends a string escaping the necessary characters.
 String toString()
          Returns string representation of object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JavaString

public JavaString()
Initializes object for encoding/decoding

Since:
1.0
Method Detail

clear

public void clear()
Resets/clears the object to its initial state.

This method can be used to reset the object to its initial state so that it can be used again to parse another string.

Since:
1.0

decode

public static String decode(String from)
                     throws ParseException
Replaces standard Java escape codes with actual character values.

This is a convenience function. It replaces standard Java escape codes with their actual character values. This is a static method that can be called from anywhere. It constructs a JavaString object, decode appends the string specified and returns the results.

Parameters:
str - String of characters to process. You can pass null - in which case we won't do anything.
Throws:
ParseException - If your string contain a invalid escape code.
Since:
1.0
See Also:
encode(java.lang.String)

decodeAppend

public void decodeAppend(char[] buf)
                  throws ParseException
Appends an array of characters looking for escape codes.

This method is used when you want to replace the Java escape codes with their actual character values. It looks for the standard escape codes (like: "\n", "\b", ...) and replaces them with their character equivalent. It also handles the unicode escape codes (\uXXXX).

This method is capable of being called multiple times and "stiches" together escape sequences. For example:

 JavaString js = new JavaString();
 js.decodeAppend("escape at end\\".toCharArray());
 js.decodeAppend("n<- the \"n\" goes with preceding "\\"".toCharArray());
 

Parameters:
buf - Array of characters to process. You can pass null - in which case we won't do anything.
Throws:
ParseException - If your characters contain a invalid escape code.
Since:
1.0
See Also:
decode(java.lang.String)

decodeAppend

public void decodeAppend(String str)
                  throws ParseException
Appends a string looking for escape codes.

This method is used when you want to replace the Java escape codes with their actual character values. It looks for the standard escape codes (like: "\n", "\b", ...) and replaces them with their character equivalent. It also handles the unicode escape codes (\uXXXX).

This method is capable of being called multiple times and "stiches" together escape sequences. For example:

 JavaString js = new JavaString();
 js.decodeAppend("escape at end\\");
 js.decodeAppend("n<- the \"n\" goes with preceding "\\"");
 

Parameters:
str - String of characters to process. You can pass null - in which case we won't do anything.
Throws:
ParseException - If your string contain a invalid escape code.
Since:
1.0
See Also:
decode(java.lang.String)

decodeAppend

public void decodeAppend(char c,
                         int i)
                  throws ParseException
Process a single character looking for escape codes.

This method is used when you want to replace the Java escape codes with their actual character values. It looks for the standard escape codes (like: "\n", "\b", ...) and replaces them with their character equivalent. It also handles the unicode escape codes (\uXXXX). It does this by keep track of prior work done (it maintains state in the object).

This method is capable of being called multiple times and "stiches" together escape sequences. For example, the following code fragment would "stitch" together a '\' and 'n' resulting in a new line character being inserted:

 JavaString js = new JavaString();
 js.decodeAppend('\\');
 js.decodeAppend('n');
 

Parameters:
c - Single character to process.
pos - Position in source array (only used if we need to throw a ParseException)
Throws:
ParseException - If the character processed results in a invalid escape code.
Since:
1.0
See Also:
decodeAppend(char[]), decodeAppend(String)

encode

public static String encode(String from)
Escapes the necessary characters in the passed string.

This method is used when you want to escape characters contained in your original string using the Java escape rules. It appends standard characters verbatim and escapes the others. For escaped characters, it uses the standard escape codes (like: "\n", "\b", ...) when possible, if not possible, it uses the longer unicode escape codes (\uXXXX).

Parameters:
str - String of characters to process. You can pass null - in which case we won't do anything.
Since:
1.0
See Also:
encode(java.lang.String)

encodeAppend

public void encodeAppend(char[] buf)
Appends a string escaping the necessary characters.

This method is used when you want to escape characters contained in your character array using the Java escape rules. It appends standard characters verbatim and escapes the others. For escaped characters, it uses the standard escape codes (like: "\n", "\b", ...) when possible, if not possible, it uses the longer unicode escape codes (\uXXXX).

 char[] data = { 'a', ' ', '\n', (char) 2303 };
 JavaString js = new JavaString();
 js.encodeAppend(data);
 

Parameters:
buf - Array of characters to process. You can pass null - in which case we won't do anything.
Since:
1.0
See Also:
encode(java.lang.String)

encodeAppend

public void encodeAppend(String str)
Appends a string escaping the necessary characters.

This method is used when you want to escape characters contained in your original string using the Java escape rules. It appends standard characters verbatim and escapes the others. For escaped characters, it uses the standard escape codes (like: "\n", "\b", ...) when possible, if not possible, it uses the longer unicode escape codes (\uXXXX).

 JavaString js = new JavaString();
 js.encodeAppend("\n\tescape at end\\");
 

Parameters:
str - String of characters to process. You can pass null - in which case we won't do anything.
Since:
1.0
See Also:
encode(java.lang.String)

encodeAppend

public void encodeAppend(char c)
Process a single and escape it if necessary.

This method is used when you want to replace the Java characters with their escape sequences (if necessary). Normal characters (typically the ones from the space character and less than 127 are added as needed).

 JavaString js = new JavaString();
 js.encodeAppend((char) 12);
 js.encodeAppend((char) 2303);
 

Parameters:
c - Single character to process.
Since:
1.0
See Also:
encodeAppend(char[]), encodeAppend(String)

toString

public String toString()
Returns string representation of object.

Overrides:
toString in class Object
Returns:
String representation of contents
Since:
1.0
See Also:
encodeAppend(char[])


Copyright 1998-1998-2006 null. All Rights Reserved.