applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / SimpleDocBinding.java
index 6815706..895bb0f 100644 (file)
-package uk.ac.vamsas.client.simpleclient;
-
-import java.io.BufferedInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.util.Vector;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-import uk.ac.vamsas.client.Vobject;
-import uk.ac.vamsas.client.VorbaIdFactory;
-import uk.ac.vamsas.client.VorbaXmlBinder;
-import uk.ac.vamsas.objects.core.VAMSAS;
-import uk.ac.vamsas.objects.core.VamsasDocument;
-import uk.ac.vamsas.objects.utils.AppDataReference;
-import uk.ac.vamsas.objects.utils.DocumentStuff;
-import uk.ac.vamsas.objects.utils.ProvenanceStuff;
-import uk.ac.vamsas.objects.utils.document.VersionEntries;
-
-/**
- * Base class for SimpleClient Vamsas Document Object Manipulation
- * holds static vamsasDocument from XML routines and
- * state objects for a particular unmarshalled Document instance.
- * @author jimp
- */
-  
-
-public class SimpleDocBinding {
-
-  protected VorbaIdFactory vorba;
-  protected static Log log = LogFactory.getLog(SimpleDocBinding.class);
-
-  /**
-   * @return Returns the vorba.
-   */
-  public VorbaIdFactory getVorba() {
-    return vorba;
-  }
-
-  /**
-   * @param vorba The vorba to set.
-   */
-  public void setVorba(VorbaIdFactory vorba) {
-    this.vorba = vorba;
-  }
-
-  /**
-   * Uses VorbaXmlBinder to retrieve the VamsasDocument from the given stream
-   */
-  public VamsasDocument getVamsasDocument(VamsasArchiveReader oReader) throws IOException, org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
-    if (oReader!=null) {
-      // check the factory
-      if (vorba==null) {
-        log.error("Invalid SimpleDocument construction - no VorbaIdFactory defined!");
-        return null;
-      }
-  
-      if (oReader.isValid()) {
-        // Read vamsasDocument.xsd instance
-        InputStreamReader vdoc = new InputStreamReader(oReader.getVamsasDocumentStream());
-        Object unmarsh[] = VorbaXmlBinder.getVamsasObjects(vdoc, vorba, new VamsasDocument());
-        if (unmarsh==null)
-          log.fatal("Couldn't unmarshall document!");
-        
-        Vobject vobjs = (Vobject) unmarsh[0];
-        if (vobjs!=null) { 
-          VamsasDocument doc=(VamsasDocument) vobjs;
-          if (doc!=null)
-            return doc;
-        }
-        log.debug("Found no VamsasDocument object in properly formatted Vamsas Archive.");
-      } else {        
-        // deprecated data handler (vamsas.xsd instance)
-        InputStream vxmlis = oReader.getVamsasXmlStream();
-        if (vxmlis!=null) { // Might be an old vamsas file.
-          BufferedInputStream ixml = new BufferedInputStream(oReader.getVamsasXmlStream());
-          InputStreamReader vxml = new InputStreamReader(ixml);
-          Object unmarsh[] = VorbaXmlBinder.getVamsasObjects(vxml, vorba, new VAMSAS());
-          
-          if (unmarsh==null)
-            log.fatal("Couldn't unmarshall document!");
-          
-          VAMSAS root[]= new VAMSAS[] { null};
-          root[0] = (VAMSAS) unmarsh[0]; 
-          
-          if (root[0]==null) {
-            log.debug("Found no VAMSAS object in VamsasXML stream.");
-          } else {
-            log.debug("Making new VamsasDocument from VamsasXML stream.");
-            VamsasDocument doc = DocumentStuff.newVamsasDocument(root, 
-                ProvenanceStuff.newProvenance(
-                    vorba.getUserHandle().getFullName(), 
-                    "Vamsas Document constructed from vamsas.xml"), VersionEntries.ALPHA_VERSION);
-            // VAMSAS: decide on 'system' operations provenance form
-            // LATER: implement classes for translating Vorba properties into provenance user fields.
-            // VAMSAS: decide on machine readable info embedding in provenance should be done
-            root[0]=null;
-            root=null;
-            return doc;
-          }
-        }
-      }
-    }
-    // otherwise - there was no valid original document to read.
-    return null;    
-  }
-
-  /**
-   * Extract all jarEntries in an archive referenced by the vamsas document
-   * LATER: a family of methods for finding extraneous jarEntries , and invalid appDataReferences
-   * @param doc
-   * @param oReader
-   * @return array of the subset of JarEntry names that are referenced in doc 
-   */
-  public Vector getReferencedEntries(VamsasDocument doc, VamsasArchiveReader oReader) {
-    if (oReader==null)
-     return null;
-    if (doc==null) {
-     try { doc = getVamsasDocument(oReader); } 
-     catch (Exception e) { log.warn("Failed to get document from "+oReader.jfileName); };
-    }
-    Vector docrefs = AppDataReference.getAppDataReferences(doc);
-    if (docrefs==null)
-      return null;
-    Vector entries = oReader.getExtraEntries();
-    if (entries!=null && entries.size()>0 && docrefs.size()>0) {
-      int i=0, j=entries.size();
-      do {
-        if (!docrefs.contains(entries.get(i))) {
-            entries.remove(i);
-            j--;
-        } else
-          i++;
-      } while (i<j);
-    }
-    return entries;
-  }
-}
+/*\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.BufferedInputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.InputStreamReader;\r
+import java.util.Vector;\r
+import org.apache.commons.logging.Log;\r
+import org.apache.commons.logging.LogFactory;\r
+\r
+import uk.ac.vamsas.client.Vobject;\r
+import uk.ac.vamsas.client.VorbaIdFactory;\r
+import uk.ac.vamsas.client.VorbaXmlBinder;\r
+import uk.ac.vamsas.objects.core.VAMSAS;\r
+import uk.ac.vamsas.objects.core.VamsasDocument;\r
+import uk.ac.vamsas.objects.utils.AppDataReference;\r
+import uk.ac.vamsas.objects.utils.DocumentStuff;\r
+import uk.ac.vamsas.objects.utils.ProvenanceStuff;\r
+import uk.ac.vamsas.objects.utils.document.VersionEntries;\r
+\r
+/**\r
+ * Base class for SimpleClient Vamsas Document Object Manipulation holds static\r
+ * vamsasDocument from XML routines and state objects for a particular\r
+ * unmarshalled Document instance.\r
+ * \r
+ * @author jimp\r
+ */\r
+\r
+public class SimpleDocBinding {\r
+\r
+  protected VorbaIdFactory vorba;\r
+\r
+  protected static Log log = LogFactory.getLog(SimpleDocBinding.class);\r
+\r
+  /**\r
+   * @return Returns the vorba.\r
+   */\r
+  public VorbaIdFactory getVorba() {\r
+    return vorba;\r
+  }\r
+\r
+  /**\r
+   * @param vorba\r
+   *          The vorba to set.\r
+   */\r
+  public void setVorba(VorbaIdFactory vorba) {\r
+    this.vorba = vorba;\r
+  }\r
+\r
+  /**\r
+   * Uses VorbaXmlBinder to retrieve the VamsasDocument from the given stream\r
+   */\r
+  public VamsasDocument getVamsasDocument(VamsasArchiveReader oReader)\r
+      throws IOException, org.exolab.castor.xml.MarshalException,\r
+      org.exolab.castor.xml.ValidationException {\r
+    if (oReader != null) {\r
+      // check the factory\r
+      if (vorba == null) {\r
+        log\r
+            .error("Invalid SimpleDocument construction - no VorbaIdFactory defined!");\r
+        return null;\r
+      }\r
+\r
+      if (oReader.isValid()) {\r
+        // Read vamsasDocument.xsd instance\r
+        InputStreamReader vdoc = new InputStreamReader(oReader\r
+            .getVamsasDocumentStream());\r
+        Object unmarsh[] = VorbaXmlBinder.getVamsasObjects(vdoc, vorba,\r
+            new VamsasDocument());\r
+        if (unmarsh == null)\r
+          log.fatal("Couldn't unmarshall document!");\r
+\r
+        Vobject vobjs = (Vobject) unmarsh[0];\r
+        if (vobjs != null) {\r
+          VamsasDocument doc = (VamsasDocument) vobjs;\r
+          if (doc != null)\r
+            return doc;\r
+        }\r
+        log\r
+            .debug("Found no VamsasDocument object in properly formatted Vamsas Archive.");\r
+      } else {\r
+        // deprecated data handler (vamsas.xsd instance)\r
+        InputStream vxmlis = oReader.getVamsasXmlStream();\r
+        if (vxmlis != null) { // Might be an old vamsas file.\r
+          BufferedInputStream ixml = new BufferedInputStream(oReader\r
+              .getVamsasXmlStream());\r
+          InputStreamReader vxml = new InputStreamReader(ixml);\r
+          Object unmarsh[] = VorbaXmlBinder.getVamsasObjects(vxml, vorba,\r
+              new VAMSAS());\r
+\r
+          if (unmarsh == null)\r
+            log.fatal("Couldn't unmarshall document!");\r
+\r
+          VAMSAS root[] = new VAMSAS[] { null };\r
+          root[0] = (VAMSAS) unmarsh[0];\r
+\r
+          if (root[0] == null) {\r
+            log.debug("Found no VAMSAS object in VamsasXML stream.");\r
+          } else {\r
+            log.debug("Making new VamsasDocument from VamsasXML stream.");\r
+            VamsasDocument doc = DocumentStuff.newVamsasDocument(root,\r
+                ProvenanceStuff.newProvenance(vorba.getUserHandle()\r
+                    .getFullName(),\r
+                    "Vamsas Document constructed from vamsas.xml"),\r
+                VersionEntries.ALPHA_VERSION);\r
+            // VAMSAS: decide on 'system' operations provenance form\r
+            // LATER: implement classes for translating Vorba properties into\r
+            // provenance user fields.\r
+            // VAMSAS: decide on machine readable info embedding in provenance\r
+            // should be done\r
+            root[0] = null;\r
+            root = null;\r
+            return doc;\r
+          }\r
+        }\r
+      }\r
+    }\r
+    // otherwise - there was no valid original document to read.\r
+    return null;\r
+  }\r
+\r
+  /**\r
+   * Extract all jarEntries in an archive referenced by the vamsas document\r
+   * LATER: a family of methods for finding extraneous jarEntries , and invalid\r
+   * appDataReferences\r
+   * \r
+   * @param doc\r
+   * @param oReader\r
+   * @return array of the subset of JarEntry names that are referenced in doc\r
+   */\r
+  public Vector getReferencedEntries(VamsasDocument doc,\r
+      VamsasArchiveReader oReader) {\r
+    if (oReader == null)\r
+      return null;\r
+    if (doc == null) {\r
+      try {\r
+        doc = getVamsasDocument(oReader);\r
+      } catch (Exception e) {\r
+        log.warn("Failed to get document from " + oReader.jfileName);\r
+      }\r
+      ;\r
+    }\r
+    Vector docrefs = AppDataReference.getAppDataReferences(doc);\r
+    if (docrefs == null)\r
+      return null;\r
+    Vector entries = oReader.getExtraEntries();\r
+    if (entries != null && entries.size() > 0 && docrefs.size() > 0) {\r
+      int i = 0, j = entries.size();\r
+      do {\r
+        if (!docrefs.contains(entries.get(i))) {\r
+          entries.remove(i);\r
+          j--;\r
+        } else\r
+          i++;\r
+      } while (i < j);\r
+    }\r
+    return entries;\r
+  }\r
+}\r