2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.5)
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 = '#';
43 * '#' separated string tagged on to end of filename or url that was clipped
44 * off to resolve to valid filename
46 protected String suffix = null;
48 protected String type = null;
50 protected BufferedReader dataIn = null;
52 protected String errormessage = "UNITIALISED SOURCE";
54 protected boolean error = true;
56 protected String warningMessage = null;
59 * size of readahead buffer used for when initial stream position is marked.
61 final int READAHEAD_LIMIT = 2048;
68 * Create a new FileParse instance reading from the same datasource starting
69 * at the current position. WARNING! Subsequent reads from either object will
70 * affect the read position of the other, but not the error state.
74 public FileParse(FileParse from) throws IOException
79 "Implementation error. Null FileParse in copy constructor");
85 suffixSeparator = from.suffixSeparator;
87 errormessage = from.errormessage; // inherit potential error messages
88 error = false; // reset any error condition.
95 dataName = from.dataName;
99 * Attempt to open a file as a datasource. Sets error and errormessage if
100 * fileStr was invalid.
103 * @return this.error (true if the source was invalid)
105 private boolean checkFileSource(String fileStr) throws IOException
108 this.inFile = new File(fileStr);
109 // check to see if it's a Jar file in disguise.
110 if (!inFile.exists())
112 errormessage = "FILE NOT FOUND";
115 if (!inFile.canRead())
117 errormessage = "FILE CANNOT BE OPENED FOR READING";
120 if (inFile.isDirectory())
122 // this is really a 'complex' filetype - but we don't handle directory
124 errormessage = "FILE IS A DIRECTORY";
129 dataIn = new BufferedReader(new FileReader(fileStr));
135 private boolean checkURLSource(String fileStr) throws IOException,
136 MalformedURLException
138 errormessage = "URL NOT FOUND";
139 URL url = new URL(fileStr);
140 dataIn = new BufferedReader(new InputStreamReader(url.openStream()));
141 // record URL as name of datasource.
147 * sets the suffix string (if any) and returns remainder (if suffix was
151 * @return truncated fileStr or null
153 private String extractSuffix(String fileStr)
155 // first check that there wasn't a suffix string tagged on.
156 int sfpos = fileStr.lastIndexOf(suffixSeparator);
157 if (sfpos > -1 && sfpos < fileStr.length() - 1)
159 suffix = fileStr.substring(sfpos + 1);
160 // System.err.println("DEBUG: Found Suffix:"+suffix);
161 return fileStr.substring(0, sfpos);
167 * Create a datasource for input to Jalview. See AppletFormatAdapter for the
168 * types of sources that are handled.
171 * - datasource locator/content
173 * - protocol of source
174 * @throws MalformedURLException
175 * @throws IOException
177 public FileParse(String fileStr, String type)
178 throws MalformedURLException, IOException
183 if (type.equals(AppletFormatAdapter.FILE))
185 if (checkFileSource(fileStr))
187 String suffixLess = extractSuffix(fileStr);
188 if (suffixLess != null)
190 if (checkFileSource(suffixLess))
192 throw new IOException("Problem opening " + inFile
193 + " (also tried " + suffixLess + ") : " + errormessage);
198 throw new IOException("Problem opening " + inFile + " : "
203 else if (type.equals(AppletFormatAdapter.URL))
209 checkURLSource(fileStr);
210 if (suffixSeparator == '#')
211 extractSuffix(fileStr); // URL lref is stored for later reference.
212 } catch (IOException e)
214 String suffixLess = extractSuffix(fileStr);
215 if (suffixLess == null)
223 checkURLSource(suffixLess);
224 } catch (IOException e2)
226 errormessage = "BAD URL WITH OR WITHOUT SUFFIX";
227 throw (e); // just pass back original - everything was wrong.
231 } catch (Exception e)
233 errormessage = "CANNOT ACCESS DATA AT URL '" + fileStr + "' ("
234 + e.getMessage() + ")";
238 else if (type.equals(AppletFormatAdapter.PASTE))
240 errormessage = "PASTE INACCESSIBLE!";
241 dataIn = new BufferedReader(new StringReader(fileStr));
244 else if (type.equals(AppletFormatAdapter.CLASSLOADER))
246 errormessage = "RESOURCE CANNOT BE LOCATED";
247 java.io.InputStream is = getClass()
248 .getResourceAsStream("/" + fileStr);
251 String suffixLess = extractSuffix(fileStr);
252 if (suffixLess != null)
253 is = getClass().getResourceAsStream("/" + suffixLess);
257 dataIn = new BufferedReader(new java.io.InputStreamReader(is));
267 errormessage = "PROBABLE IMPLEMENTATION ERROR : Datasource Type given as '"
268 + (type != null ? type : "null") + "'";
271 if (dataIn == null || error)
273 // pass up the reason why we have no source to read from
274 throw new IOException("Failed to read data from source:\n"
278 dataIn.mark(READAHEAD_LIMIT);
282 * mark the current position in the source as start for the purposes of it
283 * being analysed by IdentifyFile().identify
285 * @throws IOException
287 public void mark() throws IOException
291 dataIn.mark(READAHEAD_LIMIT);
295 throw new IOException("Unitialised Source Stream");
299 public String nextLine() throws IOException
302 return dataIn.readLine();
303 throw new IOException("Invalid Source Stream:" + errormessage);
306 public boolean isValid()
312 * closes the datasource and tidies up. source will be left in an error state
314 public void close() throws IOException
316 errormessage = "EXCEPTION ON CLOSE";
320 errormessage = "SOURCE IS CLOSED";
324 * rewinds the datasource the beginning.
327 public void reset() throws IOException
329 if (dataIn != null && !error)
335 throw new IOException(
336 "Implementation Error: Reset called for invalid source.");
342 * @return true if there is a warning for the user
344 public boolean hasWarningMessage()
346 return (warningMessage != null && warningMessage.length() > 0);
351 * @return empty string or warning message about file that was just parsed.
353 public String getWarningMessage()
355 return warningMessage;
358 public String getInFile()
362 return inFile.getAbsolutePath() + " (" + index + ")";
366 return "From Paste + (" + index + ")";
371 * @return the dataName
373 public String getDataName()
378 public Reader getReader() throws IOException
380 if (dataIn != null && dataIn.ready())