JAL-1807 explicit imports (jalview.datamodel)
[jalview.git] / src / jalview / datamodel / xdb / embl / EmblFile.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.datamodel.xdb.embl;
22
23 import jalview.bin.Cache;
24
25 import java.io.File;
26 import java.io.FileReader;
27 import java.io.PrintWriter;
28 import java.io.Reader;
29 import java.util.Vector;
30
31 import org.exolab.castor.mapping.Mapping;
32 import org.exolab.castor.xml.Unmarshaller;
33
34 /**
35  * Data model for entries returned from an EMBL query, as marshalled by a Castor
36  * binding file
37  * 
38  * For example: http://www.ebi.ac.uk/Tools/dbfetch/dbfetch/embl/x53828/emblxml
39  * 
40  * @see embl_mapping.xml
41  */
42 public class EmblFile
43 {
44   Vector<EmblEntry> entries;
45
46   Vector<EmblError> errors;
47
48   /**
49    * @return the entries
50    */
51   public Vector<EmblEntry> getEntries()
52   {
53     return entries;
54   }
55
56   /**
57    * @param entries
58    *          the entries to set
59    */
60   public void setEntries(Vector<EmblEntry> entries)
61   {
62     this.entries = entries;
63   }
64
65   /**
66    * @return the errors
67    */
68   public Vector<EmblError> getErrors()
69   {
70     return errors;
71   }
72
73   /**
74    * @param errors
75    *          the errors to set
76    */
77   public void setErrors(Vector<EmblError> errors)
78   {
79     this.errors = errors;
80   }
81
82   /**
83    * Parse an EmblXML file into an EmblFile object
84    * 
85    * @param file
86    * @return parsed EmblXML or null if exceptions were raised
87    */
88   public static EmblFile getEmblFile(File file)
89   {
90     if (file == null)
91     {
92       return null;
93     }
94     try
95     {
96       return EmblFile.getEmblFile(new FileReader(file));
97     } catch (Exception e)
98     {
99       System.err.println("Exception whilst reading EMBLfile from " + file);
100       e.printStackTrace(System.err);
101     }
102     return null;
103   }
104
105   public static EmblFile getEmblFile(Reader file)
106   {
107     EmblFile record = new EmblFile();
108     try
109     {
110       // 1. Load the mapping information from the file
111       Mapping map = new Mapping(record.getClass().getClassLoader());
112
113       java.net.URL url = record.getClass().getResource("/embl_mapping.xml");
114       map.loadMapping(url);
115
116       // 2. Unmarshal the data
117       Unmarshaller unmar = new Unmarshaller(record);
118       try
119       {
120         // uncomment to DEBUG EMBLFile reading
121         if (Cache.getDefault(
122                 Cache.CASTORLOGLEVEL, "debug")
123                 .equalsIgnoreCase("DEBUG"))
124         {
125           unmar.setDebug(Cache.log.isDebugEnabled());
126         }
127       } catch (Exception e)
128       {
129       }
130       unmar.setIgnoreExtraElements(true);
131       unmar.setIgnoreExtraAttributes(true);
132       unmar.setMapping(map);
133       unmar.setLogWriter(new PrintWriter(System.out));
134       record = (EmblFile) unmar.unmarshal(file);
135     } catch (Exception e)
136     {
137       e.printStackTrace(System.err);
138       record = null;
139     }
140
141     return record;
142   }
143 }