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/>.
24 * implements a random access wrapper around a particular datasource, for
25 * passing to identifyFile and AlignFile objects.
27 public class FileParse
30 * text specifying source of data. usually filename or url.
32 private String dataName = "unknown source";
34 public File inFile = null;
36 public int index = 1; // sequence counter for FileParse object created from
40 protected char suffixSeparator = '#';
43 * character used to write newlines
45 protected String newline = System.getProperty("line.separator");
47 public void setNewlineString(String nl)
52 public String getNewlineString()
58 * '#' separated string tagged on to end of filename or url that was clipped
59 * off to resolve to valid filename
61 protected String suffix = null;
63 protected String type = null;
65 protected BufferedReader dataIn = null;
67 protected String errormessage = "UNITIALISED SOURCE";
69 protected boolean error = true;
71 protected String warningMessage = null;
74 * size of readahead buffer used for when initial stream position is marked.
76 final int READAHEAD_LIMIT = 2048;
83 * Create a new FileParse instance reading from the same datasource starting
84 * at the current position. WARNING! Subsequent reads from either object will
85 * affect the read position of the other, but not the error state.
89 public FileParse(FileParse from) throws IOException
94 "Implementation error. Null FileParse in copy constructor");
100 suffixSeparator = from.suffixSeparator;
101 suffix = from.suffix;
102 errormessage = from.errormessage; // inherit potential error messages
103 error = false; // reset any error condition.
105 dataIn = from.dataIn;
110 dataName = from.dataName;
114 * Attempt to open a file as a datasource. Sets error and errormessage if
115 * fileStr was invalid.
118 * @return this.error (true if the source was invalid)
120 private boolean checkFileSource(String fileStr) throws IOException
123 this.inFile = new File(fileStr);
124 // check to see if it's a Jar file in disguise.
125 if (!inFile.exists())
127 errormessage = "FILE NOT FOUND";
130 if (!inFile.canRead())
132 errormessage = "FILE CANNOT BE OPENED FOR READING";
135 if (inFile.isDirectory())
137 // this is really a 'complex' filetype - but we don't handle directory
139 errormessage = "FILE IS A DIRECTORY";
144 dataIn = new BufferedReader(new FileReader(fileStr));
150 private boolean checkURLSource(String fileStr) throws IOException,
151 MalformedURLException
153 errormessage = "URL NOT FOUND";
154 URL url = new URL(fileStr);
155 dataIn = new BufferedReader(new InputStreamReader(url.openStream()));
156 // record URL as name of datasource.
162 * sets the suffix string (if any) and returns remainder (if suffix was
166 * @return truncated fileStr or null
168 private String extractSuffix(String fileStr)
170 // first check that there wasn't a suffix string tagged on.
171 int sfpos = fileStr.lastIndexOf(suffixSeparator);
172 if (sfpos > -1 && sfpos < fileStr.length() - 1)
174 suffix = fileStr.substring(sfpos + 1);
175 // System.err.println("DEBUG: Found Suffix:"+suffix);
176 return fileStr.substring(0, sfpos);
182 * Create a datasource for input to Jalview. See AppletFormatAdapter for the
183 * types of sources that are handled.
186 * - datasource locator/content
188 * - protocol of source
189 * @throws MalformedURLException
190 * @throws IOException
192 public FileParse(String fileStr, String type)
193 throws MalformedURLException, IOException
198 if (type.equals(AppletFormatAdapter.FILE))
200 if (checkFileSource(fileStr))
202 String suffixLess = extractSuffix(fileStr);
203 if (suffixLess != null)
205 if (checkFileSource(suffixLess))
207 throw new IOException("Problem opening " + inFile
208 + " (also tried " + suffixLess + ") : " + errormessage);
213 throw new IOException("Problem opening " + inFile + " : "
218 else if (type.equals(AppletFormatAdapter.URL))
224 checkURLSource(fileStr);
225 if (suffixSeparator == '#')
226 extractSuffix(fileStr); // URL lref is stored for later reference.
227 } catch (IOException e)
229 String suffixLess = extractSuffix(fileStr);
230 if (suffixLess == null)
238 checkURLSource(suffixLess);
239 } catch (IOException e2)
241 errormessage = "BAD URL WITH OR WITHOUT SUFFIX";
242 throw (e); // just pass back original - everything was wrong.
246 } catch (Exception e)
248 errormessage = "CANNOT ACCESS DATA AT URL '" + fileStr + "' ("
249 + e.getMessage() + ")";
253 else if (type.equals(AppletFormatAdapter.PASTE))
255 errormessage = "PASTE INACCESSIBLE!";
256 dataIn = new BufferedReader(new StringReader(fileStr));
259 else if (type.equals(AppletFormatAdapter.CLASSLOADER))
261 errormessage = "RESOURCE CANNOT BE LOCATED";
262 java.io.InputStream is = getClass()
263 .getResourceAsStream("/" + fileStr);
266 String suffixLess = extractSuffix(fileStr);
267 if (suffixLess != null)
268 is = getClass().getResourceAsStream("/" + suffixLess);
272 dataIn = new BufferedReader(new java.io.InputStreamReader(is));
282 errormessage = "PROBABLE IMPLEMENTATION ERROR : Datasource Type given as '"
283 + (type != null ? type : "null") + "'";
286 if (dataIn == null || error)
288 // pass up the reason why we have no source to read from
289 throw new IOException("Failed to read data from source:\n"
293 dataIn.mark(READAHEAD_LIMIT);
297 * mark the current position in the source as start for the purposes of it
298 * being analysed by IdentifyFile().identify
300 * @throws IOException
302 public void mark() throws IOException
306 dataIn.mark(READAHEAD_LIMIT);
310 throw new IOException("Unitialised Source Stream");
314 public String nextLine() throws IOException
317 return dataIn.readLine();
318 throw new IOException("Invalid Source Stream:" + errormessage);
321 public boolean isValid()
327 * closes the datasource and tidies up. source will be left in an error state
329 public void close() throws IOException
331 errormessage = "EXCEPTION ON CLOSE";
335 errormessage = "SOURCE IS CLOSED";
339 * rewinds the datasource the beginning.
342 public void reset() throws IOException
344 if (dataIn != null && !error)
350 throw new IOException(
351 "Implementation Error: Reset called for invalid source.");
357 * @return true if there is a warning for the user
359 public boolean hasWarningMessage()
361 return (warningMessage != null && warningMessage.length() > 0);
366 * @return empty string or warning message about file that was just parsed.
368 public String getWarningMessage()
370 return warningMessage;
373 public String getInFile()
377 return inFile.getAbsolutePath() + " (" + index + ")";
381 return "From Paste + (" + index + ")";
386 * @return the dataName
388 public String getDataName()
394 * set the (human readable) name or URI for this datasource
398 protected void setDataName(String dataname)
404 * get the underlying bufferedReader for this data source.
406 * @return null if no reader available
407 * @throws IOException
409 public Reader getReader()
411 if (dataIn != null) // Probably don't need to test for readiness &&