Client Toolkit String Tokenizer Activity

An example client toolkit activity implementation for the stringTokenizer activity is shown below:

package uk.org.ogsadai.client.toolkit.activity.transform;

import uk.org.ogsadai.client.toolkit.activity.Activity;
import uk.org.ogsadai.client.toolkit.activity.ActivityOutput;

/**
 * This activity seperates the blocks of input data it receives into a
 * sequence of tokens for output.
 *
 * @author The OGSA-DAI Project Team
 */
public class StringTokenizerActivity extends Activity
{
    /** The number of blocks to aggregate into each larger block. */
    private String mDelimiters;

    /**
     * Constructs an activity to tokenize the input.
     */
    public StringTokenizerActivity()
    {
        addInput("String blocks input");
        addOutput("String tokens output");
    }

    /**
     * Constructs an activity to tokenize the input using the
     * specified delimiters.
     * 
     * @param delimiters
     *     The delimiters used to separate tokens
     * @throws IllegalArgumentException
     *     If <code>delimiters</code> is <code>null</code>. 
     */
    public StringTokenizerActivity(final String delimiters)
    {
        this();
        setDelimiters(delimiters);
    }

    /**
     * Constructs an activity to tokenize the input using the
     * specified delimiters.
     * 
     * @param delimiters
     *     The delimiters used to separate tokens
     * @param input
     *     Output from another activity that will provide input to
     *     this activity.
     * @throws IllegalArgumentException
     *     If <code>input</code> or <code>delimiters</code> are.
     *     <code>null</code>. 
     */
    public StringTokenizerActivity(
        final String delimiters,
        final ActivityOutput input)
    {
        this(delimiters);
        setInput(input);
    }

    /**
     * Gets the activity's only output - the tokenized data.
     *
     * @return the activity output
     */
    public ActivityOutput getOutput()
    {
        return getOutputs()[0];
    }

    /**
     * Sets the input of this activity to be the output from another
     * activity that will provide the data to be tokenized.
     *
     * @param input
     *     Output from another activity.
     * @throws IllegalArgumentException
     *     If <code>input</code> is <code>null</code>.
     */
    public final void setInput(final ActivityOutput input)
    {
        if (input == null)
        {
            throw new IllegalArgumentException("Argument must not be null");
        }
        super.setInput(0, input.getName());
    }

    /**
     * Sets the delimiters to use when breaking up the string. 
     *
     * @param delimiters
     *     The delimiters used to separate tokens
     * @throws IllegalArgumentException
     *     If <code>delimiters</code> is <code>null</code>.
     */
    public final void setDelimiters(final String delimiters)
    {
        if (delimiters == null)
        {
            throw new IllegalArgumentException("Argument must not be null");
        }
        mDelimiters = delimiters;
    }

    /*
     * @see uk.org.ogsadai.client.toolkit.Activity#generateXML()
     */
    protected String generateXML()
    {
        final StringBuffer sb = new StringBuffer();

        sb.append("<stringTokenizer name=\"");
        sb.append(getName());
        sb.append("\">\n");

        // stringBlocksInput sub-element

        sb.append("  <stringBlocksInput from=\"");
        sb.append(getInputParameters()[0].getOutputName());
        sb.append("\"/>\n");

        // delimiters sub-element

        sb.append("  <delimiters value=\"");
        sb.append(mDelimiters);
        sb.append("\"/>\n");

        // stringTokensOutput sub-element

        sb.append("  <stringTokensOutput name=\"");
        sb.append(getOutputs()[0].getName());
        sb.append("\"/>");

        sb.append("</stringTokenizer>");

        return sb.toString();
    }
}