org.apache.maven.doxia.util

Class HtmlTools

public class HtmlTools extends Object

The HtmlTools class defines methods to HTML handling.

Since: 1.0

Version: $Id: HtmlTools.java 566741 2007-08-16 15:01:27Z ltheussl $

Author: Vincent Siveton

Method Summary
static StringencodeFragment(String text)
Replace all characters in a text.
static StringencodeId(String id)
Construct a valid id.
static StringencodeURL(String url)
Encode an url
static StringescapeHTML(String text)
Escape special characters in a text in HTML.
static booleanisId(String text)
Determines if the specified text is a valid id according to the rules laid out in encodeId(String).

Method Detail

encodeFragment

public static String encodeFragment(String text)
Replace all characters in a text.
 HtmlTools.encodeFragment( null ) = null
 HtmlTools.encodeFragment( "" ) = ""
 HtmlTools.encodeFragment( "http://www.google.com" ) = "httpwwwgooglecom"
 

Parameters: text the String to check, may be null

Returns: the text with only letter and digit, null if null String input

encodeId

public static String encodeId(String id)
Construct a valid id.

According to the HTML 4.01 specification section 6.2 SGML basic types:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

According to XHTML 1.0 section C.8. Fragment Identifiers:

When defining fragment identifiers to be backward-compatible, only strings matching the pattern [A-Za-z][A-Za-z0-9:_.-]* should be used.

To achieve this we need to convert the id String. Two conversions are necessary and one is done to get prettier ids:

  1. If the first character is not a letter, prepend the id with the letter 'a'
  2. A space is replaced with an underscore '_'
  3. Remove whitespace at the start and end before starting to process

For letters, the case is preserved in the conversion.

Here are some examples:

 HtmlTools.encodeId( null )        = null
 HtmlTools.encodeId( "" )          = ""
 HtmlTools.encodeId( " _ " )       = "a_"
 HtmlTools.encodeId( "1" )         = "a1"
 HtmlTools.encodeId( "1anchor" )   = "a1anchor"
 HtmlTools.encodeId( "_anchor" )   = "a_anchor"
 HtmlTools.encodeId( "a b-c123 " ) = "a_b-c123"
 HtmlTools.encodeId( "   anchor" ) = "anchor"
 HtmlTools.encodeId( "myAnchor" )  = "myAnchor"
 

Parameters: id The id to be encoded

Returns: The id trimmed and encoded

encodeURL

public static String encodeURL(String url)
Encode an url

Parameters: url the String to encode, may be null

Returns: the text encoded, null if null String input

escapeHTML

public static String escapeHTML(String text)
Escape special characters in a text in HTML.
 < becomes &lt;
 > becomes &gt;
 & becomes &amp;
 " becomes &quot;
 

Parameters: text the String to escape, may be null

Returns: the text escaped, "" if null String input

isId

public static boolean isId(String text)
Determines if the specified text is a valid id according to the rules laid out in encodeId(String).

Parameters: text The text to be tested

Returns: true if the text is a valid id, otherwise false

See Also: encodeId

Copyright © 2002-2011 Apache Software Foundation. All Rights Reserved.