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