package test.com.jscape.action; import com.jscape.inet.mft.workflow.AbstractAction; import com.jscape.util.reflection.PropertyDescriptor; import com.jscape.util.reflection.types.FileField; import java.io.*; /** * Example action which performs a text search and replace within a file. */ public class ReplaceTextAction extends AbstractAction { // description of action private static final String DESCRIPTION = "Performs search and replace of text."; // descriptors used to build GUI private static final PropertyDescriptor[] DESCRIPTORS = { new PropertyDescriptor("SourceFile", true, new FileField()), new PropertyDescriptor("DestinationFile", false, new FileField()), new PropertyDescriptor("Search", true), new PropertyDescriptor("Replace", true) }; // data variables private String search; private String replace; private String sourceFile; private String destinationFile; /** * Executes task. * @throws Exception if an error occurs */ public void execute() throws Exception { // option source file BufferedInputStream bin = new BufferedInputStream(new FileInputStream( sourceFile)); // temporary buffer to store converted output ByteArrayOutputStream bout = new ByteArrayOutputStream(); // read data into buffer int i = 0; while ((i = bin.read()) != -1) { bout.write(i); } // close input stream bin.close(); // convert buffer to string and perform search and replace String st = new String(bout.toByteArray()); st = st.replaceAll(search, replace); // write results to output file FileOutputStream fout = null; if ((destinationFile != null) && (destinationFile.length() > 0)) { fout = new FileOutputStream(destinationFile); } else { fout = new FileOutputStream(sourceFile); } fout.write(st.getBytes()); // close output streams bout.close(); fout.close(); } /** * Gets description. * @return description */ public String getDescription() { return DESCRIPTION; } /** * Gets property descriptors. * @return property descriptors */ public PropertyDescriptor[] getPropertyDescriptors() { return DESCRIPTORS; } /** * Gets destination file. * @return the destinationFile */ public String getDestinationFile() { return destinationFile; } /** * Sets destination file. * @param destinationFile the destinationFile to set */ public void setDestinationFile(String destinationFile) { this.destinationFile = destinationFile; } /** * Gets text to replace. * @return the replace */ public String getReplace() { return replace; } /** * Sets text to replace. * @param replace the replace to set */ public void setReplace(String replace) { this.replace = replace; } /** * Gets text to find. * @return the search */ public String getSearch() { return search; } /** * Sets text to find. * @param search the search to set */ public void setSearch(String search) { this.search = search; } /** * Gets source file. * @return the sourceFile */ public String getSourceFile() { return sourceFile; } /** * Sets source file. * @param sourceFile the sourceFile to set */ public void setSourceFile(String sourceFile) { this.sourceFile = sourceFile; } }