Class HTMLUtilities
- java.lang.Object
-
- ghidra.util.HTMLUtilities
-
public class HTMLUtilities extends java.lang.Object
A helper class providing static methods for formatting text with common HTML tags.Many clients use this class to render content as HTML. Below are a few use cases along with the method that should be used for each.
Use Case Function Description A client wishes to display a simple text message (that itself contains no HTML markup) as HTML. The message may contain newline characters. toHTML(String)
The given text has all newline characters (\n) replaced with <BR> tags so that the HTML display of the text will visually display multiple lines. Also, the final text is prepended with <HTML> so that the Java HTML rendering engine will render the result as HTML. A client wishes to display a simple text message (that itself may or may not contain HTML markup) as HTML. Further, the client wishes to not only split lines at newline characters, but also wishes to ensure that no line is longer than a specified limit. toWrappedHTML(String)
ortoWrappedHTML(String, int)
This text works the same as toHTML(String)
with the addition of line-wrapping text that passes the given cutoff.A client wishes to display a text message with dynamic content, unknown at the time of programming. toLiteralHTML(String, int)
This method works the same as toWrappedHTML(String)
, with the addition of 'friendly encoding', or escaping, any embedded HTML content. The effect of this is that any existing HTML markup is not rendered as HTML, but is displayed as plain text.A client wishes to display, as a tooltip, a text message with dynamic content, unknown at the time of programming. Tooltips are unique from general HTML in that we want them to share a common line wrapping length. toLiteralHTMLForTooltip(String)
This method works the same as toLiteralHTML(String, int)
, with the addition of capping the max text length, as well as setting the line-wrap length toDEFAULT_MAX_LINE_LENGTH
.A client wishes to convert newlines in text into HTML line breaks, without adding HTML tags around the text, which allows them to embed this text into a larger HTML document. lineWrapWithHTMLLineBreaks(String)
orlineWrapWithHTMLLineBreaks(String, int)
This first method will simply convert all newline characters to <BR> tags. The second method adds the ability to trigger line-wrapping at the given length as well.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
BLUE
static java.lang.String
BR
static java.lang.String
DARK_CYAN
static java.lang.String
GRAY
static java.lang.String
GREEN
static java.lang.String
HTML
static java.lang.String
HTML_CLOSE
static java.lang.String
HTML_NEW_LINE
static java.lang.String
HTML_SPACE
static java.lang.String
LINK_PLACEHOLDER_CLOSE
static java.lang.String
LINK_PLACEHOLDER_OPEN
static java.lang.String
MAROON
static java.lang.String
OLIVE
static java.lang.String
ORANGE
static java.lang.String
PINK
static java.lang.String
PRE
static java.lang.String
PRE_CLOSE
static java.lang.String
PURPLE
static java.lang.String
YELLOW
-
Constructor Summary
Constructors Constructor Description HTMLUtilities()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
bold(java.lang.String text)
Surrounds the specified text with the HTML begin and end tags for bold.static boolean
charNeedsHTMLEscaping(int codePoint)
Tests a unicode code point (i.e., 32 bit character) to see if it needs to be escaped before being added to a HTML document because it is non-printable or a non-standard control characterstatic java.lang.String
colorString(java.awt.Color color, java.lang.String text)
Surrounds the indicated text with HTML font coloring tags so that the text will display in color within HTML.static java.lang.String
colorString(java.lang.String rgbColor, int value)
Surrounds the indicated numeric value with HTML font coloring tags so that the numeric value will display in color within HTML.static java.lang.String
colorString(java.lang.String rgbColor, java.lang.String text)
Surrounds the indicated text with HTML font coloring tags so that the text will display in color within HTML.static java.lang.String
convertLinkPlaceholdersToHyperlinks(java.lang.String text)
Takes HTML text wrapped bywrapWithLinkPlaceholder(String, String)
and replaces the custom link comment tags with HTML anchor (A) tags, where the HREF value is the value that was in the CONTENT attribute.static java.lang.String
escapeHTML(java.lang.String text)
Escapes any HTML special characters in the specified text.static java.lang.String
friendlyEncodeHTML(java.lang.String text)
Converts any special or reserved characters in the specified string into HTML-escaped entities.static java.lang.String
fromHTML(java.lang.String text)
Checks the given string to see it is HTML, according toBasicHTML
and then will return the text without any markup tags if it is.static boolean
isHTML(java.lang.String text)
Returns true if the given text is HTML.static java.lang.String
italic(java.lang.String text)
Surrounds the specified text with the HTML begin and end tags for italic.static java.lang.String
lineWrapWithHTMLLineBreaks(java.lang.String text)
This is just a convenience call tolineWrapWithHTMLLineBreaks(String, int)
with a max line length of 0, which signals to not to wrap on line length, but only on newline characters.static java.lang.String
lineWrapWithHTMLLineBreaks(java.lang.String text, int maxLineLength)
Replaces all newline characters with HTML <BR> tags.static java.lang.String
setFont(java.lang.String text, java.awt.Color color, int ptSize)
Sets the font size and color of the given text by wrapping it in <font> tags.static java.lang.String
setFontSize(java.lang.String text, int ptSize)
Sets the font size of the given text by wrapping it in <font> tags.static java.lang.String
spaces(int num)
Creates a string with the indicated number of HTML space characters (
).static java.lang.String
toHexString(java.awt.Color color)
Returns a color string of the format #RRGGBB.static java.lang.String
toHTML(java.lang.String text)
Convert the given string to HTML by adding the HTML tag and replacing new line chars with HTML <BR> tags.static java.lang.String
toLiteralHTML(java.lang.String text, int maxLineLength)
A convenience method to split the given HTML into lines, based on the given length, and then tofriendlyEncodeHTML(String)
the text.static java.lang.String
toLiteralHTMLForTooltip(java.lang.String text)
A very specific method that will: Make sure the HTML length is clipped to a reasonable size Escape any embedded HTML (so that it is not interpreted as HTML) Put the entire result in HTMLstatic java.lang.String
toRGBString(java.awt.Color color)
Returns a color string of the format rrrgggbbb.static java.lang.String
toWrappedHTML(java.lang.String text)
This is just a convenience method to calltoWrappedHTML(String, int)
with a max line length of 75.static java.lang.String
toWrappedHTML(java.lang.String text, int maxLineLength)
Similar totoHTML(String)
in that it will wrap the given text in HTML tags and split the content into multiple lines.static java.lang.String
underline(java.lang.String text)
Surrounds the specified text with the HTML begin and end tags for underlined text.static java.lang.String
wrapAsHTML(java.lang.String text)
Marks the given text as HTML in order to be rendered thusly by Java widgets.static java.lang.String
wrapWithLinkPlaceholder(java.lang.String htmlText, java.lang.String content)
Returns the given text wrapped inLINK_PLACEHOLDER_OPEN
and close tags.
-
-
-
Field Detail
-
HTML
public static final java.lang.String HTML
- See Also:
- Constant Field Values
-
HTML_CLOSE
public static final java.lang.String HTML_CLOSE
- See Also:
- Constant Field Values
-
BR
public static final java.lang.String BR
- See Also:
- Constant Field Values
-
PRE
public static final java.lang.String PRE
- See Also:
- Constant Field Values
-
PRE_CLOSE
public static final java.lang.String PRE_CLOSE
- See Also:
- Constant Field Values
-
LINK_PLACEHOLDER_OPEN
public static final java.lang.String LINK_PLACEHOLDER_OPEN
- See Also:
- Constant Field Values
-
LINK_PLACEHOLDER_CLOSE
public static final java.lang.String LINK_PLACEHOLDER_CLOSE
- See Also:
- Constant Field Values
-
HTML_SPACE
public static java.lang.String HTML_SPACE
-
HTML_NEW_LINE
public static java.lang.String HTML_NEW_LINE
-
MAROON
public static final java.lang.String MAROON
- See Also:
- Constant Field Values
-
GREEN
public static final java.lang.String GREEN
- See Also:
- Constant Field Values
-
BLUE
public static final java.lang.String BLUE
- See Also:
- Constant Field Values
-
PURPLE
public static final java.lang.String PURPLE
- See Also:
- Constant Field Values
-
DARK_CYAN
public static final java.lang.String DARK_CYAN
- See Also:
- Constant Field Values
-
OLIVE
public static final java.lang.String OLIVE
- See Also:
- Constant Field Values
-
ORANGE
public static final java.lang.String ORANGE
- See Also:
- Constant Field Values
-
PINK
public static final java.lang.String PINK
- See Also:
- Constant Field Values
-
YELLOW
public static final java.lang.String YELLOW
- See Also:
- Constant Field Values
-
GRAY
public static final java.lang.String GRAY
- See Also:
- Constant Field Values
-
-
Method Detail
-
wrapAsHTML
public static java.lang.String wrapAsHTML(java.lang.String text)
Marks the given text as HTML in order to be rendered thusly by Java widgets.- Parameters:
text
- the original text- Returns:
- the text marked as HTML
-
colorString
public static java.lang.String colorString(java.awt.Color color, java.lang.String text)
Surrounds the indicated text with HTML font coloring tags so that the text will display in color within HTML. The given color will be converted to its hex value.- Parameters:
color
- The Java color object to usetext
- the original text- Returns:
- the string for HTML colored text
-
colorString
public static java.lang.String colorString(java.lang.String rgbColor, java.lang.String text)
Surrounds the indicated text with HTML font coloring tags so that the text will display in color within HTML.- Parameters:
rgbColor
- (eg. "#8c0000") a string indicating the RGB hexadecimal colortext
- the original text- Returns:
- the string for HTML colored text
-
colorString
public static java.lang.String colorString(java.lang.String rgbColor, int value)
Surrounds the indicated numeric value with HTML font coloring tags so that the numeric value will display in color within HTML.- Parameters:
rgbColor
- (eg. "#8c0000") a string indicating the RGB hexadecimal colorvalue
- the numeric value to be converted to text and wrapped with color tags.- Returns:
- the string for the HTML colored number
-
spaces
public static java.lang.String spaces(int num)
Creates a string with the indicated number of HTML space characters (
).- Parameters:
num
- the number of HTML spaces- Returns:
- the string o HTML spaces
-
bold
public static java.lang.String bold(java.lang.String text)
Surrounds the specified text with the HTML begin and end tags for bold.- Parameters:
text
- the original text- Returns:
- the text with the bold HTML tags
-
underline
public static java.lang.String underline(java.lang.String text)
Surrounds the specified text with the HTML begin and end tags for underlined text.- Parameters:
text
- the original text- Returns:
- the text with the underline HTML tags
-
italic
public static java.lang.String italic(java.lang.String text)
Surrounds the specified text with the HTML begin and end tags for italic.- Parameters:
text
- the original text- Returns:
- the text with the italic HTML tags
-
isHTML
public static boolean isHTML(java.lang.String text)
Returns true if the given text is HTML. For this to be true, the text must begin with the <HTML> tag.- Parameters:
text
- the text to check- Returns:
- true if the given text is HTML
-
setFontSize
public static java.lang.String setFontSize(java.lang.String text, int ptSize)
Sets the font size of the given text by wrapping it in <font> tags.- Parameters:
text
- the text to sizeptSize
- the point size of the text- Returns:
- the updated String
-
setFont
public static java.lang.String setFont(java.lang.String text, java.awt.Color color, int ptSize)
Sets the font size and color of the given text by wrapping it in <font> tags.- Parameters:
text
- the text to sizecolor
- the color of the textptSize
- the point size of the text- Returns:
- the updated String
-
wrapWithLinkPlaceholder
public static java.lang.String wrapWithLinkPlaceholder(java.lang.String htmlText, java.lang.String content)
Returns the given text wrapped inLINK_PLACEHOLDER_OPEN
and close tags. If foo is passed for the HTML text, with a content value of 123456, then the output will look like:<!-- LINK CONTENT="123456" -->foo<!-- /LINK -->
- Parameters:
htmlText
- the HTML text to wrapcontent
- the value that will be put into the CONTENT section of the generated HTML. This can later be retrieved by clients transforming this text.- Returns:
- the wrapped text
-
convertLinkPlaceholdersToHyperlinks
public static java.lang.String convertLinkPlaceholdersToHyperlinks(java.lang.String text)
Takes HTML text wrapped bywrapWithLinkPlaceholder(String, String)
and replaces the custom link comment tags with HTML anchor (A) tags, where the HREF value is the value that was in the CONTENT attribute.- Parameters:
text
- the text for which to replace the markup- Returns:
- the updated text
-
toHTML
public static java.lang.String toHTML(java.lang.String text)
Convert the given string to HTML by adding the HTML tag and replacing new line chars with HTML <BR> tags.- Parameters:
text
- The text to convert to HTML- Returns:
- the converted text
-
toWrappedHTML
public static java.lang.String toWrappedHTML(java.lang.String text)
This is just a convenience method to calltoWrappedHTML(String, int)
with a max line length of 75.- Parameters:
text
- The text to convert- Returns:
- converted text
-
toWrappedHTML
public static java.lang.String toWrappedHTML(java.lang.String text, int maxLineLength)
Similar totoHTML(String)
in that it will wrap the given text in HTML tags and split the content into multiple lines. The difference is that this method will split lines that pass the given maximum length and on '\n' characters. Alternatively,toHTML(String)
will only split the given text on '\n' characters.- Parameters:
text
- The text to convertmaxLineLength
- The maximum number of characters that should appear in a line; 0 signals not to wrap the line based upon length- Returns:
- converted text
-
toLiteralHTMLForTooltip
public static java.lang.String toLiteralHTMLForTooltip(java.lang.String text)
A very specific method that will:- Make sure the HTML length is clipped to a reasonable size
- Escape any embedded HTML (so that it is not interpreted as HTML)
- Put the entire result in HTML
- Parameters:
text
- the text to convert- Returns:
- the converted value.
-
friendlyEncodeHTML
public static java.lang.String friendlyEncodeHTML(java.lang.String text)
Converts any special or reserved characters in the specified string into HTML-escaped entities. Use this method when you have content containing HTML that you do not want interpreted as HTML, such as when displaying text that uses angle brackets around words.For example, consider the following
Input Output Rendered as (Without Friendly Encoding) Hi <b>mom </b> Hi <b>mom </b> Hi <b>mom </b> Hi mom - Parameters:
text
- string to be encoded- Returns:
- the encoded HTML string
-
escapeHTML
public static java.lang.String escapeHTML(java.lang.String text)
Escapes any HTML special characters in the specified text.Does not otherwise modify the input text or wrap lines.
Calling this twice will result in text being double-escaped, which will not display correctly.
See also
StringEscapeUtils#escapeHtml3(String)
if you need quote-safe html encoding.- Parameters:
text
- plain-text that might have some characters that should NOT be interpreted as HTML- Returns:
- string with any html characters replaced with equivalents
-
charNeedsHTMLEscaping
public static boolean charNeedsHTMLEscaping(int codePoint)
Tests a unicode code point (i.e., 32 bit character) to see if it needs to be escaped before being added to a HTML document because it is non-printable or a non-standard control character- Parameters:
codePoint
- character to test- Returns:
- boolean true if character should be escaped
-
toLiteralHTML
public static java.lang.String toLiteralHTML(java.lang.String text, int maxLineLength)
A convenience method to split the given HTML into lines, based on the given length, and then tofriendlyEncodeHTML(String)
the text.This method preserves all whitespace between line breaks.
Note: This method is not intended to handle text that already contains entity escaped text. The result will not render correctly as HTML.
- Parameters:
text
- the text to updatemaxLineLength
- the max line length upon which to wrap; 0 for no max length- Returns:
- the updated text
-
lineWrapWithHTMLLineBreaks
public static java.lang.String lineWrapWithHTMLLineBreaks(java.lang.String text)
This is just a convenience call tolineWrapWithHTMLLineBreaks(String, int)
with a max line length of 0, which signals to not to wrap on line length, but only on newline characters.- Parameters:
text
- the text to wrap- Returns:
- the updated text
- See Also:
lineWrapWithHTMLLineBreaks(String, int)
-
lineWrapWithHTMLLineBreaks
public static java.lang.String lineWrapWithHTMLLineBreaks(java.lang.String text, int maxLineLength)
Replaces all newline characters with HTML <BR> tags.Unlike
toWrappedHTML(String)
, this method does not add the <HTML> tag to the given text.Call this method when you wish to create your own HTML content, with parts of that content line wrapped.
- Parameters:
text
- the text to wrapmaxLineLength
- the max length of the line; 0 if no max is desired- Returns:
- the updated text
-
fromHTML
public static java.lang.String fromHTML(java.lang.String text)
Checks the given string to see it is HTML, according toBasicHTML
and then will return the text without any markup tags if it is.- Parameters:
text
- the text to convert- Returns:
- the converted String
-
toRGBString
public static java.lang.String toRGBString(java.awt.Color color)
Returns a color string of the format rrrgggbbb. As an example,Color.RED
would be returned as 255000000 (the values are padded with 0s to make to fill up 3 digits per component).- Parameters:
color
- The color to convert.- Returns:
- a string of the format rrrgggbbb.
-
toHexString
public static java.lang.String toHexString(java.awt.Color color)
Returns a color string of the format #RRGGBB. As an example,Color.RED
would be returned as #FF0000 (the values are padded with 0s to make to fill up 2 digits per component).- Parameters:
color
- The color to convert.- Returns:
- a string of the format #RRGGBB.
-
-