Package com.javahollic.jira.emh.api.util
Interface ISafeTextUtils
-
public interface ISafeTextUtils
a public interface for documentation, showing a restricted set of methods available for textutils use in velocity contexts re: JEMH-8129
-
-
Method Summary
All Methods Instance Methods Abstract Methods Deprecated Methods Modifier and Type Method Description String
br(String s)
Convert line breaks to html<br>
tag.String
closeTags(String str)
Search through a String for any tags that have been opened and append closing tags for those that have not been closed.String
colorToHex(Color c)
Convert Color to html hex string.String
extractNumber(String in)
Extract a number from a String.Color
hexToColor(String color)
Convert html hex string to Color.String
html(String s)
Deprecated.use htmlEncode(String) or htmlEncode(String, boolean) instead.String
htmlEncode(String s)
Escape html entity characters BUT NOT high characters.String
htmlEncode(String s, boolean encodeSpecialChars)
Escape html entity characters and high characters (eg "curvy" Word quotes).String
hyperlink(String text)
Convert all URLs and E-mail addresses in a string into hyperlinks.String
hyperlink(String text, String target)
Convert all URLs and E-mail addresses in a string into hyperlinks.String
indent(String string, int indentSize, boolean initialLine)
Indent a String with line-breaks.String
innerTrim(String s)
Returns a string that has whitespace removed from both ends of the String, as well as duplicate whitespace removed from within the String.String
join(String glue, String[] pieces)
Join an array of Strings together.String
join(String glue, Collection<String> pieces)
Join a Collection of Strings together.String
join(String glue, Iterator<String> pieces)
Join an Iteration of Strings together.String
leadingSpaces(String s)
Finds all leading spaces on each line and replaces it with an HTML space ( )String
left(String s, int n)
Returns the leftmost n chars of the string.String
linkEmail(String string)
Wrap all email addresses in specified string with href tags.String
linkURL(String str)
Wrap all urls ('http://', 'www.', and 'ftp://') in specified string with href tags.String
linkURL(String str, String target)
Wrap all urls ('abc://' and 'www.abc') in specified string with href tags.String
list(String str)
Create <li> elements in a piece of plain text; Will convert lines starting with - or *.String
noNull(String string)
Returnstring
, or""
ifstring
isnull
.String
noNull(String string, String defaultString)
Returnstring
, ordefaultString
ifstring
isnull
or""
.boolean
parseBoolean(String in)
Convert a String to an boolean.Date
parseDate(String year, String month, String day)
Given 3 Strings representing the the year, month and day, return a Date object.double
parseDouble(String in)
Convert a String to a double.float
parseFloat(String in)
Convert a String to a float.int
parseInt(String in)
Convert a String to an int.long
parseLong(String in)
Convert a String to a long.String
plainTextToHtml(String str)
Converts plain text to html code.String
plainTextToHtml(String str, boolean encodeSpecialChars)
String
plainTextToHtml(String str, String target)
String
plainTextToHtml(String str, String target, boolean encodeSpecialChars)
Converts plain text to html code.String
removeAndInsert(String str, int removeAndInsertStart, int removeEnd, String insertStr)
Removes part of the original string starting at removeAndInsertStart and ending at removeEnd.String
slashes(String s)
Escape chars that need slashes in front of them.boolean
stringSet(String string)
Check whetherstring
has been set to something other than""
ornull
.String
trimToEndingChar(String str, int len)
Trim a String to the specified length.boolean
verifyEmail(String email)
Verify that the given string is a valid email address.boolean
verifyUrl(String url)
Verify That the given String is in valid URL format.String
wrapParagraph(String s)
Wrap paragraphs in html<p>
tags.
-
-
-
Method Detail
-
br
String br(String s)
Convert line breaks to html<br>
tag.- Parameters:
s
- the String to convert- Returns:
- the converted string
-
closeTags
String closeTags(String str)
Search through a String for any tags that have been opened and append closing tags for those that have not been closed.- Parameters:
str
- A string possibly containing unclosed HTML tags- Returns:
- the converted string #see #SINGLE_TAGS
-
colorToHex
String colorToHex(Color c)
Convert Color to html hex string. (#012345)- Parameters:
c
- the Color to convert- Returns:
- A string with a hexadecimal RGB encoding
-
extractNumber
String extractNumber(String in)
Extract a number from a String. Example" 12345" -> "12345" "hello123bye" -> "123" "a2b4c6 8 " -> "2468" " -22" -> "-22" "5.512" -> "5.512" "1.2.3.4" -> "1.234" ".2" -> "0.2" "-555.7" -> "-555.7" "-..6" -> "-0.6" "abc- dx.97 9" -> "-0.979" "�1,000,000.00 per year" -> "1000000.00" "" -> "0" "asdsf" -> "0" "123." -> "123" null -> "0"
- Parameters:
in
- Original String containing number to be extracted.- Returns:
- String stripped of all non-numeric chars.
- See Also:
parseInt(String)
,parseLong(String)
-
hexToColor
Color hexToColor(String color)
Convert html hex string to Color. If the hexadecimal string is not a valid character,Color.black
is returned. Only the first six hexadecimal characters are considered; any extraneous values are discarded. Also, a leading "#", if any, is allowed (and ignored).- Parameters:
color
- the String (in RGB hexadecimal format) to convert- Returns:
- the java.awt.Color
-
html
String html(String s)
Deprecated.use htmlEncode(String) or htmlEncode(String, boolean) instead.Escape html entity characters and high characters (eg "curvy" Word quotes). Note this method can also be used to encode XML now- Parameters:
s
- unsafe html- Returns:
- safe html
-
htmlEncode
String htmlEncode(String s)
Escape html entity characters BUT NOT high characters. Note this method can also be used to encode XML now- Parameters:
s
- unsafe text- Returns:
- html safe text
-
htmlEncode
String htmlEncode(String s, boolean encodeSpecialChars)
Escape html entity characters and high characters (eg "curvy" Word quotes). Note this method can also be used to encode XML.- Parameters:
s
- the String to escape.encodeSpecialChars
- if true high characters will be encode other wise not.- Returns:
- the escaped string
-
hyperlink
String hyperlink(String text)
Convert all URLs and E-mail addresses in a string into hyperlinks.- Parameters:
text
- The block of text to hyperlink.- Returns:
- the text with known uri formats hyperlinked
- See Also:
hyperlink(String, String)
-
hyperlink
String hyperlink(String text, String target)
Convert all URLs and E-mail addresses in a string into hyperlinks.- Parameters:
text
- The block of text to hyperlink.target
- The target attribute to use for href (optional).- Returns:
- the text with known uri formats hyperlinked
- See Also:
linkEmail(String)
,linkURL(String)
-
indent
String indent(String string, int indentSize, boolean initialLine)
Indent a String with line-breaks.- Parameters:
string
- String to indent.indentSize
- Number of spaces to indent by. 0 will indent using a tab.initialLine
- Whether to indent initial line.- Returns:
- Indented string.
-
innerTrim
String innerTrim(String s)
Returns a string that has whitespace removed from both ends of the String, as well as duplicate whitespace removed from within the String.- Parameters:
s
- string to trim- Returns:
- trimmed string
-
join
String join(String glue, Iterator<String> pieces)
Join an Iteration of Strings together. Example// get Iterator of Strings ("abc","def","123"); Iterator i = getIterator(); out.print(TextUtils.join(", ", i)); // prints: "abc, def, 123"
- Parameters:
glue
- Token to place between Strings.pieces
- Iteration of Strings to join.- Returns:
- String presentation of joined Strings.
-
join
String join(String glue, String[] pieces)
Join an array of Strings together.- Parameters:
glue
- Token to place between Strings.pieces
- Array of Strings to join.- Returns:
- String presentation of joined Strings.
- See Also:
join(String, java.util.Iterator)
-
join
String join(String glue, Collection<String> pieces)
Join a Collection of Strings together.- Parameters:
glue
- Token to place between Strings.pieces
- Collection of Strings to join.- Returns:
- String presentation of joined Strings.
- See Also:
join(String, java.util.Iterator)
-
leadingSpaces
String leadingSpaces(String s)
Finds all leading spaces on each line and replaces it with an HTML space ( )- Parameters:
s
- string containing text to replaced with - Returns:
- the new string
-
left
String left(String s, int n)
Returns the leftmost n chars of the string. If n is larger than the length of the string, return the whole string unchanged.- Parameters:
s
- - the string to operate on.n
- - the number of chars to return.- Returns:
- leftmost
-
linkEmail
String linkEmail(String string)
Wrap all email addresses in specified string with href tags.- Parameters:
string
- The block of text to check.- Returns:
- String The block of text with all email addresses placed in href tags.
-
linkURL
String linkURL(String str)
Wrap all urls ('http://', 'www.', and 'ftp://') in specified string with href tags.- Parameters:
str
- The block of text to check.- Returns:
- String The block of text with all url's placed in href tags.
-
linkURL
String linkURL(String str, String target)
Wrap all urls ('abc://' and 'www.abc') in specified string with href tags.- Parameters:
str
- The block of text to check.target
- The target to use for the href (optional).- Returns:
- String The block of text with all url's placed in href tags.
-
list
String list(String str)
Create <li> elements in a piece of plain text; Will convert lines starting with - or *.- Parameters:
str
- A string, possibly containing a plaintext "list"- Returns:
- a converted string
-
noNull
String noNull(String string, String defaultString)
Returnstring
, ordefaultString
ifstring
isnull
or""
. Never returnsnull
.Examples:
// prints "hello" String s=null; System.out.println(TextUtils.noNull(s,"hello"); // prints "hello" s=""; System.out.println(TextUtils.noNull(s,"hello"); // prints "world" s="world"; System.out.println(TextUtils.noNull(s, "hello");
- Parameters:
string
- the String to check.defaultString
- The default string to return ifstring
isnull
or""
- Returns:
string
ifstring
is non-empty, anddefaultString
otherwise- See Also:
stringSet(java.lang.String)
-
noNull
String noNull(String string)
Returnstring
, or""
ifstring
isnull
. Never returnsnull
.Examples:
// prints 0 String s = null; System.out.println(TextUtils.noNull(s).length()); // prints 1 s = "a"; System.out.println(TextUtils.noNull(s).length());
- Parameters:
string
- the String to check- Returns:
- a valid (non-null) string reference
-
parseBoolean
boolean parseBoolean(String in)
Convert a String to an boolean. Accepts: 1/0, yes/no, true/false - case insensitive. If the value does not map to "true,",false
is returned.- Parameters:
in
- String to be parsed.- Returns:
- boolean representation of String.
-
parseDate
Date parseDate(String year, String month, String day)
Given 3 Strings representing the the year, month and day, return a Date object. This is only valid for Gregorian calendars.If the day cannot be determined, 1st will be used. If the month cannot be determined, Jan will be used.
- Parameters:
year
- Year : 4 digitmonth
- Month : 1 or 2 digit (1=jan, 2=feb, ...) or name (jan, JAN, January, etc). If null, default is Jan.day
- Day of month : 1 or 2 digit, prefix will be stripped (1, 30, 05, 3rd). If null, default is 1st.- Returns:
- date
-
parseDouble
double parseDouble(String in)
Convert a String to a double.- Parameters:
in
- String containing number to be parsed.- Returns:
- Double value of number or 0 if error.
- See Also:
extractNumber(String)
-
parseFloat
float parseFloat(String in)
Convert a String to a float.- Parameters:
in
- String containing number to be parsed.- Returns:
- Float value of number or 0 if error.
- See Also:
extractNumber(String)
-
parseInt
int parseInt(String in)
Convert a String to an int. Truncates numbers if it's a float string; for example, 4.5 yields a value of 4.- Parameters:
in
- String containing number to be parsed.- Returns:
- Integer value of number or 0 if error.
- See Also:
extractNumber(String)
-
parseLong
long parseLong(String in)
Convert a String to a long. Truncates numbers if it's a float or double string; for example, 4.5 yields a value of 4.- Parameters:
in
- String containing number to be parsed.- Returns:
- Long value of number or 0 if error.
- See Also:
extractNumber(String)
-
plainTextToHtml
String plainTextToHtml(String str)
Converts plain text to html code.- escapes appropriate characters
- puts in line breaks
- hyperlinks link and email addresses
- Parameters:
str
- - String containing the plain text.- Returns:
- the escaped string
-
plainTextToHtml
String plainTextToHtml(String str, String target, boolean encodeSpecialChars)
Converts plain text to html code.- escapes appropriate characters
- puts in line breaks
- hyperlinks link and email addresses
- Parameters:
str
- - String containing the plain text.target
- - Target for href tags (optional).encodeSpecialChars
- - if true high characters will be encode other wise not.- Returns:
- the escaped string
-
removeAndInsert
String removeAndInsert(String str, int removeAndInsertStart, int removeEnd, String insertStr)
Removes part of the original string starting at removeAndInsertStart and ending at removeEnd. Then insert another string at the same position where the part was removed (ie. at removeAndInsertStart).- Parameters:
str
- - the original string.removeAndInsertStart
- - the index within the original string at which removal is to start.removeEnd
- - the index within the original string at which removal is to end (exclusive - ie. does not remove the character at that index).insertStr
- - the string to be inserted.- Returns:
- clean string
-
slashes
String slashes(String s)
Escape chars that need slashes in front of them.- Parameters:
s
- the String to add escape characters to- Returns:
- the converted String
-
stringSet
boolean stringSet(String string)
Check whetherstring
has been set to something other than""
ornull
.- Parameters:
string
- theString
to check- Returns:
- a boolean indicating whether the string was non-empty (and non-null)
-
trimToEndingChar
String trimToEndingChar(String str, int len)
Trim a String to the specified length. However, if the cutoff point is in the middle of a sentence (remember that a String can contain many sentences), trim the string such that it ends with an ending char, which is defined in the isEndingChar() method.- Parameters:
str
- - String to trim.len
- - length to which string is to be trimmed.- Returns:
- the trimmed string
-
verifyEmail
boolean verifyEmail(String email)
Verify that the given string is a valid email address. "Validity" in this context only means that the address conforms to the correct syntax (not if the address actually exists).- Parameters:
email
- The email address to verify.- Returns:
- a boolean indicating whether the email address is correctly formatted.
-
verifyUrl
boolean verifyUrl(String url)
Verify That the given String is in valid URL format.- Parameters:
url
- The url string to verify.- Returns:
- a boolean indicating whether the URL seems to be incorrect.
-
-