morphis logo morphis | wax   
morphis manual
manual
 

Processing
   Dispatching
   Requesting
   Translating

Installation
   Requirements
   Instructions
   Config Properties

Dispatching
   Configuration
   Pre/Post Filters

Requestors
   URL Requesting
   Servlet Chaining
   Run-time Instructs
   Custom Requestors

Translators
   Non-Translator
   Custom Translators

SAX Translators
   XSLT Translation
   Custom SAX Filters

SAX Translator: Overview

The SAX Translator provides translation on HTML and XML streams using one or more filters which act on XML SAX events. This type of translator is useful for scenarios like:

  • Converting XML to XML or XHTML
  • Converting HTML into XML or XHTML
  • Converting HTML into another type of HTML
  • Converting XML into HTML

To use the SAX Translator you must tell Morphis you which to translate using the org.morphis.translator.SAXTranslator. This instruction is defined in the dispatch.xml as:

<context ...>
    ...
    <translator class="org.morphis.translator.SAXTranslator">
            <filter type="translate" class="my.filter"/>
            ...more filters...
     </translator>
</context>

Notice how the translator itself may have multiple filters. Each filter may also be associated with multiple parameters. Filters perform the actual translation, and are chained together to perform complex translation. Morphis includes a built-in XSLT filter for performing XSLT translation, and custom SAX translator filters may be written to perform custom behavior. Each filter is given a name that defines how it will be processed. SAX Translators will only look at filters whose type are "translate".

The general flow of the SAX Translator is as follows:

In-Stream -> Parser -> Filter.1 -> Filter.2 -> Filter.n -> Out-Stream

The "In-Stream" is the stream which originates from the Requestor. The parser parses the in-stream into SAX events. Each filter is responsible for performing SAX based translation, and handing off the results to the next filter. The final filter is responsible for converting the SAX events into an "Out-Stream" which is returned to the requesting device.

Since each filter can be chained to another filter, it is possible to perform many different types of translations in one process. It's even possible to perform multiple XSLT translations in one process.

The document obtained from the Requestor must first be parsed for SAX events. Morphis provides an HTML parser as well as an XML parser to perform this work. Morphis is usually smart enough to determine which parser to use. If, however, you suspect that Morphis is trying to parse an HTML document as XML (or, less likely, vice versa), you can inform Morphis to use the right parser with the parser processing parameter:

SAX Translator Processing Parameters
parser xml Sets the parser to be either xml or html. Morphis is usually smart enough to figure this out based on the content. You may explicity set the parser to xml or html if you suspect Morphis is using the wrong parser.
parser-class java classname Sets the parser to the object specified by the classname value. This class must implement the org.xml.sax.XMLReader interface.

The following snippet of dispatch.xml illustrates this:

<context ...>
    <parameters>
        <param name="parser" value="html"/>
        ...more parameters...
    </parameters>
    ...
</context>

Further reading:

XSLT Translation using the XSLTAdapter

Writing custom SAX Filters

morphis SourceForge Logo