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