--- /dev/null
+package org.vamsas.client.simpleclient;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Iterator;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.jar.JarInputStream;
+import java.util.jar.JarOutputStream;
+/**
+ * Basic methods for accessing an existing Vamsas Archive,
+ * and Jar entry names for creating new vamsas archives.
+ *
+ * @author jimp
+ *
+ */
+public class VamsasArchiveReader {
+ JarFile jfile;
+ public VamsasArchiveReader(File vamsasfile) {
+ jfile=null;
+ if (vamsasfile.exists()) {
+ try {
+ jfile=new JarFile(vamsasfile);
+ }
+ catch (Exception e) {
+ jfile=null;
+ }
+ }
+
+ }
+
+ /**
+ * name of the jarEntry containing a well formatted vamsas XML Document
+ */
+
+ final public static String VAMSASDOC="vamsasDocument.xml";
+
+ /**
+ * name of the jarEntry containing a root VAMSAS element, and containing a
+ * random sequence of VAMSAS DataSet elements
+ */
+
+ final public static String VAMSASXML="vamsas.xml";
+
+ /**
+ *
+ * @return JarEntry for VamsasArchiveReader.VAMSASDOC
+ */
+ protected JarEntry getVamsasDocumentEntry() {
+ if (jfile!=null)
+ return jfile.getJarEntry(VAMSASDOC);
+ return null;
+ }
+ /**
+ *
+ * @return JarEntry for VamsasArchiveReader.VAMSASXML
+ */
+ protected JarEntry getVamsasXmlEntry() {
+ if (jfile!=null)
+ return jfile.getJarEntry(VAMSASXML);
+ return null;
+ }
+ /**
+ * Test for valid vamsas document archive
+ * @return true if getVamsasDocumentStream will return a stream likely to contain valid XML
+ */
+ public boolean isValid() {
+ if (jfile!=null)
+ // TODO: check if VAMSASDOC is well formed, follows www.vamsas.ac.uk/schemas/vamsasDocument.xsd, and all appData references are resolvable - preferably as jar entries
+ return (getVamsasDocumentEntry()!=null);
+ return false;
+ }
+
+ /**
+ * get the VamsasDocument input stream, if it exists.
+ * @return null or valid input stream
+ */
+ public InputStream getVamsasDocumentStream() {
+ InputStream vdoc;
+ if (jfile==null || !isValid())
+ return null;
+ try {
+ vdoc = jfile.getInputStream(getVamsasDocumentEntry());
+ } catch (IOException e) {
+ e.printStackTrace(System.err);
+ vdoc=null;
+ }
+ return vdoc;
+ }
+
+ /**
+ * get the VamsasXML input stream, if it exists.
+ * Note: Deprecated beyond our prealpha testing.
+ * @return null or valid input stream.
+ */
+
+ public InputStream getVamsasXmlStream() {
+ JarEntry xmle=getVamsasXmlEntry();
+ InputStream vdoc;
+ if (xmle==null)
+ return null;
+ try {
+ vdoc = jfile.getInputStream(xmle);
+ } catch (IOException e) {
+ e.printStackTrace(System.err);
+ vdoc=null;
+ }
+ return vdoc;
+ }
+
+ /**
+ * silently close the jar file.
+ *
+ */
+ public void close() {
+ if (jfile!=null) {
+ try {
+ jfile.close();
+ } catch (IOException e) {
+ e.printStackTrace(System.err);
+ }
+ }
+ }
+}
--- /dev/null
+package org.vamsas.test.simpleclient;
+
+import java.io.BufferedInputStream;
+import java.io.File;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.Vector;
+
+import org.vamsas.client.simpleclient.VamsasArchiveReader;
+import org.vamsas.objects.core.VAMSAS;
+import org.vamsas.objects.core.VamsasDocument;
+
+public class ArchiveReader {
+ public static boolean reportDocument(VamsasDocument document) {
+ if (document!=null) {
+ System.out.print("Vamsas Document version '"+document.getVersion()+"'");
+ System.out.print("Document contains "+document.getVAMSASCount()+" VAMSAS Elements and "+document.getApplicationDataCount()+" elements.\n");
+ return true;
+ } else {
+ System.out.print("Document Object is null");
+ }
+ return false;
+ }
+
+ public static boolean rootReport(VAMSAS[] roots) {
+ if (roots!=null) {
+ for (int i=0; i<roots.length; i++) {
+ VAMSAS r = roots[i];
+ System.out.print("Vamsas Root "+i+" (id="
+ +((r.getId()!=null) ? r.getId():"<none>")
+ +") contains "+r.getDataSetCount()+" DataSets, "
+ + r.getTreeCount()+" Global trees");
+ }
+ return true;
+ }
+ return false;
+ }
+
+ public static void main(String args[]) {
+
+ try {
+ File av = new File(args[0]);
+ VamsasArchiveReader var = new VamsasArchiveReader(av);
+ VAMSAS roots[]=null;
+ if (var.isValid()) {
+ InputStreamReader vdoc = new InputStreamReader(var.getVamsasDocumentStream());
+ VamsasDocument doc = VamsasDocument.unmarshal(vdoc);
+ if (reportDocument(doc)) {
+ roots = doc.getVAMSAS();
+ }
+ } else {
+ InputStream vxmlis = var.getVamsasXmlStream();
+
+ if (vxmlis!=null) { // Might be an old vamsas file.
+ BufferedInputStream ixml = new BufferedInputStream(var.getVamsasXmlStream());
+ InputStreamReader vxml = new InputStreamReader(ixml);
+ VAMSAS root;
+ // unmarshal seems to always close the stream (should check this)
+ if ((root = VAMSAS.unmarshal(vxml))!=null) {
+ System.out.println("Read a root.");
+ roots = new VAMSAS[1];
+ roots[0] = root;
+ }
+ }
+ }
+ if (!rootReport(roots))
+ System.err.print(args[0]+" is not a valid vamsas archive.");
+ } catch (Exception e) {
+ e.printStackTrace(System.err);
+ }
+ }
+}