public class HtmlUtil extends Object
Constructor and Description |
---|
HtmlUtil() |
Modifier and Type | Method and Description |
---|---|
static void |
appendAttribute(StringBuilder buf,
String name,
Object value)
Appends an escaped attribute (name='value') to the passed buffer,
which is assumed to contain an in-process HTML element string.
|
static void |
appendOptionalAttribute(StringBuilder buf,
String name,
Object value)
Appends an escaped attribute (name='value') to the passed buffer,
which is assumed to contain an in-process HTML element string, iff
the attribute value is neither
null nor an empty string. |
static String |
buildQueryString(Map<?,?> params,
boolean ignoreEmpty)
Constructs a query string out of a parameter map, URL-encoding values.
|
static String |
escape(String src)
Replaces characters in the passed string with entities:
< replaced with <
> replaced with >
& replaced with &
" replaced with "
' replaced with ' (this allows single quotes to be used
in attribute values that are delimited by single quotes)
any non-ASCII character (ie, those with codepoints > 127) with a
hexadecimal Unicode escape
If passed
null , returns an empty string. |
static String |
htmlToText(String input)
Converts HTML to plain text, according to the following rules:
Replaces any newlines or carriage returns in the source text with single spaces.
|
static Map<String,String> |
parseQueryString(String query,
boolean ignoreEmpty)
Parses a query string into a parameter map, decoding values as needed.
|
static String |
unescape(String src)
Replaces entity references in the passed string with their character
values.
|
static String |
urlDecode(String src)
A wrapper around
URLDecoder that always decodes as
UTF-8, and replaces its checked exception with a RuntimeException
(that should never be thrown). |
static String |
urlEncode(String src)
A wrapper around
URLEncoder that always encodes to UTF-8,
replaces its checked exception with a RuntimeException (that should
never be thrown), and encodes spaces as "%20" rather than "+". |
public static String urlEncode(String src)
URLEncoder
that always encodes to UTF-8,
replaces its checked exception with a RuntimeException (that should
never be thrown), and encodes spaces as "%20" rather than "+".
If passed null, will return an empty string.
public static String urlDecode(String src)
URLDecoder
that always decodes as
UTF-8, and replaces its checked exception with a RuntimeException
(that should never be thrown).
If passed null, will return an empty string.
public static String escape(String src)
null
, returns an empty string.public static String unescape(String src)
If passed null
, returns an empty string. If passed a
string without any entity escapes, returns that string unchanged.
public static void appendAttribute(StringBuilder buf, String name, Object value)
If value
is null
, will append the
attribute with an empty string value. See appendOptionalAttribute(java.lang.StringBuilder, java.lang.String, java.lang.Object)
if you want to skip null values.
public static void appendOptionalAttribute(StringBuilder buf, String name, Object value)
null
nor an empty string.public static String buildQueryString(Map<?,?> params, boolean ignoreEmpty)
URLBuilder
, but does not support
multiple values per parameter.params
- The map of parameter values. This must contain only
scalar values: both key and value are converted to
strings before being added to the query string.ignoreEmpty
- If true
, entries with empty or null
values are not added to the string; if
false
, they're added in the form
"name=
"name=value
" pairs
separated by ampersands. There's no leading question mark.public static Map<String,String> parseQueryString(String query, boolean ignoreEmpty)
query
- The query string: zero or more name=value
pairs separated by ampersands, with or without a
leading question mark.ignoreEmpty
- If true
, ignores any entries without a
value (eg, "name=
"; if false
these are added to the map with an empty string for
the value.RuntimeException
- on any failure.public static String htmlToText(String input)
<P>
and <BR>
with newlines.
<LI>
with newline followed by "* ".