Merge branch 'develop' into Jalview-JS/develop
[jalview.git] / src / jalview / project / Jalview2XML.java
index 02b405e..fa104a6 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,27 @@ 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
+
+  private static void addJ2SBinaryType(String ext)
+  {
+    ext = "." + ext + "?";
+
+    /**
+     * @j2sNative
+     * 
+     *            J2S._binaryTypes.push(ext);
+     * 
+     */
+  }
+
+  static
+  {
+    addJ2SBinaryType(".jvp?");
+  }
+
   private static final String VIEWER_PREFIX = "viewer_";
 
   private static final String RNA_PREFIX = "rna_";
@@ -745,9 +768,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<>();
@@ -772,8 +796,11 @@ public class Jalview2XML
       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 +1686,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 +2057,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 +2518,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 +2536,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 +2741,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;
@@ -2755,44 +2785,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));
-        }
-      }
-
-      @Override
-      public String getFilename()
-      {
-        return 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 = /** @j2sNative ofile._bytes || */
+                                       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;
+               }
+       }
 
   /**
    * Recover jalview session from a jalview project archive. Caller may
@@ -2834,9 +2872,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 +2881,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 +4869,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);