bd7e8dee76fdc4416a652e10552e0bb585ab2dbb
[jalview.git] / src / jalview / bin / JalviewLiteURLRetrieve.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.bin;
19
20 import jalview.datamodel.Alignment;
21 import jalview.io.AppletFormatAdapter;
22 import jalview.io.FileParse;
23
24 import java.applet.Applet;
25 import java.io.InputStream;
26
27 public class JalviewLiteURLRetrieve extends Applet
28 {
29
30   private static final long serialVersionUID = 1L;
31
32   /**
33    * This is the default constructor
34    */
35   public JalviewLiteURLRetrieve()
36   {
37     super();
38   }
39
40   /**
41    * This method initializes this
42    * 
43    * @return void
44    */
45   public void init()
46   {
47     this.setSize(300, 200);
48     String file = getParameter("file");
49     if (file == null)
50     {
51       System.out
52               .println("Specify a resource to read on the file parameter");
53       return;
54     }
55     String protocol = null;
56     try
57     {
58       System.out.println("Loading thread started with:\n>>file\n" + file
59               + ">>endfile");
60       // This might throw a security exception in certain browsers
61       // Netscape Communicator for instance.
62       try
63       {
64         boolean rtn = false;
65         InputStream is = getClass().getResourceAsStream("/" + file);
66         if (is != null)
67         {
68           rtn = true;
69           is.close();
70         }
71         System.err.println("Resource '" + file + "' was "
72                 + (rtn ? "" : "not") + " located by classloader.");
73         if (rtn)
74         {
75           protocol = AppletFormatAdapter.CLASSLOADER;
76         }
77
78       } catch (Exception ex)
79       {
80         System.out.println("Exception checking resources: " + file + " "
81                 + ex);
82       }
83       if (file.indexOf("://") > -1)
84       {
85         protocol = AppletFormatAdapter.URL;
86       }
87       else
88       {
89         // skipping codebase prepend check.
90         protocol = AppletFormatAdapter.FILE;
91       }
92
93       System.out.println("Trying to get contents of resource:");
94       FileParse fp = new FileParse(file, protocol);
95       if (fp.isValid())
96       {
97         String ln = null;
98         while ((ln = fp.nextLine()) != null)
99         {
100           System.out.print(ln);
101         }
102         fp.close();
103       }
104       else
105       {
106         System.out.println("Resource at " + file
107                 + " cannot be read with protocol==" + protocol);
108         return;
109       }
110       String format = getParameter("format");
111       if (format == null || format.length() == 0)
112       {
113         format = new jalview.io.IdentifyFile().Identify(file, protocol);
114         System.out.println("Format is " + format);
115       }
116       else
117       {
118         System.out.println("User specified Format is " + format);
119       }
120       Alignment al = null;
121       try
122       {
123         al = new AppletFormatAdapter().readFile(file, protocol, format);
124       } catch (java.io.IOException ex)
125       {
126         System.err.println("Failed to open the file.");
127         ex.printStackTrace();
128       }
129       if (al != null)
130       {
131         System.out.println(new AppletFormatAdapter().formatSequences(
132                 "FASTA", al, false));
133       }
134     } catch (Exception e)
135     {
136       System.err.println("bailing out : Unexpected exception:");
137       e.printStackTrace();
138     }
139   }
140
141 }