An example implementation class for the stringTokenizer activity is shown below:
package uk.org.ogsadai.activity.transform; import java.util.StringTokenizer; import org.apache.log4j.Logger; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import uk.org.ogsadai.common.exception.common.CommonUserException; import uk.org.ogsadai.common.exception.engine.activity.ActivitySystemException; import uk.org.ogsadai.common.exception.engine.activity.ActivityUserException; import uk.org.ogsadai.engine.Activity; import uk.org.ogsadai.engine.BlockReader; import uk.org.ogsadai.engine.BlockWriter; /** * 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.StringTokenizerclass. 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 * ofStringorbyte[]will be converted into * strings using theirtoStringmethods. * * @author The OGSA-DAI Project Team */ public class StringTokenizerActivity extends Activity { // Logger object for logging in this class private static Logger mLog = Logger.getLogger(StringTokenizerActivity.class); /** 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 ActivitySystemException If there is a problem constructing the * activity due to an implementation * error or OGSA-DAI confguration problem. * @throws ActivityUserException If there is a problem constructing the * activity due to a user mistake such as * an invalid setting. */ public StringTokenizerActivity(final Element element) throws ActivitySystemException, ActivityUserException { super(element); // Publish the input and output names of the activity by setting the // mInternalInputs and mInternalOutputs fields. String inputName = ((Element) element.getElementsByTagName( "stringBlocksInput").item(0)).getAttribute("from"); String outputName = ((Element) element.getElementsByTagName( "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.getElementsByTagName("delimiters"); mDelimiters = (list.getLength()>0) ? ((Element) list.item(0)).getAttribute("value") : " "; } /* * (non-Javadoc) * @see uk.org.ogsadai.engine.Activity#initialise() */ public void initialise() throws ActivityUserException { try { // set-up references to the activity input and output mInput = mContext.getInput(mInternalInputs[0]); mOutput = mContext.getOutput(mInternalOutputs[0]); } catch (CommonUserException e) { throw new ActivityUserException(getName(), e, mLog); } } /* * (non-Javadoc) * @see uk.org.ogsadai.engine.Activity#processBlock() */ public void processBlock() { maintainTokenizer(); if (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()); } } } }
| Back: How to Write an Activity | ||
| © International Business Machines Corporation, 2004. | © The University of Edinburgh, 2004. |