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;
31 import java.io.FileInputStream;
32 import java.io.FileReader;
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.util.zip.GZIPInputStream;
45 * implements a random access wrapper around a particular datasource, for
46 * passing to identifyFile and AlignFile objects.
48 public class FileParse
50 protected static final String SPACE = " ";
52 protected static final String TAB = "\t";
55 * text specifying source of data. usually filename or url.
57 private String dataName = "unknown source";
59 public File inFile = null;
63 * a viewport associated with the current file operation. May be null. May
64 * move to different object.
66 private AlignViewportI viewport;
69 * specific settings for exporting data from the current context
71 private AlignExportSettingI exportSettings;
74 * sequence counter for FileParse object created from same data source
79 * separator for extracting specific 'frame' of a datasource for formats that
80 * support multiple records (e.g. BLC, Stockholm, etc)
82 protected char suffixSeparator = '#';
85 * character used to write newlines
87 protected String newline = System.getProperty("line.separator");
89 public void setNewlineString(String nl)
94 public String getNewlineString()
100 * '#' separated string tagged on to end of filename or url that was clipped
101 * off to resolve to valid filename
103 protected String suffix = null;
105 protected DataSourceType dataSourceType = null;
107 protected BufferedReader dataIn = null;
109 protected String errormessage = "UNINITIALISED SOURCE";
111 protected boolean error = true;
113 protected String warningMessage = null;
116 * size of readahead buffer used for when initial stream position is marked.
118 final int READAHEAD_LIMIT = 2048;
125 * Create a new FileParse instance reading from the same datasource starting
126 * at the current position. WARNING! Subsequent reads from either object will
127 * affect the read position of the other, but not the error state.
131 public FileParse(FileParse from) throws IOException
135 throw new Error(MessageManager
136 .getString("error.implementation_error_null_fileparse"));
142 index = ++from.index;
143 inFile = from.inFile;
144 suffixSeparator = from.suffixSeparator;
145 suffix = from.suffix;
146 errormessage = from.errormessage; // inherit potential error messages
147 error = false; // reset any error condition.
148 dataSourceType = from.dataSourceType;
149 dataIn = from.dataIn;
154 dataName = from.dataName;
158 * Attempt to open a file as a datasource. Sets error and errormessage if
159 * fileStr was invalid.
162 * @return this.error (true if the source was invalid)
164 private boolean checkFileSource(String fileStr) throws IOException
167 this.inFile = new File(fileStr);
168 // check to see if it's a Jar file in disguise.
169 if (!inFile.exists())
171 errormessage = "FILE NOT FOUND";
174 if (!inFile.canRead())
176 errormessage = "FILE CANNOT BE OPENED FOR READING";
179 if (inFile.isDirectory())
181 // this is really a 'complex' filetype - but we don't handle directory
183 errormessage = "FILE IS A DIRECTORY";
188 if (fileStr.toLowerCase().endsWith(".gz"))
192 dataIn = getGzipReader(new FileInputStream(fileStr));
195 } catch (Exception x)
197 warningMessage = "Failed to resolve as a GZ stream ("
198 + x.getMessage() + ")";
199 // x.printStackTrace();
204 dataIn = new BufferedReader(new FileReader(fileStr));
211 * Returns a {@code BufferedReader} which wraps the input stream with a
212 * GZIPInputStream. Throws a {@code ZipException} if a GZIP format error
213 * occurs or the compression method used is unsupported.
219 private BufferedReader getGzipReader(InputStream inputStream)
222 BufferedReader inData = new BufferedReader(
223 new InputStreamReader(new GZIPInputStream(inputStream)));
231 * Tries to read from the given URL. If successful, saves a reader to the
232 * response in field {@code dataIn}, otherwise (on exception, or HTTP response
233 * status not 200), throws an exception.
235 * If the response status includes
238 * Content-Type : application/x-gzip
241 * then tries to read as gzipped content.
244 * @throws IOException
245 * @throws MalformedURLException
247 private void checkURLSource(String urlStr)
248 throws IOException, MalformedURLException
250 errormessage = "URL NOT FOUND";
251 URL url = new URL(urlStr);
252 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
253 int rc = conn.getResponseCode();
254 if (rc != HttpURLConnection.HTTP_OK)
256 throw new IOException(
257 "Response status from " + urlStr + " was " + rc);
259 String encoding = conn.getContentEncoding();
260 String contentType = conn.getContentType();
261 boolean isgzipped = "application/x-gzip".equalsIgnoreCase(contentType)
262 || "gzip".equals(encoding);
264 InputStream inputStream = conn.getInputStream();
269 dataIn = getGzipReader(inputStream);
271 } catch (Exception e1)
273 throw new IOException(MessageManager
274 .getString("exception.failed_to_resolve_gzip_stream"), e);
279 dataIn = new BufferedReader(new InputStreamReader(inputStream));
285 * sets the suffix string (if any) and returns remainder (if suffix was
289 * @return truncated fileStr or null
291 private String extractSuffix(String fileStr)
293 // first check that there wasn't a suffix string tagged on.
294 int sfpos = fileStr.lastIndexOf(suffixSeparator);
295 if (sfpos > -1 && sfpos < fileStr.length() - 1)
297 suffix = fileStr.substring(sfpos + 1);
298 // System.err.println("DEBUG: Found Suffix:"+suffix);
299 return fileStr.substring(0, sfpos);
305 * not for general use, creates a fileParse object for an existing reader with
306 * configurable values for the origin and the type of the source
308 public FileParse(BufferedReader source, String originString,
309 DataSourceType sourceType)
311 dataSourceType = sourceType;
314 dataName = originString;
318 if (dataIn.markSupported())
320 dataIn.mark(READAHEAD_LIMIT);
322 } catch (IOException q)
329 * Create a datasource for input to Jalview. See AppletFormatAdapter for the
330 * types of sources that are handled.
333 * - datasource locator/content
335 * - protocol of source
336 * @throws MalformedURLException
337 * @throws IOException
339 public FileParse(String fileStr, DataSourceType sourceType)
340 throws MalformedURLException, IOException
342 this.dataSourceType = sourceType;
345 if (sourceType == DataSourceType.FILE)
347 if (checkFileSource(fileStr))
349 String suffixLess = extractSuffix(fileStr);
350 if (suffixLess != null)
352 if (checkFileSource(suffixLess))
354 throw new IOException(MessageManager.formatMessage(
355 "exception.problem_opening_file_also_tried",
357 { inFile.getName(), suffixLess, errormessage }));
362 throw new IOException(MessageManager.formatMessage(
363 "exception.problem_opening_file", new String[]
364 { inFile.getName(), errormessage }));
368 else if (sourceType == DataSourceType.URL)
374 checkURLSource(fileStr);
375 if (suffixSeparator == '#')
377 extractSuffix(fileStr); // URL lref is stored for later reference.
379 } catch (IOException e)
381 String suffixLess = extractSuffix(fileStr);
382 if (suffixLess == null)
390 checkURLSource(suffixLess);
391 } catch (IOException e2)
393 errormessage = "BAD URL WITH OR WITHOUT SUFFIX";
394 throw (e); // just pass back original - everything was wrong.
398 } catch (Exception e)
400 errormessage = "CANNOT ACCESS DATA AT URL '" + fileStr + "' ("
401 + e.getMessage() + ")";
405 else if (sourceType == DataSourceType.PASTE)
407 errormessage = "PASTE INACCESSIBLE!";
408 dataIn = new BufferedReader(new StringReader(fileStr));
411 else if (sourceType == DataSourceType.CLASSLOADER)
413 errormessage = "RESOURCE CANNOT BE LOCATED";
414 java.io.InputStream is = getClass()
415 .getResourceAsStream("/" + fileStr);
418 String suffixLess = extractSuffix(fileStr);
419 if (suffixLess != null)
421 is = getClass().getResourceAsStream("/" + suffixLess);
426 dataIn = new BufferedReader(new java.io.InputStreamReader(is));
436 errormessage = "PROBABLE IMPLEMENTATION ERROR : Datasource Type given as '"
437 + (sourceType != null ? sourceType : "null") + "'";
440 if (dataIn == null || error)
442 // pass up the reason why we have no source to read from
443 throw new IOException(MessageManager.formatMessage(
444 "exception.failed_to_read_data_from_source", new String[]
448 dataIn.mark(READAHEAD_LIMIT);
452 * mark the current position in the source as start for the purposes of it
453 * being analysed by IdentifyFile().identify
455 * @throws IOException
457 public void mark() throws IOException
461 dataIn.mark(READAHEAD_LIMIT);
465 throw new IOException(
466 MessageManager.getString("exception.no_init_source_stream"));
470 public String nextLine() throws IOException
474 return dataIn.readLine();
476 throw new IOException(MessageManager
477 .formatMessage("exception.invalid_source_stream", new String[]
483 * @return true if this FileParse is configured for Export only
485 public boolean isExporting()
487 return !error && dataIn == null;
492 * @return true if the data source is valid
494 public boolean isValid()
500 * closes the datasource and tidies up. source will be left in an error state
502 public void close() throws IOException
504 errormessage = "EXCEPTION ON CLOSE";
508 errormessage = "SOURCE IS CLOSED";
512 * Rewinds the datasource to the marked point if possible
517 public void reset(int bytesRead) throws IOException
519 if (bytesRead >= READAHEAD_LIMIT)
521 System.err.println(String.format(
522 "File reset error: read %d bytes but reset limit is %d",
523 bytesRead, READAHEAD_LIMIT));
525 if (dataIn != null && !error)
531 throw new IOException(MessageManager.getString(
532 "error.implementation_error_reset_called_for_invalid_source"));
538 * @return true if there is a warning for the user
540 public boolean hasWarningMessage()
542 return (warningMessage != null && warningMessage.length() > 0);
547 * @return empty string or warning message about file that was just parsed.
549 public String getWarningMessage()
551 return warningMessage;
554 public String getInFile()
558 return inFile.getAbsolutePath() + " (" + index + ")";
562 return "From Paste + (" + index + ")";
567 * @return the dataName
569 public String getDataName()
575 * set the (human readable) name or URI for this datasource
579 protected void setDataName(String dataname)
585 * get the underlying bufferedReader for this data source.
587 * @return null if no reader available
588 * @throws IOException
590 public Reader getReader()
592 if (dataIn != null) // Probably don't need to test for readiness &&
600 public AlignViewportI getViewport()
605 public void setViewport(AlignViewportI viewport)
607 this.viewport = viewport;
611 * @return the currently configured exportSettings for writing data.
613 public AlignExportSettingI getExportSettings()
615 return exportSettings;
619 * Set configuration for export of data.
621 * @param exportSettings
622 * the exportSettings to set
624 public void setExportSettings(AlignExportSettingI exportSettings)
626 this.exportSettings = exportSettings;
630 * method overridden by complex file exporter/importers which support
631 * exporting visualisation and layout settings for a view
635 public void configureForView(AlignmentViewPanel avpanel)
639 setViewport(avpanel.getAlignViewport());
641 // could also set export/import settings
645 * Returns the preferred feature colour configuration if there is one, else
650 public FeatureSettingsModelI getFeatureColourScheme()
655 public DataSourceType getDataSourceType()
657 return dataSourceType;