+package compbio.stat.collector;\r
+\r
+import java.io.BufferedReader;\r
+import java.io.File;\r
+import java.io.FileReader;\r
+import java.io.IOException;\r
+\r
+import org.apache.log4j.Logger;\r
+\r
+import compbio.util.FileUtil;\r
+import compbio.util.Util;\r
+import compbio.ws.client.WSTester;\r
+\r
+public class InputFilter {\r
+\r
+ private static final Logger log = Logger.getLogger(InputFilter.class);\r
+ /**\r
+ * Accepts input as valid unless it is a test input\r
+ * \r
+ * @param input\r
+ * @return\r
+ */\r
+ static boolean accept(File input) {\r
+ if (input == null)\r
+ return true;\r
+ assert input.isFile() : "Input file is not a file! " + input;\r
+ String[] fastainput = WSTester.fastaInput.split("\n");\r
+ assert fastainput.length == 4;\r
+ String[] aligninput = WSTester.fastaAlignment.split("\n");\r
+ assert aligninput.length == 4;\r
+ // We do now know the type of the input here so must compare with both\r
+ // references\r
+ boolean isReference = compareLines(input, fastainput);\r
+ if (!isReference) {\r
+ isReference = compareLines(input, aligninput);\r
+ }\r
+ // only accept genuine input\r
+ return !isReference;\r
+ }\r
+\r
+ private static boolean compareLines(File input, String[] reference) {\r
+ BufferedReader reader = null;\r
+ try {\r
+ reader = new BufferedReader(new FileReader(input));\r
+ // only compare first four lines of the file with reference\r
+ // because the reference length is only 4 lines\r
+ for (int i = 0; i < 4; i++) {\r
+ String line = reader.readLine();\r
+ if (Util.isEmpty(line)) {\r
+ return false;\r
+ }\r
+ line = line.trim();\r
+ if (!line.equals(reference[i].trim())) {\r
+ return false;\r
+ }\r
+ }\r
+ reader.close();\r
+ } catch (IOException ioe) {\r
+ log.warn(ioe, ioe.getCause());\r
+ } finally {\r
+ FileUtil.closeSilently(reader);\r
+ }\r
+ return true;\r
+ }\r
+}\r