net.xoetrope.builder.w3c.html.tags
Class XHtmlTagHandler

java.lang.Object
  extended by net.xoetrope.builder.w3c.html.tags.XHtmlTagHandler
Direct Known Subclasses:
A, Address, BaseFont, Br, Col, Div, Font, Form, Hr, Img, Input, Map, P, Select, Span, Style, Table, Td, TextArea, XDataTagHandler, XDefaultTagHandler, XFormatTagHandler, XObjectTagHandler, XStructuralTagHandler

public abstract class XHtmlTagHandler
extends java.lang.Object

An generic processor for html tag/element processing. The handlers are managed as a singlely linked list such that navigating the parent handlers will ultimately lead to the handler for the root element. Each node stores its attribute set so the context for a particular node can always be determined.

The handler also stores the handlers for child nodes so that a complete model of the node hierarchy is created. This hierarchy can be navigated both forwards and backwards. Visitors can used to build up properties such as fonts and styling by iterating over the node hierarchy, with each node being given the option to stop the iteration should the visitor go out of scope, for example if a style applies within a DIV element it should not be applied in the parent of the DIV, so the DIV handler should stop the iteration.

The tag handlers are abstract so that derived classes are forced to define the newInstance method, this is necessary as the XHtmlBuilder holds a set of prototype objects for each tag type. Thus, to customize the handling of a particular tag type a new handler should be inserted into the builder. The builder can then instantiate the correct type of handler for each tag encountered.

Copyright (c) Xoetrope Ltd., 2002-2006

$Revision: 1.2 $

License: see License.txt


Field Summary
protected  javax.swing.text.MutableAttributeSet attribSet
           
protected  XHtmlBuilder builder
           
protected  java.util.ArrayList<XHtmlTagHandler> children
           
protected  javax.swing.JComponent comp
           
protected  java.lang.String content
           
protected  XHtmlTagHandler parent
           
protected  java.awt.Container parentComponent
           
protected  XHtmlStyle style
           
 
Constructor Summary
XHtmlTagHandler()
           
 
Method Summary
 void addChild(XHtmlTagHandler c)
          Add a child to this handler.
 boolean addElement(XHtmlTagHandler child)
          Add a child element, possibly data such as when an option is added to a SELECT element.
 void applyAlignment(java.lang.String value)
          Set the alignment for the component
 void applyStyle(java.lang.String styleStr)
          Apply a style to the component
 boolean breaksFlow()
          Does this tag break the flow?
 boolean decorateAttribute(java.lang.Object attribute)
          Decorate an attribute such as a font according to the relevant attributes that this element includes.
 void endProcessing(XComponentFactory cf)
          The closing tag has been parsed and now the element can calulate its complete setup should it be dependant on its children for any information.
 java.lang.Object getAttribute(javax.swing.text.html.HTML.Attribute name)
          Get an attribute value
 java.util.ArrayList<XHtmlTagHandler> getChildren()
          Get teh children of this node, if any
 javax.swing.JComponent getComponent()
          Get the component corresponding to this tag, if any
 java.lang.String getComponentName()
          Get the component name, preferring the document ID over the name attribute
 java.lang.String getContent()
          Get the content of this tag/element
 int getHeight(java.awt.Dimension parentSize)
          Get a width in pixels or in percent of the parent width
 int getHeight(java.awt.Dimension parentSize, double height)
          Get a height in pixels or in percent of the parent width
 double getLength(java.lang.String lenStr)
          Get the value of the width string
 XHtmlTagHandler getParent()
          Get the parent handler
 java.lang.String getTextStyleAttributes(javax.swing.JComponent comp, boolean openingTag)
          Get the text formatting tags for this element
 int getWidth(java.awt.Dimension parentSize)
          Get a width in pixels or in percent of the parent width
 int getWidth(java.awt.Dimension parentSize, double width)
          Get a width in pixels or in percent of the parent width
abstract  XHtmlTagHandler newInstance(XHtmlBuilder builder, XHtmlTagHandler parent)
          Create a new instance of this object
 void processComment(java.lang.String comment)
          Process the element comment
 void processCommonAttributes(java.lang.String attribName, java.lang.Object attribValue)
          Process the common html attributes, includeing style, class, id, and lang.
 void processText(XComponentFactory cf, java.lang.String text)
          Process the element text
 void setBuilder(XHtmlBuilder htmlBuilder)
          Set the html builder that instantiated this tag handler and that owns the document being processed.
 void setComponent(javax.swing.JComponent c)
          Set the component used to represent this tag
 void setParent(XHtmlTagHandler p)
          Set the parent handler for this object
 void startProcessing(XHtmlBuilder htmlBuilder, XComponentFactory cf, javax.swing.text.MutableAttributeSet as)
          Process the opening html tag/element
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

parent

protected XHtmlTagHandler parent

comp

protected javax.swing.JComponent comp

attribSet

protected javax.swing.text.MutableAttributeSet attribSet

content

protected java.lang.String content

children

protected java.util.ArrayList<XHtmlTagHandler> children

parentComponent

protected java.awt.Container parentComponent

builder

protected XHtmlBuilder builder

style

protected XHtmlStyle style
Constructor Detail

XHtmlTagHandler

public XHtmlTagHandler()
Method Detail

getComponentName

public java.lang.String getComponentName()
Get the component name, preferring the document ID over the name attribute

Returns:
the name

startProcessing

public void startProcessing(XHtmlBuilder htmlBuilder,
                            XComponentFactory cf,
                            javax.swing.text.MutableAttributeSet as)
Process the opening html tag/element

Parameters:
htmlBuilder - the xui builder instance that is processing the html file
cf - the component factory to use for the creation of individual components
as - the attributes of this html tag.

endProcessing

public void endProcessing(XComponentFactory cf)
The closing tag has been parsed and now the element can calulate its complete setup should it be dependant on its children for any information. In the case of a table, the table determines the row and column count from the child elements and cannot calculate its layout till all the children have been initially processed.

Parameters:
cf - the component factory to use for the creation of individual components

processComment

public void processComment(java.lang.String comment)
Process the element comment

Parameters:
comment - the content

processText

public void processText(XComponentFactory cf,
                        java.lang.String text)
Process the element text

Parameters:
cf - the component factory to use for the creation of individual components
text - the content

processCommonAttributes

public void processCommonAttributes(java.lang.String attribName,
                                    java.lang.Object attribValue)
Process the common html attributes, includeing style, class, id, and lang. This method should not be called prior to instantiation of the component associated with this html element.


addElement

public boolean addElement(XHtmlTagHandler child)
Add a child element, possibly data such as when an option is added to a SELECT element.

Parameters:
child - the child element
Returns:
true if the item is added, false if further processing is required.

newInstance

public abstract XHtmlTagHandler newInstance(XHtmlBuilder builder,
                                            XHtmlTagHandler parent)
Create a new instance of this object

Parameters:
builder - the html builder and processor for the document
parent - the parent handler
Returns:
the new handler

setParent

public void setParent(XHtmlTagHandler p)
Set the parent handler for this object


getParent

public XHtmlTagHandler getParent()
Get the parent handler

Returns:
the handler that immediately preceeded this one in the hierarchy

setBuilder

public void setBuilder(XHtmlBuilder htmlBuilder)
Set the html builder that instantiated this tag handler and that owns the document being processed. The builder also holds references to objects like styles and so on

Parameters:
htmlBuilder - the page builder

getChildren

public java.util.ArrayList<XHtmlTagHandler> getChildren()
Get teh children of this node, if any

Returns:
the children or null if none have been added

addChild

public void addChild(XHtmlTagHandler c)
Add a child to this handler. Creates a new store for the children if none has yet been created.

Parameters:
c - the child handler

getAttribute

public java.lang.Object getAttribute(javax.swing.text.html.HTML.Attribute name)
Get an attribute value

Parameters:
name - the attribute name
Returns:
the attribute value

getComponent

public javax.swing.JComponent getComponent()
Get the component corresponding to this tag, if any

Returns:
the component or null if none is relevant

setComponent

public void setComponent(javax.swing.JComponent c)
Set the component used to represent this tag

Parameters:
c - the component

decorateAttribute

public boolean decorateAttribute(java.lang.Object attribute)
Decorate an attribute such as a font according to the relevant attributes that this element includes.

Parameters:
attribute - the object that needs decoration
Returns:
true if the object is modified or otherwise false.

applyStyle

public void applyStyle(java.lang.String styleStr)
Apply a style to the component

Parameters:
the - style string

applyAlignment

public void applyAlignment(java.lang.String value)
Set the alignment for the component

Parameters:
value - the alignment LEFT|RIGHT|CENTER

getLength

public double getLength(java.lang.String lenStr)
Get the value of the width string

Returns:
the width in pixels or a value less than 1.0 if a percentage is specified

getWidth

public int getWidth(java.awt.Dimension parentSize)
Get a width in pixels or in percent of the parent width

Parameters:
parentSize - the dimensions of the parent component
the - width specification in pixels or a fraction of the paret
Returns:
the width in pixels

getWidth

public int getWidth(java.awt.Dimension parentSize,
                    double width)
Get a width in pixels or in percent of the parent width

Parameters:
parentSize - the dimensions of the parent component
Returns:
the width in pixels

getHeight

public int getHeight(java.awt.Dimension parentSize)
Get a width in pixels or in percent of the parent width

Parameters:
parentSize - the dimensions of the parent component
the - width specification in pixels or a fraction of the paret
Returns:
the width in pixels

getHeight

public int getHeight(java.awt.Dimension parentSize,
                     double height)
Get a height in pixels or in percent of the parent width

Parameters:
parentSize - the dimensions of the parent component
the - height specification in pixels or a fraction of the paret
Returns:
the height in pixels

getContent

public java.lang.String getContent()
Get the content of this tag/element

Returns:
the content

getTextStyleAttributes

public java.lang.String getTextStyleAttributes(javax.swing.JComponent comp,
                                               boolean openingTag)
Get the text formatting tags for this element

Parameters:
comp - the component to format
openingTag - true if the opening tags are being requested, false for the closing tags
Returns:
the html attribute elements or an empty string if no attributes are specified

breaksFlow

public boolean breaksFlow()
Does this tag break the flow?

Returns:
true if the flow is broken, otherwsie false