org.extex.scanner.stream
Class TokenStreamFactory

java.lang.Object
  extended by org.extex.framework.AbstractFactory
      extended by org.extex.scanner.stream.TokenStreamFactory
All Implemented Interfaces:
Configurable, LogEnabled, RegistrarObserver, ResourceAware, OpenFileObservable, OpenReaderObservable, OpenStringObservable, OpenWriterObservable

public class TokenStreamFactory
extends AbstractFactory
implements OpenFileObservable, OpenStringObservable, OpenReaderObservable, OpenWriterObservable

This is the factory to provide an instance of a TokenStream. Like any good factory it is controlled by its configuration.

Configuration

Mainly the configuration needs to specify which class to use for the TokenStream. The name of the class is given as the argument class as shown below.

   <Scanner class="the.pack.age.TheClass"/>
 

The class given must implement the interface TokenStream. In addition an appropriate constructor is required:

     public TheClass(Configuration config, Reader reader, Boolean isFile,
          String theSource) throws IOException
 

If the Token stream is fed from a file then the additional parameter buffersize is taken into account. This parameter is optional. Its usage can look as follows:

   <Scanner class="the.pack.age.TheClass"
         buffersize="0"/>
 
The value given is a number. If this number is positive then it is interpreted as the size of the buffer for the file reading operation. If it is 0 or empty then no buffer will be used. If it is negative, then the default buffer size will be used.

In addition to the class for the Token stream the reader class can be specified for the case that reading from a file is requested. In this case the mapping from bytes to characters according to an encoding. The name is given as the parameter reader as shown below:

   <Scanner class="the.pack.age.TheClass"
         reader="another.pack.age.TheReaderClass"/>
 

Note that the attribute reader is optional. If none is given or the value is the empty string then java.io.InputStreamReader is used instead.

Observable Events

Observers can be registered for several events:

file
This event is triggered by the request for a TokenStream fed from a file. It is deferred until the file has been found and opened. The name of the file is passed as argument to the observer.
reader
This event is triggered by the request for a TokenStream fed from an arbitrary Reader. The reader is passed as argument to the observer.
string
This event is triggered by the request for a TokenStream fed from a String. The string is passed as argument to the observer.

Version:
$Revision:5563 $
Author:
Gerd Neugebauer, Michael Niedermair

Field Summary
 
Fields inherited from class org.extex.framework.AbstractFactory
DEFAULT_ATTRIBUTE, SELECT_ATTRIBUTE
 
Constructor Summary
TokenStreamFactory(java.lang.String tag)
          Creates a new object.
 
Method Summary
 void configure(Configuration config)
          Configure an object according to a given Configuration.
 TokenStream getStream(java.lang.CharSequence line)
          Provide a new instance of a token stream reading from a string.
 TokenStream getStream(java.io.Reader reader)
          Provide a new instance of a token stream reading from a Reader.
protected  TokenStream getStream(java.io.Reader reader, java.lang.Boolean isFile, java.lang.String source)
          Provide a new instance of a token stream reading from a Reader.
 TokenStream getStream(java.lang.String name, java.lang.String type, java.lang.String encoding)
          Provide a new instance of a token stream reading from a file or other resource.
 void register(InputStreamInterceptor interceptor)
          Register an input stream interceptor to be applied for each token stream originated at a resource.
 void register(OutputStreamInterceptor interceptor)
          Register an output stream interceptor to be applied for each output stream.
 void register(ReaderInterceptor interceptor)
          Register a reader interceptor to be applied for each token stream originated at a resource.
 void register(WriterInterceptor interceptor)
          Register a writer interceptor to be applied for each writer.
 void registerObserver(OpenFileObserver observer)
          Register an open file observer for later use.
 void registerObserver(OpenReaderObserver observer)
          Register an open reader observer for later use.
 void registerObserver(OpenStringObserver observer)
          Register an open string observer for later use.
 void registerObserver(OpenWriterObserver observer)
          Register an open reader observer for later use.
 void setOptions(TokenStreamOptions options)
          Setter for options.
 java.io.Writer writerStream(java.io.OutputStream stream, java.lang.String key, java.lang.String encoding)
          Construct a Writer for an output stream.
 
Methods inherited from class org.extex.framework.AbstractFactory
configure, createInstance, createInstance, createInstance, createInstance, createInstanceForConfiguration, createInstanceForConfiguration, createInstanceForConfiguration, enableLogging, enableLogging, getConfiguration, getLogger, getResourceFinder, reconnect, selectConfiguration, setResourceFinder
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TokenStreamFactory

public TokenStreamFactory(java.lang.String tag)
                   throws ConfigurationException
Creates a new object.

Parameters:
tag - the tag name of the sub-configuration to use
Throws:
ConfigurationException - in case of an error in the configuration
Method Detail

configure

public void configure(Configuration config)
               throws ConfigurationException
Configure an object according to a given Configuration.

Specified by:
configure in interface Configurable
Overrides:
configure in class AbstractFactory
Parameters:
config - the configuration object to consider
Throws:
ConfigurationException - in case that something went wrong
See Also:
AbstractFactory.configure( org.extex.framework.configuration.Configuration)

getStream

public TokenStream getStream(java.lang.CharSequence line)
                      throws ConfigurationException
Provide a new instance of a token stream reading from a string.

Parameters:
line - the line of input to read from
Returns:
the new instance
Throws:
ConfigurationException - in case of an error in the configuration

getStream

public TokenStream getStream(java.io.Reader reader)
                      throws ConfigurationException
Provide a new instance of a token stream reading from a Reader.

Parameters:
reader - the reader to get new characters from
Returns:
the new instance
Throws:
ConfigurationException - in case of an error in the configuration

getStream

protected TokenStream getStream(java.io.Reader reader,
                                java.lang.Boolean isFile,
                                java.lang.String source)
                         throws ConfigurationException
Provide a new instance of a token stream reading from a Reader.

Parameters:
reader - the reader to get new characters from
isFile - the indicator for file readers
source - the description of the source
Returns:
the new instance
Throws:
ConfigurationException - in case of an error in the configuration

getStream

public TokenStream getStream(java.lang.String name,
                             java.lang.String type,
                             java.lang.String encoding)
                      throws ConfigurationException
Provide a new instance of a token stream reading from a file or other resource.

Parameters:
name - the name of the file to be read
type - the type of the file to be read
encoding - the name of the encoding to use
Returns:
the new instance or null if the resource could not be located
Throws:
ConfigurationException - in case of an error in the configuration

register

public void register(InputStreamInterceptor interceptor)
Register an input stream interceptor to be applied for each token stream originated at a resource.

Parameters:
interceptor - the additional interceptor

register

public void register(OutputStreamInterceptor interceptor)
Register an output stream interceptor to be applied for each output stream.

Parameters:
interceptor - the additional interceptor

register

public void register(ReaderInterceptor interceptor)
Register a reader interceptor to be applied for each token stream originated at a resource.

Parameters:
interceptor - the additional interceptor

register

public void register(WriterInterceptor interceptor)
Register a writer interceptor to be applied for each writer.

Parameters:
interceptor - the additional interceptor

registerObserver

public void registerObserver(OpenFileObserver observer)
Description copied from interface: OpenFileObservable
Register an open file observer for later use.

Specified by:
registerObserver in interface OpenFileObservable
Parameters:
observer - the observer to be registered
See Also:
OpenFileObservable.registerObserver( org.extex.scanner.stream.observer.file.OpenFileObserver)

registerObserver

public void registerObserver(OpenReaderObserver observer)
Description copied from interface: OpenReaderObservable
Register an open reader observer for later use.

Specified by:
registerObserver in interface OpenReaderObservable
Parameters:
observer - the observer to be registered
See Also:
OpenReaderObservable.registerObserver( org.extex.scanner.stream.observer.reader.OpenReaderObserver)

registerObserver

public void registerObserver(OpenStringObserver observer)
Description copied from interface: OpenStringObservable
Register an open string observer for later use.

Specified by:
registerObserver in interface OpenStringObservable
Parameters:
observer - the observer to be registered
See Also:
OpenStringObservable.registerObserver( org.extex.scanner.stream.observer.string.OpenStringObserver)

registerObserver

public void registerObserver(OpenWriterObserver observer)
Register an open reader observer for later use.

Specified by:
registerObserver in interface OpenWriterObservable
Parameters:
observer - the observer to be registered
See Also:
OpenWriterObservable.registerObserver( org.extex.scanner.stream.observer.writer.OpenWriterObserver)

setOptions

public void setOptions(TokenStreamOptions options)
Setter for options.

Parameters:
options - the options to set.

writerStream

public java.io.Writer writerStream(java.io.OutputStream stream,
                                   java.lang.String key,
                                   java.lang.String encoding)
                            throws java.io.UnsupportedEncodingException
Construct a Writer for an output stream. This may build up a pipe of output stream and writers to perform all desirable steps.

The encoding can be given as a hint.

Parameters:
stream - the stream to put bytes to
key - the name in terms of the interpreter
encoding - the optional encoding. If the encoding is null then it is ignored
Returns:
the writer for the task
Throws:
java.io.UnsupportedEncodingException - in case of an error with the encoding