update author list in license for (JAL-826)
[jalview.git] / src / jalview / datamodel / xdb / embl / EmblFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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  */
18 package jalview.datamodel.xdb.embl;
19
20 import java.io.File;
21 import java.io.FileReader;
22 import java.io.Reader;
23 import java.util.Vector;
24
25 import org.exolab.castor.mapping.Mapping;
26 import org.exolab.castor.xml.Unmarshaller;
27
28 public class EmblFile
29 {
30   Vector entries;
31
32   Vector errors;
33
34   /**
35    * @return the entries
36    */
37   public Vector getEntries()
38   {
39     return entries;
40   }
41
42   /**
43    * @param entries
44    *          the entries to set
45    */
46   public void setEntries(Vector entries)
47   {
48     this.entries = entries;
49   }
50
51   /**
52    * @return the errors
53    */
54   public Vector getErrors()
55   {
56     return errors;
57   }
58
59   /**
60    * @param errors
61    *          the errors to set
62    */
63   public void setErrors(Vector errors)
64   {
65     this.errors = errors;
66   }
67
68   /**
69    * Parse an EmblXML file into an EmblFile object
70    * 
71    * @param file
72    * @return parsed EmblXML or null if exceptions were raised
73    */
74   public static EmblFile getEmblFile(File file)
75   {
76     if (file == null)
77       return null;
78     try
79     {
80       return EmblFile.getEmblFile(new FileReader(file));
81     } catch (Exception e)
82     {
83       System.err.println("Exception whilst reading EMBLfile from " + file);
84       e.printStackTrace(System.err);
85     }
86     return null;
87   }
88
89   public static EmblFile getEmblFile(Reader file)
90   {
91     EmblFile record = new EmblFile();
92     try
93     {
94       // 1. Load the mapping information from the file
95       Mapping map = new Mapping(record.getClass().getClassLoader());
96       java.net.URL url = record.getClass().getResource("/embl_mapping.xml");
97       map.loadMapping(url);
98
99       // 2. Unmarshal the data
100       Unmarshaller unmar = new Unmarshaller(record);
101       try
102       {
103         // uncomment to DEBUG EMBLFile reading
104         if (((String) jalview.bin.Cache.getDefault(
105                 jalview.bin.Cache.CASTORLOGLEVEL, "debug"))
106                 .equalsIgnoreCase("DEBUG"))
107           unmar.setDebug(jalview.bin.Cache.log.isDebugEnabled());
108       } catch (Exception e)
109       {
110       }
111       ;
112       unmar.setIgnoreExtraElements(true);
113       unmar.setMapping(map);
114
115       record = (EmblFile) unmar.unmarshal(file);
116     } catch (Exception e)
117     {
118       e.printStackTrace(System.err);
119       record = null;
120     }
121
122     return record;
123   }
124
125   public static void main(String args[])
126   {
127     File mf = null;
128     if (args.length == 1)
129     {
130       mf = new File(args[0]);
131     }
132     if (!mf.exists())
133     {
134       mf = new File(
135               "C:\\Documents and Settings\\JimP\\workspace-3.2\\Jalview Release\\schemas\\embleRecordV1.1.xml");
136     }
137     EmblFile myfile = EmblFile.getEmblFile(mf);
138     if (myfile != null && myfile.entries != null
139             && myfile.entries.size() > 0)
140       System.out.println(myfile.entries.size() + " Records read. (" + mf
141               + ")");
142   }
143 }