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