public final class JavaEscape extends Object
Utility class for performing Java escape/unescape operations.
Configuration of escape/unescape operationsEscape operations can be (optionally) configured by means of:
JavaEscapeLevel
enum.Unbescape does not define a 'type' for Java escaping (just a level) because, given the way Unicode Escapes work in Java, there is no possibility to choose whether we want to escape, for example, a tab character (U+0009) as a Single Escape Char (\t) or as a Unicode Escape (\u0009). Given Unicode Escapes are processed by the compiler and not the runtime, using \u0009 instead of \t would really insert a tab character inside our source code before compiling, which is not equivalent to inserting "\t" in a String literal.
Unescape operations need no configuration parameters. Unescape operations will always perform complete unescape of SECs (\n), u-based (\u00E1) and octal (\341) escapes.
FeaturesSpecific features of the Java escape/unescape operations performed by means of this class:
The way Unicode Escapes work in Java is different to other languages like e.g. JavaScript. In Java, these UHEXA escapes are processed by the compiler itself, and therefore resolved before any other type of escapes. Besides, UHEXA escapes can appear anywhere in the code, not only String literals. This means that, while in JavaScript 'a\u005Cna' would be displayed as a\na, in Java "a\u005Cna" would in fact be displayed in two lines: a+<LF>+a.
Going even further, this is perfectly valid Java code:
final String hello = \u0022Hello, World!\u0022;
Also, Java allows to write any number of 'u' characters in this type of escapes, like \uu00E1 or even \uuuuuuuuu00E1. This is so in order to enable legacy compatibility with older code-processing tools that didn't support Unicode processing at all, which would fail when finding an Unicode escape like \u00E1, but not \uu00E1 (because they would consider \u as the escape). So this is valid Java code too:
final String hello = \uuuuuuuu0022Hello, World!\u0022;
In order to correctly unescape Java UHEXA escapes like "a\u005Cna", Unbescape will perform a two-pass process so that all unicode escapes are processed in the first pass, and then the single escape characters and octal escapes in the second pass.
Input/OutputThere are two different input/output modes that can be used in escape/unescape operations:
The following references apply:
Modifier and Type | Method and Description |
---|---|
static void |
escapeJava(char[] text,
int offset,
int len,
Writer writer)
Perform a Java level 2 (basic set and all non-ASCII chars) escape operation
on a char[] input.
|
static void |
escapeJava(char[] text,
int offset,
int len,
Writer writer,
JavaEscapeLevel level)
Perform a (configurable) Java escape operation on a char[] input.
|
static String |
escapeJava(String text)
Perform a Java level 2 (basic set and all non-ASCII chars) escape operation
on a String input.
|
static String |
escapeJava(String text,
JavaEscapeLevel level)
Perform a (configurable) Java escape operation on a String input.
|
static void |
escapeJavaMinimal(char[] text,
int offset,
int len,
Writer writer)
Perform a Java level 1 (only basic set) escape operation
on a char[] input.
|
static String |
escapeJavaMinimal(String text)
Perform a Java level 1 (only basic set) escape operation
on a String input.
|
static void |
unescapeJava(char[] text,
int offset,
int len,
Writer writer)
Perform a Java unescape operation on a char[] input.
|
static String |
unescapeJava(String text)
Perform a Java unescape operation on a String input.
|
public static String escapeJavaMinimal(String text)
Perform a Java level 1 (only basic set) escape operation on a String input.
Level 1 means this method will only escape the Java basic escape set:
This method calls escapeJava(String, JavaEscapeLevel)
with the following preconfigured values:
This method is thread-safe.
text
- the String to be escaped.public static String escapeJava(String text)
Perform a Java level 2 (basic set and all non-ASCII chars) escape operation on a String input.
Level 2 means this method will escape:
This escape will be performed by using the Single Escape Chars whenever possible. For escaped characters that do not have an associated SEC, default to \uFFFF Hexadecimal Escapes.
This method calls escapeJava(String, JavaEscapeLevel)
with the following preconfigured values:
This method is thread-safe.
text
- the String to be escaped.public static String escapeJava(String text, JavaEscapeLevel level)
Perform a (configurable) Java escape operation on a String input.
This method will perform an escape operation according to the specified
JavaEscapeLevel
argument value.
All other String-based escapeJava*(...) methods call this one with preconfigured level values.
This method is thread-safe.
text
- the String to be escaped.level
- the escape level to be applied, see JavaEscapeLevel
.public static void escapeJavaMinimal(char[] text, int offset, int len, Writer writer) throws IOException
Perform a Java level 1 (only basic set) escape operation on a char[] input.
Level 1 means this method will only escape the Java basic escape set:
This method calls escapeJava(char[], int, int, java.io.Writer, JavaEscapeLevel)
with the following preconfigured values:
This method is thread-safe.
text
- the char[] to be escaped.offset
- the position in text at which the escape operation should start.len
- the number of characters in text that should be escaped.writer
- the java.io.Writer to which the escaped result will be written. Nothing will
be written at all to this writer if text is null.IOException
- if an input/output exception occurspublic static void escapeJava(char[] text, int offset, int len, Writer writer) throws IOException
Perform a Java level 2 (basic set and all non-ASCII chars) escape operation on a char[] input.
Level 2 means this method will escape:
This escape will be performed by using the Single Escape Chars whenever possible. For escaped characters that do not have an associated SEC, default to \uFFFF Hexadecimal Escapes.
This method calls escapeJava(char[], int, int, java.io.Writer, JavaEscapeLevel)
with the following preconfigured values:
This method is thread-safe.
text
- the char[] to be escaped.offset
- the position in text at which the escape operation should start.len
- the number of characters in text that should be escaped.writer
- the java.io.Writer to which the escaped result will be written. Nothing will
be written at all to this writer if text is null.IOException
- if an input/output exception occurspublic static void escapeJava(char[] text, int offset, int len, Writer writer, JavaEscapeLevel level) throws IOException
Perform a (configurable) Java escape operation on a char[] input.
This method will perform an escape operation according to the specified
JavaEscapeLevel
argument value.
All other char[]-based escapeJava*(...) methods call this one with preconfigured level values.
This method is thread-safe.
text
- the char[] to be escaped.offset
- the position in text at which the escape operation should start.len
- the number of characters in text that should be escaped.writer
- the java.io.Writer to which the escaped result will be written. Nothing will
be written at all to this writer if text is null.level
- the escape level to be applied, see JavaEscapeLevel
.IOException
- if an input/output exception occurspublic static String unescapeJava(String text)
Perform a Java unescape operation on a String input.
No additional configuration arguments are required. Unescape operations will always perform complete Java unescape of SECs, u-based and octal escapes.
This method is thread-safe.
text
- the String to be unescaped.public static void unescapeJava(char[] text, int offset, int len, Writer writer) throws IOException
Perform a Java unescape operation on a char[] input.
No additional configuration arguments are required. Unescape operations will always perform complete Java unescape of SECs, u-based and octal escapes.
This method is thread-safe.
text
- the char[] to be unescaped.offset
- the position in text at which the unescape operation should start.len
- the number of characters in text that should be unescaped.writer
- the java.io.Writer to which the unescaped result will be written. Nothing will
be written at all to this writer if text is null.IOException
- if an input/output exception occursCopyright © 2015 The UNBESCAPE team. All rights reserved.