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