Refactored client class to accomodate new WS interface and reduce the complexity
[jabaws.git] / webservices / compbio / ws / client / IOHelper.java
1 package compbio.ws.client;\r
2 \r
3 import static compbio.ws.client.Constraints.pseparator;\r
4 \r
5 import java.io.BufferedReader;\r
6 import java.io.File;\r
7 import java.io.FileNotFoundException;\r
8 import java.io.FileOutputStream;\r
9 import java.io.FileReader;\r
10 import java.io.IOException;\r
11 import java.io.OutputStream;\r
12 import java.util.ArrayList;\r
13 import java.util.List;\r
14 \r
15 import compbio.data.sequence.Alignment;\r
16 import compbio.data.sequence.ClustalAlignmentUtil;\r
17 \r
18 public class IOHelper {\r
19 \r
20         /**\r
21          * Checks -i options and return the File if one was provided, null otherwise\r
22          * \r
23          * @param cmd\r
24          * @param key\r
25          * @param mustExist\r
26          * @return\r
27          * @throws IOException\r
28          */\r
29         static File getFile(String[] cmd, String key, boolean mustExist)\r
30                         throws IOException {\r
31                 assert key != null && key.trim().length() != 0;\r
32                 for (int i = 0; i < cmd.length; i++) {\r
33                         String filename = cmd[i];\r
34                         filename = filename.trim();\r
35                         if (filename.toLowerCase().startsWith(key + pseparator)) {\r
36                                 filename = filename.substring((key + pseparator).length());\r
37                                 File file = new File(filename);\r
38                                 if (mustExist && !file.exists()) {\r
39                                         System.out.println(key + " file " + file.getAbsolutePath()\r
40                                                         + " does not exist");\r
41                                         return null;\r
42                                 }\r
43                                 if (!mustExist && !file.exists()) {\r
44                                         file.createNewFile();\r
45                                 }\r
46                                 if (!file.canRead()) {\r
47                                         System.out.println("Cannot read " + key + " file "\r
48                                                         + file.getAbsolutePath());\r
49                                         return null;\r
50                                 }\r
51                                 return file;\r
52                         }\r
53                 }\r
54                 return null;\r
55         }\r
56 \r
57         /**\r
58          * Load parameters from file\r
59          * \r
60          * @throws IOException\r
61          */\r
62         static List<String> loadParameters(File paramsfile) throws IOException {\r
63                 assert paramsfile != null && paramsfile.exists();\r
64                 BufferedReader reader = new BufferedReader(new FileReader(paramsfile));\r
65                 String line = null;\r
66                 ArrayList<String> params = new ArrayList<String>();\r
67                 while ((line = reader.readLine()) != null) {\r
68                         line = line.trim();\r
69                         if (line.length() == 0)\r
70                                 continue;\r
71                         params.add(line);\r
72                 }\r
73                 return params;\r
74         }\r
75 \r
76         static OutputStream getOutStream(File file) {\r
77                 assert file != null && file.exists();\r
78                 try {\r
79                         return new FileOutputStream(file);\r
80                 } catch (FileNotFoundException e) {\r
81                         e.printStackTrace();\r
82                 }\r
83                 return null;\r
84         }\r
85 \r
86         /**\r
87          * Outputs clustal formatted alignment into the file represented by the\r
88          * outStream\r
89          * \r
90          * @param outStream\r
91          * @param align\r
92          *            the alignment to output\r
93          */\r
94         static void writeOut(OutputStream outStream, Alignment align) {\r
95                 try {\r
96                         ClustalAlignmentUtil.writeClustalAlignment(outStream, align);\r
97                 } catch (IOException e) {\r
98                         System.err\r
99                                         .println("Problems writing output file! Stack trace is below: ");\r
100                         e.printStackTrace();\r
101                 } finally {\r
102                         if (outStream != null) {\r
103                                 try {\r
104                                         outStream.close();\r
105                                 } catch (IOException ignored) {\r
106                                         // e.printStackTrace();\r
107                                 }\r
108                         }\r
109                 }\r
110         }\r
111 \r
112 }\r