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