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