java.lang.Object
org.bzdev.net.WebEncoder
Encoder for HTML and Javascript strings.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
formEncode
(Map<String, String> map) Convert a map of keyword-value pairs to application/x-www-form-encoded data, using a default charset with ampersands separating the keyword-value pairs.static String
formEncode
(Map<String, String> map, boolean semicolonDelimiter) Convert a map of keyword-value pairs to application/x-www-form-encoded data, using a default charset.static String
formEncode
(Map<String, String> map, boolean semicolonDelimiter, Charset charset) Convert a map of keyword-value pairs to application/x-www-form-encoded data, given a charset for representing strings.static String
htmlEncode
(String string) Encode a string so that it will be displayed as is in an HTML page.static String
quoteEncode
(String string) Encode a string so that it can be used inside a Javascript string.
-
Constructor Details
-
WebEncoder
public WebEncoder()
-
-
Method Details
-
htmlEncode
Encode a string so that it will be displayed as is in an HTML page. If the string appears in an element's attribute value, the attribute is assumed to be delimited by double quotes: the characters encoded are '&', '"', '>' and '<'.- Parameters:
string
- the input string- Returns:
- the encoded string; null if the input string is null
-
quoteEncode
Encode a string so that it can be used inside a Javascript string. The characters that are encoded are the '\', '"', newline, form-feed, carriage return, "'", backspace, and tab characters, which are encoded as '\\', '\"', '\n', '\f', '\r', "\'" '\b', and '\t' respectively.- Parameters:
string
- the input string- Returns:
- the encoded string; null if the input string is null
-
formEncode
Convert a map of keyword-value pairs to application/x-www-form-encoded data, using a default charset with ampersands separating the keyword-value pairs. The default charset is UTF-8. The order in which each keyword-value pair appears is determined by the map's iterator.- Parameters:
map
- the map of keyword-value pairs (both elements are strings)- Returns:
- a String containing the URL-encoded data.
-
formEncode
Convert a map of keyword-value pairs to application/x-www-form-encoded data, using a default charset. The default charset is UTF-8. The order in which each keyword-value pair appears is determined by the map's iterator.- Parameters:
map
- the map of keyword-value pairs (both elements are strings)semicolonDelimiter
- true if the delimiter should be a semicolon (";") rather than an ampersand ("&").- Returns:
- a String containing the URL-encoded data.
-
formEncode
public static String formEncode(Map<String, String> map, boolean semicolonDelimiter, Charset charset) Convert a map of keyword-value pairs to application/x-www-form-encoded data, given a charset for representing strings. The order in which each keyword-value pair appears is determined by the map's iterator. Some servers may use a semicolon instead of an ampersand to simply the placement of URLs with queries in HTML documents as HTML uses ampersands to start an HTML entity. This is not an issue for HTML forms and hence the ampersand is frequently used. The charset should normally be UTF-8. A query string is first converted to a sequence of bytes using the specified charset and URL percent encoding is then applied. After that encoding the delimiters are added. Charsets used for URLs have U.S. ASCII as a subset, but the percent encodings generated for other characters will depend on the charset.- Parameters:
map
- the map of keyword-value pairs (both elements are strings)semicolonDelimiter
- true if the delimiter should be a semicolon (";") rather than an ampersand ("&").charset
- the charset used to represent strings.- Returns:
- a Sting containing the URL-encoded data.
-