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