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