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.HttpURLConnection;
34 import java.net.MalformedURLException;
36 import java.util.zip.GZIPInputStream;
38 import jalview.api.AlignExportSettingsI;
39 import jalview.api.AlignViewportI;
40 import jalview.api.AlignmentViewPanel;
41 import jalview.api.FeatureSettingsModelI;
42 import jalview.util.MessageManager;
43 import jalview.util.Platform;
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 private byte[] bytes; // from JavaScript
64 public byte[] getBytes()
70 * a viewport associated with the current file operation. May be null. May
71 * move to different object.
73 private AlignViewportI viewport;
76 * specific settings for exporting data from the current context
78 private AlignExportSettingsI exportSettings;
81 * sequence counter for FileParse object created from same data source
86 * separator for extracting specific 'frame' of a datasource for formats that
87 * support multiple records (e.g. BLC, Stockholm, etc)
89 protected char suffixSeparator = '#';
92 * character used to write newlines
94 protected String newline = System.getProperty("line.separator");
96 public void setNewlineString(String nl)
101 public String getNewlineString()
107 * '#' separated string tagged on to end of filename or url that was clipped
108 * off to resolve to valid filename
110 protected String suffix = null;
112 protected DataSourceType dataSourceType = null;
114 protected BufferedReader dataIn = null;
116 protected String errormessage = "UNINITIALISED SOURCE";
118 protected boolean error = true;
120 protected String warningMessage = null;
123 * size of readahead buffer used for when initial stream position is marked.
125 final int READAHEAD_LIMIT = 2048;
132 * Create a new FileParse instance reading from the same datasource starting
133 * at the current position. WARNING! Subsequent reads from either object will
134 * affect the read position of the other, but not the error state.
138 public FileParse(FileParse from) throws IOException
142 throw new Error(MessageManager
143 .getString("error.implementation_error_null_fileparse"));
149 index = ++from.index;
150 inFile = from.inFile;
151 suffixSeparator = from.suffixSeparator;
152 suffix = from.suffix;
153 errormessage = from.errormessage; // inherit potential error messages
154 error = false; // reset any error condition.
155 dataSourceType = from.dataSourceType;
156 dataIn = from.dataIn;
161 dataName = from.dataName;
165 * Attempt to open a file as a datasource. Sets error and errormessage if
166 * fileStr was invalid.
169 * @return this.error (true if the source was invalid)
171 private boolean checkFileSource(String fileStr) throws IOException
174 this.inFile = new File(fileStr);
175 // check to see if it's a Jar file in disguise.
176 if (!inFile.exists())
178 errormessage = "FILE NOT FOUND";
181 if (!inFile.canRead())
183 errormessage = "FILE CANNOT BE OPENED FOR READING";
186 if (inFile.isDirectory())
188 // this is really a 'complex' filetype - but we don't handle directory
190 errormessage = "FILE IS A DIRECTORY";
195 if (fileStr.toLowerCase().endsWith(".gz"))
199 dataIn = getGzipReader(new FileInputStream(fileStr));
202 } catch (Exception x)
204 warningMessage = "Failed to resolve as a GZ stream ("
205 + x.getMessage() + ")";
206 // x.printStackTrace();
211 dataIn = new BufferedReader(new FileReader(fileStr));
218 * Returns a {@code BufferedReader} which wraps the input stream with a
219 * GZIPInputStream. Throws a {@code ZipException} if a GZIP format error
220 * occurs or the compression method used is unsupported.
226 private BufferedReader getGzipReader(InputStream inputStream)
229 BufferedReader inData = new BufferedReader(
230 new InputStreamReader(new GZIPInputStream(inputStream)));
238 * Tries to read from the given URL. If successful, saves a reader to the
239 * response in field {@code dataIn}, otherwise (on exception, or HTTP response
240 * status not 200), throws an exception.
242 * If the response status includes
245 * Content-Type : application/x-gzip
248 * then tries to read as gzipped content.
251 * @throws IOException
252 * @throws MalformedURLException
254 private void checkURLSource(String urlStr)
255 throws IOException, MalformedURLException
257 errormessage = "URL NOT FOUND";
258 URL url = new URL(urlStr);
259 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
260 int rc = conn.getResponseCode();
261 if (rc != HttpURLConnection.HTTP_OK)
263 throw new IOException(
264 "Response status from " + urlStr + " was " + rc);
266 String encoding = conn.getContentEncoding();
267 String contentType = conn.getContentType();
268 boolean isgzipped = "application/x-gzip".equalsIgnoreCase(contentType)
269 || "gzip".equals(encoding);
271 InputStream inputStream = conn.getInputStream();
276 dataIn = getGzipReader(inputStream);
278 } catch (Exception e1)
280 throw new IOException(MessageManager
281 .getString("exception.failed_to_resolve_gzip_stream"), e);
286 dataIn = new BufferedReader(new InputStreamReader(inputStream));
292 * sets the suffix string (if any) and returns remainder (if suffix was
296 * @return truncated fileStr or null
298 private String extractSuffix(String fileStr)
300 // first check that there wasn't a suffix string tagged on.
301 int sfpos = fileStr.lastIndexOf(suffixSeparator);
302 if (sfpos > -1 && sfpos < fileStr.length() - 1)
304 suffix = fileStr.substring(sfpos + 1);
305 // System.err.println("DEBUG: Found Suffix:"+suffix);
306 return fileStr.substring(0, sfpos);
312 * not for general use, creates a fileParse object for an existing reader with
313 * configurable values for the origin and the type of the source
315 public FileParse(BufferedReader source, String originString,
316 DataSourceType sourceType)
318 dataSourceType = sourceType;
321 dataName = originString;
325 if (dataIn.markSupported())
327 dataIn.mark(READAHEAD_LIMIT);
329 } catch (IOException q)
336 * Create a datasource for input to Jalview. See AppletFormatAdapter for the
337 * types of sources that are handled.
340 * - datasource locator/content as File or String
342 * - protocol of source
343 * @throws MalformedURLException
344 * @throws IOException
346 public FileParse(Object file, DataSourceType sourceType)
347 throws MalformedURLException, IOException
349 if (file instanceof File)
351 parse((File) file, ((File) file).getPath(), sourceType, true);
355 parse(null, file.toString(), sourceType, false);
359 private void parse(File file, String fileStr, DataSourceType sourceType,
360 boolean isFileObject) throws IOException
362 bytes = Platform.getFileBytes(file);
363 dataSourceType = sourceType;
366 if (sourceType == DataSourceType.FILE)
371 // this will be from JavaScript
373 dataIn = new BufferedReader(
374 new InputStreamReader(new ByteArrayInputStream(bytes)));
377 else if (checkFileSource(fileStr))
379 String suffixLess = extractSuffix(fileStr);
380 if (suffixLess != null)
382 if (checkFileSource(suffixLess))
384 throw new IOException(MessageManager.formatMessage(
385 "exception.problem_opening_file_also_tried",
387 { inFile.getName(), suffixLess, errormessage }));
392 throw new IOException(MessageManager.formatMessage(
393 "exception.problem_opening_file", new String[]
394 { inFile.getName(), errormessage }));
398 else if (sourceType == DataSourceType.RELATIVE_URL)
400 // BH 2018 hack for no support for access-origin
401 bytes = Platform.getFileAsBytes(fileStr);
402 dataIn = new BufferedReader(
403 new InputStreamReader(new ByteArrayInputStream(bytes)));
407 else if (sourceType == DataSourceType.URL)
413 checkURLSource(fileStr);
414 if (suffixSeparator == '#')
416 extractSuffix(fileStr); // URL lref is stored for later reference.
418 } catch (IOException e)
420 String suffixLess = extractSuffix(fileStr);
421 if (suffixLess == null)
429 checkURLSource(suffixLess);
430 } catch (IOException e2)
432 errormessage = "BAD URL WITH OR WITHOUT SUFFIX";
433 throw (e); // just pass back original - everything was wrong.
437 } catch (Exception e)
439 errormessage = "CANNOT ACCESS DATA AT URL '" + fileStr + "' ("
440 + e.getMessage() + ")";
444 else if (sourceType == DataSourceType.PASTE)
446 errormessage = "PASTE INACCESSIBLE!";
447 dataIn = new BufferedReader(new StringReader(fileStr));
450 else if (sourceType == DataSourceType.CLASSLOADER)
452 errormessage = "RESOURCE CANNOT BE LOCATED";
453 InputStream is = getClass().getResourceAsStream("/" + fileStr);
456 String suffixLess = extractSuffix(fileStr);
457 if (suffixLess != null)
459 is = getClass().getResourceAsStream("/" + suffixLess);
464 dataIn = new BufferedReader(new InputStreamReader(is));
474 errormessage = "PROBABLE IMPLEMENTATION ERROR : Datasource Type given as '"
475 + (sourceType != null ? sourceType : "null") + "'";
478 if (dataIn == null || error)
480 // pass up the reason why we have no source to read from
481 throw new IOException(MessageManager.formatMessage(
482 "exception.failed_to_read_data_from_source", new String[]
486 dataIn.mark(READAHEAD_LIMIT);
490 * mark the current position in the source as start for the purposes of it
491 * being analysed by IdentifyFile().identify
493 * @throws IOException
495 public void mark() throws IOException
499 dataIn.mark(READAHEAD_LIMIT);
503 throw new IOException(
504 MessageManager.getString("exception.no_init_source_stream"));
508 public String nextLine() throws IOException
512 return dataIn.readLine();
514 throw new IOException(MessageManager
515 .formatMessage("exception.invalid_source_stream", new String[]
521 * @return true if this FileParse is configured for Export only
523 public boolean isExporting()
525 return !error && dataIn == null;
530 * @return true if the data source is valid
532 public boolean isValid()
538 * closes the datasource and tidies up. source will be left in an error state
540 public void close() throws IOException
542 errormessage = "EXCEPTION ON CLOSE";
546 errormessage = "SOURCE IS CLOSED";
550 * Rewinds the datasource to the marked point if possible
555 public void reset(int bytesRead) throws IOException
557 if (bytesRead >= READAHEAD_LIMIT)
559 System.err.println(String.format(
560 "File reset error: read %d bytes but reset limit is %d",
561 bytesRead, READAHEAD_LIMIT));
563 if (dataIn != null && !error)
569 throw new IOException(MessageManager.getString(
570 "error.implementation_error_reset_called_for_invalid_source"));
576 * @return true if there is a warning for the user
578 public boolean hasWarningMessage()
580 return (warningMessage != null && warningMessage.length() > 0);
585 * @return empty string or warning message about file that was just parsed.
587 public String getWarningMessage()
589 return warningMessage;
592 public String getInFile()
596 return inFile.getAbsolutePath() + " (" + index + ")";
600 return "From Paste + (" + index + ")";
605 * @return the dataName
607 public String getDataName()
613 * set the (human readable) name or URI for this datasource
617 protected void setDataName(String dataname)
623 * get the underlying bufferedReader for this data source.
625 * @return null if no reader available
626 * @throws IOException
628 public Reader getReader()
630 if (dataIn != null) // Probably don't need to test for readiness &&
638 public AlignViewportI getViewport()
643 public void setViewport(AlignViewportI viewport)
645 this.viewport = viewport;
649 * @return the currently configured exportSettings for writing data.
651 public AlignExportSettingsI getExportSettings()
653 return exportSettings;
657 * Set configuration for export of data.
659 * @param exportSettings
660 * the exportSettings to set
662 public void setExportSettings(AlignExportSettingsI exportSettings)
664 this.exportSettings = exportSettings;
668 * method overridden by complex file exporter/importers which support
669 * exporting visualisation and layout settings for a view
673 public void configureForView(AlignmentViewPanel avpanel)
677 setViewport(avpanel.getAlignViewport());
679 // could also set export/import settings
683 * Returns the preferred feature colour configuration if there is one, else
688 public FeatureSettingsModelI getFeatureColourScheme()
693 public DataSourceType getDataSourceType()
695 return dataSourceType;
699 * Returns a buffered reader for the input object. Returns null, or throws
700 * IOException, on failure.
703 * a File, or a String which is a name of a file
706 * @throws IOException
708 public BufferedReader getBufferedReader(Object file,
709 DataSourceType sourceType) throws IOException
711 BufferedReader in = null;
717 if (file instanceof String)
719 return new BufferedReader(new FileReader((String) file));
721 bytes = Platform.getFileBytes((File) file);
724 return new BufferedReader(
725 new InputStreamReader(new ByteArrayInputStream(bytes)));
727 return new BufferedReader(new FileReader((File) file));
729 URL url = new URL(file.toString());
730 in = new BufferedReader(new InputStreamReader(url.openStream()));
732 case RELATIVE_URL: // JalviewJS only
733 bytes = Platform.getFileAsBytes(file.toString());
736 in = new BufferedReader(
737 new InputStreamReader(new ByteArrayInputStream(bytes)));
741 in = new BufferedReader(new StringReader(file.toString()));
744 InputStream is = getClass().getResourceAsStream("/" + file);
747 in = new BufferedReader(new InputStreamReader(is));