org.webmacro.util
Class Encoder

java.lang.Object
  |
  +--org.webmacro.util.Encoder
All Implemented Interfaces:
ResourceLoader

public class Encoder
extends java.lang.Object
implements ResourceLoader

An encoder is used to encode strings into a particular encoding in preparation for sending the data as part of a response. An encoder is constructed with a particular output encoding and is then expected to properly encode strings with that output encoding for its lifetime.

Encoder instances are obtained via the EncoderProvider implementation that is configured at initialization time. The encoder provider is responsible communicating the encoding scheme to be used by the encoder at construct time.

The mechanism is factored into the encoder interface to allow for caching encoders with differing caching schemes based on the server environment's requirements or no caching scheme at all.

Since:
0.96
Author:
Michael Bayne
See Also:
EncoderProvider, SimpleEncoder

Inner Class Summary
static class Encoder.Block
          The block class provides a means by which encoder users can encode entire blocks of text at once (and have those encoded blocks cached).
 
Constructor Summary
Encoder(java.lang.String encoding)
          Creates an encoder instance with the supplied encoding.
 
Method Summary
 byte[][] encode(Encoder.Block source)
          Encodes the supplied block of strings using the encoding bound to this encoder at construct time.
 byte[] encode(java.lang.String source)
          Encodes the supplied string using the encoding bound to this encoder at construct time.
 void init(Broker b, Settings config)
          Initialize this provider based on the specified config.
 java.lang.Object load(java.lang.Object query, CacheElement ce)
          Load an object from permanent storage (or construct it) on demand.
 java.lang.Object load(java.lang.String query, CacheElement ce)
          Load an object from permanent storage (or construct it) on demand.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Encoder

public Encoder(java.lang.String encoding)
        throws java.io.UnsupportedEncodingException
Creates an encoder instance with the supplied encoding.
Throws:
java.io.UnsupportedEncodingException - Thrown when the underlying Java encoding mechanism does not provide support for the requesting encoding.
Method Detail

init

public void init(Broker b,
                 Settings config)
          throws InitException
Description copied from interface: ResourceLoader
Initialize this provider based on the specified config.
Specified by:
init in interface ResourceLoader

load

public java.lang.Object load(java.lang.Object query,
                             CacheElement ce)
                      throws ResourceException
Load an object from permanent storage (or construct it) on demand.
Specified by:
load in interface ResourceLoader

load

public java.lang.Object load(java.lang.String query,
                             CacheElement ce)
                      throws ResourceException
Description copied from interface: ResourceLoader
Load an object from permanent storage (or construct it) on demand.
Specified by:
load in interface ResourceLoader

encode

public final byte[] encode(java.lang.String source)
                    throws java.io.UnsupportedEncodingException
Encodes the supplied string using the encoding bound to this encoder at construct time.
Returns:
The encoded version of the supplied string.
Throws:
java.io.UnsupportedEncodingException - Thrown when the underlying Java encoding mechanism does not provide support for the encoding used by this encoder instance.

encode

public final byte[][] encode(Encoder.Block source)
                      throws java.io.UnsupportedEncodingException
Encodes the supplied block of strings using the encoding bound to this encoder at construct time.
Returns:
The encoded version of the supplied block of strings.
Throws:
java.io.UnsupportedEncodingException - Thrown when the underlying Java encoding mechanism does not provide support for the encoding used by this encoder instance.