X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Forg%2Fvamsas%2Fclient%2Fsimpleclient%2FVamsasArchiveReader.java;fp=src%2Forg%2Fvamsas%2Fclient%2Fsimpleclient%2FVamsasArchiveReader.java;h=3ca12880d946d7e14f5920a25d9504d1d7f95604;hb=8c0230fceb94cba911790b1622b030d02eb0e7ac;hp=0000000000000000000000000000000000000000;hpb=5fb0ffd7883f1723b9461a318c107b64a24de3f3;p=vamsas.git 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); + } + } + } +}