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

Translators: Writing Custom Translators

Morphis provides an interface for building custom translators. A custom translator is useful for performing stream to stream translation. For example, you may wish to parse an incoming document and perform actions on the parsed elements. Or, you can perform image resizing on the incoming stream.

To create a custom Translator, you must implement the Morphis class org.morphis.translator.Translator. This interface has only one method:

public void process(DispatchContext context)
    throws TranslationException

The org.morphis.DispatchContext class is the key to obtaining the input and output stream required for translation. The method getInputStream() returns the java.io.InputStream obtained by the Requestor.

InputStream in = context.getInputStream();
OutputStream out = context.getOutputStream();
...do processing...
out.close();

You may also query the InputStream to see when it expires. An expired InputStream means your Translator ought not to cache its contents.

boolean isExpired = context.isInputStreamExpired();
long expires = context.getInputStreamExpiration();

The DispatchContext provides many other useful fields, such as the Servlet's HttpServletRequest and HttpServletResponse objects, as well as objects to obtain any Translator filters which may have been defined for processing.

In order to configure Morphis to use your custom Translator, you will need to define it in dispatch.xml. Translator Filters are not required, and are only necessary when creating complex translation schemes like SAX Translation.

<context ...>
    ...
    <translator class="your.translator.class.here">
            <filter type="translate" class="my.filter"/>
            ...more filters...
     </translator>
</context>

morphis SourceForge Logo