public class HTML
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
private static java.util.regex.Pattern |
AMP |
private static java.util.regex.Pattern |
AMPERSAND |
private static java.util.regex.Pattern |
AT_OR_DOT |
private static java.util.regex.Pattern |
BACKSLASH |
private static java.util.regex.Pattern |
DOUBLE_QUOTE |
static java.util.regex.Pattern |
EMAIL_REGEXP
This pattern can be used to find email addresses.
|
private static java.util.regex.Pattern |
GT |
private static java.util.regex.Pattern |
HASH |
private static java.util.regex.Pattern |
IMAGE_EXTENSION |
private static java.util.regex.Pattern |
LEADING_TRAILING_LINEBREAKS |
static java.util.regex.Pattern |
LINEBREAKS_REGEXP
This pattern can be used to find line breaks.
|
static int |
LINK_EMAIL
This flag is used in
scanForLinks(String,int)
when you want to create links for email addresses. |
static int |
LINK_URL
This flag is used in
scanForLinks(String,int)
when you want to create links for URL:s. |
private static java.util.regex.Pattern |
LT |
static java.util.regex.Pattern |
MARKUP
Pattern that matches everything inside a HTML tag.
|
private static java.util.regex.Pattern |
NEWLINE |
private static java.util.regex.Pattern |
PERCENT |
private static java.util.regex.Pattern |
PLUS |
private static java.util.regex.Pattern |
QUOTE |
static java.util.regex.Pattern |
SAFE_TAGS
This pattern is a list of HTML tags considered "safe".
|
private static java.util.regex.Pattern |
SINGLE_QUOTE |
static int |
SMART_IMAGES
This flag is used in
scanForLinks(String,int)
when you want to create links for images with a <img> tag instead of
an <a> tag. |
private static java.util.regex.Pattern |
SPACE |
static java.util.regex.Pattern |
TAG_REGEXP
This pattern can be used to fins HTML tags.
|
static java.util.regex.Pattern |
URL_REGEXP
This pattern can be used to find URL:s.
|
Constructor and Description |
---|
HTML() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
encodeTags(java.lang.String in)
Scans a string for HTML tags and replaces all & with &,
< with <, > with > and all " with "
|
static java.lang.String |
encodeTags(java.lang.String in,
java.util.regex.Pattern safeTags)
Scans a string for HTML tags and replaces all < and > for tags
not found matching the
safeTags pattern with < and >
respectively. |
static java.lang.String |
encodeTags(java.lang.String in,
java.lang.String safeTags)
Scans a string for HTML tags and replaces all < and > for tags
not found in the
safeTags pattern with < and >
respectively. |
static java.lang.String |
formatLineBreaks(java.lang.String in)
Finds all linebreaks in a string and replaces them with a <br> tag, except
that leading and trailing linebreaks will be removed.
|
static boolean |
isValidEmail(java.lang.String email)
Checks if the given string looks like an email address.
|
static boolean |
isValidUrl(java.lang.String url)
Checks if the given string looks like an URL.
|
static java.lang.String |
javaScriptEncode(java.lang.String in)
Escape a string to make it safe for use in a JavaScript statement.
|
static java.lang.String |
niceFormat(java.lang.String in)
A convenience method for doing
encodeTags(in, SAFE_TAGS) ,
scanForLinks(in, "_blank") and formatLineBreaks(in) in a single operation. |
static java.lang.String |
niceFormat(java.lang.String in,
int flags)
A convenience method for doing
encodeTags(in, SAFE_TAGS) ,
scanForLinks(in, flags, "_blank") and
formatLineBreaks(in) in a single operation. |
static java.lang.String |
niceFormat(java.lang.String in,
int flags,
java.lang.String linkTarget)
A convenience method for doing
encodeTags(in, SAFE_TAGS) ,
scanForLinks(in, flags, link_target)
and formatLineBreaks(in) in a single operation. |
static java.lang.String |
niceFormat(java.lang.String in,
java.lang.String linkTarget)
A convenience method for doing
encodeTags(in, SAFE_TAGS) ,
scanForLinks(in, target) and
formatLineBreaks(in) in a single operation. |
static java.lang.String |
scanForLinks(java.lang.String in)
Scans a string for email addresses and URL:s and replaces them
with
<a href="---"> tags. |
static java.lang.String |
scanForLinks(java.lang.String in,
int flags)
Scans a string for email addresses and URL:s and replaces them
with
<a href="---"> tags. |
static java.lang.String |
scanForLinks(java.lang.String in,
int flags,
java.lang.String target)
Scans a string for email addresses and URL:s and replaces them
with
<a href="---"> tags. |
static java.lang.String |
scanForLinks(java.lang.String in,
java.lang.String target)
Scans a string for email addresses and URL:s and replaces them
with
<a href="---"> tags. |
static java.lang.String |
stripMarkup(java.lang.CharSequence in)
Remove all HTML markup in a string and return what is left.
|
static int |
textLength(java.lang.CharSequence html)
Counts the length of a string ignoring all characters in HTML markup tags.
|
static java.lang.String |
urlEncode(java.lang.String in)
Encode URL-unsafe characters in a string.
|
public static final int LINK_URL
scanForLinks(String,int)
when you want to create links for URL:s.public static final int LINK_EMAIL
scanForLinks(String,int)
when you want to create links for email addresses.public static final int SMART_IMAGES
scanForLinks(String,int)
when you want to create links for images with a <img> tag instead of
an <a> tag.public static final java.util.regex.Pattern EMAIL_REGEXP
public static final java.util.regex.Pattern URL_REGEXP
http://
, https://
, ftp://
or
www.
followed by at least one subdomain and one topdomain followed by
an optional port number and an optional path including query information.public static final java.util.regex.Pattern LINEBREAKS_REGEXP
public static final java.util.regex.Pattern TAG_REGEXP
public static final java.util.regex.Pattern SAFE_TAGS
public static final java.util.regex.Pattern MARKUP
private static final java.util.regex.Pattern LEADING_TRAILING_LINEBREAKS
private static final java.util.regex.Pattern AT_OR_DOT
private static final java.util.regex.Pattern IMAGE_EXTENSION
private static final java.util.regex.Pattern AMP
private static final java.util.regex.Pattern LT
private static final java.util.regex.Pattern GT
private static final java.util.regex.Pattern QUOTE
private static final java.util.regex.Pattern PERCENT
private static final java.util.regex.Pattern PLUS
private static final java.util.regex.Pattern SPACE
private static final java.util.regex.Pattern HASH
private static final java.util.regex.Pattern AMPERSAND
private static final java.util.regex.Pattern BACKSLASH
private static final java.util.regex.Pattern NEWLINE
private static final java.util.regex.Pattern SINGLE_QUOTE
private static final java.util.regex.Pattern DOUBLE_QUOTE
public static boolean isValidEmail(java.lang.String email)
EMAIL_REGEXP
pattern.email
- The string to checkEMAIL_REGEXP
public static boolean isValidUrl(java.lang.String url)
URL_REGEXP
pattern.url
- The string to checkURL_REGEXP
public static java.lang.String formatLineBreaks(java.lang.String in)
in
- The string to searchpublic static java.lang.String scanForLinks(java.lang.String in)
<a href="---">
tags.
Using this method is equivalent to:
scanForLinks(in, LINK_URL+LINK_EMAIL+SMART_IMAGES, null);
in
- The string to searchpublic static java.lang.String scanForLinks(java.lang.String in, int flags)
<a href="---">
tags.
Using this method is equivalent to:
scanForLinks(in, flags, null);
in
- The string to searchflags
- A combination of the following flags to indicate what we should
search for: LINK_URL
, LINK_EMAIL
SMART_IMAGES
public static java.lang.String scanForLinks(java.lang.String in, java.lang.String target)
<a href="---">
tags.
Using this method is equivalent to:
scanForLinks(in, LINK_URL+LINK_EMAIL+SMART_IMAGES, target);
in
- The string to searchtarget
- The name of the target window in which the link should be openedpublic static java.lang.String scanForLinks(java.lang.String in, int flags, java.lang.String target)
<a href="---">
tags.in
- The string to searchflags
- A combination of the following flags to indicate what we should
search for: LINK_URL
, LINK_EMAIL
SMART_IMAGES
target
- The name of the target window in which the link should be openedpublic static java.lang.String encodeTags(java.lang.String in)
in
- The string to searchpublic static java.lang.String encodeTags(java.lang.String in, java.lang.String safeTags)
safeTags
pattern with < and >
respectively. Tags that are found in the safeTags
pattern
are not modified. Using this method is equivalent to
encodeTags(in, Pattern.compile(safeTags));
in
- The string to searchsafeTags
- A regular expression that should match all
safe tagsSAFE_TAGS
public static java.lang.String encodeTags(java.lang.String in, java.util.regex.Pattern safeTags)
safeTags
pattern with < and >
respectively. Tags that matches the safeTags
pattern
are not modified.in
- The string to searchsafeTags
- A regular expression pattern that matches all safe tagsSAFE_TAGS
public static java.lang.String urlEncode(java.lang.String in)
in
- The string to encodepublic static java.lang.String javaScriptEncode(java.lang.String in)
in
- String to escape.public static java.lang.String niceFormat(java.lang.String in)
encodeTags(in, SAFE_TAGS)
,
scanForLinks(in, "_blank")
and formatLineBreaks(in)
in a single operation.in
- The string to formatpublic static java.lang.String niceFormat(java.lang.String in, int flags)
encodeTags(in, SAFE_TAGS)
,
scanForLinks(in, flags, "_blank")
and
formatLineBreaks(in)
in a single operation.in
- The string to formatflags
- Flags to be used in the call to scanForLinks
public static java.lang.String niceFormat(java.lang.String in, java.lang.String linkTarget)
encodeTags(in, SAFE_TAGS)
,
scanForLinks(in, target)
and
formatLineBreaks(in)
in a single operation.in
- The string to formatlinkTarget
- The target parameter to be used in the call to
scanForLinks(String,String)
public static java.lang.String niceFormat(java.lang.String in, int flags, java.lang.String linkTarget)
encodeTags(in, SAFE_TAGS)
,
scanForLinks(in, flags, link_target)
and formatLineBreaks(in)
in a single operation.in
- The string to formatflags
- Flags to be used in the call to scanForLinks
linkTarget
- The target parameter to be used in the call to
scanForLinks(String,int,String)
public static java.lang.String stripMarkup(java.lang.CharSequence in)
in
- The string to strip from HTMLpublic static int textLength(java.lang.CharSequence html)
html
- The HTML string