+ /**
+ * Access and return current vamsas Document, if it exists, or create a new one
+ * (without affecting VamsasArchive object state - so is NOT THREAD SAFE)
+ * TODO: possibly modify internal state to lock low-level files
+ * (like the IClientDocument interface instance constructer would do)
+ * @see org.vamsas.simpleclient.VamsasArchive.getOriginalVamsasDocument for additional caveats
+ *
+ * @return
+ * @throws IOException
+ * @throws org.exolab.castor.xml.MarshalException
+ * @throws org.exolab.castor.xml.ValidationException
+ */
+ public VamsasDocument getVamsasDocument() throws IOException,
+ org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
+ VamsasDocument doc = getOriginalVamsasDocument(this);
+ if (doc!=null)
+ return doc;
+ // Create a new document and return it
+ doc = DocumentStuff.newVamsasDocument(new VAMSAS[] { new VAMSAS()},
+ ProvenanceStuff.newProvenance("org.vamsas.simpleclient.VamsasArchive", "Created new empty document")
+ , VersionEntries.latestVersion());
+ return doc;
+ }
+ /**
+ * Access the original vamsas document for a VamsasArchive class, and return it.
+ * Users of the VamsasArchive class should use the getVamsasDocument method to retrieve
+ * the current document - only use this one if you want the 'backup' version.
+ * TODO: catch OutOfMemoryError - they are likely to occur here.
+ * NOTE: vamsas.xml datastreams are constructed as 'ALPHA_VERSION' vamsas documents.
+ * @param ths
+ * @return null if no document exists.
+ * @throws IOException
+ * @throws org.exolab.castor.xml.MarshalException
+ * @throws org.exolab.castor.xml.ValidationException
+ */
+ public static VamsasDocument getOriginalVamsasDocument(VamsasArchive ths) throws IOException,
+ org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException {
+ VamsasArchiveReader oReader = ths.getOriginalArchiveReader();
+ if (oReader!=null) {
+ if (oReader.isValid()) {
+ InputStreamReader vdoc = new InputStreamReader(oReader.getVamsasDocumentStream());
+ VamsasDocument doc = VamsasDocument.unmarshal(vdoc);
+ if (doc!=null)
+ return doc;
+ } else {
+ // deprecated data handler
+ InputStream vxmlis = oReader.getVamsasXmlStream();
+ if (vxmlis!=null) { // Might be an old vamsas file.
+ BufferedInputStream ixml = new BufferedInputStream(oReader.getVamsasXmlStream());
+ InputStreamReader vxml = new InputStreamReader(ixml);
+ VAMSAS root[] = new VAMSAS[1];
+ root[0] = VAMSAS.unmarshal(vxml);
+ if (root[0]!=null) {
+ log.debug("Reading old format vamsas.xml into a dummy document.");
+ VamsasDocument doc = DocumentStuff.newVamsasDocument(root,
+ ProvenanceStuff.newProvenance(
+ "org.vamsas.simpleclient.VamsasArchive", // TODO: VAMSAS: decide on 'system' operations provenance form
+ "Vamsas Document constructed from vamsas.xml in <file>"
+ // TODO: VAMSAS: decide on machine readable info embedding in provenance should be done
+ +ths.original+"</file>"), VersionEntries.ALPHA_VERSION);
+ root[0]=null;
+ root=null;
+ return doc;
+ }
+ }
+ }
+ } // otherwise - there was no valid original document to read.
+ return null;
+ }