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