fix minor exception when syncing document with appdata references when no appdata...
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / SimpleDocBinding.java
1 package uk.ac.vamsas.client.simpleclient;
2
3 import java.io.BufferedInputStream;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.util.Vector;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import uk.ac.vamsas.client.Vobject;
12 import uk.ac.vamsas.client.VorbaIdFactory;
13 import uk.ac.vamsas.client.VorbaXmlBinder;
14 import uk.ac.vamsas.objects.core.VAMSAS;
15 import uk.ac.vamsas.objects.core.VamsasDocument;
16 import uk.ac.vamsas.objects.utils.AppDataReference;
17 import uk.ac.vamsas.objects.utils.DocumentStuff;
18 import uk.ac.vamsas.objects.utils.ProvenanceStuff;
19 import uk.ac.vamsas.objects.utils.document.VersionEntries;
20
21 /**
22  * Base class for SimpleClient Vamsas Document Object Manipulation
23  * holds static vamsasDocument from XML routines and
24  * state objects for a particular unmarshalled Document instance.
25  * @author jimp
26  */
27   
28
29 public class SimpleDocBinding {
30
31   protected VorbaIdFactory vorba;
32   protected static Log log = LogFactory.getLog(SimpleDocBinding.class);
33
34   /**
35    * @return Returns the vorba.
36    */
37   public VorbaIdFactory getVorba() {
38     return vorba;
39   }
40
41   /**
42    * @param vorba The vorba to set.
43    */
44   public void setVorba(VorbaIdFactory vorba) {
45     this.vorba = vorba;
46   }
47
48   /**
49    * Uses VorbaXmlBinder to retrieve the VamsasDocument from the given stream
50    */
51   public VamsasDocument getVamsasDocument(VamsasArchiveReader oReader) throws IOException, org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
52     if (oReader!=null) {
53       // check the factory
54       if (vorba==null) {
55         log.error("Invalid SimpleDocument construction - no VorbaIdFactory defined!");
56         return null;
57       }
58   
59       if (oReader.isValid()) {
60         // Read vamsasDocument.xsd instance
61         InputStreamReader vdoc = new InputStreamReader(oReader.getVamsasDocumentStream());
62         Object unmarsh[] = VorbaXmlBinder.getVamsasObjects(vdoc, vorba, new VamsasDocument());
63         if (unmarsh==null)
64           log.fatal("Couldn't unmarshall document!");
65         
66         Vobject vobjs = (Vobject) unmarsh[0];
67         if (vobjs!=null) { 
68           VamsasDocument doc=(VamsasDocument) vobjs;
69           if (doc!=null)
70             return doc;
71         }
72         log.debug("Found no VamsasDocument object in properly formatted Vamsas Archive.");
73       } else {        
74         // deprecated data handler (vamsas.xsd instance)
75         InputStream vxmlis = oReader.getVamsasXmlStream();
76         if (vxmlis!=null) { // Might be an old vamsas file.
77           BufferedInputStream ixml = new BufferedInputStream(oReader.getVamsasXmlStream());
78           InputStreamReader vxml = new InputStreamReader(ixml);
79           Object unmarsh[] = VorbaXmlBinder.getVamsasObjects(vxml, vorba, new VAMSAS());
80           
81           if (unmarsh==null)
82             log.fatal("Couldn't unmarshall document!");
83           
84           VAMSAS root[]= new VAMSAS[] { null};
85           root[0] = (VAMSAS) unmarsh[0]; 
86           
87           if (root[0]==null) {
88             log.debug("Found no VAMSAS object in VamsasXML stream.");
89           } else {
90             log.debug("Making new VamsasDocument from VamsasXML stream.");
91             VamsasDocument doc = DocumentStuff.newVamsasDocument(root, 
92                 ProvenanceStuff.newProvenance(
93                     vorba.getUserHandle().getFullName(), 
94                     "Vamsas Document constructed from vamsas.xml"), VersionEntries.ALPHA_VERSION);
95             // VAMSAS: decide on 'system' operations provenance form
96             // LATER: implement classes for translating Vorba properties into provenance user fields.
97             // VAMSAS: decide on machine readable info embedding in provenance should be done
98             root[0]=null;
99             root=null;
100             return doc;
101           }
102         }
103       }
104     }
105     // otherwise - there was no valid original document to read.
106     return null;    
107   }
108
109   /**
110    * Extract all jarEntries in an archive referenced by the vamsas document
111    * LATER: a family of methods for finding extraneous jarEntries , and invalid appDataReferences
112    * @param doc
113    * @param oReader
114    * @return array of the subset of JarEntry names that are referenced in doc 
115    */
116   public Vector getReferencedEntries(VamsasDocument doc, VamsasArchiveReader oReader) {
117     if (oReader==null)
118      return null;
119     if (doc==null) {
120      try { doc = getVamsasDocument(oReader); } 
121      catch (Exception e) { log.warn("Failed to get document from "+oReader.jfileName); };
122     }
123     Vector docrefs = AppDataReference.getAppDataReferences(doc);
124     if (docrefs==null)
125       return null;
126     Vector entries = oReader.getExtraEntries();
127     if (entries!=null && entries.size()>0 && docrefs.size()>0) {
128       int i=0, j=entries.size();
129       do {
130         if (!docrefs.contains(entries.get(i))) {
131             entries.remove(i);
132             j--;
133         } else
134           i++;
135       } while (i<j);
136     }
137     return entries;
138   }
139 }