2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
22 import java.util.zip.GZIPInputStream;
25 * implements a random access wrapper around a particular datasource, for
26 * passing to identifyFile and AlignFile objects.
28 public class FileParse
31 * text specifying source of data. usually filename or url.
33 private String dataName = "unknown source";
35 public File inFile = null;
37 public int index = 1; // sequence counter for FileParse object created from
41 protected char suffixSeparator = '#';
44 * character used to write newlines
46 protected String newline = System.getProperty("line.separator");
48 public void setNewlineString(String nl)
53 public String getNewlineString()
59 * '#' separated string tagged on to end of filename or url that was clipped
60 * off to resolve to valid filename
62 protected String suffix = null;
64 protected String type = null;
66 protected BufferedReader dataIn = null;
68 protected String errormessage = "UNITIALISED SOURCE";
70 protected boolean error = true;
72 protected String warningMessage = null;
75 * size of readahead buffer used for when initial stream position is marked.
77 final int READAHEAD_LIMIT = 2048;
84 * Create a new FileParse instance reading from the same datasource starting
85 * at the current position. WARNING! Subsequent reads from either object will
86 * affect the read position of the other, but not the error state.
90 public FileParse(FileParse from) throws IOException
95 "Implementation error. Null FileParse in copy constructor");
100 inFile = from.inFile;
101 suffixSeparator = from.suffixSeparator;
102 suffix = from.suffix;
103 errormessage = from.errormessage; // inherit potential error messages
104 error = false; // reset any error condition.
106 dataIn = from.dataIn;
111 dataName = from.dataName;
115 * Attempt to open a file as a datasource. Sets error and errormessage if
116 * fileStr was invalid.
119 * @return this.error (true if the source was invalid)
121 private boolean checkFileSource(String fileStr) throws IOException
124 this.inFile = new File(fileStr);
125 // check to see if it's a Jar file in disguise.
126 if (!inFile.exists())
128 errormessage = "FILE NOT FOUND";
131 if (!inFile.canRead())
133 errormessage = "FILE CANNOT BE OPENED FOR READING";
136 if (inFile.isDirectory())
138 // this is really a 'complex' filetype - but we don't handle directory
140 errormessage = "FILE IS A DIRECTORY";
145 if (fileStr.toLowerCase().endsWith(".gz"))
149 dataIn = tryAsGzipSource(new FileInputStream(fileStr));
152 } catch (Exception x)
154 warningMessage = "Failed to resolve as a GZ stream ("
155 + x.getMessage() + ")";
161 dataIn = new BufferedReader(new FileReader(fileStr));
166 private BufferedReader tryAsGzipSource(InputStream inputStream) throws Exception
168 BufferedReader inData = new BufferedReader(new InputStreamReader(new GZIPInputStream(inputStream)));
174 private boolean checkURLSource(String fileStr) throws IOException,
175 MalformedURLException
177 errormessage = "URL NOT FOUND";
178 URL url = new URL(fileStr);
180 // GZIPInputStream code borrowed from Aquaria (soon to be open sourced) via Kenny Sabir
182 if (fileStr.toLowerCase().endsWith(".gz")) {
184 InputStream inputStream = url.openStream();
185 dataIn = tryAsGzipSource(inputStream);
188 } catch (Exception ex) {
194 dataIn = new BufferedReader(new InputStreamReader(url.openStream()));
195 } catch (IOException q) {
198 throw new IOException("Failed to resolve GZIP stream", e);
202 // record URL as name of datasource.
208 * sets the suffix string (if any) and returns remainder (if suffix was
212 * @return truncated fileStr or null
214 private String extractSuffix(String fileStr)
216 // first check that there wasn't a suffix string tagged on.
217 int sfpos = fileStr.lastIndexOf(suffixSeparator);
218 if (sfpos > -1 && sfpos < fileStr.length() - 1)
220 suffix = fileStr.substring(sfpos + 1);
221 // System.err.println("DEBUG: Found Suffix:"+suffix);
222 return fileStr.substring(0, sfpos);
228 * Create a datasource for input to Jalview. See AppletFormatAdapter for the
229 * types of sources that are handled.
232 * - datasource locator/content
234 * - protocol of source
235 * @throws MalformedURLException
236 * @throws IOException
238 public FileParse(String fileStr, String type)
239 throws MalformedURLException, IOException
244 if (type.equals(AppletFormatAdapter.FILE))
246 if (checkFileSource(fileStr))
248 String suffixLess = extractSuffix(fileStr);
249 if (suffixLess != null)
251 if (checkFileSource(suffixLess))
253 throw new IOException("Problem opening " + inFile
254 + " (also tried " + suffixLess + ") : " + errormessage);
259 throw new IOException("Problem opening " + inFile + " : "
264 else if (type.equals(AppletFormatAdapter.URL))
270 checkURLSource(fileStr);
271 if (suffixSeparator == '#')
272 extractSuffix(fileStr); // URL lref is stored for later reference.
273 } catch (IOException e)
275 String suffixLess = extractSuffix(fileStr);
276 if (suffixLess == null)
284 checkURLSource(suffixLess);
285 } catch (IOException e2)
287 errormessage = "BAD URL WITH OR WITHOUT SUFFIX";
288 throw (e); // just pass back original - everything was wrong.
292 } catch (Exception e)
294 errormessage = "CANNOT ACCESS DATA AT URL '" + fileStr + "' ("
295 + e.getMessage() + ")";
299 else if (type.equals(AppletFormatAdapter.PASTE))
301 errormessage = "PASTE INACCESSIBLE!";
302 dataIn = new BufferedReader(new StringReader(fileStr));
305 else if (type.equals(AppletFormatAdapter.CLASSLOADER))
307 errormessage = "RESOURCE CANNOT BE LOCATED";
308 java.io.InputStream is = getClass()
309 .getResourceAsStream("/" + fileStr);
312 String suffixLess = extractSuffix(fileStr);
313 if (suffixLess != null)
314 is = getClass().getResourceAsStream("/" + suffixLess);
318 dataIn = new BufferedReader(new java.io.InputStreamReader(is));
328 errormessage = "PROBABLE IMPLEMENTATION ERROR : Datasource Type given as '"
329 + (type != null ? type : "null") + "'";
332 if (dataIn == null || error)
334 // pass up the reason why we have no source to read from
335 throw new IOException("Failed to read data from source:\n"
339 dataIn.mark(READAHEAD_LIMIT);
343 * mark the current position in the source as start for the purposes of it
344 * being analysed by IdentifyFile().identify
346 * @throws IOException
348 public void mark() throws IOException
352 dataIn.mark(READAHEAD_LIMIT);
356 throw new IOException("Unitialised Source Stream");
360 public String nextLine() throws IOException
363 return dataIn.readLine();
364 throw new IOException("Invalid Source Stream:" + errormessage);
367 public boolean isValid()
373 * closes the datasource and tidies up. source will be left in an error state
375 public void close() throws IOException
377 errormessage = "EXCEPTION ON CLOSE";
381 errormessage = "SOURCE IS CLOSED";
385 * rewinds the datasource the beginning.
388 public void reset() throws IOException
390 if (dataIn != null && !error)
396 throw new IOException(
397 "Implementation Error: Reset called for invalid source.");
403 * @return true if there is a warning for the user
405 public boolean hasWarningMessage()
407 return (warningMessage != null && warningMessage.length() > 0);
412 * @return empty string or warning message about file that was just parsed.
414 public String getWarningMessage()
416 return warningMessage;
419 public String getInFile()
423 return inFile.getAbsolutePath() + " (" + index + ")";
427 return "From Paste + (" + index + ")";
432 * @return the dataName
434 public String getDataName()
440 * set the (human readable) name or URI for this datasource
444 protected void setDataName(String dataname)
450 * get the underlying bufferedReader for this data source.
452 * @return null if no reader available
453 * @throws IOException
455 public Reader getReader()
457 if (dataIn != null) // Probably don't need to test for readiness &&