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 dataIn = new BufferedReader(new FileReader(fileStr));
151 private boolean checkURLSource(String fileStr) throws IOException,
152 MalformedURLException
154 errormessage = "URL NOT FOUND";
155 URL url = new URL(fileStr);
157 // GZIPInputStream code borrowed from Aquaria (soon to be open sourced) via Kenny Sabir
159 if (fileStr.endsWith(".gz")) {
161 InputStream inputStream = url.openStream();
162 dataIn = new BufferedReader(new InputStreamReader(new GZIPInputStream(inputStream)));
169 } catch (Exception ex) {
175 dataIn = new BufferedReader(new InputStreamReader(url.openStream()));
176 } catch (IOException q) {
179 throw new IOException("Failed to resolve GZIP stream", e);
183 // record URL as name of datasource.
189 * sets the suffix string (if any) and returns remainder (if suffix was
193 * @return truncated fileStr or null
195 private String extractSuffix(String fileStr)
197 // first check that there wasn't a suffix string tagged on.
198 int sfpos = fileStr.lastIndexOf(suffixSeparator);
199 if (sfpos > -1 && sfpos < fileStr.length() - 1)
201 suffix = fileStr.substring(sfpos + 1);
202 // System.err.println("DEBUG: Found Suffix:"+suffix);
203 return fileStr.substring(0, sfpos);
209 * Create a datasource for input to Jalview. See AppletFormatAdapter for the
210 * types of sources that are handled.
213 * - datasource locator/content
215 * - protocol of source
216 * @throws MalformedURLException
217 * @throws IOException
219 public FileParse(String fileStr, String type)
220 throws MalformedURLException, IOException
225 if (type.equals(AppletFormatAdapter.FILE))
227 if (checkFileSource(fileStr))
229 String suffixLess = extractSuffix(fileStr);
230 if (suffixLess != null)
232 if (checkFileSource(suffixLess))
234 throw new IOException("Problem opening " + inFile
235 + " (also tried " + suffixLess + ") : " + errormessage);
240 throw new IOException("Problem opening " + inFile + " : "
245 else if (type.equals(AppletFormatAdapter.URL))
251 checkURLSource(fileStr);
252 if (suffixSeparator == '#')
253 extractSuffix(fileStr); // URL lref is stored for later reference.
254 } catch (IOException e)
256 String suffixLess = extractSuffix(fileStr);
257 if (suffixLess == null)
265 checkURLSource(suffixLess);
266 } catch (IOException e2)
268 errormessage = "BAD URL WITH OR WITHOUT SUFFIX";
269 throw (e); // just pass back original - everything was wrong.
273 } catch (Exception e)
275 errormessage = "CANNOT ACCESS DATA AT URL '" + fileStr + "' ("
276 + e.getMessage() + ")";
280 else if (type.equals(AppletFormatAdapter.PASTE))
282 errormessage = "PASTE INACCESSIBLE!";
283 dataIn = new BufferedReader(new StringReader(fileStr));
286 else if (type.equals(AppletFormatAdapter.CLASSLOADER))
288 errormessage = "RESOURCE CANNOT BE LOCATED";
289 java.io.InputStream is = getClass()
290 .getResourceAsStream("/" + fileStr);
293 String suffixLess = extractSuffix(fileStr);
294 if (suffixLess != null)
295 is = getClass().getResourceAsStream("/" + suffixLess);
299 dataIn = new BufferedReader(new java.io.InputStreamReader(is));
309 errormessage = "PROBABLE IMPLEMENTATION ERROR : Datasource Type given as '"
310 + (type != null ? type : "null") + "'";
313 if (dataIn == null || error)
315 // pass up the reason why we have no source to read from
316 throw new IOException("Failed to read data from source:\n"
320 dataIn.mark(READAHEAD_LIMIT);
324 * mark the current position in the source as start for the purposes of it
325 * being analysed by IdentifyFile().identify
327 * @throws IOException
329 public void mark() throws IOException
333 dataIn.mark(READAHEAD_LIMIT);
337 throw new IOException("Unitialised Source Stream");
341 public String nextLine() throws IOException
344 return dataIn.readLine();
345 throw new IOException("Invalid Source Stream:" + errormessage);
348 public boolean isValid()
354 * closes the datasource and tidies up. source will be left in an error state
356 public void close() throws IOException
358 errormessage = "EXCEPTION ON CLOSE";
362 errormessage = "SOURCE IS CLOSED";
366 * rewinds the datasource the beginning.
369 public void reset() throws IOException
371 if (dataIn != null && !error)
377 throw new IOException(
378 "Implementation Error: Reset called for invalid source.");
384 * @return true if there is a warning for the user
386 public boolean hasWarningMessage()
388 return (warningMessage != null && warningMessage.length() > 0);
393 * @return empty string or warning message about file that was just parsed.
395 public String getWarningMessage()
397 return warningMessage;
400 public String getInFile()
404 return inFile.getAbsolutePath() + " (" + index + ")";
408 return "From Paste + (" + index + ")";
413 * @return the dataName
415 public String getDataName()
421 * set the (human readable) name or URI for this datasource
425 protected void setDataName(String dataname)
431 * get the underlying bufferedReader for this data source.
433 * @return null if no reader available
434 * @throws IOException
436 public Reader getReader()
438 if (dataIn != null) // Probably don't need to test for readiness &&