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