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