public class HtmlUtil
extends java.lang.Object
Constructor and Description |
---|
HtmlUtil() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
getHtmlIcon(java.lang.String src,
java.lang.String alt)
Build an icon
|
static java.lang.String |
getHtmlIcon(java.lang.String src,
java.lang.String alt,
java.lang.String title)
Build an icon
|
static java.lang.String |
getHtmlIcon(java.lang.String src,
java.lang.String alt,
java.lang.String title,
java.lang.String css)
Build an icon
|
static java.lang.String |
getHtmlIcon(java.lang.String src,
java.lang.String alt,
java.lang.String title,
java.lang.String css,
java.lang.String htmlAttributes)
Build an icon
|
static java.lang.String |
getSpanIcon(java.lang.String css,
java.lang.String alt,
java.lang.String title,
java.lang.String htmlAttributes)
Build a span icon
|
static java.lang.String |
html2text(java.lang.String html)
Extract all text from the specified html and returns it.
|
static boolean |
isSameHtml(java.lang.String htmlFragment1,
java.lang.String htmlFragment2)
Check if the two HTML fragment are the same.
|
static void |
traverse(org.jsoup.nodes.Node node,
org.jsoup.select.NodeVisitor visitor,
int depth,
boolean reverse)
Traverse the tree of the specified HTML node.
|
static void |
trimHtml(org.jsoup.nodes.Element element)
Remove leading and trailing empty node from the specified JSoup element.
|
static java.lang.String |
truncate(java.lang.String fragmentHtml,
int maxTextLength)
Truncate the specified HTML fragment to the maximum text length specified.
|
static java.lang.String |
truncate(java.lang.String fragmentHtml,
int maxTextLength,
java.lang.String suffix)
Truncate the specified HTML fragment to the maximum text length specified.
|
public static java.lang.String truncate(java.lang.String fragmentHtml, int maxTextLength)
Important remark regarding behavior :
Specified HTML fragment is parsed using an HTML parser, thus when the HTML is
rewritten it WILL be modified according to default output rule of the parser.
Therefore line break, indentation and other whitespace may be lost during the process, but
the output should not be changed as seen from the web browser.
fragmentHtml
- the HTML fragment to truncatemaxTextLength
- the maximum length of text to keep (does not include HTML tag, comment and attribute length)public static java.lang.String truncate(java.lang.String fragmentHtml, int maxTextLength, java.lang.String suffix)
Inserting an optionnal suffix inside the block element in which text was truncated.
Important remark regarding behavior :
Specified HTML fragment is parsed using an HTML parser, thus when the HTML is
rewritten it WILL be modified according to default output rule of the parser.
Therefore line break, indentation and other whitespace may be lost during the process, but
the output should not be changed as seen from the web browser.
Example (notice the indentation and HTML compliance output):
String html = "<div><p>Hello <span>World!</span></p></div>"; String suffix = "<a href='#'> Read More...</a>"; assertEquals("<div>\n <p>Hello <span>Wo</span><a href=\"#\"> Read More...</a></p>\n</div>", HtmlUtil.truncate(html, 8, suffix));
fragmentHtml
- the HTML fragment to truncatemaxTextLength
- the maximum length of text to keep (does not include HTML tag, comment and attribute length)suffix
- a suffix to append inside the first truncated HTML nodepublic static void traverse(org.jsoup.nodes.Node node, org.jsoup.select.NodeVisitor visitor, int depth, boolean reverse)
Contrary to native JSoup NodeTraversor
, this
implementation is resurive and is robust to node removal.
node
- node to traversevisitor
- the visitor to usedepth
- current depthreverse
- set to true to traverse in reverse order (starting from the end), in which case head is invoked for the "closing" part of an Nodepublic static void trimHtml(org.jsoup.nodes.Element element)
Example of use :
final String html = "<p> <br/> </p> \n" + "<p> Hello <strong>World!</strong>! </p> \n" + "<p> <br/> </p> "; org.jsoup.nodes.Document doc = Jsoup.parseBodyFragment(html); HtmlUtil.trimHtml(doc.body()); assertEquals("<p> Hello <strong>World!</strong>! </p>", doc.body().html());
element
- the JSoup Element to trim, usually the Document body.public static java.lang.String html2text(java.lang.String html)
html
- the html from which text should be extractedpublic static java.lang.String getHtmlIcon(java.lang.String src, java.lang.String alt)
src
- the image sourcealt
- the iamge titlepublic static java.lang.String getHtmlIcon(java.lang.String src, java.lang.String alt, java.lang.String title)
src
- the image sourcealt
- the image alttitle
- the image titlepublic static java.lang.String getHtmlIcon(java.lang.String src, java.lang.String alt, java.lang.String title, java.lang.String css)
src
- the image sourcealt
- the image alttitle
- the image titlecss
- the css for class=""
attribute. "icon" is set by default.public static java.lang.String getHtmlIcon(java.lang.String src, java.lang.String alt, java.lang.String title, java.lang.String css, java.lang.String htmlAttributes)
src
- the image sourcealt
- the image alttitle
- the image titlecss
- the css for class=""
attribute. "icon" is set by default.htmlAttributes
- the html attributespublic static java.lang.String getSpanIcon(java.lang.String css, java.lang.String alt, java.lang.String title, java.lang.String htmlAttributes)
css
- the css valuealt
- the alternative text for the glyph icontitle
- the title attributehtmlAttributes
- the html attributespublic static boolean isSameHtml(java.lang.String htmlFragment1, java.lang.String htmlFragment2)
htmlFragment1
- the first HTML fragment to comparehtmlFragment2
- the second HTML fragment to compareCopyright © 2001-2017 Jalios SA. All Rights Reserved.