Class ReportEncoder
The methods in this class are substitutes for java.net.URLEncoder.encode()
and
java.net.URLDecoder.decode()
.
The de- and encoding uses the same coding mechanism as JavaScript, special characters are
replaced with %hex
where hex is a two digit hex number.
Note: On the client side (browser) instead of using corresponding escape
and unescape
JavaScript functions, better use encodeURIComponent
and
decodeURIComponent
functions which are work properly with unicode characters.
These functions are supported in IE 5.5+ and NS 6+ only.
-
Field Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic String
adjustHtmlEncoding
(String input, String encoding) Adjusts the given String by making sure all characters that can be displayed in the given charset are contained as chars, whereas all other non-displayable characters are converted to HTML entities.static byte[]
changeEncoding
(byte[] input, String oldEncoding, String newEncoding) Changes the encoding of a byte array that represents a String.static String
createString
(byte[] bytes, String encoding) Creates a String out of a byte array with the specified encoding, falling back to the system default in case the encoding name is not valid.static String
Decodes a String using UTF-8 encoding, which is the standard for http data transmission with GET ant POST requests.static String
This method is a substitute forURLDecoder.decode()
.static String
decodeHtmlEntities
(String input, String encoding) Decodes HTML entity references like€
that are contained in the String to a regular character, but only if that character is contained in the given encodings charset.static String
decodeParameter
(String input) Decodes a string used as parameter in an uri in a way independent of other encodings/decodings applied before.static String
Encodes a String using UTF-8 encoding, which is the standard for http data transmission with GET ant POST requests.static String
This method is a substitute forURLEncoder.encode()
.static String
encodeHtmlEntities
(String input, String encoding) Encodes all characters that are contained in the String which can not displayed in the given encodings charset with HTML entity references like€
.static String
encodeJavaEntities
(String input, String encoding) Encodes all characters that are contained in the String which can not displayed in the given encodings charset with Java escaping like€
.static String
encodeParameter
(String input) Encodes a string used as parameter in an uri in a way independent of other encodings/decodings applied later.static String
Encodes a String in a way that is compatible with the JavaScript escape function.static String
escapeHtml
(String source) Escapes special characters in a HTML-String with their number-based entity representation, for example & becomes &.static String
escapeNonAscii
(String source) Escapes non ASCII characters in a HTML-String with their number-based entity representation, for example & becomes &.static String
escapeWBlanks
(String source, String encoding) Encodes a String in a way that is compatible with the JavaScript escape function.static String
Escapes a String so it may be printed as text content or attribute value in a HTML page or an XML file.static String
Escapes a String so it may be printed as text content or attribute value in a HTML page or an XML file.static String
lookupEncoding
(String encoding, String fallback) Checks if a given encoding name is actually supported, and if so resolves it to it's canonical name, if not it returns the given fallback value.static String
Decodes a String in a way that is compatible with the JavaScript unescape function.
-
Field Details
-
ENCODING_ISO_8859_1
Constant for the standardISO-8859-1
encoding.- See Also:
-
ENCODING_US_ASCII
Constant for the standardUS-ASCII
encoding.- See Also:
-
ENCODING_UTF_8
Constant for the standardUTF-8
encoding.Default encoding for JavaScript decodeUriComponent methods is
UTF-8
by w3c standard.- See Also:
-
-
Method Details
-
adjustHtmlEncoding
Adjusts the given String by making sure all characters that can be displayed in the given charset are contained as chars, whereas all other non-displayable characters are converted to HTML entities.Just calls
decodeHtmlEntities(String, String)
first and feeds the result toencodeHtmlEntities(String, String)
.- Parameters:
input
- the input to adjust the HTML encoding forencoding
- the charset to encode the result with\- Returns:
- the input with the decoded/encoded HTML entities
-
changeEncoding
Changes the encoding of a byte array that represents a String.- Parameters:
input
- the byte array to convertoldEncoding
- the current encoding of the byte arraynewEncoding
- the new encoding of the byte array- Returns:
- the byte array encoded in the new encoding
-
createString
Creates a String out of a byte array with the specified encoding, falling back to the system default in case the encoding name is not valid.Use this method as a replacement for
new String(byte[], encoding)
to avoid possible encoding problems.- Parameters:
bytes
- the bytes to decodeencoding
- the encoding scheme to use for decoding the bytes- Returns:
- the bytes decoded to a String
-
decode
Decodes a String using UTF-8 encoding, which is the standard for http data transmission with GET ant POST requests.- Parameters:
source
- the String to decode- Returns:
- String the decoded source String
-
decode
This method is a substitute forURLDecoder.decode()
.In case you don't know what encoding to use, set the value of the
encoding
parameter tonull
. This method will then default to UTF-8 encoding, which is probably the right one.- Parameters:
source
- The string to decodeencoding
- The encoding to use (if null, the system default is used)- Returns:
- The decoded source String
-
decodeHtmlEntities
Decodes HTML entity references like€
that are contained in the String to a regular character, but only if that character is contained in the given encodings charset.- Parameters:
input
- the input to decode the HTML entities inencoding
- the charset to decode the input for- Returns:
- the input with the decoded HTML entities
- See Also:
-
decodeParameter
Decodes a string used as parameter in an uri in a way independent of other encodings/decodings applied before.- Parameters:
input
- the encoded parameter string- Returns:
- the decoded parameter string
- See Also:
-
encode
Encodes a String using UTF-8 encoding, which is the standard for http data transmission with GET ant POST requests.- Parameters:
source
- the String to encode- Returns:
- String the encoded source String
-
encode
This method is a substitute forURLEncoder.encode()
.In case you don't know what encoding to use, set the value of the
encoding
parameter tonull
. This method will then default to UTF-8 encoding, which is probably the right one.- Parameters:
source
- the String to encodeencoding
- the encoding to use (if null, the system default is used)- Returns:
- the encoded source String
-
encodeHtmlEntities
Encodes all characters that are contained in the String which can not displayed in the given encodings charset with HTML entity references like€
.This is required since a Java String is internally always stored as Unicode, meaning it can contain almost every character, but the HTML charset used might not support all such characters.
- Parameters:
input
- the input to encode for HTMLencoding
- the charset to encode the result with- Returns:
- the input with the encoded HTML entities
- See Also:
-
encodeJavaEntities
Encodes all characters that are contained in the String which can not displayed in the given encodings charset with Java escaping like€
.This can be used to escape values used in Java property files.
- Parameters:
input
- the input to encode for Javaencoding
- the charset to encode the result with- Returns:
- the input with the encoded Java entities
-
encodeParameter
Encodes a string used as parameter in an uri in a way independent of other encodings/decodings applied later.Used to ensure that GET parameters are not wrecked by wrong or incompatible configuration settings. In order to ensure this, the String is first encoded with html entities for any character that cannot encoded in US-ASCII; additionally, the plus sign is also encoded to avoid problems with the white-space replacer. Finally, the entity prefix is replaced with characters not used as delimiters in urls.
- Parameters:
input
- the parameter string- Returns:
- the encoded parameter string
-
escape
Encodes a String in a way that is compatible with the JavaScript escape function.- Parameters:
source
- The text to be encodedencoding
- the encoding type- Returns:
- The JavaScript escaped string
-
escapeHtml
Escapes special characters in a HTML-String with their number-based entity representation, for example & becomes &.A character
num
is replaced if
((ch != 32) && ((ch > 122) || (ch < 48) || (ch == 60) || (ch == 62)))
- Parameters:
source
- the String to escape- Returns:
- String the escaped String
- See Also:
-
escapeNonAscii
Escapes non ASCII characters in a HTML-String with their number-based entity representation, for example & becomes &.A character
num
is replaced if
(ch > 255)
- Parameters:
source
- the String to escape- Returns:
- String the escaped String
- See Also:
-
escapeWBlanks
Encodes a String in a way that is compatible with the JavaScript escape function. Multiple blanks are encoded _multiply _with%20
.- Parameters:
source
- The text to be encodedencoding
- the encoding type- Returns:
- The JavaScript escaped string
-
escapeXml
Escapes a String so it may be printed as text content or attribute value in a HTML page or an XML file.This method replaces the following characters in a String:
- < with <
- > with >
- & with &
- " with "
- Parameters:
source
- the string to escape- Returns:
- the escaped string
- See Also:
-
escapeXml
Escapes a String so it may be printed as text content or attribute value in a HTML page or an XML file.This method replaces the following characters in a String:
- < with <
- > with >
- & with &
- " with "
- Parameters:
source
- the string to escapedoubleEscape
- iffalse
, all entities that already are escaped are left untouched- Returns:
- the escaped string
- See Also:
-
lookupEncoding
Checks if a given encoding name is actually supported, and if so resolves it to it's canonical name, if not it returns the given fallback value.Charsets have a set of aliases. For example, valid aliases for "UTF-8" are "UTF8", "utf-8" or "utf8". This method resolves any given valid charset name to it's "canonical" form, so that simple String comparison can be used when checking charset names internally later.
Please see http://www.iana.org/assignments/character-sets for a list of valid charset alias names.
- Parameters:
encoding
- the encoding to check and resolvefallback
- the fallback encoding scheme- Returns:
- the resolved encoding name, or the fallback value
-
unescape
Decodes a String in a way that is compatible with the JavaScript unescape function.- Parameters:
source
- The String to be decodedencoding
- the encoding type- Returns:
- The JavaScript unescaped String
-