JAL-2245 Castor mapping and code changes for change to ENA XML format
[jalview.git] / src / jalview / datamodel / xdb / embl / EmblFile.java
index 69870b6..48acb88 100644 (file)
@@ -20,6 +20,7 @@
  */
 package jalview.datamodel.xdb.embl;
 
+import jalview.bin.Cache;
 import jalview.datamodel.DBRefEntry;
 import jalview.ws.dbsources.Uniprot;
 
@@ -27,6 +28,7 @@ import java.io.File;
 import java.io.FileReader;
 import java.io.PrintWriter;
 import java.io.Reader;
+import java.net.URL;
 import java.util.Vector;
 
 import org.exolab.castor.mapping.Mapping;
@@ -81,12 +83,12 @@ public class EmblFile
   }
 
   /**
-   * Parse an EmblXML file into an EmblFile object
+   * Parse an Embl XML file into an EmblEntry object
    * 
    * @param file
    * @return parsed EmblXML or null if exceptions were raised
    */
-  public static EmblFile getEmblFile(File file)
+  public static EmblEntry getEmblEntry(File file)
   {
     if (file == null)
     {
@@ -94,7 +96,7 @@ public class EmblFile
     }
     try
     {
-      return EmblFile.getEmblFile(new FileReader(file));
+      return EmblFile.getEntry(new FileReader(file));
     } catch (Exception e)
     {
       System.err.println("Exception whilst reading EMBLfile from " + file);
@@ -103,26 +105,32 @@ public class EmblFile
     return null;
   }
 
-  public static EmblFile getEmblFile(Reader file)
+  /**
+   * Reads the XML response from file and unmarshals into a Java object
+   * 
+   * @param fileReader
+   * @return
+   */
+  public static EmblEntry getEntry(Reader fileReader)
   {
-    EmblFile record = new EmblFile();
+    EmblEntry record = new EmblEntry();
     try
     {
       // 1. Load the mapping information from the file
       Mapping map = new Mapping(record.getClass().getClassLoader());
 
-      java.net.URL url = record.getClass().getResource("/embl_mapping.xml");
+      URL url = record.getClass().getResource("/embl_mapping.xml");
       map.loadMapping(url);
 
       // 2. Unmarshal the data
       Unmarshaller unmar = new Unmarshaller(record);
       try
       {
-        // uncomment to DEBUG EMBLFile reading
-        if (jalview.bin.Cache.getDefault(jalview.bin.Cache.CASTORLOGLEVEL,
+        if (Cache.getDefault(Cache.CASTORLOGLEVEL,
                 "debug").equalsIgnoreCase("DEBUG"))
         {
-          unmar.setDebug(jalview.bin.Cache.log.isDebugEnabled());
+          unmar.setDebug(Cache.log.isDebugEnabled());
+          // unmar.setDebug(true);// uncomment to debug unmarshalling
         }
       } catch (Exception e)
       {
@@ -131,7 +139,7 @@ public class EmblFile
       unmar.setIgnoreExtraAttributes(true);
       unmar.setMapping(map);
       unmar.setLogWriter(new PrintWriter(System.out));
-      record = (EmblFile) unmar.unmarshal(file);
+      record = (EmblEntry) unmar.unmarshal(fileReader);
 
       canonicaliseDbRefs(record);
     } catch (Exception e)
@@ -147,13 +155,17 @@ public class EmblFile
    * Change blank version to "0" in any DBRefEntry, to ensure consistent
    * comparison with other DBRefEntry in Jalview
    * 
-   * @param record
+   * @param entry
    * @see Uniprot#getDbVersion
    */
-  static void canonicaliseDbRefs(EmblFile record)
+  static void canonicaliseDbRefs(EmblEntry entry)
   {
-    for (EmblEntry entry : record.getEntries())
+    if (entry == null)
     {
+      return;
+    }
+//    for (EmblEntry entry : record.getEntries())
+//    {
       if (entry.getDbRefs() != null)
       {
         for (DBRefEntry dbref : entry.getDbRefs())
@@ -165,7 +177,7 @@ public class EmblFile
         }
       }
 
-      if (entry.getFeatures() != null)
+//      if (entry.getFeatures() != null)
       {
         for (EmblFeature feature : entry.getFeatures())
         {
@@ -181,6 +193,6 @@ public class EmblFile
           }
         }
       }
-    }
+    // }
   }
 }