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); } } } }