|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.util.JavaString
public class JavaString
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 | \u0008 | Backspace |
| \f | \u000c | Form feed |
| \n | \u000a | Line feed (new line) |
| \r | \u000d | Carriage return |
| \t | \u0009 | Tab |
| \' | \u0027 | Single Quote |
| \" | \u0022 | Double Quote |
| \\ | \u005c | Backslash |
| \( | \u0040 | Begin Parenthesis |
| \) | \u0041 | End Parenthesis |
| \uXXXX | \uXXXX | Any 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."'
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 |
|---|
public JavaString()
encoding/decoding
| Method Detail |
|---|
public void clear()
This method can be used to reset the object to its initial state so that it can be used again to parse another string.
public static String decode(String from)
throws ParseException
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.
str - String of characters to process. You can pass null - in which
case we won't do anything.
ParseException - If your string contain a invalid escape code.encode(java.lang.String)
public void decodeAppend(char[] buf)
throws ParseException
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());
buf - Array of characters to process. You can pass null - in which
case we won't do anything.
ParseException - If your characters contain a invalid escape code.decode(java.lang.String)
public void decodeAppend(String str)
throws ParseException
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 "\\"");
str - String of characters to process. You can pass null - in which
case we won't do anything.
ParseException - If your string contain a invalid escape code.decode(java.lang.String)
public void decodeAppend(char c,
int i)
throws ParseException
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');
c - Single character to process.pos - Position in source array (only used if we need to throw a
ParseException)
ParseException - If the character processed results in a invalid escape code.decodeAppend(char[]),
decodeAppend(String)public static String encode(String from)
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).
str - String of characters to process. You can pass null - in which
case we won't do anything.encode(java.lang.String)public void encodeAppend(char[] buf)
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);
buf - Array of characters to process. You can pass null - in which
case we won't do anything.encode(java.lang.String)public void encodeAppend(String str)
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\\");
str - String of characters to process. You can pass null - in which
case we won't do anything.encode(java.lang.String)public void encodeAppend(char c)
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);
c - Single character to process.encodeAppend(char[]),
encodeAppend(String)public String toString()
string representation of object.
toString in class ObjectencodeAppend(char[])
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||