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 java.io.BufferedReader;
24 import java.io.ByteArrayInputStream;
26 import java.io.FileInputStream;
27 import java.io.FileReader;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.io.Reader;
32 import java.io.StringReader;
33 import java.net.MalformedURLException;
35 import java.util.zip.GZIPInputStream;
37 import jalview.api.AlignExportSettingsI;
38 import jalview.api.AlignViewportI;
39 import jalview.api.AlignmentViewPanel;
40 import jalview.api.FeatureSettingsModelI;
41 import jalview.util.MessageManager;
42 import jalview.util.Platform;
43 import jalview.ws.dbsources.Pfam;
44 import jalview.ws.dbsources.Rfam;
47 * implements a random access wrapper around a particular datasource, for
48 * passing to identifyFile and AlignFile objects.
50 public class FileParse
52 protected static final String SPACE = " ";
54 protected static final String TAB = "\t";
56 private static final String GZ_EXT = ".gz";
59 * text specifying source of data. usually filename or url.
61 private String dataName = "unknown source";
63 public File inFile = null;
65 private byte[] bytes; // from JavaScript
67 public byte[] getBytes()
73 * a viewport associated with the current file operation. May be null. May
74 * move to different object.
76 private AlignViewportI viewport;
79 * specific settings for exporting data from the current context
81 private AlignExportSettingsI exportSettings;
84 * sequence counter for FileParse object created from same data source
89 * separator for extracting specific 'frame' of a datasource for formats that
90 * support multiple records (e.g. BLC, Stockholm, etc)
92 protected char suffixSeparator = '#';
95 * character used to write newlines
97 protected String newline = System.getProperty("line.separator");
99 public void setNewlineString(String nl)
104 public String getNewlineString()
110 * '#' separated string tagged on to end of filename or url that was clipped
111 * off to resolve to valid filename
113 protected String suffix = null;
115 protected DataSourceType dataSourceType = null;
117 protected BufferedReader dataIn = null;
119 protected String errormessage = "UNINITIALISED SOURCE";
121 protected boolean error = true;
123 protected String warningMessage = null;
126 * size of readahead buffer used for when initial stream position is marked.
128 final int READAHEAD_LIMIT = 2048;
135 * Create a new FileParse instance reading from the same datasource starting
136 * at the current position. WARNING! Subsequent reads from either object will
137 * affect the read position of the other, but not the error state.
141 public FileParse(FileParse from) throws IOException
145 throw new Error(MessageManager
146 .getString("error.implementation_error_null_fileparse"));
152 index = ++from.index;
153 inFile = from.inFile;
154 suffixSeparator = from.suffixSeparator;
155 suffix = from.suffix;
156 errormessage = from.errormessage; // inherit potential error messages
157 error = false; // reset any error condition.
158 dataSourceType = from.dataSourceType;
159 dataIn = from.dataIn;
164 dataName = from.dataName;
168 * Attempt to open a file as a datasource. Sets error and errormessage if
169 * fileStr was invalid.
172 * @return this.error (true if the source was invalid)
174 private boolean checkFileSource(String fileStr) throws IOException
177 this.inFile = new File(fileStr);
178 // check to see if it's a Jar file in disguise.
179 if (!inFile.exists())
181 errormessage = "FILE NOT FOUND";
184 if (!inFile.canRead())
186 errormessage = "FILE CANNOT BE OPENED FOR READING";
189 if (inFile.isDirectory())
191 // this is really a 'complex' filetype - but we don't handle directory
193 errormessage = "FILE IS A DIRECTORY";
198 if (fileStr.toLowerCase().endsWith(GZ_EXT))
202 dataIn = tryAsGzipSource(new FileInputStream(fileStr));
205 } catch (Exception x)
207 warningMessage = "Failed to resolve as a GZ stream ("
208 + x.getMessage() + ")";
209 // x.printStackTrace();
214 dataIn = new BufferedReader(new FileReader(fileStr));
220 private BufferedReader tryAsGzipSource(InputStream inputStream)
223 BufferedReader inData = new BufferedReader(
224 new InputStreamReader(new GZIPInputStream(inputStream)));
231 private boolean checkURLSource(String fileStr)
232 throws IOException, MalformedURLException
234 errormessage = "URL NOT FOUND";
235 URL url = new URL(fileStr);
237 // GZIPInputStream code borrowed from Aquaria (soon to be open sourced) via
240 if (isGzipped(fileStr))
244 InputStream inputStream = url.openStream();
245 dataIn = tryAsGzipSource(inputStream);
248 } catch (Exception ex)
256 dataIn = new BufferedReader(new InputStreamReader(url.openStream()));
257 } catch (IOException q)
261 throw new IOException(MessageManager
262 .getString("exception.failed_to_resolve_gzip_stream"), e);
266 // record URL as name of datasource.
272 * Answers true if the filename (or URL) has a format which Jalview recognises
273 * as denoting gzipped content.
275 * Currently this means having a ".gz" extension, or ending in "/gzipped" or
276 * "?gz=1" (used to retrieve gzipped from Pfam and Rfam respectively).
281 protected static boolean isGzipped(String filename)
283 if (filename == null)
287 String lower = filename.toLowerCase();
288 return lower.endsWith(GZ_EXT) || lower.endsWith(Pfam.GZIPPED)
289 || lower.endsWith(Rfam.GZIPPED);
293 * sets the suffix string (if any) and returns remainder (if suffix was
297 * @return truncated fileStr or null
299 private String extractSuffix(String fileStr)
301 // first check that there wasn't a suffix string tagged on.
302 int sfpos = fileStr.lastIndexOf(suffixSeparator);
303 if (sfpos > -1 && sfpos < fileStr.length() - 1)
305 suffix = fileStr.substring(sfpos + 1);
306 // System.err.println("DEBUG: Found Suffix:"+suffix);
307 return fileStr.substring(0, sfpos);
313 * not for general use, creates a fileParse object for an existing reader with
314 * configurable values for the origin and the type of the source
316 public FileParse(BufferedReader source, String originString,
317 DataSourceType sourceType)
319 dataSourceType = sourceType;
322 dataName = originString;
326 if (dataIn.markSupported())
328 dataIn.mark(READAHEAD_LIMIT);
330 } catch (IOException q)
337 * Create a datasource for input to Jalview. See AppletFormatAdapter for the
338 * types of sources that are handled.
341 * - datasource locator/content as File or String
343 * - protocol of source
344 * @throws MalformedURLException
345 * @throws IOException
347 public FileParse(Object file, DataSourceType sourceType)
348 throws MalformedURLException, IOException
350 if (file instanceof File)
352 parse((File) file, ((File) file).getPath(), sourceType, true);
356 parse(null, file.toString(), sourceType, false);
360 private void parse(File file, String fileStr, DataSourceType sourceType,
361 boolean isFileObject) throws IOException
363 bytes = Platform.getFileBytes(file);
364 dataSourceType = sourceType;
367 if (sourceType == DataSourceType.FILE)
372 // this will be from JavaScript
374 dataIn = new BufferedReader(
375 new InputStreamReader(new ByteArrayInputStream(bytes)));
378 else if (checkFileSource(fileStr))
380 String suffixLess = extractSuffix(fileStr);
381 if (suffixLess != null)
383 if (checkFileSource(suffixLess))
385 throw new IOException(MessageManager.formatMessage(
386 "exception.problem_opening_file_also_tried",
388 { inFile.getName(), suffixLess, errormessage }));
393 throw new IOException(MessageManager.formatMessage(
394 "exception.problem_opening_file", new String[]
395 { inFile.getName(), errormessage }));
399 else if (sourceType == DataSourceType.RELATIVE_URL)
401 // BH 2018 hack for no support for access-origin
402 bytes = Platform.getFileAsBytes(fileStr);
403 dataIn = new BufferedReader(
404 new InputStreamReader(new ByteArrayInputStream(bytes)));
408 else if (sourceType == DataSourceType.URL)
414 checkURLSource(fileStr);
415 if (suffixSeparator == '#')
417 extractSuffix(fileStr); // URL lref is stored for later reference.
419 } catch (IOException e)
421 String suffixLess = extractSuffix(fileStr);
422 if (suffixLess == null)
430 checkURLSource(suffixLess);
431 } catch (IOException e2)
433 errormessage = "BAD URL WITH OR WITHOUT SUFFIX";
434 throw (e); // just pass back original - everything was wrong.
438 } catch (Exception e)
440 errormessage = "CANNOT ACCESS DATA AT URL '" + fileStr + "' ("
441 + e.getMessage() + ")";
445 else if (sourceType == DataSourceType.PASTE)
447 errormessage = "PASTE INACCESSIBLE!";
448 dataIn = new BufferedReader(new StringReader(fileStr));
451 else if (sourceType == DataSourceType.CLASSLOADER)
453 errormessage = "RESOURCE CANNOT BE LOCATED";
454 InputStream is = getClass().getResourceAsStream("/" + fileStr);
457 String suffixLess = extractSuffix(fileStr);
458 if (suffixLess != null)
460 is = getClass().getResourceAsStream("/" + suffixLess);
465 dataIn = new BufferedReader(new InputStreamReader(is));
475 errormessage = "PROBABLE IMPLEMENTATION ERROR : Datasource Type given as '"
476 + (sourceType != null ? sourceType : "null") + "'";
479 if (dataIn == null || error)
481 // pass up the reason why we have no source to read from
482 throw new IOException(MessageManager.formatMessage(
483 "exception.failed_to_read_data_from_source", new String[]
487 dataIn.mark(READAHEAD_LIMIT);
491 * mark the current position in the source as start for the purposes of it
492 * being analysed by IdentifyFile().identify
494 * @throws IOException
496 public void mark() throws IOException
500 dataIn.mark(READAHEAD_LIMIT);
504 throw new IOException(
505 MessageManager.getString("exception.no_init_source_stream"));
509 public String nextLine() throws IOException
513 return dataIn.readLine();
515 throw new IOException(MessageManager
516 .formatMessage("exception.invalid_source_stream", new String[]
522 * @return true if this FileParse is configured for Export only
524 public boolean isExporting()
526 return !error && dataIn == null;
531 * @return true if the data source is valid
533 public boolean isValid()
539 * closes the datasource and tidies up. source will be left in an error state
541 public void close() throws IOException
543 errormessage = "EXCEPTION ON CLOSE";
547 errormessage = "SOURCE IS CLOSED";
551 * Rewinds the datasource to the marked point if possible
556 public void reset(int bytesRead) throws IOException
558 if (bytesRead >= READAHEAD_LIMIT)
560 System.err.println(String.format(
561 "File reset error: read %d bytes but reset limit is %d",
562 bytesRead, READAHEAD_LIMIT));
564 if (dataIn != null && !error)
570 throw new IOException(MessageManager.getString(
571 "error.implementation_error_reset_called_for_invalid_source"));
577 * @return true if there is a warning for the user
579 public boolean hasWarningMessage()
581 return (warningMessage != null && warningMessage.length() > 0);
586 * @return empty string or warning message about file that was just parsed.
588 public String getWarningMessage()
590 return warningMessage;
593 public String getInFile()
597 return inFile.getAbsolutePath() + " (" + index + ")";
601 return "From Paste + (" + index + ")";
606 * @return the dataName
608 public String getDataName()
614 * set the (human readable) name or URI for this datasource
618 protected void setDataName(String dataname)
624 * get the underlying bufferedReader for this data source.
626 * @return null if no reader available
627 * @throws IOException
629 public Reader getReader()
631 if (dataIn != null) // Probably don't need to test for readiness &&
639 public AlignViewportI getViewport()
644 public void setViewport(AlignViewportI viewport)
646 this.viewport = viewport;
650 * @return the currently configured exportSettings for writing data.
652 public AlignExportSettingsI getExportSettings()
654 return exportSettings;
658 * Set configuration for export of data.
660 * @param exportSettings
661 * the exportSettings to set
663 public void setExportSettings(AlignExportSettingsI exportSettings)
665 this.exportSettings = exportSettings;
669 * method overridden by complex file exporter/importers which support
670 * exporting visualisation and layout settings for a view
674 public void configureForView(AlignmentViewPanel avpanel)
678 setViewport(avpanel.getAlignViewport());
680 // could also set export/import settings
684 * Returns the preferred feature colour configuration if there is one, else
689 public FeatureSettingsModelI getFeatureColourScheme()
694 public DataSourceType getDataSourceType()
696 return dataSourceType;
700 * Returns a buffered reader for the input object. Returns null, or throws
701 * IOException, on failure.
704 * a File, or a String which is a name of a file
707 * @throws IOException
709 public BufferedReader getBufferedReader(Object file,
710 DataSourceType sourceType) throws IOException
712 BufferedReader in = null;
718 if (file instanceof String)
720 return new BufferedReader(new FileReader((String) file));
722 bytes = Platform.getFileBytes((File) file);
725 return new BufferedReader(
726 new InputStreamReader(new ByteArrayInputStream(bytes)));
728 return new BufferedReader(new FileReader((File) file));
730 URL url = new URL(file.toString());
731 in = new BufferedReader(new InputStreamReader(url.openStream()));
733 case RELATIVE_URL: // JalviewJS only
734 bytes = Platform.getFileAsBytes(file.toString());
737 in = new BufferedReader(
738 new InputStreamReader(new ByteArrayInputStream(bytes)));
742 in = new BufferedReader(new StringReader(file.toString()));
745 InputStream is = getClass().getResourceAsStream("/" + file);
748 in = new BufferedReader(new InputStreamReader(is));