Top Description Inners Fields Constructors Methods
com.github.javaparser.utils

public Class StringEscapeUtils

extends Object
Class Inheritance
Imports
java.io.IOException, .StringWriter, .Writer, java.util.HashMap, .HashSet

Adapted from apache commons-lang3 project.

Unescapes escaped chars in strings.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
StringEscapeUtils.AggregateTranslator

Adapted from apache commons-lang3 project.

private abstract static class
StringEscapeUtils.CharSequenceTranslator

Adapted from apache commons-lang3 project.

private static class
StringEscapeUtils.LookupTranslator

Adapted from apache commons-lang3 project.

private static class
StringEscapeUtils.OctalUnescaper

Adapted from apache commons-lang3 project.

private static class
StringEscapeUtils.UnicodeUnescaper

Adapted from apache commons-lang3 project.

Field Summary

Modifier and TypeField and Description
private static final StringEscapeUtils.CharSequenceTranslator
private static final String[][]
private static final String[][]
private static final StringEscapeUtils.CharSequenceTranslator

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static String

Returns:

String with escaped values, null if null string input
escapeJava
(final String
String to escape values in, may be null
input
)

Escapes the characters in a String using Java String rules.

public static String

Returns:

a new unescaped String, null if null string input
unescapeJava
(final String
the String to unescape, may be null
input
)

Unescapes any Java literals found in the String.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ESCAPE_JAVAback to summary
private static final StringEscapeUtils.CharSequenceTranslator ESCAPE_JAVA
JAVA_CTRL_CHARS_ESCAPEback to summary
private static final String[][] JAVA_CTRL_CHARS_ESCAPE
JAVA_CTRL_CHARS_UNESCAPEback to summary
private static final String[][] JAVA_CTRL_CHARS_UNESCAPE
UNESCAPE_JAVAback to summary
private static final StringEscapeUtils.CharSequenceTranslator UNESCAPE_JAVA

Constructor Detail

StringEscapeUtilsback to summary
private StringEscapeUtils()

Method Detail

escapeJavaback to summary
public static String escapeJava(final String input)

Escapes the characters in a String using Java String rules.

Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)

So a tab becomes the characters '\\' and 't'.

The only difference between Java strings and JavaScript strings is that in JavaScript, a single quote and forward-slash (/) are escaped.

Example:

input string: He didn't say, "Stop!"
output string: He didn't say, \"Stop!\"
Parameters
input:String

String to escape values in, may be null

Returns:String

String with escaped values, null if null string input

unescapeJavaback to summary
public static String unescapeJava(final String input)

Unescapes any Java literals found in the String. For example, it will turn a sequence of '\' and 'n' into a newline character, unless the '\' is preceded by another '\'.

This can be replaced by String::translateEscapes in JDK 13
Parameters
input:String

the String to unescape, may be null

Returns:String

a new unescaped String, null if null string input

com.github.javaparser.utils back to summary

private Class StringEscapeUtils.AggregateTranslator

extends CharSequenceTranslator
Class Inheritance

Adapted from apache commons-lang3 project.

Executes a sequence of translators one after the other. Execution ends whenever the first translator consumes codepoints from the input.

Since
3.0

Field Summary

Modifier and TypeField and Description
private final StringEscapeUtils.CharSequenceTranslator[]

Constructor Summary

AccessConstructor and Description
public
AggregateTranslator(final StringEscapeUtils.CharSequenceTranslator...
CharSequenceTranslator array to aggregate
translators
)

Specify the translators to be used at creation time.

Method Summary

Modifier and TypeMethod and Description
public int
translate(final CharSequence
CharSequence that is being translated
input
,
final int
int representing the current point of translation
index
,
final Writer
Writer to translate the text to
out
)

Implements abstract com.github.javaparser.utils.StringEscapeUtils.CharSequenceTranslator.translate.

The first translator to consume codepoints from the input is the 'winner'.
Inherited from com.github.javaparser.utils.StringEscapeUtils.CharSequenceTranslator:
translatetranslatewith

Field Detail

translatorsback to summary
private final StringEscapeUtils.CharSequenceTranslator[] translators

Constructor Detail

AggregateTranslatorback to summary
public AggregateTranslator(final StringEscapeUtils.CharSequenceTranslator... translators)

Specify the translators to be used at creation time.

Parameters
translators:StringEscapeUtils.CharSequenceTranslator[]

CharSequenceTranslator array to aggregate

Method Detail

translateback to summary
public int translate(final CharSequence input, final int index, final Writer out) throws IOException

Implements abstract com.github.javaparser.utils.StringEscapeUtils.CharSequenceTranslator.translate.

Doc from com.github.javaparser.utils.StringEscapeUtils.CharSequenceTranslator.translate.

The first translator to consume codepoints from the input is the 'winner'. Execution stops with the number of consumed codepoints being returned. Translate a set of codepoints, represented by an int index into a CharSequence, into another set of codepoints. The number of codepoints consumed must be returned, and the only IOExceptions thrown must be from interacting with the Writer so that the top level API may reliably ignore StringWriter IOExceptions.

Parameters
input:CharSequence

CharSequence that is being translated

index:int

int representing the current point of translation

out:Writer

Writer to translate the text to

Returns:int

int count of codepoints consumed

Annotations
@Override
Exceptions
IOException:
if and only if the Writer produces an IOException
com.github.javaparser.utils back to summary

private abstract Class StringEscapeUtils.CharSequenceTranslator

extends Object
Class Inheritance
  • java.lang.Object
  • com.github.javaparser.utils.StringEscapeUtils.CharSequenceTranslator
Known Direct Subclasses
com.github.javaparser.utils.StringEscapeUtils.LookupTranslator, com.github.javaparser.utils.StringEscapeUtils.AggregateTranslator, com.github.javaparser.utils.StringEscapeUtils.OctalUnescaper, com.github.javaparser.utils.StringEscapeUtils.UnicodeUnescaper

Adapted from apache commons-lang3 project.

An API for translating text. Its core use is to escape and unescape text. Because escaping and unescaping is completely contextual, the API does not present two separate signatures.

Since
3.0

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public abstract int

Returns:

int count of codepoints consumed
translate
(CharSequence
CharSequence that is being translated
input
,
int
int representing the current point of translation
index
,
Writer
Writer to translate the text to
out
)

Translate a set of codepoints, represented by an int index into a CharSequence, into another set of codepoints.

public final String

Returns:

String output of translation
translate
(final CharSequence
CharSequence to be translated
input
)

Helper for non-Writer usage.

public final void
translate(final CharSequence
CharSequence that is being translated
input
,
final Writer
Writer to translate the text to
out
)

Translate an input onto a Writer.

public final StringEscapeUtils.CharSequenceTranslator

Returns:

CharSequenceTranslator merging this translator with the others
with
(final StringEscapeUtils.CharSequenceTranslator...
CharSequenceTranslator array of translators to merge with this one
translators
)

Helper method to create a merger of this translator with another set of translators.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

CharSequenceTranslatorback to summary
private CharSequenceTranslator()

Method Detail

translateback to summary
public abstract int translate(CharSequence input, int index, Writer out) throws IOException

Translate a set of codepoints, represented by an int index into a CharSequence, into another set of codepoints. The number of codepoints consumed must be returned, and the only IOExceptions thrown must be from interacting with the Writer so that the top level API may reliably ignore StringWriter IOExceptions.

Parameters
input:CharSequence

CharSequence that is being translated

index:int

int representing the current point of translation

out:Writer

Writer to translate the text to

Returns:int

int count of codepoints consumed

Exceptions
IOException:
if and only if the Writer produces an IOException
translateback to summary
public final String translate(final CharSequence input)

Helper for non-Writer usage.

Parameters
input:CharSequence

CharSequence to be translated

Returns:String

String output of translation

translateback to summary
public final void translate(final CharSequence input, final Writer out) throws IOException

Translate an input onto a Writer. This is intentionally final as its algorithm is tightly coupled with the abstract method of this class.

Parameters
input:CharSequence

CharSequence that is being translated

out:Writer

Writer to translate the text to

Exceptions
IOException:
if and only if the Writer produces an IOException
withback to summary
public final StringEscapeUtils.CharSequenceTranslator with(final StringEscapeUtils.CharSequenceTranslator... translators)

Helper method to create a merger of this translator with another set of translators. Useful in customizing the standard functionality.

Parameters
translators:StringEscapeUtils.CharSequenceTranslator[]

CharSequenceTranslator array of translators to merge with this one

Returns:StringEscapeUtils.CharSequenceTranslator

CharSequenceTranslator merging this translator with the others

com.github.javaparser.utils back to summary

private Class StringEscapeUtils.LookupTranslator

extends CharSequenceTranslator
Class Inheritance

Adapted from apache commons-lang3 project.

Translates a value using a lookup table.

Since
3.0

Field Summary

Modifier and TypeField and Description
private final int
private final HashMap<String, String>
private final HashSet<Character>
private final int

Constructor Summary

AccessConstructor and Description
public
LookupTranslator(final CharSequence[]...
CharSequence[][] table of size [*][2]
lookup
)

Define the lookup table to be used in translation

Note that, as of Lang 3.1, the key to the lookup table is converted to a java.lang.String.

Method Summary

Modifier and TypeMethod and Description
public int
translate(final CharSequence
CharSequence that is being translated
input
,
final int
int representing the current point of translation
index
,
final Writer
Writer to translate the text to
out
)

Implements abstract com.github.javaparser.utils.StringEscapeUtils.CharSequenceTranslator.translate.

Translate a set of codepoints, represented by an int index into a CharSequence, into another set of codepoints.
Inherited from com.github.javaparser.utils.StringEscapeUtils.CharSequenceTranslator:
translatetranslatewith

Field Detail

longestback to summary
private final int longest
lookupMapback to summary
private final HashMap<String, String> lookupMap
prefixSetback to summary
private final HashSet<Character> prefixSet
shortestback to summary
private final int shortest

Constructor Detail

LookupTranslatorback to summary
public LookupTranslator(final CharSequence[]... lookup)

Define the lookup table to be used in translation

Note that, as of Lang 3.1, the key to the lookup table is converted to a java.lang.String. This is because we need the key to support hashCode and equals(Object), allowing it to be the key for a HashMap. See LANG-882.

Parameters
lookup:CharSequence[][]

CharSequence[][] table of size [*][2]

Method Detail

translateback to summary
public int translate(final CharSequence input, final int index, final Writer out) throws IOException

Implements abstract com.github.javaparser.utils.StringEscapeUtils.CharSequenceTranslator.translate.

Doc from com.github.javaparser.utils.StringEscapeUtils.CharSequenceTranslator.translate.

Translate a set of codepoints, represented by an int index into a CharSequence, into another set of codepoints. The number of codepoints consumed must be returned, and the only IOExceptions thrown must be from interacting with the Writer so that the top level API may reliably ignore StringWriter IOExceptions.

Parameters
input:CharSequence

CharSequence that is being translated

index:int

int representing the current point of translation

out:Writer

Writer to translate the text to

Returns:int

int count of codepoints consumed

Annotations
@Override
Exceptions
IOException:
if and only if the Writer produces an IOException
com.github.javaparser.utils back to summary

private Class StringEscapeUtils.OctalUnescaper

extends CharSequenceTranslator
Class Inheritance

Adapted from apache commons-lang3 project.

Translate escaped octal Strings back to their octal values.

For example, "\45" should go back to being the specific value (a %).

Note that this currently only supports the viable range of octal for Java; namely 1 to 377. This is because parsing Java is the main use case.

Since
3.0

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private boolean

Returns:

true if the given char is the character representation of one of the digits from 0 to 7
isOctalDigit
(final char
the char to check
ch
)

Checks if the given char is an octal digit.

private boolean

Returns:

true if the given char is the character representation of one of the digits from 0 to 3
isZeroToThree
(final char
the char to check
ch
)

Checks if the given char is the character representation of one of the digit from 0 to 3.

public int
translate(final CharSequence
CharSequence that is being translated
input
,
final int
int representing the current point of translation
index
,
final Writer
Writer to translate the text to
out
)

Implements abstract com.github.javaparser.utils.StringEscapeUtils.CharSequenceTranslator.translate.

Translate a set of codepoints, represented by an int index into a CharSequence, into another set of codepoints.
Inherited from com.github.javaparser.utils.StringEscapeUtils.CharSequenceTranslator:
translatetranslatewith

Constructor Detail

OctalUnescaperback to summary
private OctalUnescaper()

Method Detail

isOctalDigitback to summary
private boolean isOctalDigit(final char ch)

Checks if the given char is an octal digit. Octal digits are the character representations of the digits 0 to 7.

Parameters
ch:char

the char to check

Returns:boolean

true if the given char is the character representation of one of the digits from 0 to 7

isZeroToThreeback to summary
private boolean isZeroToThree(final char ch)

Checks if the given char is the character representation of one of the digit from 0 to 3.

Parameters
ch:char

the char to check

Returns:boolean

true if the given char is the character representation of one of the digits from 0 to 3

translateback to summary
public int translate(final CharSequence input, final int index, final Writer out) throws IOException

Implements abstract com.github.javaparser.utils.StringEscapeUtils.CharSequenceTranslator.translate.

Doc from com.github.javaparser.utils.StringEscapeUtils.CharSequenceTranslator.translate.

Translate a set of codepoints, represented by an int index into a CharSequence, into another set of codepoints. The number of codepoints consumed must be returned, and the only IOExceptions thrown must be from interacting with the Writer so that the top level API may reliably ignore StringWriter IOExceptions.

Parameters
input:CharSequence

CharSequence that is being translated

index:int

int representing the current point of translation

out:Writer

Writer to translate the text to

Returns:int

int count of codepoints consumed

Annotations
@Override
Exceptions
IOException:
if and only if the Writer produces an IOException
com.github.javaparser.utils back to summary

private Class StringEscapeUtils.UnicodeUnescaper

extends CharSequenceTranslator
Class Inheritance

Adapted from apache commons-lang3 project.

Translates escaped Unicode values of the form \\u+\d\d\d\d back to Unicode. It supports multiple 'u' characters and will work with or without the +.

Since
3.0

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public int
translate(final CharSequence
CharSequence that is being translated
input
,
final int
int representing the current point of translation
index
,
final Writer
Writer to translate the text to
out
)

Implements abstract com.github.javaparser.utils.StringEscapeUtils.CharSequenceTranslator.translate.

Translate a set of codepoints, represented by an int index into a CharSequence, into another set of codepoints.
Inherited from com.github.javaparser.utils.StringEscapeUtils.CharSequenceTranslator:
translatetranslatewith

Constructor Detail

UnicodeUnescaperback to summary
private UnicodeUnescaper()

Method Detail

translateback to summary
public int translate(final CharSequence input, final int index, final Writer out) throws IOException

Implements abstract com.github.javaparser.utils.StringEscapeUtils.CharSequenceTranslator.translate.

Doc from com.github.javaparser.utils.StringEscapeUtils.CharSequenceTranslator.translate.

Translate a set of codepoints, represented by an int index into a CharSequence, into another set of codepoints. The number of codepoints consumed must be returned, and the only IOExceptions thrown must be from interacting with the Writer so that the top level API may reliably ignore StringWriter IOExceptions.

Parameters
input:CharSequence

CharSequence that is being translated

index:int

int representing the current point of translation

out:Writer

Writer to translate the text to

Returns:int

int count of codepoints consumed

Annotations
@Override
Exceptions
IOException:
if and only if the Writer produces an IOException