JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / bin / jalview / io / FileParse.js
1 Clazz.declarePackage ("jalview.io");\r
2 Clazz.load (null, "jalview.io.FileParse", ["jalview.io.AppletFormatAdapter", "jalview.util.MessageManager", "java.io.BufferedReader", "$.IOException", "$.InputStreamReader", "$.StringReader", "java.lang.Error", "java.net.URL", "java.util.zip.GZIPInputStream"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.dataName = "unknown source";\r
5 this.inFile = null;\r
6 this.viewport = null;\r
7 this.exportSettings = null;\r
8 this.index = 1;\r
9 this.suffixSeparator = '#';\r
10 this.newline = null;\r
11 this.suffix = null;\r
12 this.type = null;\r
13 this.dataIn = null;\r
14 this.errormessage = "UNITIALISED SOURCE";\r
15 this.error = true;\r
16 this.warningMessage = null;\r
17 this.READAHEAD_LIMIT = 2048;\r
18 Clazz.instantialize (this, arguments);\r
19 }, jalview.io, "FileParse");\r
20 Clazz.prepareFields (c$, function () {\r
21 this.newline = System.getProperty ("line.separator");\r
22 });\r
23 Clazz.defineMethod (c$, "setNewlineString", \r
24 function (nl) {\r
25 this.newline = nl;\r
26 }, "~S");\r
27 Clazz.defineMethod (c$, "getNewlineString", \r
28 function () {\r
29 return this.newline;\r
30 });\r
31 Clazz.makeConstructor (c$, \r
32 function () {\r
33 });\r
34 Clazz.makeConstructor (c$, \r
35 function (from) {\r
36 if (from == null) {\r
37 throw  new Error (jalview.util.MessageManager.getString ("error.implementation_error_null_fileparse"));\r
38 }if (from === this) {\r
39 return;\r
40 }this.index = ++from.index;\r
41 this.inFile = from.inFile;\r
42 this.suffixSeparator = from.suffixSeparator;\r
43 this.suffix = from.suffix;\r
44 this.errormessage = from.errormessage;\r
45 this.error = false;\r
46 this.type = from.type;\r
47 this.dataIn = from.dataIn;\r
48 if (this.dataIn != null) {\r
49 this.mark ();\r
50 }this.dataName = from.dataName;\r
51 }, "jalview.io.FileParse");\r
52 Clazz.defineMethod (c$, "tryAsGzipSource", \r
53 ($fz = function (inputStream) {\r
54 var inData =  new java.io.BufferedReader ( new java.io.InputStreamReader ( new java.util.zip.GZIPInputStream (inputStream)));\r
55 inData.mark (2048);\r
56 inData.read ();\r
57 inData.reset ();\r
58 return inData;\r
59 }, $fz.isPrivate = true, $fz), "java.io.InputStream");\r
60 Clazz.defineMethod (c$, "checkURLSource", \r
61 ($fz = function (fileStr) {\r
62 this.errormessage = "URL NOT FOUND";\r
63 var url =  new java.net.URL (fileStr);\r
64 var e = null;\r
65 if (fileStr.toLowerCase ().endsWith (".gz")) {\r
66 try {\r
67 var inputStream = url.openStream ();\r
68 this.dataIn = this.tryAsGzipSource (inputStream);\r
69 this.dataName = fileStr;\r
70 return false;\r
71 } catch (ex) {\r
72 if (Clazz.exceptionOf (ex, Exception)) {\r
73 e = ex;\r
74 } else {\r
75 throw ex;\r
76 }\r
77 }\r
78 }try {\r
79 this.dataIn =  new java.io.BufferedReader ( new java.io.InputStreamReader (url.openStream ()));\r
80 } catch (q) {\r
81 if (Clazz.exceptionOf (q, java.io.IOException)) {\r
82 if (e != null) {\r
83 throw  new java.io.IOException (jalview.util.MessageManager.getString ("exception.failed_to_resolve_gzip_stream"), e);\r
84 }throw q;\r
85 } else {\r
86 throw q;\r
87 }\r
88 }\r
89 this.dataName = fileStr;\r
90 return false;\r
91 }, $fz.isPrivate = true, $fz), "~S");\r
92 Clazz.defineMethod (c$, "extractSuffix", \r
93 ($fz = function (fileStr) {\r
94 var sfpos = fileStr.lastIndexOf (this.suffixSeparator);\r
95 if (sfpos > -1 && sfpos < fileStr.length - 1) {\r
96 this.suffix = fileStr.substring (sfpos + 1);\r
97 return fileStr.substring (0, sfpos);\r
98 }return null;\r
99 }, $fz.isPrivate = true, $fz), "~S");\r
100 Clazz.makeConstructor (c$, \r
101 function (fileStr, type) {\r
102 this.type = type;\r
103 this.error = false;\r
104 if (type.equals (jalview.io.AppletFormatAdapter.FILE)) {\r
105 {\r
106 }} else if (type.equals (jalview.io.AppletFormatAdapter.URL)) {\r
107 try {\r
108 try {\r
109 this.checkURLSource (fileStr);\r
110 if (this.suffixSeparator == '#') {\r
111 this.extractSuffix (fileStr);\r
112 }} catch (e) {\r
113 if (Clazz.exceptionOf (e, java.io.IOException)) {\r
114 var suffixLess = this.extractSuffix (fileStr);\r
115 if (suffixLess == null) {\r
116 throw (e);\r
117 } else {\r
118 try {\r
119 this.checkURLSource (suffixLess);\r
120 } catch (e2) {\r
121 if (Clazz.exceptionOf (e2, java.io.IOException)) {\r
122 this.errormessage = "BAD URL WITH OR WITHOUT SUFFIX";\r
123 throw (e);\r
124 } else {\r
125 throw e2;\r
126 }\r
127 }\r
128 }} else {\r
129 throw e;\r
130 }\r
131 }\r
132 } catch (e) {\r
133 if (Clazz.exceptionOf (e, Exception)) {\r
134 this.errormessage = "CANNOT ACCESS DATA AT URL '" + fileStr + "' (" + e.getMessage () + ")";\r
135 this.error = true;\r
136 } else {\r
137 throw e;\r
138 }\r
139 }\r
140 } else if (type.equals (jalview.io.AppletFormatAdapter.PASTE)) {\r
141 this.errormessage = "PASTE INACCESSIBLE!";\r
142 this.dataIn =  new java.io.BufferedReader ( new java.io.StringReader (fileStr));\r
143 this.dataName = "Paste";\r
144 } else if (type.equals (jalview.io.AppletFormatAdapter.CLASSLOADER)) {\r
145 this.errormessage = "RESOURCE CANNOT BE LOCATED";\r
146 var is = this.getClass ().getResourceAsStream ("/" + fileStr);\r
147 if (is == null) {\r
148 var suffixLess = this.extractSuffix (fileStr);\r
149 if (suffixLess != null) {\r
150 is = this.getClass ().getResourceAsStream ("/" + suffixLess);\r
151 }}if (is != null) {\r
152 this.dataIn =  new java.io.BufferedReader ( new java.io.InputStreamReader (is));\r
153 this.dataName = fileStr;\r
154 } else {\r
155 this.error = true;\r
156 }} else {\r
157 this.errormessage = "PROBABLE IMPLEMENTATION ERROR : Datasource Type given as '" + (type != null ? type : "null") + "'";\r
158 this.error = true;\r
159 }if (this.dataIn == null || this.error) {\r
160 throw  new java.io.IOException (jalview.util.MessageManager.formatMessage ("exception.failed_to_read_data_from_source",  Clazz.newArray (-1, [this.errormessage])));\r
161 }this.error = false;\r
162 this.dataIn.mark (2048);\r
163 }, "~S,~S");\r
164 Clazz.defineMethod (c$, "mark", \r
165 function () {\r
166 if (this.dataIn != null) {\r
167 this.dataIn.mark (2048);\r
168 } else {\r
169 throw  new java.io.IOException (jalview.util.MessageManager.getString ("exception.no_init_source_stream"));\r
170 }});\r
171 Clazz.defineMethod (c$, "nextLine", \r
172 function () {\r
173 if (!this.error) {\r
174 return this.dataIn.readLine ();\r
175 }throw  new java.io.IOException (jalview.util.MessageManager.formatMessage ("exception.invalid_source_stream",  Clazz.newArray (-1, [this.errormessage])));\r
176 });\r
177 Clazz.defineMethod (c$, "isExporting", \r
178 function () {\r
179 return !this.error && this.dataIn == null;\r
180 });\r
181 Clazz.defineMethod (c$, "isValid", \r
182 function () {\r
183 return !this.error;\r
184 });\r
185 Clazz.defineMethod (c$, "close", \r
186 function () {\r
187 this.errormessage = "EXCEPTION ON CLOSE";\r
188 this.error = true;\r
189 this.dataIn.close ();\r
190 this.dataIn = null;\r
191 this.errormessage = "SOURCE IS CLOSED";\r
192 });\r
193 Clazz.defineMethod (c$, "reset", \r
194 function () {\r
195 if (this.dataIn != null && !this.error) {\r
196 this.dataIn.reset ();\r
197 } else {\r
198 throw  new java.io.IOException (jalview.util.MessageManager.getString ("error.implementation_error_reset_called_for_invalid_source"));\r
199 }});\r
200 Clazz.defineMethod (c$, "hasWarningMessage", \r
201 function () {\r
202 return (this.warningMessage != null && this.warningMessage.length > 0);\r
203 });\r
204 Clazz.defineMethod (c$, "getWarningMessage", \r
205 function () {\r
206 return this.warningMessage;\r
207 });\r
208 Clazz.defineMethod (c$, "getInFile", \r
209 function () {\r
210 if (this.inFile != null) {\r
211 return this.inFile.getAbsolutePath () + " (" + this.index + ")";\r
212 } else {\r
213 return "From Paste + (" + this.index + ")";\r
214 }});\r
215 Clazz.defineMethod (c$, "getDataName", \r
216 function () {\r
217 return this.dataName;\r
218 });\r
219 Clazz.defineMethod (c$, "setDataName", \r
220 function (dataname) {\r
221 this.dataName = dataname;\r
222 }, "~S");\r
223 Clazz.defineMethod (c$, "getReader", \r
224 function () {\r
225 if (this.dataIn != null) {\r
226 return this.dataIn;\r
227 }return null;\r
228 });\r
229 Clazz.defineMethod (c$, "getViewport", \r
230 function () {\r
231 return this.viewport;\r
232 });\r
233 Clazz.defineMethod (c$, "setViewport", \r
234 function (viewport) {\r
235 this.viewport = viewport;\r
236 }, "jalview.api.AlignViewportI");\r
237 Clazz.defineMethod (c$, "getExportSettings", \r
238 function () {\r
239 return this.exportSettings;\r
240 });\r
241 Clazz.defineMethod (c$, "setExportSettings", \r
242 function (exportSettings) {\r
243 this.exportSettings = exportSettings;\r
244 }, "jalview.api.AlignExportSettingI");\r
245 Clazz.defineMethod (c$, "configureForView", \r
246 function (avpanel) {\r
247 if (avpanel != null) {\r
248 this.setViewport (avpanel.getAlignViewport ());\r
249 }}, "jalview.api.AlignmentViewPanel");\r
250 });\r