String Tokenizer Activity Java Implementation

An example implementation class for the stringTokenizer activity is shown below:

package my.ownpackage;

import java.util.StringTokenizer;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import uk.org.ogsadai.activity.Activity;
import uk.org.ogsadai.activity.ActivityCreationException;
import uk.org.ogsadai.activity.ActivityExecutionException;
import uk.org.ogsadai.activity.ActivitySpecificationException;
import uk.org.ogsadai.activity.ActivityStreamNotFoundException;
import uk.org.ogsadai.common.BlockReader;
import uk.org.ogsadai.common.BlockWriter;
import uk.org.ogsadai.service.OGSADAIConstants;

/**
 * This activity tokenizes the input data it recieves using a specified set of
 * delimiters. The functionality is similar to that provided by the
 * java.util.StringTokenizer class. This class has one input
 * (the blocks of data to de tokenized) and one output (the tokens). This
 * activity supports any type of input data, but blocks which are not instances
 * of String or byte[] will be converted into
 * strings using their toString methods.
 *
 * @author The OGSA-DAI Project Team
 */
public class StringTokenizerActivity extends Activity
{
    /** Input string data. */
    private BlockReader mInput;

    /** Tokenized output data. */
    private BlockWriter mOutput;

    /** String containing the delimiter(s) used for tokenizing. */
    private String mDelimiters;

    /** Used for tokenizing the input data. */
    private StringTokenizer mTokenizer;

    /**
     * Constructs a string tokenizer activity.
     *
     * @param   element                  The configuration for the tokenizer.
     * @throws  ActivityCreationException  
     *     If there is a problem constructing the
     *     activity due to an implementation
     *     error or OGSA-DAI confguration problem.
     * @throws  ActivitySpecificationException    
     *     If there is a problem constructing the
     *     activity due to a user mistake such as
     *     an invalid setting.
     * @see uk.org.ogsadai.activity.Activity#Activity(org.w3c.dom.Element)
     */
    public StringTokenizerActivity(final Element element)
        throws ActivityCreationException, ActivitySpecificationException
    {
        super(element);

        // Publish the input and output names of the activity by setting the
        // mInternalInputs and mInternalOutputs fields.

        String inputName = ((Element) element.getElementsByTagNameNS(
            OGSADAIConstants.TYPES_NAMESPACE,
            "stringBlocksInput").item(0)).getAttribute("from");
        String outputName = ((Element) element.getElementsByTagNameNS(
            OGSADAIConstants.TYPES_NAMESPACE,
            "stringTokensOutput").item(0)).getAttribute("name");
        mInternalInputs = new String[] {inputName};
        mInternalOutputs = new String[] {outputName};

        // Read the delimiter characters that will be used to tokenize the
        // input data. Set a default value to " "

        NodeList list = element.getElementsByTagNameNS(
            OGSADAIConstants.TYPES_NAMESPACE,"delimiters");
        mDelimiters = (list.getLength()>0)
            ? ((Element) list.item(0)).getAttribute("value")
            : " ";
    }

    /*
     * (non-Javadoc)
     * @see uk.org.ogsadai.activity.Activity#initialise()
     */
    public void initialise()
        throws ActivitySpecificationException, ActivityExecutionException 
    {
        try 
        {
            // set-up references to the activity input and output
            mInput = mContext.getInput(mInternalInputs[0]);
            mOutput = mContext.getOutput(mInternalOutputs[0]);
        } 
        catch (ActivityStreamNotFoundException e) 
        {
            throw new ActivitySpecificationException(getName(), e);
        }
    }

    /*
     * (non-Javadoc)
     * @see uk.org.ogsadai.activity.Activity#processBlock()
     */
    public void processBlock()
    {
        maintainTokenizer();

        if (mTokenizer != null && mTokenizer.hasMoreTokens())
        {
            final Object token = mTokenizer.nextToken();
            mOutput.put( token );
        }
        else
        {
            setCompleted();
        }
    }

    /**
     * This method maintains the mTokenizer field by making sure that it is
     * initialised with the next block of input data whenever it runs out of
     * tokens.
     */
    private final void maintainTokenizer()
    {
        if ((mTokenizer == null || !mTokenizer.hasMoreTokens())
            && mInput.hasNext())
        {
            final Object block = mInput.next();

            if (block instanceof String)
            {
                mTokenizer = new StringTokenizer((String) block, mDelimiters);
            }
            else if (block instanceof byte[])
            {
                mTokenizer = new StringTokenizer(
                    new String(((byte[]) block)),
                    mDelimiters);
            }
            else
            {
                mTokenizer = new StringTokenizer(block.toString());
            }
        }
    }
}