import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.PrintStream; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.List; import edu.uiuc.ncsa.cyberintegrator.executor.cyberintegrator.tool.CyberintegratorToolFile; import edu.uiuc.ncsa.cyberintegrator.executor.cyberintegrator.tool.Dataset; import edu.uiuc.ncsa.cyberintegrator.executor.cyberintegrator.tool.Parameter; import edu.uiuc.ncsa.cyberintegrator.executor.cyberintegrator.tool.Parameter.ParameterType; /** * Find a word in a sentence. This will only return those sentences that contain * the specified word. * * @author Rob Kooper * */ public class WordFinder implements CyberintegratorToolFile { private List input; private File output; private String word; /** * Return the name of the tool. * * @return name of the tool. */ public String getName() { return "Find word"; } /** * Return the version number of the tool. * * @return version of the tool. */ public int getVersion() { return 4; } /** * Return a long description of the tool. This description is used to help * the user understand what the tool does, and what the inputs, outputs and * parameters mean. This can also contain links to papers published, * references to algorithms etc. * * @return description of the tool. */ public String getDescription() { return "Search through the input file and find the word specified as parameter." + " Only those lines that contain the word will be put in the output."; } /** * Return a list of inputs needed by the tool. In this particular case we * need a text file that will be examined. * * @return list of inputs needed by the tool. */ public Collection getInputs() { HashSet result = new HashSet(); result.add(new Dataset("1", "input", "", "text/plain")); return result; } /** * Set the input for the tool. In this case it will set the name of the * input file. * * @param id * the id of the input. * @param input * a pointer to a file on disk with the input data. */ public void setInput(String id, List input) { if ("1".equals(id)) { this.input = input; } } /** * Return a list of outputs generated by the tool. In this particular case * we return a text file with only those lines that contain the word * specified as a parameter. * * @return list of outputs generated by the tool. */ public Collection getOutputs() { HashSet result = new HashSet(); result.add(new Dataset("1", "output", "", "text/plain")); return result; } /** * Retrieve the output of the tool. * * @param id * the id of the output. * @return a pointer to a file on disk with the output data. */ public List getOutput(String id) { if ("1".equals(id)) { List outputs = new ArrayList(); outputs.add(output); return outputs; } return null; } /** * Return a list of parameters needed by the tool. In this particular case * we need a single word that will be checked for. * * @return list of parameters needed by the tool. */ public Collection getParameters() { HashSet result = new HashSet(); result.add(new Parameter("1", "output", "", ParameterType.STRING, "test", false)); return result; } /** * Set the parameter for the tool. In this case it will set the word that * will be searched for in the input file. * * @param id * the id of the parameter. * @param value * the value for the parameter */ public void setParameter(String id, String value) { if ("1".equals(id)) { word = value; } } /** * Execute the tool. In this case it will load the input file, read the * input file, check each line for the presence of the word and create the * output file with only those lines that contain the word. * * @throws Exception * an exception is thrown if the function fails to execute * properly. */ public void execute() throws Exception { output = File.createTempFile("output", ".txt"); PrintStream ps = new PrintStream(output); for (File file : input) { BufferedReader br = new BufferedReader(new FileReader(file)); String line; while ((line = br.readLine()) != null) { if (line.contains(word)) { ps.println(line); } } } ps.close(); } }