2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 The Jalview Authors
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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.util.zip.GZIPInputStream;
26 * implements a random access wrapper around a particular datasource, for
27 * passing to identifyFile and AlignFile objects.
29 public class FileParse
32 * text specifying source of data. usually filename or url.
34 private String dataName = "unknown source";
36 public File inFile = null;
38 public int index = 1; // sequence counter for FileParse object created from
42 protected char suffixSeparator = '#';
45 * character used to write newlines
47 protected String newline = System.getProperty("line.separator");
49 public void setNewlineString(String nl)
54 public String getNewlineString()
60 * '#' separated string tagged on to end of filename or url that was clipped
61 * off to resolve to valid filename
63 protected String suffix = null;
65 protected String type = null;
67 protected BufferedReader dataIn = null;
69 protected String errormessage = "UNITIALISED SOURCE";
71 protected boolean error = true;
73 protected String warningMessage = null;
76 * size of readahead buffer used for when initial stream position is marked.
78 final int READAHEAD_LIMIT = 2048;
85 * Create a new FileParse instance reading from the same datasource starting
86 * at the current position. WARNING! Subsequent reads from either object will
87 * affect the read position of the other, but not the error state.
91 public FileParse(FileParse from) throws IOException
96 "Implementation error. Null FileParse in copy constructor");
100 index = ++from.index;
101 inFile = from.inFile;
102 suffixSeparator = from.suffixSeparator;
103 suffix = from.suffix;
104 errormessage = from.errormessage; // inherit potential error messages
105 error = false; // reset any error condition.
107 dataIn = from.dataIn;
112 dataName = from.dataName;
116 * Attempt to open a file as a datasource. Sets error and errormessage if
117 * fileStr was invalid.
120 * @return this.error (true if the source was invalid)
122 private boolean checkFileSource(String fileStr) throws IOException
125 this.inFile = new File(fileStr);
126 // check to see if it's a Jar file in disguise.
127 if (!inFile.exists())
129 errormessage = "FILE NOT FOUND";
132 if (!inFile.canRead())
134 errormessage = "FILE CANNOT BE OPENED FOR READING";
137 if (inFile.isDirectory())
139 // this is really a 'complex' filetype - but we don't handle directory
141 errormessage = "FILE IS A DIRECTORY";
146 if (fileStr.toLowerCase().endsWith(".gz"))
150 dataIn = tryAsGzipSource(new FileInputStream(fileStr));
153 } catch (Exception x)
155 warningMessage = "Failed to resolve as a GZ stream ("
156 + x.getMessage() + ")";
162 dataIn = new BufferedReader(new FileReader(fileStr));
167 private BufferedReader tryAsGzipSource(InputStream inputStream) throws Exception
169 BufferedReader inData = new BufferedReader(new InputStreamReader(new GZIPInputStream(inputStream)));
175 private boolean checkURLSource(String fileStr) throws IOException,
176 MalformedURLException
178 errormessage = "URL NOT FOUND";
179 URL url = new URL(fileStr);
181 // GZIPInputStream code borrowed from Aquaria (soon to be open sourced) via Kenny Sabir
183 if (fileStr.toLowerCase().endsWith(".gz")) {
185 InputStream inputStream = url.openStream();
186 dataIn = tryAsGzipSource(inputStream);
189 } catch (Exception ex) {
195 dataIn = new BufferedReader(new InputStreamReader(url.openStream()));
196 } catch (IOException q) {
199 throw new IOException("Failed to resolve GZIP stream", e);
203 // record URL as name of datasource.
209 * sets the suffix string (if any) and returns remainder (if suffix was
213 * @return truncated fileStr or null
215 private String extractSuffix(String fileStr)
217 // first check that there wasn't a suffix string tagged on.
218 int sfpos = fileStr.lastIndexOf(suffixSeparator);
219 if (sfpos > -1 && sfpos < fileStr.length() - 1)
221 suffix = fileStr.substring(sfpos + 1);
222 // System.err.println("DEBUG: Found Suffix:"+suffix);
223 return fileStr.substring(0, sfpos);
229 * Create a datasource for input to Jalview. See AppletFormatAdapter for the
230 * types of sources that are handled.
233 * - datasource locator/content
235 * - protocol of source
236 * @throws MalformedURLException
237 * @throws IOException
239 public FileParse(String fileStr, String type)
240 throws MalformedURLException, IOException
245 if (type.equals(AppletFormatAdapter.FILE))
247 if (checkFileSource(fileStr))
249 String suffixLess = extractSuffix(fileStr);
250 if (suffixLess != null)
252 if (checkFileSource(suffixLess))
254 throw new IOException("Problem opening " + inFile
255 + " (also tried " + suffixLess + ") : " + errormessage);
260 throw new IOException("Problem opening " + inFile + " : "
265 else if (type.equals(AppletFormatAdapter.URL))
271 checkURLSource(fileStr);
272 if (suffixSeparator == '#')
273 extractSuffix(fileStr); // URL lref is stored for later reference.
274 } catch (IOException e)
276 String suffixLess = extractSuffix(fileStr);
277 if (suffixLess == null)
285 checkURLSource(suffixLess);
286 } catch (IOException e2)
288 errormessage = "BAD URL WITH OR WITHOUT SUFFIX";
289 throw (e); // just pass back original - everything was wrong.
293 } catch (Exception e)
295 errormessage = "CANNOT ACCESS DATA AT URL '" + fileStr + "' ("
296 + e.getMessage() + ")";
300 else if (type.equals(AppletFormatAdapter.PASTE))
302 errormessage = "PASTE INACCESSIBLE!";
303 dataIn = new BufferedReader(new StringReader(fileStr));
306 else if (type.equals(AppletFormatAdapter.CLASSLOADER))
308 errormessage = "RESOURCE CANNOT BE LOCATED";
309 java.io.InputStream is = getClass()
310 .getResourceAsStream("/" + fileStr);
313 String suffixLess = extractSuffix(fileStr);
314 if (suffixLess != null)
315 is = getClass().getResourceAsStream("/" + suffixLess);
319 dataIn = new BufferedReader(new java.io.InputStreamReader(is));
329 errormessage = "PROBABLE IMPLEMENTATION ERROR : Datasource Type given as '"
330 + (type != null ? type : "null") + "'";
333 if (dataIn == null || error)
335 // pass up the reason why we have no source to read from
336 throw new IOException("Failed to read data from source:\n"
340 dataIn.mark(READAHEAD_LIMIT);
344 * mark the current position in the source as start for the purposes of it
345 * being analysed by IdentifyFile().identify
347 * @throws IOException
349 public void mark() throws IOException
353 dataIn.mark(READAHEAD_LIMIT);
357 throw new IOException("Unitialised Source Stream");
361 public String nextLine() throws IOException
364 return dataIn.readLine();
365 throw new IOException("Invalid Source Stream:" + errormessage);
368 public boolean isValid()
374 * closes the datasource and tidies up. source will be left in an error state
376 public void close() throws IOException
378 errormessage = "EXCEPTION ON CLOSE";
382 errormessage = "SOURCE IS CLOSED";
386 * rewinds the datasource the beginning.
389 public void reset() throws IOException
391 if (dataIn != null && !error)
397 throw new IOException(
398 "Implementation Error: Reset called for invalid source.");
404 * @return true if there is a warning for the user
406 public boolean hasWarningMessage()
408 return (warningMessage != null && warningMessage.length() > 0);
413 * @return empty string or warning message about file that was just parsed.
415 public String getWarningMessage()
417 return warningMessage;
420 public String getInFile()
424 return inFile.getAbsolutePath() + " (" + index + ")";
428 return "From Paste + (" + index + ")";
433 * @return the dataName
435 public String getDataName()
441 * set the (human readable) name or URI for this datasource
445 protected void setDataName(String dataname)
451 * get the underlying bufferedReader for this data source.
453 * @return null if no reader available
454 * @throws IOException
456 public Reader getReader()
458 if (dataIn != null) // Probably don't need to test for readiness &&