1 package org.vamsas.client.simpleclient;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.Enumeration;
7 import java.util.Iterator;
8 import java.util.Vector;
9 import java.util.jar.JarEntry;
10 import java.util.jar.JarFile;
11 import java.util.jar.JarInputStream;
12 import java.util.jar.JarOutputStream;
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
17 * Basic methods for accessing an existing Vamsas Archive,
18 * and Jar entry names for creating new vamsas archives.
23 public class VamsasArchiveReader {
24 private static Log log = LogFactory.getLog(VamsasArchiveReader.class);
26 public VamsasArchiveReader(File vamsasfile) {
28 if (vamsasfile.exists()) {
30 jfile=new JarFile(vamsasfile);
39 * the vamsas document version(s) handled by this Reader
41 final public static String DOCUMENT_VERSION="0.1";
43 * name of the jarEntry containing a well formatted vamsas XML Document
46 final public static String VAMSASDOC="vamsasDocument.xml";
49 * name of the jarEntry containing a root VAMSAS element, and containing a
50 * random sequence of VAMSAS DataSet elements
53 final public static String VAMSASXML="vamsas.xml";
57 * @return JarEntry for VamsasArchiveReader.VAMSASDOC
59 protected JarEntry getVamsasDocumentEntry() {
61 return jfile.getJarEntry(VAMSASDOC);
66 * @return JarEntry for VamsasArchiveReader.VAMSASXML
68 protected JarEntry getVamsasXmlEntry() {
70 return jfile.getJarEntry(VAMSASXML);
74 * Test for valid vamsas document archive
75 * @return true if getVamsasDocumentStream will return a stream likely to contain valid XML
77 public boolean isValid() {
79 // 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
80 return (getVamsasDocumentEntry()!=null);
85 protected JarEntry getAppdataEntry(String AppdataRef) {
87 if (jfile==null || !isValid() || (entry=jfile.getJarEntry(AppdataRef))==null)
92 public InputStream getAppdataStream(String AppdataRef) {
93 JarEntry entry=getAppdataEntry(AppdataRef);
96 return jfile.getInputStream(entry);
97 } catch (IOException e) {
98 System.err.println("Failed when opening AppdataStream for "+AppdataRef);
99 e.printStackTrace(System.err);
104 * get the VamsasDocument input stream, if it exists.
105 * @return null or valid input stream
107 public InputStream getVamsasDocumentStream() {
109 if (jfile==null || !isValid())
112 vdoc = jfile.getInputStream(getVamsasDocumentEntry());
113 } catch (IOException e) {
114 e.printStackTrace(System.err);
121 * get the VamsasXML input stream, if it exists.
122 * Note: Deprecated beyond our prealpha testing.
123 * @return null or valid input stream.
126 public InputStream getVamsasXmlStream() {
127 // log.warn("Deprecated call");
128 JarEntry xmle=getVamsasXmlEntry();
133 vdoc = jfile.getInputStream(xmle);
134 } catch (IOException e) {
135 e.printStackTrace(System.err);
142 * silently close the jar file.
145 public void close() {
149 } catch (IOException e) {
150 e.printStackTrace(System.err);
156 * returns all entries not matching the filespec of a vamsas xml entry
157 * @return array of entries.
159 public Vector getExtraEntries() {
160 if (jfile==null || !isValid())
162 Enumeration entries = jfile.entries();
163 if (entries!=null && entries.hasMoreElements()) {
164 Vector e = new Vector();
166 JarEntry el = (JarEntry) entries.nextElement();
167 if (!el.getName().equals(VAMSASDOC) && !el.getName().equals(VAMSASXML))
168 e.add(new String(el.getName())); // avoid references
169 } while (entries.hasMoreElements());