bc8cbbef230ace38615113e6bbebcdcecf6bc483
[jalview.git] / src / jalview / datamodel / xdb / embl / EmblFile.java
1 package jalview.datamodel.xdb.embl;\r
2 \r
3 \r
4 import java.io.File;\r
5 import java.io.FileReader;\r
6 import java.io.Reader;\r
7 import java.util.Vector;\r
8 \r
9 import org.exolab.castor.mapping.Mapping;\r
10 import org.exolab.castor.xml.Unmarshaller;\r
11 \r
12 public class EmblFile {\r
13     Vector entries;\r
14     Vector errors;\r
15     /**\r
16      * @return the entries\r
17      */\r
18     public Vector getEntries() {\r
19         return entries;\r
20     }\r
21     /**\r
22      * @param entries the entries to set\r
23      */\r
24     public void setEntries(Vector entries) {\r
25         this.entries = entries;\r
26     }\r
27     /**\r
28      * @return the errors\r
29      */\r
30     public Vector getErrors() {\r
31         return errors;\r
32     }\r
33     /**\r
34      * @param errors the errors to set\r
35      */\r
36     public void setErrors(Vector errors) {\r
37         this.errors = errors;\r
38     }\r
39     /**\r
40      * Parse an EmblXML file into an EmblFile object\r
41      * @param file\r
42      * @return parsed EmblXML or null if exceptions were raised\r
43      */\r
44     public static EmblFile getEmblFile(File file)\r
45     {\r
46         if (file==null)\r
47             return null;\r
48         try {\r
49             return EmblFile.getEmblFile(new FileReader(file));\r
50         }\r
51         catch (Exception e) {\r
52             System.err.println("Exception whilst reading EMBLfile from "+file);\r
53             e.printStackTrace(System.err);\r
54         }\r
55         return null;\r
56     }\r
57     public static EmblFile getEmblFile(Reader file) {\r
58         EmblFile record = new EmblFile();\r
59         try\r
60         {\r
61           // 1. Load the mapping information from the file\r
62           Mapping map = new Mapping(record.getClass().getClassLoader());\r
63           java.net.URL url = record.getClass().getResource("/embl_mapping.xml");\r
64           map.loadMapping(url);\r
65 \r
66           // 2. Unmarshal the data\r
67           Unmarshaller unmar = new Unmarshaller(record);\r
68           try {\r
69               // uncomment to DEBUG EMBLFile reading unmar.setDebug(jalview.bin.Cache.log.isDebugEnabled());\r
70           } catch (Exception e) {};\r
71           unmar.setIgnoreExtraElements(true);\r
72           unmar.setMapping(map);\r
73 \r
74           record = (EmblFile) unmar.unmarshal(file);\r
75         }\r
76         catch (Exception e)\r
77         {\r
78           e.printStackTrace(System.err);\r
79           record=null;\r
80         }\r
81 \r
82 \r
83         return record;\r
84       }\r
85     public static void main(String args[]) {\r
86         EmblFile myfile = EmblFile.getEmblFile(new File("C:\\Documents and Settings\\JimP\\workspace-3.2\\Jalview Release\\schemas\\embleRecord.xml"));\r
87         if (myfile!=null && myfile.entries!=null && myfile.entries.size()>0)\r
88             System.out.println(myfile.entries.size()+" Records read.");\r
89         }\r
90 }\r