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