Custom Task Guide

Custom Task Guide

Supporting Sub-elements in Tasks

Supporting Sub-elements in Tasks

Now let us take a look at how one can support nested elements inside a Task. For example, if we want to enhance our Copy Task to support multiple source files using the standard FileSet component. The XML for our task in the project would look something like this:
<example:copy destinationDirectory=”/destdir”> <fileset dir=”/mydir”> <include pattern=”*.pdf” /> </fileset> </example:copy>
For each sub-element in your task, you need to create a new method that creates a new object that is of type ProjectComponent. In essence, each tag in the project XML should correspond to a class (or an object) that implements the com.linoma.dpa.ProjectComponentInterface. The snippet below demonstrates how a nested file set element can be supported by our task:
private List<FileSet> filesetElements = null; public FileSet createFilesetElement() { if (filesetElements == null) { filesetElements = new ArrayList<FileSet>(1); } FileSet fs = new LocalFileSet(this); filesetElements.add(fs); return fs; } public void removeFilesetElement(FileSet fileSet) { filesetElements.remove(fileSet); } public List<FileSet> getFilesetElements() { return filesetElements; }
Note that the FileSet class must implement the ProjectComponentInterface. The FileSet class must also follow the same guidelines for supporting its own attributes and or sub-elements. For example, you need to have setDirAttribute and getDirAttribute methods defined in the FileSet class. You also need to have createIncludeElement which returns another type of ProjectComponent that handles the include tag and its attributes.
During the project execution (in the task’s execute method), you can manipulate  all the subelements (e.g. filesets)  any way you like.
In order for the sub-element to show up to a user in the Project Designer, the Bean Info XML will need to be updated per the red text shown below:
<?xml version="1.0" encoding="UTF-8" ?> <projectComponent name="CopyTask"> <label>Example Copy Task</label> <description>A simple task for copying a file from one directory to another. </description> <tabPanel name="main"> <tab name="basic"> <attribute name="sourceFile"> <label>Source File</label> <description>Specify the file to copy. </description> <fieldType>localOrNetworkFile</fieldType> <required>true</required> <cols>60</cols> </attribute> <attribute name="destinationDirectory"> <label>Destination Directory</label> <description>Specify the directory to which the source file should be copied. </description> <fieldType>localOrNetworkDirectory</fieldType> <required>true</required> <cols>60</cols> </attribute> </tab> <tab name="control" /> <tab name="onError" /> </tabPanel> <subelement name="fileset"> <label>FileSet</label> <description>Specify a directory of files to copy</description> </subelement> </projectComponent>

0 COMMENTS

We’d like to hear from you!