Merge branch 'releases/Release_2_11_3_Branch'
[jalview.git] / src / jalview / bin / JalviewLiteURLRetrieve.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.bin;
22
23 import jalview.datamodel.AlignmentI;
24 import jalview.io.AppletFormatAdapter;
25 import jalview.io.DataSourceType;
26 import jalview.io.FileFormat;
27 import jalview.io.FileFormatI;
28 import jalview.io.FileFormats;
29 import jalview.io.FileParse;
30 import jalview.io.IdentifyFile;
31
32 import java.applet.Applet;
33 import java.io.InputStream;
34
35 public class JalviewLiteURLRetrieve extends Applet
36 {
37
38   private static final long serialVersionUID = 1L;
39
40   /**
41    * This is the default constructor
42    */
43   public JalviewLiteURLRetrieve()
44   {
45     super();
46   }
47
48   /**
49    * This method initializes this
50    * 
51    * @return void
52    */
53   @Override
54   public void init()
55   {
56     this.setSize(300, 200);
57     String file = getParameter("file");
58     if (file == null)
59     {
60       System.out
61               .println("Specify a resource to read on the file parameter");
62       return;
63     }
64     DataSourceType protocol = null;
65     try
66     {
67       jalview.bin.Console
68               .outPrintln("Loading thread started with:\n>>file\n" + file
69                       + ">>endfile");
70       // This might throw a security exception in certain browsers
71       // Netscape Communicator for instance.
72       try
73       {
74         boolean rtn = false;
75         InputStream is = getClass().getResourceAsStream("/" + file);
76         if (is != null)
77         {
78           rtn = true;
79           is.close();
80         }
81         jalview.bin.Console.errPrintln("Resource '" + file + "' was "
82                 + (rtn ? "" : "not") + " located by classloader.");
83         if (rtn)
84         {
85           protocol = DataSourceType.CLASSLOADER;
86         }
87
88       } catch (Exception ex)
89       {
90         jalview.bin.Console.outPrintln(
91                 "Exception checking resources: " + file + " " + ex);
92       }
93       if (file.indexOf("://") > -1)
94       {
95         protocol = DataSourceType.URL;
96       }
97       else
98       {
99         // skipping codebase prepend check.
100         protocol = DataSourceType.FILE;
101       }
102
103       jalview.bin.Console.outPrintln("Trying to get contents of resource:");
104       FileParse fp = new FileParse(file, protocol);
105       if (fp.isValid())
106       {
107         String ln = null;
108         while ((ln = fp.nextLine()) != null)
109         {
110           System.out.print(ln);
111         }
112         fp.close();
113       }
114       else
115       {
116         jalview.bin.Console.outPrintln("Resource at " + file
117                 + " cannot be read with protocol==" + protocol);
118         return;
119       }
120       FileFormatI format = FileFormats.getInstance()
121               .forName(getParameter("format"));
122       if (format == null)
123       {
124         format = new IdentifyFile().identify(file, protocol);
125         jalview.bin.Console.outPrintln("Format is " + format);
126       }
127       else
128       {
129         jalview.bin.Console
130                 .outPrintln("User specified Format is " + format);
131       }
132       AlignmentI al = null;
133       try
134       {
135         al = new AppletFormatAdapter().readFile(file, protocol, format);
136       } catch (java.io.IOException ex)
137       {
138         jalview.bin.Console.errPrintln("Failed to open the file.");
139         ex.printStackTrace();
140       }
141       if (al != null)
142       {
143         jalview.bin.Console.outPrintln(new AppletFormatAdapter()
144                 .formatSequences(FileFormat.Fasta, al, false));
145       }
146     } catch (Exception e)
147     {
148       jalview.bin.Console.errPrintln("bailing out : Unexpected exception:");
149       e.printStackTrace();
150     }
151   }
152
153 }