applied LGPLv3 and source code formatting.
[vamsas.git] / src / uk / ac / vamsas / client / simpleclient / SimpleDocBinding.java
1 /*\r
2  * This file is part of the Vamsas Client version 0.1. \r
3  * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite, \r
4  *  Andrew Waterhouse and Dominik Lindner.\r
5  * \r
6  * Earlier versions have also been incorporated into Jalview version 2.4 \r
7  * since 2008, and TOPALi version 2 since 2007.\r
8  * \r
9  * The Vamsas Client is free software: you can redistribute it and/or modify\r
10  * it under the terms of the GNU Lesser General Public License as published by\r
11  * the Free Software Foundation, either version 3 of the License, or\r
12  * (at your option) any later version.\r
13  *  \r
14  * The Vamsas Client is distributed in the hope that it will be useful,\r
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
17  * GNU Lesser General Public License for more details.\r
18  * \r
19  * You should have received a copy of the GNU Lesser General Public License\r
20  * along with the Vamsas Client.  If not, see <http://www.gnu.org/licenses/>.\r
21  */\r
22 package uk.ac.vamsas.client.simpleclient;\r
23 \r
24 import java.io.BufferedInputStream;\r
25 import java.io.IOException;\r
26 import java.io.InputStream;\r
27 import java.io.InputStreamReader;\r
28 import java.util.Vector;\r
29 import org.apache.commons.logging.Log;\r
30 import org.apache.commons.logging.LogFactory;\r
31 \r
32 import uk.ac.vamsas.client.Vobject;\r
33 import uk.ac.vamsas.client.VorbaIdFactory;\r
34 import uk.ac.vamsas.client.VorbaXmlBinder;\r
35 import uk.ac.vamsas.objects.core.VAMSAS;\r
36 import uk.ac.vamsas.objects.core.VamsasDocument;\r
37 import uk.ac.vamsas.objects.utils.AppDataReference;\r
38 import uk.ac.vamsas.objects.utils.DocumentStuff;\r
39 import uk.ac.vamsas.objects.utils.ProvenanceStuff;\r
40 import uk.ac.vamsas.objects.utils.document.VersionEntries;\r
41 \r
42 /**\r
43  * Base class for SimpleClient Vamsas Document Object Manipulation holds static\r
44  * vamsasDocument from XML routines and state objects for a particular\r
45  * unmarshalled Document instance.\r
46  * \r
47  * @author jimp\r
48  */\r
49 \r
50 public class SimpleDocBinding {\r
51 \r
52   protected VorbaIdFactory vorba;\r
53 \r
54   protected static Log log = LogFactory.getLog(SimpleDocBinding.class);\r
55 \r
56   /**\r
57    * @return Returns the vorba.\r
58    */\r
59   public VorbaIdFactory getVorba() {\r
60     return vorba;\r
61   }\r
62 \r
63   /**\r
64    * @param vorba\r
65    *          The vorba to set.\r
66    */\r
67   public void setVorba(VorbaIdFactory vorba) {\r
68     this.vorba = vorba;\r
69   }\r
70 \r
71   /**\r
72    * Uses VorbaXmlBinder to retrieve the VamsasDocument from the given stream\r
73    */\r
74   public VamsasDocument getVamsasDocument(VamsasArchiveReader oReader)\r
75       throws IOException, org.exolab.castor.xml.MarshalException,\r
76       org.exolab.castor.xml.ValidationException {\r
77     if (oReader != null) {\r
78       // check the factory\r
79       if (vorba == null) {\r
80         log\r
81             .error("Invalid SimpleDocument construction - no VorbaIdFactory defined!");\r
82         return null;\r
83       }\r
84 \r
85       if (oReader.isValid()) {\r
86         // Read vamsasDocument.xsd instance\r
87         InputStreamReader vdoc = new InputStreamReader(oReader\r
88             .getVamsasDocumentStream());\r
89         Object unmarsh[] = VorbaXmlBinder.getVamsasObjects(vdoc, vorba,\r
90             new VamsasDocument());\r
91         if (unmarsh == null)\r
92           log.fatal("Couldn't unmarshall document!");\r
93 \r
94         Vobject vobjs = (Vobject) unmarsh[0];\r
95         if (vobjs != null) {\r
96           VamsasDocument doc = (VamsasDocument) vobjs;\r
97           if (doc != null)\r
98             return doc;\r
99         }\r
100         log\r
101             .debug("Found no VamsasDocument object in properly formatted Vamsas Archive.");\r
102       } else {\r
103         // deprecated data handler (vamsas.xsd instance)\r
104         InputStream vxmlis = oReader.getVamsasXmlStream();\r
105         if (vxmlis != null) { // Might be an old vamsas file.\r
106           BufferedInputStream ixml = new BufferedInputStream(oReader\r
107               .getVamsasXmlStream());\r
108           InputStreamReader vxml = new InputStreamReader(ixml);\r
109           Object unmarsh[] = VorbaXmlBinder.getVamsasObjects(vxml, vorba,\r
110               new VAMSAS());\r
111 \r
112           if (unmarsh == null)\r
113             log.fatal("Couldn't unmarshall document!");\r
114 \r
115           VAMSAS root[] = new VAMSAS[] { null };\r
116           root[0] = (VAMSAS) unmarsh[0];\r
117 \r
118           if (root[0] == null) {\r
119             log.debug("Found no VAMSAS object in VamsasXML stream.");\r
120           } else {\r
121             log.debug("Making new VamsasDocument from VamsasXML stream.");\r
122             VamsasDocument doc = DocumentStuff.newVamsasDocument(root,\r
123                 ProvenanceStuff.newProvenance(vorba.getUserHandle()\r
124                     .getFullName(),\r
125                     "Vamsas Document constructed from vamsas.xml"),\r
126                 VersionEntries.ALPHA_VERSION);\r
127             // VAMSAS: decide on 'system' operations provenance form\r
128             // LATER: implement classes for translating Vorba properties into\r
129             // provenance user fields.\r
130             // VAMSAS: decide on machine readable info embedding in provenance\r
131             // should be done\r
132             root[0] = null;\r
133             root = null;\r
134             return doc;\r
135           }\r
136         }\r
137       }\r
138     }\r
139     // otherwise - there was no valid original document to read.\r
140     return null;\r
141   }\r
142 \r
143   /**\r
144    * Extract all jarEntries in an archive referenced by the vamsas document\r
145    * LATER: a family of methods for finding extraneous jarEntries , and invalid\r
146    * appDataReferences\r
147    * \r
148    * @param doc\r
149    * @param oReader\r
150    * @return array of the subset of JarEntry names that are referenced in doc\r
151    */\r
152   public Vector getReferencedEntries(VamsasDocument doc,\r
153       VamsasArchiveReader oReader) {\r
154     if (oReader == null)\r
155       return null;\r
156     if (doc == null) {\r
157       try {\r
158         doc = getVamsasDocument(oReader);\r
159       } catch (Exception e) {\r
160         log.warn("Failed to get document from " + oReader.jfileName);\r
161       }\r
162       ;\r
163     }\r
164     Vector docrefs = AppDataReference.getAppDataReferences(doc);\r
165     if (docrefs == null)\r
166       return null;\r
167     Vector entries = oReader.getExtraEntries();\r
168     if (entries != null && entries.size() > 0 && docrefs.size() > 0) {\r
169       int i = 0, j = entries.size();\r
170       do {\r
171         if (!docrefs.contains(entries.get(i))) {\r
172           entries.remove(i);\r
173           j--;\r
174         } else\r
175           i++;\r
176       } while (i < j);\r
177     }\r
178     return entries;\r
179   }\r
180 }\r