2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, 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 = '#';
42 * character used to write newlines
44 protected String newline = System.getProperty("line.separator");
45 public void setNewlineString(String nl)
49 public String getNewlineString()
54 * '#' separated string tagged on to end of filename or url that was clipped
55 * off to resolve to valid filename
57 protected String suffix = null;
59 protected String type = null;
61 protected BufferedReader dataIn = null;
63 protected String errormessage = "UNITIALISED SOURCE";
65 protected boolean error = true;
67 protected String warningMessage = null;
70 * size of readahead buffer used for when initial stream position is marked.
72 final int READAHEAD_LIMIT = 2048;
79 * Create a new FileParse instance reading from the same datasource starting
80 * at the current position. WARNING! Subsequent reads from either object will
81 * affect the read position of the other, but not the error state.
85 public FileParse(FileParse from) throws IOException
90 "Implementation error. Null FileParse in copy constructor");
96 suffixSeparator = from.suffixSeparator;
98 errormessage = from.errormessage; // inherit potential error messages
99 error = false; // reset any error condition.
101 dataIn = from.dataIn;
106 dataName = from.dataName;
110 * Attempt to open a file as a datasource. Sets error and errormessage if
111 * fileStr was invalid.
114 * @return this.error (true if the source was invalid)
116 private boolean checkFileSource(String fileStr) throws IOException
119 this.inFile = new File(fileStr);
120 // check to see if it's a Jar file in disguise.
121 if (!inFile.exists())
123 errormessage = "FILE NOT FOUND";
126 if (!inFile.canRead())
128 errormessage = "FILE CANNOT BE OPENED FOR READING";
131 if (inFile.isDirectory())
133 // this is really a 'complex' filetype - but we don't handle directory
135 errormessage = "FILE IS A DIRECTORY";
140 dataIn = new BufferedReader(new FileReader(fileStr));
146 private boolean checkURLSource(String fileStr) throws IOException,
147 MalformedURLException
149 errormessage = "URL NOT FOUND";
150 URL url = new URL(fileStr);
151 dataIn = new BufferedReader(new InputStreamReader(url.openStream()));
152 // record URL as name of datasource.
158 * sets the suffix string (if any) and returns remainder (if suffix was
162 * @return truncated fileStr or null
164 private String extractSuffix(String fileStr)
166 // first check that there wasn't a suffix string tagged on.
167 int sfpos = fileStr.lastIndexOf(suffixSeparator);
168 if (sfpos > -1 && sfpos < fileStr.length() - 1)
170 suffix = fileStr.substring(sfpos + 1);
171 // System.err.println("DEBUG: Found Suffix:"+suffix);
172 return fileStr.substring(0, sfpos);
178 * Create a datasource for input to Jalview. See AppletFormatAdapter for the
179 * types of sources that are handled.
182 * - datasource locator/content
184 * - protocol of source
185 * @throws MalformedURLException
186 * @throws IOException
188 public FileParse(String fileStr, String type)
189 throws MalformedURLException, IOException
194 if (type.equals(AppletFormatAdapter.FILE))
196 if (checkFileSource(fileStr))
198 String suffixLess = extractSuffix(fileStr);
199 if (suffixLess != null)
201 if (checkFileSource(suffixLess))
203 throw new IOException("Problem opening " + inFile
204 + " (also tried " + suffixLess + ") : " + errormessage);
209 throw new IOException("Problem opening " + inFile + " : "
214 else if (type.equals(AppletFormatAdapter.URL))
220 checkURLSource(fileStr);
221 if (suffixSeparator == '#')
222 extractSuffix(fileStr); // URL lref is stored for later reference.
223 } catch (IOException e)
225 String suffixLess = extractSuffix(fileStr);
226 if (suffixLess == null)
234 checkURLSource(suffixLess);
235 } catch (IOException e2)
237 errormessage = "BAD URL WITH OR WITHOUT SUFFIX";
238 throw (e); // just pass back original - everything was wrong.
242 } catch (Exception e)
244 errormessage = "CANNOT ACCESS DATA AT URL '" + fileStr + "' ("
245 + e.getMessage() + ")";
249 else if (type.equals(AppletFormatAdapter.PASTE))
251 errormessage = "PASTE INACCESSIBLE!";
252 dataIn = new BufferedReader(new StringReader(fileStr));
255 else if (type.equals(AppletFormatAdapter.CLASSLOADER))
257 errormessage = "RESOURCE CANNOT BE LOCATED";
258 java.io.InputStream is = getClass()
259 .getResourceAsStream("/" + fileStr);
262 String suffixLess = extractSuffix(fileStr);
263 if (suffixLess != null)
264 is = getClass().getResourceAsStream("/" + suffixLess);
268 dataIn = new BufferedReader(new java.io.InputStreamReader(is));
278 errormessage = "PROBABLE IMPLEMENTATION ERROR : Datasource Type given as '"
279 + (type != null ? type : "null") + "'";
282 if (dataIn == null || error)
284 // pass up the reason why we have no source to read from
285 throw new IOException("Failed to read data from source:\n"
289 dataIn.mark(READAHEAD_LIMIT);
293 * mark the current position in the source as start for the purposes of it
294 * being analysed by IdentifyFile().identify
296 * @throws IOException
298 public void mark() throws IOException
302 dataIn.mark(READAHEAD_LIMIT);
306 throw new IOException("Unitialised Source Stream");
310 public String nextLine() throws IOException
313 return dataIn.readLine();
314 throw new IOException("Invalid Source Stream:" + errormessage);
317 public boolean isValid()
323 * closes the datasource and tidies up. source will be left in an error state
325 public void close() throws IOException
327 errormessage = "EXCEPTION ON CLOSE";
331 errormessage = "SOURCE IS CLOSED";
335 * rewinds the datasource the beginning.
338 public void reset() throws IOException
340 if (dataIn != null && !error)
346 throw new IOException(
347 "Implementation Error: Reset called for invalid source.");
353 * @return true if there is a warning for the user
355 public boolean hasWarningMessage()
357 return (warningMessage != null && warningMessage.length() > 0);
362 * @return empty string or warning message about file that was just parsed.
364 public String getWarningMessage()
366 return warningMessage;
369 public String getInFile()
373 return inFile.getAbsolutePath() + " (" + index + ")";
377 return "From Paste + (" + index + ")";
382 * @return the dataName
384 public String getDataName()
389 * set the (human readable) name or URI for this datasource
392 protected void setDataName(String dataname) {
398 * get the underlying bufferedReader for this data source.
399 * @return null if no reader available
400 * @throws IOException
402 public Reader getReader()
404 if (dataIn != null) // Probably don't need to test for readiness && dataIn.ready())