org.apache.maven.doxia.util
Class DoxiaUtils

java.lang.Object
  extended by org.apache.maven.doxia.util.DoxiaUtils

public class DoxiaUtils
extends java.lang.Object

General Doxia utility methods. The methods in this class should not assume any specific Doxia module or document format.

Since:
1.1
Version:
$Id: DoxiaUtils.java 733395 2009-01-10 23:09:40Z ltheussl $
Author:
ltheussl

Method Summary
static java.lang.String encodeId(java.lang.String id)
          Construct a valid Doxia id.
static boolean isExternalLink(java.lang.String link)
          Checks if the given string corresponds to an external URI, ie is not a link within the same document nor a relative link to another document (a local link) of the same site.
static boolean isInternalLink(java.lang.String link)
          Checks if the given string corresponds to an internal link, ie it is a link to an anchor within the same document.
static boolean isLocalLink(java.lang.String link)
          Checks if the given string corresponds to a relative link to another document within the same site, ie it is neither an internal nor an external link.
static boolean isValidId(java.lang.String text)
          Determines if the specified text is a valid id according to the rules laid out in encodeId(String).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isInternalLink

public static boolean isInternalLink(java.lang.String link)
Checks if the given string corresponds to an internal link, ie it is a link to an anchor within the same document.

Parameters:
link - The link to check.
Returns:
True if the link starts with "#".
See Also:
isExternalLink(String), isLocalLink(String)

isExternalLink

public static boolean isExternalLink(java.lang.String link)
Checks if the given string corresponds to an external URI, ie is not a link within the same document nor a relative link to another document (a local link) of the same site.

Parameters:
link - The link to check.
Returns:
True if the link (ignoring case) starts with either "http:/", "https:/", "ftp:/", "mailto:", "file:/", or contains the string "://". Note that Windows style separators "\" are not allowed for URIs, see http://www.ietf.org/rfc/rfc2396.txt , section 2.4.3.
See Also:
isInternalLink(String), isLocalLink(String)

isLocalLink

public static boolean isLocalLink(java.lang.String link)
Checks if the given string corresponds to a relative link to another document within the same site, ie it is neither an internal nor an external link.

Parameters:
link - The link to check.
Returns:
True if the link is neither an external nor an internal link.
See Also:
isExternalLink(String), isInternalLink(String)

encodeId

public static java.lang.String encodeId(java.lang.String id)
Construct a valid Doxia id.

A valid Doxia id obeys the same constraints as an HTML ID or NAME token. 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. Remove whitespace at the start and end before starting to process
  2. If the first character is not a letter, prepend the id with the letter 'a'
  3. Any spaces are replaced with an underscore '_'
  4. Any characters not matching the above pattern are replaced according to the rules specified in the HTML specs.

For letters, the case is preserved in the conversion.

Here are some examples:

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

Parameters:
id - The id to be encoded.
Returns:
The trimmed and encoded id, or null if id is null.

isValidId

public static boolean isValidId(java.lang.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(String)


Copyright © 2005-2009 The Apache Software Foundation. All Rights Reserved.