org.jasig.portal.serialize
Class HTMLdtd

java.lang.Object
  extended by org.jasig.portal.serialize.HTMLdtd

public final class HTMLdtd
extends java.lang.Object

Utility class for accessing information specific to HTML documents. The HTML DTD is expressed as three utility function groups. Two methods allow for checking whether an element requires an open tag on printing (isEmptyTag(java.lang.String)) or on parsing (isOptionalClosing(java.lang.String)).

Two other methods translate character references from name to value and from value to name. A small entities resource is loaded into memory the first time any of these methods is called for fast and efficient access.

Version:
$Revision: 1.3.10.2 $ $Date: 2005/08/04 22:17:46 $
Author:
Assaf Arkin

Field Summary
private static java.util.Map _boolAttrs
           
private static java.util.Map _byName
          Table of entity name to value mapping.
private static java.util.Map _elemDefs
          Holds element definitions.
private static java.lang.String[] _entity
          Table of reverse character reference mapping.
private static int ALLOWED_HEAD
          Allowed to appear in head.
private static int CLOSE_DD_DT
          When opened, closes DD or DT.
private static int CLOSE_P
          When opened, closes P.
private static int CLOSE_SELF
          When opened, closes itself.
private static int CLOSE_TABLE
          When opened, closes another table section.
private static int CLOSE_TH_TD
          When opened, closes TH or TD.
private static int ELEM_CONTENT
          Element contains element content only.
private static int EMPTY
          Element is empty (also means only opening tag)
private static java.lang.String ENTITIES_RESOURCE
          Locates the HTML entities file that is loaded upon initialization.
static java.lang.String HTMLPublicId
          Public identifier for HTML 4.01 (Strict) document type.
static java.lang.String HTMLSystemId
          System identifier for HTML 4.01 (Strict) document type.
private static int ONLY_OPENING
          Only opening tag should be printed.
private static int OPT_CLOSING
          Optional closing tag.
private static int PRESERVE
          Element preserve spaces.
static java.lang.String XHTMLPublicId
          Public identifier for XHTML 1.0 (Strict) document type.
static java.lang.String XHTMLSystemId
          System identifier for XHTML 1.0 (Strict) document type.
 
Constructor Summary
HTMLdtd()
           
 
Method Summary
static int charFromName(java.lang.String name)
          Returns the value of an HTML character reference by its name.
private static void defineBoolean(java.lang.String tagName, java.lang.String attrName)
           
private static void defineBoolean(java.lang.String tagName, java.lang.String[] attrNames)
           
private static void defineElement(java.lang.String name, int flags)
           
private static void defineEntity(java.lang.String name, char value)
          Defines a new character reference.
static java.lang.String fromChar(int value)
          Returns the name of an HTML character reference based on its character value.
private static void initialize()
          Initialize upon first access.
static boolean isBoolean(java.lang.String tagName, java.lang.String attrName)
          Returns true if the specified attribute is a boolean and should be printed without the value.
static boolean isClosing(java.lang.String tagName, java.lang.String openTag)
          Returns true if the opening of one element (tagName) implies the closing of another open element (openTag).
private static boolean isElement(java.lang.String name, int flag)
           
static boolean isElementContent(java.lang.String tagName)
          Returns true if element is declared to have element content.
static boolean isEmptyTag(java.lang.String tagName)
          Returns true if element is declared to be empty.
static boolean isOnlyOpening(java.lang.String tagName)
          Returns true if element's closing tag is generally not printed.
static boolean isOptionalClosing(java.lang.String tagName)
          Returns true if element's closing tag is optional and need not exist.
static boolean isPreserveSpace(java.lang.String tagName)
          Returns true if element's textual contents preserves spaces.
static boolean isURI(java.lang.String tagName, java.lang.String attrName)
          Returns true if the specified attribute it a URI and should be escaped appropriately.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HTMLPublicId

public static final java.lang.String HTMLPublicId
Public identifier for HTML 4.01 (Strict) document type.

See Also:
Constant Field Values

HTMLSystemId

public static final java.lang.String HTMLSystemId
System identifier for HTML 4.01 (Strict) document type.

See Also:
Constant Field Values

XHTMLPublicId

public static final java.lang.String XHTMLPublicId
Public identifier for XHTML 1.0 (Strict) document type.

See Also:
Constant Field Values

XHTMLSystemId

public static final java.lang.String XHTMLSystemId
System identifier for XHTML 1.0 (Strict) document type.

See Also:
Constant Field Values

_entity

private static java.lang.String[] _entity
Table of reverse character reference mapping. Character codes are held as single-character strings, mapped to their reference name.


_byName

private static java.util.Map _byName
Table of entity name to value mapping. Entities are held as strings, character references as Character objects.


_boolAttrs

private static java.util.Map _boolAttrs

_elemDefs

private static java.util.Map _elemDefs
Holds element definitions.


ENTITIES_RESOURCE

private static final java.lang.String ENTITIES_RESOURCE
Locates the HTML entities file that is loaded upon initialization. This file is a resource loaded with the default class loader.

See Also:
Constant Field Values

ONLY_OPENING

private static final int ONLY_OPENING
Only opening tag should be printed.

See Also:
Constant Field Values

ELEM_CONTENT

private static final int ELEM_CONTENT
Element contains element content only.

See Also:
Constant Field Values

PRESERVE

private static final int PRESERVE
Element preserve spaces.

See Also:
Constant Field Values

OPT_CLOSING

private static final int OPT_CLOSING
Optional closing tag.

See Also:
Constant Field Values

EMPTY

private static final int EMPTY
Element is empty (also means only opening tag)

See Also:
Constant Field Values

ALLOWED_HEAD

private static final int ALLOWED_HEAD
Allowed to appear in head.

See Also:
Constant Field Values

CLOSE_P

private static final int CLOSE_P
When opened, closes P.

See Also:
Constant Field Values

CLOSE_DD_DT

private static final int CLOSE_DD_DT
When opened, closes DD or DT.

See Also:
Constant Field Values

CLOSE_SELF

private static final int CLOSE_SELF
When opened, closes itself.

See Also:
Constant Field Values

CLOSE_TABLE

private static final int CLOSE_TABLE
When opened, closes another table section.

See Also:
Constant Field Values

CLOSE_TH_TD

private static final int CLOSE_TH_TD
When opened, closes TH or TD.

See Also:
Constant Field Values
Constructor Detail

HTMLdtd

public HTMLdtd()
Method Detail

isEmptyTag

public static boolean isEmptyTag(java.lang.String tagName)
Returns true if element is declared to be empty. HTML elements are defines as empty in the DTD, not by the document syntax.

Parameters:
tagName - The element tag name (upper case)
Returns:
True if element is empty

isElementContent

public static boolean isElementContent(java.lang.String tagName)
Returns true if element is declared to have element content. Whitespaces appearing inside element content will be ignored, other text will simply report an error.

Parameters:
tagName - The element tag name (upper case)
Returns:
True if element content

isPreserveSpace

public static boolean isPreserveSpace(java.lang.String tagName)
Returns true if element's textual contents preserves spaces. This only applies to PRE and TEXTAREA, all other HTML elements do not preserve space.

Parameters:
tagName - The element tag name (upper case)
Returns:
True if element's text content preserves spaces

isOptionalClosing

public static boolean isOptionalClosing(java.lang.String tagName)
Returns true if element's closing tag is optional and need not exist. An error will not be reported for such elements if they are not closed. For example, LI is most often not closed.

Parameters:
tagName - The element tag name (upper case)
Returns:
True if closing tag implied

isOnlyOpening

public static boolean isOnlyOpening(java.lang.String tagName)
Returns true if element's closing tag is generally not printed. For example, LI should not print the closing tag.

Parameters:
tagName - The element tag name (upper case)
Returns:
True if only opening tag should be printed

isClosing

public static boolean isClosing(java.lang.String tagName,
                                java.lang.String openTag)
Returns true if the opening of one element (tagName) implies the closing of another open element (openTag). For example, every opening LI will close the previously open LI, and every opening BODY will close the previously open HEAD.

Parameters:
tagName - The newly opened element
openTag - The already opened element
Returns:
True if closing tag closes opening tag

isURI

public static boolean isURI(java.lang.String tagName,
                            java.lang.String attrName)
Returns true if the specified attribute it a URI and should be escaped appropriately. In HTML URIs are escaped differently than normal attributes.

Parameters:
tagName - The element's tag name
attrName - The attribute's name

isBoolean

public static boolean isBoolean(java.lang.String tagName,
                                java.lang.String attrName)
Returns true if the specified attribute is a boolean and should be printed without the value. This applies to attributes that are true if they exist, such as selected (OPTION/INPUT).

Parameters:
tagName - The element's tag name
attrName - The attribute's name

charFromName

public static int charFromName(java.lang.String name)
Returns the value of an HTML character reference by its name. If the reference is not found or was not defined as a character reference, returns EOF (-1).

Parameters:
name - Name of character reference
Returns:
Character code or EOF (-1)

fromChar

public static java.lang.String fromChar(int value)
Returns the name of an HTML character reference based on its character value. Only valid for entities defined from character references. If no such character value was defined, return null.

Parameters:
value - Character value of entity
Returns:
Entity's name or null

initialize

private static void initialize()
Initialize upon first access. Will load all the HTML character references into a list that is accessible by name or character value and is optimized for character substitution. This method may be called any number of times but will execute only once.


defineEntity

private static void defineEntity(java.lang.String name,
                                 char value)
Defines a new character reference. The reference's name and value are supplied. Nothing happens if the character reference is already defined.

Unlike internal entities, character references are a string to single character mapping. They are used to map non-ASCII characters both on parsing and printing, primarily for HTML documents. '<amp;' is an example of a character reference.

Parameters:
name - The entity's name
value - The entity's value

defineElement

private static void defineElement(java.lang.String name,
                                  int flags)

defineBoolean

private static void defineBoolean(java.lang.String tagName,
                                  java.lang.String attrName)

defineBoolean

private static void defineBoolean(java.lang.String tagName,
                                  java.lang.String[] attrNames)

isElement

private static boolean isElement(java.lang.String name,
                                 int flag)