2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
25 import java.util.zip.GZIPInputStream;
28 * implements a random access wrapper around a particular datasource, for
29 * passing to identifyFile and AlignFile objects.
31 public class FileParse
34 * text specifying source of data. usually filename or url.
36 private String dataName = "unknown source";
38 public File inFile = null;
40 public int index = 1; // sequence counter for FileParse object created from
44 protected char suffixSeparator = '#';
47 * character used to write newlines
49 protected String newline = System.getProperty("line.separator");
51 public void setNewlineString(String nl)
56 public String getNewlineString()
62 * '#' separated string tagged on to end of filename or url that was clipped
63 * off to resolve to valid filename
65 protected String suffix = null;
67 protected String type = null;
69 protected BufferedReader dataIn = null;
71 protected String errormessage = "UNITIALISED SOURCE";
73 protected boolean error = true;
75 protected String warningMessage = null;
78 * size of readahead buffer used for when initial stream position is marked.
80 final int READAHEAD_LIMIT = 2048;
87 * Create a new FileParse instance reading from the same datasource starting
88 * at the current position. WARNING! Subsequent reads from either object will
89 * affect the read position of the other, but not the error state.
93 public FileParse(FileParse from) throws IOException
98 "Implementation error. Null FileParse in copy constructor");
102 index = ++from.index;
103 inFile = from.inFile;
104 suffixSeparator = from.suffixSeparator;
105 suffix = from.suffix;
106 errormessage = from.errormessage; // inherit potential error messages
107 error = false; // reset any error condition.
109 dataIn = from.dataIn;
114 dataName = from.dataName;
118 * Attempt to open a file as a datasource. Sets error and errormessage if
119 * fileStr was invalid.
122 * @return this.error (true if the source was invalid)
124 private boolean checkFileSource(String fileStr) throws IOException
127 this.inFile = new File(fileStr);
128 // check to see if it's a Jar file in disguise.
129 if (!inFile.exists())
131 errormessage = "FILE NOT FOUND";
134 if (!inFile.canRead())
136 errormessage = "FILE CANNOT BE OPENED FOR READING";
139 if (inFile.isDirectory())
141 // this is really a 'complex' filetype - but we don't handle directory
143 errormessage = "FILE IS A DIRECTORY";
148 if (fileStr.toLowerCase().endsWith(".gz"))
152 dataIn = tryAsGzipSource(new FileInputStream(fileStr));
155 } catch (Exception x)
157 warningMessage = "Failed to resolve as a GZ stream ("
158 + x.getMessage() + ")";
164 dataIn = new BufferedReader(new FileReader(fileStr));
170 private BufferedReader tryAsGzipSource(InputStream inputStream)
173 BufferedReader inData = new BufferedReader(new InputStreamReader(
174 new GZIPInputStream(inputStream)));
181 private boolean checkURLSource(String fileStr) throws IOException,
182 MalformedURLException
184 errormessage = "URL NOT FOUND";
185 URL url = new URL(fileStr);
187 // GZIPInputStream code borrowed from Aquaria (soon to be open sourced) via
190 if (fileStr.toLowerCase().endsWith(".gz"))
194 InputStream inputStream = url.openStream();
195 dataIn = tryAsGzipSource(inputStream);
198 } catch (Exception ex)
206 dataIn = new BufferedReader(new InputStreamReader(url.openStream()));
207 } catch (IOException q)
211 throw new IOException("Failed to resolve GZIP stream", e);
215 // record URL as name of datasource.
221 * sets the suffix string (if any) and returns remainder (if suffix was
225 * @return truncated fileStr or null
227 private String extractSuffix(String fileStr)
229 // first check that there wasn't a suffix string tagged on.
230 int sfpos = fileStr.lastIndexOf(suffixSeparator);
231 if (sfpos > -1 && sfpos < fileStr.length() - 1)
233 suffix = fileStr.substring(sfpos + 1);
234 // System.err.println("DEBUG: Found Suffix:"+suffix);
235 return fileStr.substring(0, sfpos);
241 * Create a datasource for input to Jalview. See AppletFormatAdapter for the
242 * types of sources that are handled.
245 * - datasource locator/content
247 * - protocol of source
248 * @throws MalformedURLException
249 * @throws IOException
251 public FileParse(String fileStr, String type)
252 throws MalformedURLException, IOException
257 if (type.equals(AppletFormatAdapter.FILE))
259 if (checkFileSource(fileStr))
261 String suffixLess = extractSuffix(fileStr);
262 if (suffixLess != null)
264 if (checkFileSource(suffixLess))
266 throw new IOException("Problem opening " + inFile
267 + " (also tried " + suffixLess + ") : " + errormessage);
272 throw new IOException("Problem opening " + inFile + " : "
277 else if (type.equals(AppletFormatAdapter.URL))
283 checkURLSource(fileStr);
284 if (suffixSeparator == '#')
285 extractSuffix(fileStr); // URL lref is stored for later reference.
286 } catch (IOException e)
288 String suffixLess = extractSuffix(fileStr);
289 if (suffixLess == null)
297 checkURLSource(suffixLess);
298 } catch (IOException e2)
300 errormessage = "BAD URL WITH OR WITHOUT SUFFIX";
301 throw (e); // just pass back original - everything was wrong.
305 } catch (Exception e)
307 errormessage = "CANNOT ACCESS DATA AT URL '" + fileStr + "' ("
308 + e.getMessage() + ")";
312 else if (type.equals(AppletFormatAdapter.PASTE))
314 errormessage = "PASTE INACCESSIBLE!";
315 dataIn = new BufferedReader(new StringReader(fileStr));
318 else if (type.equals(AppletFormatAdapter.CLASSLOADER))
320 errormessage = "RESOURCE CANNOT BE LOCATED";
321 java.io.InputStream is = getClass()
322 .getResourceAsStream("/" + fileStr);
325 String suffixLess = extractSuffix(fileStr);
326 if (suffixLess != null)
327 is = getClass().getResourceAsStream("/" + suffixLess);
331 dataIn = new BufferedReader(new java.io.InputStreamReader(is));
341 errormessage = "PROBABLE IMPLEMENTATION ERROR : Datasource Type given as '"
342 + (type != null ? type : "null") + "'";
345 if (dataIn == null || error)
347 // pass up the reason why we have no source to read from
348 throw new IOException("Failed to read data from source:\n"
352 dataIn.mark(READAHEAD_LIMIT);
356 * mark the current position in the source as start for the purposes of it
357 * being analysed by IdentifyFile().identify
359 * @throws IOException
361 public void mark() throws IOException
365 dataIn.mark(READAHEAD_LIMIT);
369 throw new IOException("Unitialised Source Stream");
373 public String nextLine() throws IOException
376 return dataIn.readLine();
377 throw new IOException("Invalid Source Stream:" + errormessage);
380 public boolean isValid()
386 * closes the datasource and tidies up. source will be left in an error state
388 public void close() throws IOException
390 errormessage = "EXCEPTION ON CLOSE";
394 errormessage = "SOURCE IS CLOSED";
398 * rewinds the datasource the beginning.
401 public void reset() throws IOException
403 if (dataIn != null && !error)
409 throw new IOException(
410 "Implementation Error: Reset called for invalid source.");
416 * @return true if there is a warning for the user
418 public boolean hasWarningMessage()
420 return (warningMessage != null && warningMessage.length() > 0);
425 * @return empty string or warning message about file that was just parsed.
427 public String getWarningMessage()
429 return warningMessage;
432 public String getInFile()
436 return inFile.getAbsolutePath() + " (" + index + ")";
440 return "From Paste + (" + index + ")";
445 * @return the dataName
447 public String getDataName()
453 * set the (human readable) name or URI for this datasource
457 protected void setDataName(String dataname)
463 * get the underlying bufferedReader for this data source.
465 * @return null if no reader available
466 * @throws IOException
468 public Reader getReader()
470 if (dataIn != null) // Probably don't need to test for readiness &&