com.jalios.util
Class HtmlUtil

java.lang.Object
  extended by com.jalios.util.HtmlUtil

public class HtmlUtil
extends Object

Html manipulation methods.

Since:
jcms-7.1.1

Constructor Summary
HtmlUtil()
           
 
Method Summary
static String html2text(String html)
          Extract all text from the specified html and returns it.
static String truncate(String fragmentHtml, int maxTextLength)
          Truncate the specified HTML fragment to the maximum text length specified.
static String truncate(String fragmentHtml, int maxTextLength, String suffix)
          Truncate the specified HTML fragment to the maximum text length specified.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HtmlUtil

public HtmlUtil()
Method Detail

truncate

public static String truncate(String fragmentHtml,
                              int maxTextLength)
Truncate the specified HTML fragment to the maximum text length specified.

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.

Parameters:
fragmentHtml - the HTML fragment to truncate
maxTextLength - the maximum length of text to keep (does not include HTML tag, comment and attribute length)
Returns:
the cleaned and truncated HTML, never return null (return empty string if null was specified in input)
Since:
jcms-7.1.1

truncate

public static String truncate(String fragmentHtml,
                              int maxTextLength,
                              String suffix)
Truncate the specified HTML fragment to the maximum text length specified.

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));

 

Parameters:
fragmentHtml - the HTML fragment to truncate
maxTextLength - 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 node
Returns:
the cleaned and truncated HTML, never return null (return empty string if null was specified in input)
Since:
jcms-7.1.1

html2text

public static String html2text(String html)
Extract all text from the specified html and returns it.

Parameters:
html - the html from which text should be extracted
Returns:
a clear text string without any HTML content whatsoever (no tags, comment, attribute, ...). Never returns null : return an empty string if specified html was null,
Since:
jcms-7.0.3


Copyright © 2001-2010 Jalios SA. All Rights Reserved.