applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / VamsasArchiveReader.java
index 97efc73..f863eb7 100644 (file)
-package uk.ac.vamsas.client.simpleclient;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.BufferedInputStream;
-import java.io.FileInputStream;
-import java.io.RandomAccessFile;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.util.Iterator;
-import java.util.Vector;
-import org.apache.tools.zip.*;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-import uk.ac.vamsas.client.AppDataInputStream;
-import uk.ac.vamsas.objects.utils.document.VersionEntries;
-/**
- * Basic methods for accessing an existing Vamsas Archive, 
- * and Jar entry names for creating new vamsas archives.
- * 
- * @author jimp
- *
- */
-public class VamsasArchiveReader {
-  private static final int JARFILE_OPEN_RETRIES = 50;
-  private static final int JARFILE_OPEN_RETRYWAIT = 1;
-  private static Log log = LogFactory.getLog(VamsasArchiveReader.class);
-  ZipFile jfile=null;
-  String jfileName="randomAccessFile";
-  boolean stream=false; // true if we are seeking on the stream.
-  RandomAccessFile rfile;
-  // ZipInputStream jstream=null;
-  Hashtable strmentries = null;
-  private void streamInit() {
-    //throw new Error("VamsasArchiveReader(Stream) Not implemented!");
-     if (!stream) {
-      log.debug("Skipping init for Jar Stream input.");
-      return;
-    } else {
-      throw new Error("Implementation error - we don't do streams - only files or RA files");
-    }
-     /*strmentries = new Hashtable();
-    log.debug("Jar Stream input Initialisation");
-    try {
-      rfile.seek(0);
-      // no buffering - we need to be able to move around the random access stream.
-      jstream = new ZipInputStream(new FileInputStream(rfile.getFD())); // no manifest (probably)
-      if (jstream.available()==0)
-        log.warn("Can't read from JarInputStream (Locked stream!)");
-      ZipEntry entry=null;
-      long pos=0;
-      do {
-        if ((entry=jstream.getNextEntry())!=null) {
-          if (strmentries.containsKey(entry.getName())) {
-            log.info("Only recording last of duplicate entries '"+entry.getName()+"'");
-          } 
-          strmentries.put(entry.getName(), new Long(pos++));
-          jstream.closeEntry();
-        }
-      } while (entry!=null);
-    }
-    catch (Exception e) {
-      log.warn("Exceptions during init!",e);
-      jstream=null;
-    } */
-  }
-  
-  public VamsasArchiveReader(File vamsasfile) {
-    jfile=null;
-    int retries=JARFILE_OPEN_RETRIES;
-    Exception ex=null;
-    if (vamsasfile.exists()) {
-      while (jfile==null && --retries>0)
-      {
-      try {
-        jfile=new ZipFile(vamsasfile);
-        jfileName = vamsasfile.toString();
-      }
-      catch (Exception e) {
-        ex = e;
-        jfile=null;
-        try { 
-          Thread.sleep(JARFILE_OPEN_RETRYWAIT); 
-        } catch (Exception w) {};
-      }
-      }
-      if (jfile==null && ex!=null)
-      {
-        log.debug("non-serious? Exceptions when opening JarFile at "+vamsasfile,ex);
-      }
-    }
-  }
-  /**
-   * in an ideal world - this constructor will create a reader object
-   * for the locked file's random access stream.
-   * 
-   * @param vamsaslock
-   */
-  public VamsasArchiveReader(Lock vamsaslock) {
-    jfile = null;
-    if (vamsaslock==null || !vamsaslock.isLocked())
-      throw new Error("IMPLEMENTATION ERROR: Cannot create a VamsasArchiveReader without a valid lock.");
-    // throw new Error("VamsasArchiveReading from locked IO stream not yet implemented.");
-    try {
-      rfile = vamsaslock.getRaFile();
-      jfile = new ZipFile(rfile);
-      if (vamsaslock.target!=null)
-        jfileName = vamsaslock.target.toString();
-    } catch (Exception e) {
-      rfile = null;
-      jfile = null;
-      log.warn("Unexpected IO Exception when accessing locked vamsas archive stream "+vamsaslock.target,e);
-    }
-    /*stream = true;
-    streamInit();
-    if (jstream==null)
-      throw new Error("Failed to open archive from Locked random access stream.");
-      */
-  }
-  
-  /**
-   * the vamsas document version(s) handled by this Reader
-   */
-  final public static String DOCUMENT_VERSION=VersionEntries.BETA_VERSION; 
-  /**
-   * name of the jarEntry containing a well formatted vamsas XML Document
-   */
-  
-  final public static String VAMSASDOC="vamsasDocument.xml";
-  
-  /**
-   * name of the jarEntry containing a root VAMSAS element, and containing a 
-   * random sequence of VAMSAS DataSet elements 
-   */
-  
-  final public static String VAMSASXML="vamsas.xml";
-  /**
-   * seeks jstream to the given entry name and reads it.
-   * @param entryname
-   * @return
-  private JarEntry seekEntry(String entryname) {
-    if (jstream==null)
-      return null;
-    if (!strmentries.containsKey(entryname)) 
-      return null;
-    Long entrypos = (Long) strmentries.get(entryname);
-    if (entrypos==null) {
-      log.error("Null entry position for "+entryname);
-      return null;
-    }
-    try {
-      jstream=null;
-      rfile.seek(0);
-      jstream = new ZipInputStream(new FileInputStream(rfile.getFD()));
-      ZipEntry entry = null;
-      long epos = entrypos.longValue();
-      do {
-        entry = jstream.getNextEntry();
-      } while (entry!=null && --epos>=0);  
-        // rfile.seek(entrypos.longValue());
-      // make a Jar entry from a zip entry.
-      return new JarEntry(entry);
-    }
-    catch (Exception e) {
-      log.warn("Whilst seeking for "+entryname, e);
-    }
-    return null;
-  }
-   */
-  /**
-   * 
-   * @return JarEntry for VamsasArchiveReader.VAMSASDOC
-   */
-  protected ZipEntry getVamsasDocumentEntry() {
-    return getJarEntry(VAMSASDOC);
-  }
-  /**
-   * 
-   * @return JarEntry for VamsasArchiveReader.VAMSASXML
-   */
-  protected ZipEntry getVamsasXmlEntry() {
-    return getJarEntry(VAMSASXML);
-  }
-  /**
-   * Test for valid vamsas document archive
-   * @return true if getVamsasDocumentStream will return a stream likely to contain valid XML
-   */
-  public boolean isValid() {
-    // TODO: check if VAMSASDOC is well formed (follows www.vamsas.ac.uk/schemas/vamsasDocument.xsd) and all appData references are resolvable - preferably as jar entries
-    if (jfile!=null) //  || jstream!=null)
-      return (getVamsasDocumentEntry()!=null);
-    return false;   
-  }
-  
-  
-  protected ZipEntry getAppdataEntry(String AppdataRef) {
-    ZipEntry entry;
-    if (jfile==null || !isValid() || (entry=getJarEntry(AppdataRef))==null)
-      return null;
-    
-    return entry;
-  }
-  
-  public InputStream getAppdataStream(String AppdataRef) {
-    ZipEntry entry=getAppdataEntry(AppdataRef);
-    try {
-      if (entry!=null)
-        return getInputStream(entry);
-    } catch (IOException e) {
-      log.error("Failed when opening AppdataStream for "+AppdataRef, e);
-    }
-    return null;
-  }
-  /**
-   * get the VamsasDocument input stream, if it exists.
-   * @return null or valid input stream
-   */
-  public InputStream getVamsasDocumentStream() {
-    InputStream vdoc;
-    if (jfile==null || !isValid())
-      return null;
-    try {
-      vdoc = getInputStream(getVamsasDocumentEntry());
-    } catch (IOException e) {
-      log.error("Whilst geting document stream",e);
-      vdoc=null;
-    }
-    return vdoc;
-  }
-  
-  /**
-   * get the VamsasXML input stream, if it exists.
-   * Note: Deprecated beyond our prealpha testing.
-   * @return null or valid input stream.
-   */
-  
-  public InputStream getVamsasXmlStream() {
-    // log.warn("Deprecated call");
-    ZipEntry xmle=getVamsasXmlEntry();
-    InputStream vdoc;
-    if (xmle==null)
-      return null;
-    try {
-      vdoc = getInputStream(xmle);
-    } catch (IOException e) {
-      log.error("Whilst getting VamsasXmlStream",e);
-      vdoc=null;
-    }
-    return vdoc;
-  }
-  
-  /**
-   * silently close the jar file.
-   *
-   */
-  public void close() {
-    if (jfile!=null) {
-      try {
-        jfile.close();
-        rfile=null;
-      } 
-      catch (Exception e) {
-        log.error("Whilst closing JarFile "+jfileName, e);
-      }
-    }
-  }
-  
-  /**
-   * returns all entries not matching the filespec of a vamsas xml entry
-   * @return array of entries.
-   */
-  public Vector getExtraEntries() {
-    if (jfile==null || !isValid())
-      return null;
-    Vector e = new Vector();
-    /*if (jstream!=null) {
-      Enumeration entries = strmentries.keys();
-      if (entries!=null && entries.hasMoreElements()) {
-        do {
-          JarEntry el = (JarEntry) entries.nextElement();
-          if (!el.getName().equals(VAMSASDOC) && !el.getName().equals(VAMSASXML))
-            e.add(new String(el.getName())); // avoid references
-        } while (entries.hasMoreElements());
-      }
-    } else */
-    Enumeration entries = jfile.getEntries();
-    if (entries!=null && entries.hasMoreElements()) {
-      do {
-        ZipEntry el = (ZipEntry) entries.nextElement();
-        if (!el.getName().equals(VAMSASDOC) && !el.getName().equals(VAMSASXML))
-          e.add(new String(el.getName())); // avoid references
-      } while (entries.hasMoreElements());
-    }
-    return e;
-  }
-  
-  /* (non-Javadoc)
-   * @see java.util.jar.JarFile#getInputStream(java.util.zip.ZipEntry)
-   */
-  private InputStream getInputStream(ZipEntry ze) throws IOException {
-    if (jfile!=null)
-      return jfile.getInputStream(ze);
-    return null;
-  }
-    
-    /* (non-Javadoc)
-     * @see java.util.jar.JarFile#getJarEntry(java.lang.String)
-     */
-    private ZipEntry getJarEntry(String name) {
-      if (jfile!=null)
-        return jfile.getEntry(name);
-      return null;
-    }
-  }
+/*\r
+ * This file is part of the Vamsas Client version 0.1. \r
+ * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
+ *  Andrew Waterhouse and Dominik Lindner.\r
+ * \r
+ * Earlier versions have also been incorporated into Jalview version 2.4 \r
+ * since 2008, and TOPALi version 2 since 2007.\r
+ * \r
+ * The Vamsas Client is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU Lesser General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *  \r
+ * The Vamsas Client is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU Lesser General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU Lesser General Public License\r
+ * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
+ */\r
+package uk.ac.vamsas.client.simpleclient;\r
+\r
+import java.io.File;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.BufferedInputStream;\r
+import java.io.FileInputStream;\r
+import java.io.RandomAccessFile;\r
+import java.util.Enumeration;\r
+import java.util.Hashtable;\r
+import java.util.Iterator;\r
+import java.util.Vector;\r
+import org.apache.tools.zip.*;\r
+import org.apache.commons.logging.Log;\r
+import org.apache.commons.logging.LogFactory;\r
+\r
+import uk.ac.vamsas.client.AppDataInputStream;\r
+import uk.ac.vamsas.objects.utils.document.VersionEntries;\r
+\r
+/**\r
+ * Basic methods for accessing an existing Vamsas Archive, and Jar entry names\r
+ * for creating new vamsas archives.\r
+ * \r
+ * @author jimp\r
+ * \r
+ */\r
+public class VamsasArchiveReader {\r
+  private static final int JARFILE_OPEN_RETRIES = 50;\r
+\r
+  private static final int JARFILE_OPEN_RETRYWAIT = 1;\r
+\r
+  private static Log log = LogFactory.getLog(VamsasArchiveReader.class);\r
+\r
+  ZipFile jfile = null;\r
+\r
+  String jfileName = "randomAccessFile";\r
+\r
+  boolean stream = false; // true if we are seeking on the stream.\r
+\r
+  RandomAccessFile rfile;\r
+\r
+  // ZipInputStream jstream=null;\r
+  Hashtable strmentries = null;\r
+\r
+  private void streamInit() {\r
+    // throw new Error("VamsasArchiveReader(Stream) Not implemented!");\r
+    if (!stream) {\r
+      log.debug("Skipping init for Jar Stream input.");\r
+      return;\r
+    } else {\r
+      throw new Error(\r
+          "Implementation error - we don't do streams - only files or RA files");\r
+    }\r
+    /*\r
+     * strmentries = new Hashtable();\r
+     * log.debug("Jar Stream input Initialisation"); try { rfile.seek(0); // no\r
+     * buffering - we need to be able to move around the random access stream.\r
+     * jstream = new ZipInputStream(new FileInputStream(rfile.getFD())); // no\r
+     * manifest (probably) if (jstream.available()==0)\r
+     * log.warn("Can't read from JarInputStream (Locked stream!)"); ZipEntry\r
+     * entry=null; long pos=0; do { if ((entry=jstream.getNextEntry())!=null) {\r
+     * if (strmentries.containsKey(entry.getName())) {\r
+     * log.info("Only recording last of duplicate entries '"\r
+     * +entry.getName()+"'"); } strmentries.put(entry.getName(), new\r
+     * Long(pos++)); jstream.closeEntry(); } } while (entry!=null); } catch\r
+     * (Exception e) { log.warn("Exceptions during init!",e); jstream=null; }\r
+     */\r
+  }\r
+\r
+  public VamsasArchiveReader(File vamsasfile) {\r
+    jfile = null;\r
+    int retries = JARFILE_OPEN_RETRIES;\r
+    Exception ex = null;\r
+    if (vamsasfile.exists()) {\r
+      while (jfile == null && --retries > 0) {\r
+        try {\r
+          jfile = new ZipFile(vamsasfile);\r
+          jfileName = vamsasfile.toString();\r
+        } catch (Exception e) {\r
+          ex = e;\r
+          jfile = null;\r
+          try {\r
+            Thread.sleep(JARFILE_OPEN_RETRYWAIT);\r
+          } catch (Exception w) {\r
+          }\r
+          ;\r
+        }\r
+      }\r
+      if (jfile == null && ex != null) {\r
+        log.debug("non-serious? Exceptions when opening JarFile at "\r
+            + vamsasfile, ex);\r
+      }\r
+    }\r
+  }\r
+\r
+  /**\r
+   * in an ideal world - this constructor will create a reader object for the\r
+   * locked file's random access stream.\r
+   * \r
+   * @param vamsaslock\r
+   */\r
+  public VamsasArchiveReader(Lock vamsaslock) {\r
+    jfile = null;\r
+    if (vamsaslock == null || !vamsaslock.isLocked())\r
+      throw new Error(\r
+          "IMPLEMENTATION ERROR: Cannot create a VamsasArchiveReader without a valid lock.");\r
+    // throw new\r
+    // Error("VamsasArchiveReading from locked IO stream not yet implemented.");\r
+    try {\r
+      rfile = vamsaslock.getRaFile();\r
+      jfile = new ZipFile(rfile);\r
+      if (vamsaslock.target != null)\r
+        jfileName = vamsaslock.target.toString();\r
+    } catch (Exception e) {\r
+      rfile = null;\r
+      jfile = null;\r
+      log.warn(\r
+          "Unexpected IO Exception when accessing locked vamsas archive stream "\r
+              + vamsaslock.target, e);\r
+    }\r
+    /*\r
+     * stream = true; streamInit(); if (jstream==null) throw new\r
+     * Error("Failed to open archive from Locked random access stream.");\r
+     */\r
+  }\r
+\r
+  /**\r
+   * the vamsas document version(s) handled by this Reader\r
+   */\r
+  final public static String DOCUMENT_VERSION = VersionEntries.BETA_VERSION;\r
+\r
+  /**\r
+   * name of the jarEntry containing a well formatted vamsas XML Document\r
+   */\r
+\r
+  final public static String VAMSASDOC = "vamsasDocument.xml";\r
+\r
+  /**\r
+   * name of the jarEntry containing a root VAMSAS element, and containing a\r
+   * random sequence of VAMSAS DataSet elements\r
+   */\r
+\r
+  final public static String VAMSASXML = "vamsas.xml";\r
+\r
+  /**\r
+   * seeks jstream to the given entry name and reads it.\r
+   * \r
+   * @param entryname\r
+   * @return private JarEntry seekEntry(String entryname) { if (jstream==null)\r
+   *         return null; if (!strmentries.containsKey(entryname)) return null;\r
+   *         Long entrypos = (Long) strmentries.get(entryname); if\r
+   *         (entrypos==null) { log.error("Null entry position for "+entryname);\r
+   *         return null; } try { jstream=null; rfile.seek(0); jstream = new\r
+   *         ZipInputStream(new FileInputStream(rfile.getFD())); ZipEntry entry\r
+   *         = null; long epos = entrypos.longValue(); do { entry =\r
+   *         jstream.getNextEntry(); } while (entry!=null && --epos>=0); //\r
+   *         rfile.seek(entrypos.longValue()); // make a Jar entry from a zip\r
+   *         entry. return new JarEntry(entry); } catch (Exception e) {\r
+   *         log.warn("Whilst seeking for "+entryname, e); } return null; }\r
+   */\r
+  /**\r
+   * \r
+   * @return JarEntry for VamsasArchiveReader.VAMSASDOC\r
+   */\r
+  protected ZipEntry getVamsasDocumentEntry() {\r
+    return getJarEntry(VAMSASDOC);\r
+  }\r
+\r
+  /**\r
+   * \r
+   * @return JarEntry for VamsasArchiveReader.VAMSASXML\r
+   */\r
+  protected ZipEntry getVamsasXmlEntry() {\r
+    return getJarEntry(VAMSASXML);\r
+  }\r
+\r
+  /**\r
+   * Test for valid vamsas document archive\r
+   * \r
+   * @return true if getVamsasDocumentStream will return a stream likely to\r
+   *         contain valid XML\r
+   */\r
+  public boolean isValid() {\r
+    // TODO: check if VAMSASDOC is well formed (follows\r
+    // www.vamsas.ac.uk/schemas/vamsasDocument.xsd) and all appData references\r
+    // are resolvable - preferably as jar entries\r
+    if (jfile != null) // || jstream!=null)\r
+      return (getVamsasDocumentEntry() != null);\r
+    return false;\r
+  }\r
+\r
+  protected ZipEntry getAppdataEntry(String AppdataRef) {\r
+    ZipEntry entry;\r
+    if (jfile == null || !isValid()\r
+        || (entry = getJarEntry(AppdataRef)) == null)\r
+      return null;\r
+\r
+    return entry;\r
+  }\r
+\r
+  public InputStream getAppdataStream(String AppdataRef) {\r
+    ZipEntry entry = getAppdataEntry(AppdataRef);\r
+    try {\r
+      if (entry != null)\r
+        return getInputStream(entry);\r
+    } catch (IOException e) {\r
+      log.error("Failed when opening AppdataStream for " + AppdataRef, e);\r
+    }\r
+    return null;\r
+  }\r
+\r
+  /**\r
+   * get the VamsasDocument input stream, if it exists.\r
+   * \r
+   * @return null or valid input stream\r
+   */\r
+  public InputStream getVamsasDocumentStream() {\r
+    InputStream vdoc;\r
+    if (jfile == null || !isValid())\r
+      return null;\r
+    try {\r
+      vdoc = getInputStream(getVamsasDocumentEntry());\r
+    } catch (IOException e) {\r
+      log.error("Whilst geting document stream", e);\r
+      vdoc = null;\r
+    }\r
+    return vdoc;\r
+  }\r
+\r
+  /**\r
+   * get the VamsasXML input stream, if it exists. Note: Deprecated beyond our\r
+   * prealpha testing.\r
+   * \r
+   * @return null or valid input stream.\r
+   */\r
+\r
+  public InputStream getVamsasXmlStream() {\r
+    // log.warn("Deprecated call");\r
+    ZipEntry xmle = getVamsasXmlEntry();\r
+    InputStream vdoc;\r
+    if (xmle == null)\r
+      return null;\r
+    try {\r
+      vdoc = getInputStream(xmle);\r
+    } catch (IOException e) {\r
+      log.error("Whilst getting VamsasXmlStream", e);\r
+      vdoc = null;\r
+    }\r
+    return vdoc;\r
+  }\r
+\r
+  /**\r
+   * silently close the jar file.\r
+   * \r
+   */\r
+  public void close() {\r
+    if (jfile != null) {\r
+      try {\r
+        jfile.close();\r
+        rfile = null;\r
+      } catch (Exception e) {\r
+        log.error("Whilst closing JarFile " + jfileName, e);\r
+      }\r
+    }\r
+  }\r
+\r
+  /**\r
+   * returns all entries not matching the filespec of a vamsas xml entry\r
+   * \r
+   * @return array of entries.\r
+   */\r
+  public Vector getExtraEntries() {\r
+    if (jfile == null || !isValid())\r
+      return null;\r
+    Vector e = new Vector();\r
+    /*\r
+     * if (jstream!=null) { Enumeration entries = strmentries.keys(); if\r
+     * (entries!=null && entries.hasMoreElements()) { do { JarEntry el =\r
+     * (JarEntry) entries.nextElement(); if (!el.getName().equals(VAMSASDOC) &&\r
+     * !el.getName().equals(VAMSASXML)) e.add(new String(el.getName())); //\r
+     * avoid references } while (entries.hasMoreElements()); } } else\r
+     */\r
+    Enumeration entries = jfile.getEntries();\r
+    if (entries != null && entries.hasMoreElements()) {\r
+      do {\r
+        ZipEntry el = (ZipEntry) entries.nextElement();\r
+        if (!el.getName().equals(VAMSASDOC) && !el.getName().equals(VAMSASXML))\r
+          e.add(new String(el.getName())); // avoid references\r
+      } while (entries.hasMoreElements());\r
+    }\r
+    return e;\r
+  }\r
+\r
+  /*\r
+   * (non-Javadoc)\r
+   * \r
+   * @see java.util.jar.JarFile#getInputStream(java.util.zip.ZipEntry)\r
+   */\r
+  private InputStream getInputStream(ZipEntry ze) throws IOException {\r
+    if (jfile != null)\r
+      return jfile.getInputStream(ze);\r
+    return null;\r
+  }\r
+\r
+  /*\r
+   * (non-Javadoc)\r
+   * \r
+   * @see java.util.jar.JarFile#getJarEntry(java.lang.String)\r
+   */\r
+  private ZipEntry getJarEntry(String name) {\r
+    if (jfile != null)\r
+      return jfile.getEntry(name);\r
+    return null;\r
+  }\r
+}\r