Merge branch 'develop' into trialMerge
[jalview.git] / src / jalview / project / Jalview2XML.java
index 0d2ec68..5618529 100644 (file)
@@ -38,6 +38,7 @@ import jalview.datamodel.AlignedCodonFrame;
 import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
+import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.GraphLine;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.Point;
@@ -152,6 +153,7 @@ import java.awt.Color;
 import java.awt.Font;
 import java.awt.Rectangle;
 import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
 import java.io.File;
@@ -208,6 +210,15 @@ import javax.xml.stream.XMLStreamReader;
  */
 public class Jalview2XML
 {
+
+  // BH 2018 we add the .jvp binary extension to J2S so that
+  // it will declare that binary when we do the file save from the browser
+
+  static
+  {
+    Platform.addJ2SBinaryType(".jvp?");
+  }
+
   private static final String VIEWER_PREFIX = "viewer_";
 
   private static final String RNA_PREFIX = "rna_";
@@ -451,7 +462,7 @@ public class Jalview2XML
       public boolean isResolvable()
       {
         return super.isResolvable() && mp.getTo() != null;
-      };
+      }
 
       @Override
       boolean resolve()
@@ -683,7 +694,6 @@ public class Jalview2XML
       } catch (Exception foo)
       {
       }
-      ;
       jout.close();
     } catch (Exception ex)
     {
@@ -745,9 +755,10 @@ public class Jalview2XML
     try
     {
       // create backupfiles object and get new temp filename destination
-      BackupFiles backupfiles = new BackupFiles(jarFile);
-      FileOutputStream fos = new FileOutputStream(
-              backupfiles.getTempFilePath());
+      boolean doBackup = BackupFiles.getEnabled();
+      BackupFiles backupfiles = doBackup ? new BackupFiles(jarFile) : null;
+      FileOutputStream fos = new FileOutputStream(doBackup ? 
+              backupfiles.getTempFilePath() : jarFile);
 
       JarOutputStream jout = new JarOutputStream(fos);
       List<AlignFrame> frames = new ArrayList<>();
@@ -768,12 +779,14 @@ public class Jalview2XML
       } catch (Exception foo)
       {
       }
-      ;
       jout.close();
       boolean success = true;
 
-      backupfiles.setWriteSuccess(success);
-      success = backupfiles.rollBackupsAndRenameTempFile();
+      if (doBackup)
+      {
+        backupfiles.setWriteSuccess(success);
+        success = backupfiles.rollBackupsAndRenameTempFile();
+      }
 
       return success;
     } catch (Exception ex)
@@ -1659,6 +1672,7 @@ public class Jalview2XML
       // using save and then load
       try
       {
+        fileName = fileName.replace('\\', '/');
         System.out.println("Writing jar entry " + fileName);
         JarEntry entry = new JarEntry(fileName);
         jout.putNextEntry(entry);
@@ -2029,6 +2043,7 @@ public class Jalview2XML
   {
     if (jout != null)
     {
+      jarEntryName = jarEntryName.replace('\\','/');
       System.out.println("Writing jar entry " + jarEntryName);
       jout.putNextEntry(new JarEntry(jarEntryName));
       DataOutputStream dout = new DataOutputStream(jout);
@@ -2489,7 +2504,7 @@ public class Jalview2XML
     vamsasSeq.setName(jds.getName());
     vamsasSeq.setSequence(jds.getSequenceAsString());
     vamsasSeq.setDescription(jds.getDescription());
-    jalview.datamodel.DBRefEntry[] dbrefs = null;
+    List<DBRefEntry> dbrefs = null;
     if (jds.getDatasetSequence() != null)
     {
       vamsasSeq.setDsseqid(seqHash(jds.getDatasetSequence()));
@@ -2507,15 +2522,16 @@ public class Jalview2XML
     }
     if (dbrefs != null)
     {
-      for (int d = 0; d < dbrefs.length; d++)
+      for (int d = 0, nd = dbrefs.size(); d < nd; d++)
       {
         DBRef dbref = new DBRef();
-        dbref.setSource(dbrefs[d].getSource());
-        dbref.setVersion(dbrefs[d].getVersion());
-        dbref.setAccessionId(dbrefs[d].getAccessionId());
-        if (dbrefs[d].hasMap())
+        DBRefEntry ref = dbrefs.get(d);
+        dbref.setSource(ref.getSource());
+        dbref.setVersion(ref.getVersion());
+        dbref.setAccessionId(ref.getAccessionId());
+        if (ref.hasMap())
         {
-          Mapping mp = createVamsasMapping(dbrefs[d].getMap(), parentseq,
+          Mapping mp = createVamsasMapping(ref.getMap(), parentseq,
                   jds, recurse);
           dbref.setMapping(mp);
         }
@@ -2711,7 +2727,7 @@ public class Jalview2XML
    * @param file
    *          - HTTP URL or filename
    */
-  public AlignFrame loadJalviewAlign(final String file)
+  public AlignFrame loadJalviewAlign(final Object file)
   {
 
     jalview.gui.AlignFrame af = null;
@@ -2745,7 +2761,7 @@ public class Jalview2XML
           public void run()
           {
             setLoadingFinishedForNewStructureViewers();
-          };
+          }
         });
       } catch (Exception x)
       {
@@ -2755,44 +2771,52 @@ public class Jalview2XML
     return af;
   }
 
-  private jarInputStreamProvider createjarInputStreamProvider(
-          final String file) throws MalformedURLException
-  {
-    URL url = null;
-    errorMessage = null;
-    uniqueSetSuffix = null;
-    seqRefIds = null;
-    viewportsAdded.clear();
-    frefedSequence = null;
-
-    if (file.startsWith("http://"))
-    {
-      url = new URL(file);
-    }
-    final URL _url = url;
-    return new jarInputStreamProvider()
-    {
-
-      @Override
-      public JarInputStream getJarInputStream() throws IOException
-      {
-        if (_url != null)
-        {
-          return new JarInputStream(_url.openStream());
-        }
-        else
-        {
-          return new JarInputStream(new FileInputStream(file));
+        @SuppressWarnings("unused")
+        private jarInputStreamProvider createjarInputStreamProvider(final Object ofile) throws MalformedURLException {
+
+                // BH 2018 allow for bytes already attached to File object
+                try {
+                        String file = (ofile instanceof File ? ((File) ofile).getCanonicalPath() : ofile.toString());
+      byte[] bytes = Platform.isJS() ? Platform.getFileBytes((File) ofile)
+              : null;
+                        URL url = null;
+                        errorMessage = null;
+                        uniqueSetSuffix = null;
+                        seqRefIds = null;
+                        viewportsAdded.clear();
+                        frefedSequence = null;
+
+                        if (file.startsWith("http://")) {
+                                url = new URL(file);
+                        }
+                        final URL _url = url;
+                        return new jarInputStreamProvider() {
+
+                                @Override
+                                public JarInputStream getJarInputStream() throws IOException {
+                                        if (bytes != null) {
+//                                              System.out.println("Jalview2XML: opening byte jarInputStream for bytes.length=" + bytes.length);
+                                                return new JarInputStream(new ByteArrayInputStream(bytes));
+                                        }
+                                        if (_url != null) {
+//                                              System.out.println("Jalview2XML: opening url jarInputStream for " + _url);
+                                                return new JarInputStream(_url.openStream());
+                                        } else {
+//                                              System.out.println("Jalview2XML: opening file jarInputStream for " + file);
+                                                return new JarInputStream(new FileInputStream(file));
+                                        }
+                                }
+
+                                @Override
+                                public String getFilename() {
+                                        return file;
+                                }
+                        };
+                } catch (IOException e) {
+                        e.printStackTrace();
+                        return null;
+                }
         }
-      }
-
-      @Override
-      public String getFilename()
-      {
-        return file;
-      }
-    };
-  }
 
   /**
    * Recover jalview session from a jalview project archive. Caller may
@@ -2834,9 +2858,6 @@ public class Jalview2XML
 
         if (jarentry != null && jarentry.getName().endsWith(".xml"))
         {
-          InputStreamReader in = new InputStreamReader(jin, UTF_8);
-          // JalviewModel object = new JalviewModel();
-
           JAXBContext jc = JAXBContext
                   .newInstance("jalview.xml.binding.jalview");
           XMLStreamReader streamReader = XMLInputFactory.newInstance()
@@ -2846,11 +2867,6 @@ public class Jalview2XML
                   .unmarshal(streamReader, JalviewModel.class);
           JalviewModel object = jbe.getValue();
 
-          /*
-          Unmarshaller unmar = new Unmarshaller(object);
-          unmar.setValidation(false);
-          object = (JalviewModel) unmar.unmarshal(in);
-          */
           if (true) // !skipViewport(object))
           {
             _af = loadFromObject(object, file, true, jprovider);
@@ -4839,7 +4855,18 @@ public class Jalview2XML
   {
     AlignFrame af = null;
     af = new AlignFrame(al, safeInt(view.getWidth()),
-            safeInt(view.getHeight()), uniqueSeqSetId, viewId);
+            safeInt(view.getHeight()), uniqueSeqSetId, viewId) 
+//    {
+//      
+//      @Override
+//      protected void processKeyEvent(java.awt.event.KeyEvent e) {
+//              System.out.println("Jalview2XML   AF " + e);
+//              super.processKeyEvent(e);
+//              
+//      }
+//      
+//    }
+    ;
 
     af.setFileName(file, FileFormat.Jalview);