2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.api.AlignExportSettingI;
24 import jalview.api.AlignViewportI;
25 import jalview.api.AlignmentViewPanel;
26 import jalview.api.FeatureSettingsModelI;
27 import jalview.util.MessageManager;
29 import java.io.BufferedReader;
30 import java.io.ByteArrayInputStream;
32 import java.io.FileInputStream;
33 import java.io.FileReader;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.io.InputStreamReader;
37 import java.io.Reader;
38 import java.io.StringReader;
39 import java.net.MalformedURLException;
41 import java.util.zip.GZIPInputStream;
43 import javajs.util.Rdr;
46 * implements a random access wrapper around a particular datasource, for
47 * passing to identifyFile and AlignFile objects.
49 public class FileParse
51 protected static final String SPACE = " ";
53 protected static final String TAB = "\t";
56 * text specifying source of data. usually filename or url.
58 private String dataName = "unknown source";
60 public File inFile = null;
62 public byte[] bytes; // from JavaScript
65 * a viewport associated with the current file operation. May be null. May
66 * move to different object.
68 private AlignViewportI viewport;
71 * specific settings for exporting data from the current context
73 private AlignExportSettingI exportSettings;
76 * sequence counter for FileParse object created from same data source
81 * separator for extracting specific 'frame' of a datasource for formats that
82 * support multiple records (e.g. BLC, Stockholm, etc)
84 protected char suffixSeparator = '#';
87 * character used to write newlines
89 protected String newline = System.getProperty("line.separator");
91 public void setNewlineString(String nl)
96 public String getNewlineString()
102 * '#' separated string tagged on to end of filename or url that was clipped
103 * off to resolve to valid filename
105 protected String suffix = null;
107 protected DataSourceType dataSourceType = null;
109 protected BufferedReader dataIn = null;
111 protected String errormessage = "UNINITIALISED SOURCE";
113 protected boolean error = true;
115 protected String warningMessage = null;
118 * size of readahead buffer used for when initial stream position is marked.
120 final int READAHEAD_LIMIT = 2048;
127 * Create a new FileParse instance reading from the same datasource starting
128 * at the current position. WARNING! Subsequent reads from either object will
129 * affect the read position of the other, but not the error state.
133 public FileParse(FileParse from) throws IOException
137 throw new Error(MessageManager
138 .getString("error.implementation_error_null_fileparse"));
144 index = ++from.index;
145 inFile = from.inFile;
146 suffixSeparator = from.suffixSeparator;
147 suffix = from.suffix;
148 errormessage = from.errormessage; // inherit potential error messages
149 error = false; // reset any error condition.
150 dataSourceType = from.dataSourceType;
151 dataIn = from.dataIn;
156 dataName = from.dataName;
160 * Attempt to open a file as a datasource. Sets error and errormessage if
161 * fileStr was invalid.
164 * @return this.error (true if the source was invalid)
166 private boolean checkFileSource(String fileStr) throws IOException
169 this.inFile = new File(fileStr);
170 // check to see if it's a Jar file in disguise.
171 if (!inFile.exists())
173 errormessage = "FILE NOT FOUND";
176 if (!inFile.canRead())
178 errormessage = "FILE CANNOT BE OPENED FOR READING";
181 if (inFile.isDirectory())
183 // this is really a 'complex' filetype - but we don't handle directory
185 errormessage = "FILE IS A DIRECTORY";
190 if (fileStr.toLowerCase().endsWith(".gz"))
194 dataIn = tryAsGzipSource(new FileInputStream(fileStr));
197 } catch (Exception x)
199 warningMessage = "Failed to resolve as a GZ stream ("
200 + x.getMessage() + ")";
201 // x.printStackTrace();
206 dataIn = new BufferedReader(new FileReader(fileStr));
212 private BufferedReader tryAsGzipSource(InputStream inputStream)
215 BufferedReader inData = new BufferedReader(
216 new InputStreamReader(new GZIPInputStream(inputStream)));
223 private boolean checkURLSource(String fileStr)
224 throws IOException, MalformedURLException
226 errormessage = "URL NOT FOUND";
227 URL url = new URL(fileStr);
229 // GZIPInputStream code borrowed from Aquaria (soon to be open sourced) via
232 if (fileStr.toLowerCase().endsWith(".gz"))
236 InputStream inputStream = url.openStream();
237 dataIn = tryAsGzipSource(inputStream);
240 } catch (Exception ex)
248 dataIn = new BufferedReader(new InputStreamReader(url.openStream()));
249 } catch (IOException q)
253 throw new IOException(MessageManager
254 .getString("exception.failed_to_resolve_gzip_stream"), e);
258 // record URL as name of datasource.
264 * sets the suffix string (if any) and returns remainder (if suffix was
268 * @return truncated fileStr or null
270 private String extractSuffix(String fileStr)
272 // first check that there wasn't a suffix string tagged on.
273 int sfpos = fileStr.lastIndexOf(suffixSeparator);
274 if (sfpos > -1 && sfpos < fileStr.length() - 1)
276 suffix = fileStr.substring(sfpos + 1);
277 // System.err.println("DEBUG: Found Suffix:"+suffix);
278 return fileStr.substring(0, sfpos);
284 * not for general use, creates a fileParse object for an existing reader with
285 * configurable values for the origin and the type of the source
287 public FileParse(BufferedReader source, String originString,
288 DataSourceType sourceType)
290 dataSourceType = sourceType;
293 dataName = originString;
297 if (dataIn.markSupported())
299 dataIn.mark(READAHEAD_LIMIT);
301 } catch (IOException q)
308 * Create a datasource for input to Jalview. See AppletFormatAdapter for the
309 * types of sources that are handled.
312 * - datasource locator/content
314 * - protocol of source
315 * @throws MalformedURLException
316 * @throws IOException
318 public FileParse(String fileStr, DataSourceType sourceType)
319 throws MalformedURLException, IOException
322 this(null, fileStr, sourceType, false);
325 public FileParse(File file, DataSourceType sourceType)
326 throws MalformedURLException, IOException
329 this(file, file.getPath(), sourceType, true);
332 private FileParse(File file, String fileStr, DataSourceType sourceType,
333 boolean isFileObject) throws MalformedURLException, IOException
339 * this.bytes = file && file._bytes;
342 this.dataSourceType = sourceType;
345 if (sourceType == DataSourceType.FILE)
350 // this will be from JavaScript
352 dataIn = new BufferedReader(new java.io.InputStreamReader(new ByteArrayInputStream(bytes)));
355 else if (checkFileSource(fileStr))
357 String suffixLess = extractSuffix(fileStr);
358 if (suffixLess != null)
360 if (checkFileSource(suffixLess))
362 throw new IOException(MessageManager.formatMessage(
363 "exception.problem_opening_file_also_tried",
365 { inFile.getName(), suffixLess, errormessage }));
370 throw new IOException(MessageManager.formatMessage(
371 "exception.problem_opening_file", new String[]
372 { inFile.getName(), errormessage }));
376 else if (sourceType == DataSourceType.RELATIVE_URL)
380 * BH 2018 hack for no support for access-origin
384 * data = $.ajax({url:fileStr, async:false}).responseText;
388 dataIn = Rdr.getBR(data);
393 else if (sourceType == DataSourceType.URL)
399 checkURLSource(fileStr);
400 if (suffixSeparator == '#')
402 extractSuffix(fileStr); // URL lref is stored for later reference.
404 } catch (IOException e)
406 String suffixLess = extractSuffix(fileStr);
407 if (suffixLess == null)
415 checkURLSource(suffixLess);
416 } catch (IOException e2)
418 errormessage = "BAD URL WITH OR WITHOUT SUFFIX";
419 throw (e); // just pass back original - everything was wrong.
423 } catch (Exception e)
425 errormessage = "CANNOT ACCESS DATA AT URL '" + fileStr + "' ("
426 + e.getMessage() + ")";
430 else if (sourceType == DataSourceType.PASTE)
432 errormessage = "PASTE INACCESSIBLE!";
433 dataIn = new BufferedReader(new StringReader(fileStr));
436 else if (sourceType == DataSourceType.CLASSLOADER)
438 errormessage = "RESOURCE CANNOT BE LOCATED";
439 java.io.InputStream is = getClass()
440 .getResourceAsStream("/" + fileStr);
443 String suffixLess = extractSuffix(fileStr);
444 if (suffixLess != null)
446 is = getClass().getResourceAsStream("/" + suffixLess);
451 dataIn = new BufferedReader(new java.io.InputStreamReader(is));
461 errormessage = "PROBABLE IMPLEMENTATION ERROR : Datasource Type given as '"
462 + (sourceType != null ? sourceType : "null") + "'";
465 if (dataIn == null || error)
467 // pass up the reason why we have no source to read from
468 throw new IOException(MessageManager.formatMessage(
469 "exception.failed_to_read_data_from_source",
474 dataIn.mark(READAHEAD_LIMIT);
478 * mark the current position in the source as start for the purposes of it
479 * being analysed by IdentifyFile().identify
481 * @throws IOException
483 public void mark() throws IOException
487 dataIn.mark(READAHEAD_LIMIT);
491 throw new IOException(
492 MessageManager.getString("exception.no_init_source_stream"));
496 public String nextLine() throws IOException
500 return dataIn.readLine();
502 throw new IOException(MessageManager
503 .formatMessage("exception.invalid_source_stream", new String[]
509 * @return true if this FileParse is configured for Export only
511 public boolean isExporting()
513 return !error && dataIn == null;
518 * @return true if the data source is valid
520 public boolean isValid()
526 * closes the datasource and tidies up. source will be left in an error state
528 public void close() throws IOException
530 errormessage = "EXCEPTION ON CLOSE";
534 errormessage = "SOURCE IS CLOSED";
538 * Rewinds the datasource to the marked point if possible
543 public void reset(int bytesRead) throws IOException
545 if (bytesRead >= READAHEAD_LIMIT)
547 System.err.println(String.format(
548 "File reset error: read %d bytes but reset limit is %d",
549 bytesRead, READAHEAD_LIMIT));
551 if (dataIn != null && !error)
557 throw new IOException(MessageManager.getString(
558 "error.implementation_error_reset_called_for_invalid_source"));
564 * @return true if there is a warning for the user
566 public boolean hasWarningMessage()
568 return (warningMessage != null && warningMessage.length() > 0);
573 * @return empty string or warning message about file that was just parsed.
575 public String getWarningMessage()
577 return warningMessage;
580 public String getInFile()
584 return inFile.getAbsolutePath() + " (" + index + ")";
588 return "From Paste + (" + index + ")";
593 * @return the dataName
595 public String getDataName()
601 * set the (human readable) name or URI for this datasource
605 protected void setDataName(String dataname)
611 * get the underlying bufferedReader for this data source.
613 * @return null if no reader available
614 * @throws IOException
616 public Reader getReader()
618 if (dataIn != null) // Probably don't need to test for readiness &&
626 public AlignViewportI getViewport()
631 public void setViewport(AlignViewportI viewport)
633 this.viewport = viewport;
637 * @return the currently configured exportSettings for writing data.
639 public AlignExportSettingI getExportSettings()
641 return exportSettings;
645 * Set configuration for export of data.
647 * @param exportSettings
648 * the exportSettings to set
650 public void setExportSettings(AlignExportSettingI exportSettings)
652 this.exportSettings = exportSettings;
656 * method overridden by complex file exporter/importers which support
657 * exporting visualisation and layout settings for a view
661 public void configureForView(AlignmentViewPanel avpanel)
665 setViewport(avpanel.getAlignViewport());
667 // could also set export/import settings
671 * Returns the preferred feature colour configuration if there is one, else
676 public FeatureSettingsModelI getFeatureColourScheme()
681 public DataSourceType getDataSourceType()
683 return dataSourceType;