From: jprocter Date: Thu, 3 Nov 2005 14:10:01 +0000 (+0000) Subject: Vamsas Archive Reader and simple test class. X-Git-Tag: Release_0.2~433 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=8c0230fceb94cba911790b1622b030d02eb0e7ac;p=vamsas.git Vamsas Archive Reader and simple test class. git-svn-id: https://svn.lifesci.dundee.ac.uk/svn/repository/trunk@87 be28352e-c001-0410-b1a7-c7978e42abec --- diff --git a/src/org/vamsas/client/simpleclient/VamsasArchiveReader.java b/src/org/vamsas/client/simpleclient/VamsasArchiveReader.java new file mode 100644 index 0000000..3ca1288 --- /dev/null +++ b/src/org/vamsas/client/simpleclient/VamsasArchiveReader.java @@ -0,0 +1,125 @@ +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); + } + } + } +} diff --git a/src/org/vamsas/test/simpleclient/ArchiveReader.java b/src/org/vamsas/test/simpleclient/ArchiveReader.java new file mode 100644 index 0000000..2156c51 --- /dev/null +++ b/src/org/vamsas/test/simpleclient/ArchiveReader.java @@ -0,0 +1,72 @@ +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") + +") 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); + } + } +}