1 package org.vamsas.client.simpleclient;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.RandomAccessFile;
7 import java.util.Enumeration;
8 import java.util.Iterator;
9 import java.util.Vector;
10 import java.util.jar.JarEntry;
11 import java.util.jar.JarFile;
12 import java.util.jar.JarInputStream;
13 import java.util.jar.JarOutputStream;
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
18 * Basic methods for accessing an existing Vamsas Archive,
19 * and Jar entry names for creating new vamsas archives.
24 public class VamsasArchiveReader {
25 private static Log log = LogFactory.getLog(VamsasArchiveReader.class);
27 boolean stream=false; // true if we are seeking on the stream.
28 RandomAccessFile rfile;
29 JarInputStream jstream;
30 public VamsasArchiveReader(File vamsasfile) {
32 if (vamsasfile.exists()) {
34 jfile=new JarFile(vamsasfile);
43 * in an ideal world - this constructor will create a reader object
44 * for the locked file's random access stream.
48 public VamsasArchiveReader(Lock vamsaslock) {
49 rfile = vamsaslock.rafile;
51 // TODO: Implement stream based JarFile access
52 log.error("NOT IMPLEMENTED STREAM-BASED JAR ACCESS");
53 throw new Error("Can't access a locked VamsasArchive file as a random access stream yet.");
58 * the vamsas document version(s) handled by this Reader
60 final public static String DOCUMENT_VERSION="0.1";
62 * name of the jarEntry containing a well formatted vamsas XML Document
65 final public static String VAMSASDOC="vamsasDocument.xml";
68 * name of the jarEntry containing a root VAMSAS element, and containing a
69 * random sequence of VAMSAS DataSet elements
72 final public static String VAMSASXML="vamsas.xml";
76 * @return JarEntry for VamsasArchiveReader.VAMSASDOC
78 protected JarEntry getVamsasDocumentEntry() {
80 return jfile.getJarEntry(VAMSASDOC);
85 * @return JarEntry for VamsasArchiveReader.VAMSASXML
87 protected JarEntry getVamsasXmlEntry() {
89 return jfile.getJarEntry(VAMSASXML);
93 * Test for valid vamsas document archive
94 * @return true if getVamsasDocumentStream will return a stream likely to contain valid XML
96 public boolean isValid() {
98 // 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
99 return (getVamsasDocumentEntry()!=null);
104 protected JarEntry getAppdataEntry(String AppdataRef) {
106 if (jfile==null || !isValid() || (entry=jfile.getJarEntry(AppdataRef))==null)
111 public InputStream getAppdataStream(String AppdataRef) {
112 JarEntry entry=getAppdataEntry(AppdataRef);
115 return jfile.getInputStream(entry);
116 } catch (IOException e) {
117 System.err.println("Failed when opening AppdataStream for "+AppdataRef);
118 e.printStackTrace(System.err);
123 * get the VamsasDocument input stream, if it exists.
124 * @return null or valid input stream
126 public InputStream getVamsasDocumentStream() {
128 if (jfile==null || !isValid())
131 vdoc = jfile.getInputStream(getVamsasDocumentEntry());
132 } catch (IOException e) {
133 e.printStackTrace(System.err);
140 * get the VamsasXML input stream, if it exists.
141 * Note: Deprecated beyond our prealpha testing.
142 * @return null or valid input stream.
145 public InputStream getVamsasXmlStream() {
146 // log.warn("Deprecated call");
147 JarEntry xmle=getVamsasXmlEntry();
152 vdoc = jfile.getInputStream(xmle);
153 } catch (IOException e) {
154 e.printStackTrace(System.err);
161 * silently close the jar file.
164 public void close() {
168 } catch (IOException e) {
169 e.printStackTrace(System.err);
175 * returns all entries not matching the filespec of a vamsas xml entry
176 * @return array of entries.
178 public Vector getExtraEntries() {
179 if (jfile==null || !isValid())
181 Enumeration entries = jfile.entries();
182 if (entries!=null && entries.hasMoreElements()) {
183 Vector e = new Vector();
185 JarEntry el = (JarEntry) entries.nextElement();
186 if (!el.getName().equals(VAMSASDOC) && !el.getName().equals(VAMSASXML))
187 e.add(new String(el.getName())); // avoid references
188 } while (entries.hasMoreElements());