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.BufferedInputStream;
30 import java.io.BufferedReader;
32 import java.io.FileInputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.InputStreamReader;
36 import java.io.Reader;
37 import java.io.StringReader;
38 import java.net.HttpURLConnection;
39 import java.net.MalformedURLException;
41 import java.net.URLConnection;
42 import java.util.zip.GZIPInputStream;
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;
64 * a viewport associated with the current file operation. May be null. May
65 * move to different object.
67 private AlignViewportI viewport;
70 * specific settings for exporting data from the current context
72 private AlignExportSettingI exportSettings;
75 * sequence counter for FileParse object created from same data source
80 * separator for extracting specific 'frame' of a datasource for formats that
81 * support multiple records (e.g. BLC, Stockholm, etc)
83 protected char suffixSeparator = '#';
86 * character used to write newlines
88 protected String newline = System.getProperty("line.separator");
90 public void setNewlineString(String nl)
95 public String getNewlineString()
101 * '#' separated string tagged on to end of filename or url that was clipped
102 * off to resolve to valid filename
104 protected String suffix = null;
106 protected DataSourceType dataSourceType = null;
108 protected BufferedReader dataIn = null;
110 protected String errormessage = "UNINITIALISED SOURCE";
112 protected boolean error = true;
114 protected String warningMessage = null;
117 * size of readahead buffer used for when initial stream position is marked.
119 final int READAHEAD_LIMIT = 2048;
126 * Create a new FileParse instance reading from the same datasource starting
127 * at the current position. WARNING! Subsequent reads from either object will
128 * affect the read position of the other, but not the error state.
132 public FileParse(FileParse from) throws IOException
136 throw new Error(MessageManager
137 .getString("error.implementation_error_null_fileparse"));
143 index = ++from.index;
144 inFile = from.inFile;
145 suffixSeparator = from.suffixSeparator;
146 suffix = from.suffix;
147 errormessage = from.errormessage; // inherit potential error messages
148 error = false; // reset any error condition.
149 dataSourceType = from.dataSourceType;
150 dataIn = from.dataIn;
155 dataName = from.dataName;
159 * Attempt to open a file as a datasource. Sets error and errormessage if
160 * fileStr was invalid.
163 * @return this.error (true if the source was invalid)
165 private boolean checkFileSource(String fileStr) throws IOException
168 this.inFile = new File(fileStr);
169 // check to see if it's a Jar file in disguise.
170 if (!inFile.exists())
172 errormessage = "FILE NOT FOUND";
175 if (!inFile.canRead())
177 errormessage = "FILE CANNOT BE OPENED FOR READING";
180 if (inFile.isDirectory())
182 // this is really a 'complex' filetype - but we don't handle directory
184 errormessage = "FILE IS A DIRECTORY";
191 dataIn = checkForGzipStream(new FileInputStream(fileStr));
193 } catch (Exception x)
195 warningMessage = "Failed to resolve " + fileStr
196 + " as a data source. (" + x.getMessage() + ")";
197 // x.printStackTrace();
206 * Recognise the 2-byte magic header for gzip streams
208 * https://recalll.co/ask/v/topic/java-How-to-check-if-InputStream-is-Gzipped/555aadd62bd27354438b90f6
210 * @param bytes - at least two bytes
213 private static boolean isGzipStream(byte[] bytes) {
214 int head = ((int) bytes[0] & 0xff) | ((bytes[1] << 8) & 0xff00);
215 return (GZIPInputStream.GZIP_MAGIC == head);
219 * Returns a Reader for the given input after wrapping it in a buffered input
220 * stream, and then checking if it needs to be wrapped by a GZipInputStream
225 private BufferedReader checkForGzipStream(InputStream input) throws Exception {
227 // NB: stackoverflow https://stackoverflow.com/questions/4818468/how-to-check-if-inputstream-is-gzipped
228 // could use a PushBackInputStream rather than a BufferedInputStream
230 BufferedInputStream bufinput;
231 if (!input.markSupported()) {
232 bufinput= new BufferedInputStream(input,16);
236 byte[] bytes=input.readNBytes(2);
238 if (bytes.length==2 && isGzipStream(bytes)) {
239 return getGzipReader(input);
241 // return a buffered reader for the stream.
242 InputStreamReader isReader= new InputStreamReader(input);
243 BufferedReader toReadFrom=new BufferedReader(isReader);
247 * Returns a {@code BufferedReader} which wraps the input stream with a
248 * GZIPInputStream. Throws a {@code ZipException} if a GZIP format error
249 * occurs or the compression method used is unsupported.
255 private BufferedReader getGzipReader(InputStream inputStream)
258 BufferedReader inData = new BufferedReader(
259 new InputStreamReader(new GZIPInputStream(inputStream)));
267 * Tries to read from the given URL. If successful, saves a reader to the
268 * response in field {@code dataIn}, otherwise (on exception, or HTTP response
269 * status not 200), throws an exception.
271 * If the response status includes
274 * Content-Type : application/x-gzip
277 * then tries to read as gzipped content.
280 * @throws IOException
281 * @throws MalformedURLException
283 private void checkURLSource(String urlStr)
284 throws IOException, MalformedURLException
286 errormessage = "URL NOT FOUND";
287 URL url = new URL(urlStr);
288 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
289 int rc = conn.getResponseCode();
290 if (rc != HttpURLConnection.HTTP_OK)
292 throw new IOException(
293 "Response status from " + urlStr + " was " + rc);
295 String encoding = conn.getContentEncoding();
296 String contentType = conn.getContentType();
297 boolean isgzipped = "application/x-gzip".equalsIgnoreCase(contentType)
298 || "gzip".equals(encoding);
300 InputStream inputStream = conn.getInputStream();
305 dataIn = getGzipReader(inputStream);
307 } catch (Exception e1)
309 throw new IOException(MessageManager
310 .getString("exception.failed_to_resolve_gzip_stream"), e);
315 dataIn = new BufferedReader(new InputStreamReader(inputStream));
321 * sets the suffix string (if any) and returns remainder (if suffix was
325 * @return truncated fileStr or null
327 private String extractSuffix(String fileStr)
329 // first check that there wasn't a suffix string tagged on.
330 int sfpos = fileStr.lastIndexOf(suffixSeparator);
331 if (sfpos > -1 && sfpos < fileStr.length() - 1)
333 suffix = fileStr.substring(sfpos + 1);
334 // System.err.println("DEBUG: Found Suffix:"+suffix);
335 return fileStr.substring(0, sfpos);
341 * not for general use, creates a fileParse object for an existing reader with
342 * configurable values for the origin and the type of the source
344 public FileParse(BufferedReader source, String originString,
345 DataSourceType sourceType)
347 dataSourceType = sourceType;
350 dataName = originString;
354 if (dataIn.markSupported())
356 dataIn.mark(READAHEAD_LIMIT);
358 } catch (IOException q)
365 * Create a datasource for input to Jalview. See AppletFormatAdapter for the
366 * types of sources that are handled.
369 * - datasource locator/content
371 * - protocol of source
372 * @throws MalformedURLException
373 * @throws IOException
375 public FileParse(String fileStr, DataSourceType sourceType)
376 throws MalformedURLException, IOException
378 this.dataSourceType = sourceType;
381 if (sourceType == DataSourceType.FILE)
383 if (checkFileSource(fileStr))
385 String suffixLess = extractSuffix(fileStr);
386 if (suffixLess != null)
388 if (checkFileSource(suffixLess))
390 throw new IOException(MessageManager.formatMessage(
391 "exception.problem_opening_file_also_tried",
393 { inFile.getName(), suffixLess, errormessage }));
398 throw new IOException(MessageManager.formatMessage(
399 "exception.problem_opening_file", new String[]
400 { inFile.getName(), errormessage }));
404 else if (sourceType == DataSourceType.URL)
410 checkURLSource(fileStr);
411 if (suffixSeparator == '#')
413 extractSuffix(fileStr); // URL lref is stored for later reference.
415 } catch (IOException e)
417 String suffixLess = extractSuffix(fileStr);
418 if (suffixLess == null)
426 checkURLSource(suffixLess);
427 } catch (IOException e2)
429 errormessage = "BAD URL WITH OR WITHOUT SUFFIX";
430 throw (e); // just pass back original - everything was wrong.
434 } catch (Exception e)
436 errormessage = "CANNOT ACCESS DATA AT URL '" + fileStr + "' ("
437 + e.getMessage() + ")";
441 else if (sourceType == DataSourceType.PASTE)
443 errormessage = "PASTE INACCESSIBLE!";
444 dataIn = new BufferedReader(new StringReader(fileStr));
447 else if (sourceType == DataSourceType.CLASSLOADER)
449 errormessage = "RESOURCE CANNOT BE LOCATED";
450 java.io.InputStream is = getClass()
451 .getResourceAsStream("/" + fileStr);
454 String suffixLess = extractSuffix(fileStr);
455 if (suffixLess != null)
457 is = getClass().getResourceAsStream("/" + suffixLess);
462 dataIn = new BufferedReader(new java.io.InputStreamReader(is));
472 errormessage = "PROBABLE IMPLEMENTATION ERROR : Datasource Type given as '"
473 + (sourceType != null ? sourceType : "null") + "'";
476 if (dataIn == null || error)
478 // pass up the reason why we have no source to read from
479 throw new IOException(MessageManager.formatMessage(
480 "exception.failed_to_read_data_from_source", new String[]
484 dataIn.mark(READAHEAD_LIMIT);
488 * mark the current position in the source as start for the purposes of it
489 * being analysed by IdentifyFile().identify
491 * @throws IOException
493 public void mark() throws IOException
497 dataIn.mark(READAHEAD_LIMIT);
501 throw new IOException(
502 MessageManager.getString("exception.no_init_source_stream"));
506 public String nextLine() throws IOException
510 return dataIn.readLine();
512 throw new IOException(MessageManager
513 .formatMessage("exception.invalid_source_stream", new String[]
519 * @return true if this FileParse is configured for Export only
521 public boolean isExporting()
523 return !error && dataIn == null;
528 * @return true if the data source is valid
530 public boolean isValid()
536 * closes the datasource and tidies up. source will be left in an error state
538 public void close() throws IOException
540 errormessage = "EXCEPTION ON CLOSE";
544 errormessage = "SOURCE IS CLOSED";
548 * Rewinds the datasource to the marked point if possible
553 public void reset(int bytesRead) throws IOException
555 if (bytesRead >= READAHEAD_LIMIT)
557 System.err.println(String.format(
558 "File reset error: read %d bytes but reset limit is %d",
559 bytesRead, READAHEAD_LIMIT));
561 if (dataIn != null && !error)
567 throw new IOException(MessageManager.getString(
568 "error.implementation_error_reset_called_for_invalid_source"));
574 * @return true if there is a warning for the user
576 public boolean hasWarningMessage()
578 return (warningMessage != null && warningMessage.length() > 0);
583 * @return empty string or warning message about file that was just parsed.
585 public String getWarningMessage()
587 return warningMessage;
590 public String getInFile()
594 return inFile.getAbsolutePath() + " (" + index + ")";
598 return "From Paste + (" + index + ")";
603 * @return the dataName
605 public String getDataName()
611 * set the (human readable) name or URI for this datasource
615 protected void setDataName(String dataname)
621 * get the underlying bufferedReader for this data source.
623 * @return null if no reader available
624 * @throws IOException
626 public Reader getReader()
628 if (dataIn != null) // Probably don't need to test for readiness &&
636 public AlignViewportI getViewport()
641 public void setViewport(AlignViewportI viewport)
643 this.viewport = viewport;
647 * @return the currently configured exportSettings for writing data.
649 public AlignExportSettingI getExportSettings()
651 return exportSettings;
655 * Set configuration for export of data.
657 * @param exportSettings
658 * the exportSettings to set
660 public void setExportSettings(AlignExportSettingI exportSettings)
662 this.exportSettings = exportSettings;
666 * method overridden by complex file exporter/importers which support
667 * exporting visualisation and layout settings for a view
671 public void configureForView(AlignmentViewPanel avpanel)
675 setViewport(avpanel.getAlignViewport());
677 // could also set export/import settings
681 * Returns the preferred feature colour configuration if there is one, else
686 public FeatureSettingsModelI getFeatureColourScheme()
691 public DataSourceType getDataSourceType()
693 return dataSourceType;