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