Extending classes for tag SPAN

SimplyHTML extends classes HTMLDocument, HTMLDocument.HTMLReader, HTMLEditorKit and HTMLWriter to support the SPAN tag of HTML. In this chapter the overall approach of how this is done is described. Please consult the source code and API documents of the mentioned classes for additional details.

Why extending the mentioned classes?

Class HTMLDocument has an inner class HTMLReader which is used by HTMLEditorKit to read HTML files. HTMLReader does not support SPAN tags so it is extended by SHTMLReader accordingly. To use SHTMLReader in favor of HTMLReader, class HTMLDocument has to be extended too.

When a HTML document is edited in SimplyHTML, all attributes are stored as CSS attributes which is why it is also necessary to extend class HTMLWriter to write out those CSS styles inside SPAN tags. To use both our own reader and our own writer, HTMLEditorKit needs to be extended as well.

SHTMLWriter

Usually a class is extended simply by overriding one or more of its public methods. Unfortunately class HTMLWriter only has one public method write which calls other private methods making up the actual write process.

If not a completely new writer is to be created, the only way to extend HTMLWriter is to copy its source code completely into a new subclass SHTMLWriter and change some of the code to our needs (please do let me know if you can provide a more elegant way to do this...).

To allow to write SPAN tags for style attributes, new class SHTMLWriter changes method convertToHTML32. Whenever a CONTENT tag is encountered during write, any CSS attributes are converted to a syntax used in STYLE attributes. The resulting style string then is added to a new SPAN tag and the SPAN tag including the found styles is added to the CONTENT tag.

SHTMLDocument

Whenever data is read into an instance of SHTMLDocument, method getReader returns an instance of the reader special to this type of document, so this method is overridden to provide an instance of SHTMLReader (see below).

SHTMLDocument.SHTMLReader

Class HTMLDocument contains an inner class HTMLReader to read HTML data into an instance of HTMLDocument. To support SPAN tags SHTMLDocument creates a new inner class SHTMLReader.

SHTMLReader overrides methods handleStartTag, handleSimpleTag and handleEndTag which are called by the parser for every tag found. SPAN tags are delivered to the reader through method handleSimpleTag. SHTMLReader deviates this tag to be handled by handleStartTag instead.

In method handleStartTag, for any SPAN tag found an instance of SHTMLCharacterAction is invoked. SHTMLCharacterAction is an inner class of SHTMLReader and extends class CharacterAction of class HTMLReader. It does the actual handling of the SPAN tag by removing the SPAN tag and by adding its style attributes to the CONTENT tag the SPAN tag belongs to.

Method handleEndTag properly terminates SHTMLCharacterActions for any SPAN tag in process.

SHTMLEditorKit

Class HTMLEditorKit provides methods to read and write data stored inside an instance of HTMLDocument. It uses method getReader of HTMLDocument in its read an write methods. To support our own set of classes as described above, class SHTMLEditorKit overrides methods read and write accordingly.

SHTMLEditorKit also ensures that a SHTMLDocument is created instead of a HTMLDocument by overriding method createDefaultDocument.