2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25 * implements a random access wrapper around a particular datasource, for
26 * passing to identifyFile and AlignFile objects.
28 public class FileParse
30 public File inFile = null;
32 public int index = 1; // sequence counter for FileParse object created from
35 protected char suffixSeparator = '#';
38 * '#' separated string tagged on to end of filename or url that was clipped
39 * off to resolve to valid filename
41 protected String suffix = null;
43 protected String type = null;
45 protected BufferedReader dataIn = null;
47 protected String errormessage = "UNITIALISED SOURCE";
49 protected boolean error = true;
51 protected String warningMessage = null;
54 * size of readahead buffer used for when initial stream position is marked.
56 final int READAHEAD_LIMIT = 2048;
63 * Create a new FileParse instance reading from the same datasource starting
64 * at the current position. WARNING! Subsequent reads from either object will
65 * affect the read position of the other, but not the error state.
69 public FileParse(FileParse from) throws IOException
74 "Implementation error. Null FileParse in copy constructor");
80 suffixSeparator = from.suffixSeparator;
82 errormessage = from.errormessage; // inherit potential error messages
83 error = false; // reset any error condition.
93 * Attempt to open a file as a datasource. Sets error and errormessage if
94 * fileStr was invalid.
97 * @return this.error (true if the source was invalid)
99 private boolean checkFileSource(String fileStr) throws IOException
102 this.inFile = new File(fileStr);
103 // check to see if it's a Jar file in disguise.
104 if (!inFile.exists())
106 errormessage = "FILE NOT FOUND";
109 if (!inFile.canRead())
111 errormessage = "FILE CANNOT BE OPENED FOR READING";
114 if (inFile.isDirectory())
116 // this is really a 'complex' filetype - but we don't handle directory
118 errormessage = "FILE IS A DIRECTORY";
123 dataIn = new BufferedReader(new FileReader(fileStr));
128 private boolean checkURLSource(String fileStr) throws IOException,
129 MalformedURLException
131 errormessage = "URL NOT FOUND";
132 URL url = new URL(fileStr);
133 dataIn = new BufferedReader(new InputStreamReader(url.openStream()));
138 * sets the suffix string (if any) and returns remainder (if suffix was
142 * @return truncated fileStr or null
144 private String extractSuffix(String fileStr)
146 // first check that there wasn't a suffix string tagged on.
147 int sfpos = fileStr.lastIndexOf(suffixSeparator);
148 if (sfpos > -1 && sfpos < fileStr.length() - 1)
150 suffix = fileStr.substring(sfpos + 1);
151 // System.err.println("DEBUG: Found Suffix:"+suffix);
152 return fileStr.substring(0, sfpos);
158 * Create a datasource for input to Jalview. See AppletFormatAdapter for the
159 * types of sources that are handled.
162 * datasource locator/content
165 * @throws MalformedURLException
166 * @throws IOException
168 public FileParse(String fileStr, String type)
169 throws MalformedURLException, IOException
174 if (type.equals(AppletFormatAdapter.FILE))
176 if (checkFileSource(fileStr))
178 String suffixLess = extractSuffix(fileStr);
179 if (suffixLess != null)
181 if (checkFileSource(suffixLess))
183 throw new IOException("Problem opening " + inFile
184 + " (also tried " + suffixLess + ") : " + errormessage);
189 throw new IOException("Problem opening " + inFile + " : "
194 else if (type.equals(AppletFormatAdapter.URL))
200 checkURLSource(fileStr);
201 if (suffixSeparator == '#')
202 extractSuffix(fileStr); // URL lref is stored for later reference.
203 } catch (IOException e)
205 String suffixLess = extractSuffix(fileStr);
206 if (suffixLess == null)
214 checkURLSource(suffixLess);
215 } catch (IOException e2)
217 errormessage = "BAD URL WITH OR WITHOUT SUFFIX";
218 throw (e); // just pass back original - everything was wrong.
222 } catch (Exception e)
224 errormessage = "CANNOT ACCESS DATA AT URL '" + fileStr + "' ("
225 + e.getMessage() + ")";
229 else if (type.equals(AppletFormatAdapter.PASTE))
231 errormessage = "PASTE INACCESSIBLE!";
232 dataIn = new BufferedReader(new StringReader(fileStr));
234 else if (type.equals(AppletFormatAdapter.CLASSLOADER))
236 errormessage = "RESOURCE CANNOT BE LOCATED";
237 java.io.InputStream is = getClass()
238 .getResourceAsStream("/" + fileStr);
241 String suffixLess = extractSuffix(fileStr);
242 if (suffixLess != null)
243 is = getClass().getResourceAsStream("/" + suffixLess);
247 dataIn = new BufferedReader(new java.io.InputStreamReader(is));
256 errormessage = "PROBABLE IMPLEMENTATION ERROR : Datasource Type given as '"
257 + (type != null ? type : "null") + "'";
260 if (dataIn == null || error)
262 // pass up the reason why we have no source to read from
263 throw new IOException("Failed to read data from source:\n"
267 dataIn.mark(READAHEAD_LIMIT);
271 * mark the current position in the source as start for the purposes of it
272 * being analysed by IdentifyFile().identify
274 * @throws IOException
276 public void mark() throws IOException
280 dataIn.mark(READAHEAD_LIMIT);
284 throw new IOException("Unitialised Source Stream");
288 public String nextLine() throws IOException
291 return dataIn.readLine();
292 throw new IOException("Invalid Source Stream:" + errormessage);
295 public boolean isValid()
301 * closes the datasource and tidies up. source will be left in an error state
303 public void close() throws IOException
305 errormessage = "EXCEPTION ON CLOSE";
309 errormessage = "SOURCE IS CLOSED";
313 * rewinds the datasource the beginning.
316 public void reset() throws IOException
318 if (dataIn != null && !error)
324 throw new IOException(
325 "Implementation Error: Reset called for invalid source.");
331 * @return true if there is a warning for the user
333 public boolean hasWarningMessage()
335 return (warningMessage != null && warningMessage.length() > 0);
340 * @return empty string or warning message about file that was just parsed.
342 public String getWarningMessage()
344 return warningMessage;
347 public String getInFile()
351 return inFile.getAbsolutePath() + " (" + index + ")";
355 return "From Paste + (" + index + ")";
359 public Reader getReader() throws IOException
361 if (dataIn != null && dataIn.ready())