1 package org.vamsas.client.simpleclient;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.BufferedInputStream;
7 import java.io.FileInputStream;
8 import java.io.RandomAccessFile;
9 import java.util.Enumeration;
10 import java.util.Hashtable;
11 import java.util.Iterator;
12 import java.util.Vector;
13 import java.util.jar.JarEntry;
14 import java.util.jar.JarFile;
15 import java.util.jar.JarInputStream;
16 import java.util.jar.JarOutputStream;
17 import java.util.zip.ZipEntry;
18 import java.util.zip.ZipInputStream;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.vamsas.objects.utils.document.VersionEntries;
24 * Basic methods for accessing an existing Vamsas Archive,
25 * and Jar entry names for creating new vamsas archives.
30 public class VamsasArchiveReader {
31 private static Log log = LogFactory.getLog(VamsasArchiveReader.class);
33 boolean stream=false; // true if we are seeking on the stream.
34 RandomAccessFile rfile;
35 ZipInputStream jstream=null;
36 Hashtable strmentries = null;
37 private void streamInit() {
38 throw new Error("VamsasArchiveReader(Stream) Not implemented!");
40 log.debug("Skipping init for Jar Stream input.");
43 strmentries = new Hashtable();
44 log.debug("Jar Stream input Initialisation");
47 // no buffering - we need to be able to move around the random access stream.
48 jstream = new ZipInputStream(new FileInputStream(rfile.getFD())); // no manifest (probably)
49 if (jstream.available()==0)
50 log.warn("Can't read from JarInputStream (Locked stream!)");
54 if ((entry=jstream.getNextEntry())!=null) {
55 if (strmentries.containsKey(entry.getName())) {
56 log.info("Only recording last of duplicate entries '"+entry.getName()+"'");
58 strmentries.put(entry.getName(), new Long(pos++));
61 } while (entry!=null);
64 log.warn("Exceptions during init!",e);
69 public VamsasArchiveReader(File vamsasfile) {
71 if (vamsasfile.exists()) {
73 jfile=new JarFile(vamsasfile);
76 log.debug("non-serious? couldn't open new JarFile on "+vamsasfile,e);
83 * in an ideal world - this constructor will create a reader object
84 * for the locked file's random access stream.
88 public VamsasArchiveReader(Lock vamsaslock) {
89 // LATER: implement or remove
90 throw new Error("VamsasArchiveReading from locked IO stream not yet implemented.");
91 //rfile = vamsaslock.rafile;
95 // throw new Error("Failed to open archive from Locked random access stream.");
99 * the vamsas document version(s) handled by this Reader
101 final public static String DOCUMENT_VERSION=VersionEntries.BETA_VERSION;
103 * name of the jarEntry containing a well formatted vamsas XML Document
106 final public static String VAMSASDOC="vamsasDocument.xml";
109 * name of the jarEntry containing a root VAMSAS element, and containing a
110 * random sequence of VAMSAS DataSet elements
113 final public static String VAMSASXML="vamsas.xml";
115 * seeks jstream to the given entry name and reads it.
119 private JarEntry seekEntry(String entryname) {
122 if (!strmentries.containsKey(entryname))
124 Long entrypos = (Long) strmentries.get(entryname);
125 if (entrypos==null) {
126 log.error("Null entry position for "+entryname);
132 jstream = new ZipInputStream(new FileInputStream(rfile.getFD()));
133 ZipEntry entry = null;
134 long epos = entrypos.longValue();
136 entry = jstream.getNextEntry();
137 } while (entry!=null && epos-->0);
138 // rfile.seek(entrypos.longValue());
139 // make a Jar entry from a zip entry.
140 return new JarEntry(entry);
142 catch (Exception e) {
143 log.warn("Whilst seeking for "+entryname, e);
149 * @return JarEntry for VamsasArchiveReader.VAMSASDOC
151 protected JarEntry getVamsasDocumentEntry() {
152 return getJarEntry(VAMSASDOC);
156 * @return JarEntry for VamsasArchiveReader.VAMSASXML
158 protected JarEntry getVamsasXmlEntry() {
159 return getJarEntry(VAMSASXML);
162 * Test for valid vamsas document archive
163 * @return true if getVamsasDocumentStream will return a stream likely to contain valid XML
165 public boolean isValid() {
166 // 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
167 if (jfile!=null || jstream!=null)
168 return (getVamsasDocumentEntry()!=null);
173 protected JarEntry getAppdataEntry(String AppdataRef) {
175 if ((jfile==null && jstream==null) || !isValid() || (entry=getJarEntry(AppdataRef))==null)
181 public InputStream getAppdataStream(String AppdataRef) {
182 JarEntry entry=getAppdataEntry(AppdataRef);
185 return getInputStream(entry);
186 } catch (IOException e) {
187 log.error("Failed when opening AppdataStream for "+AppdataRef, e);
192 * get the VamsasDocument input stream, if it exists.
193 * @return null or valid input stream
195 public InputStream getVamsasDocumentStream() {
197 if ((jfile==null && jstream==null) || !isValid())
200 vdoc = getInputStream(getVamsasDocumentEntry());
201 } catch (IOException e) {
202 log.error("Whilst geting document stream",e);
209 * get the VamsasXML input stream, if it exists.
210 * Note: Deprecated beyond our prealpha testing.
211 * @return null or valid input stream.
214 public InputStream getVamsasXmlStream() {
215 // log.warn("Deprecated call");
216 JarEntry xmle=getVamsasXmlEntry();
221 vdoc = getInputStream(xmle);
222 } catch (IOException e) {
223 log.error("Whilst getting VamsasXmlStream",e);
230 * silently close the jar file.
233 public void close() {
237 } catch (IOException e) {
238 log.error("Whilst closing JarFile "+jfile.getName(), e);
243 jstream.closeEntry();
245 // LATER: reference counting for random access file instances is necessary.
247 catch (Exception e) {
248 log.error("Whilst finishing reading from jar input stream",e);
254 * returns all entries not matching the filespec of a vamsas xml entry
255 * @return array of entries.
257 public Vector getExtraEntries() {
258 if ((jfile==null && jstream==null)|| !isValid())
260 Vector e = new Vector();
262 Enumeration entries = strmentries.keys();
263 if (entries!=null && entries.hasMoreElements()) {
265 JarEntry el = (JarEntry) entries.nextElement();
266 if (!el.getName().equals(VAMSASDOC) && !el.getName().equals(VAMSASXML))
267 e.add(new String(el.getName())); // avoid references
268 } while (entries.hasMoreElements());
271 Enumeration entries = jfile.entries();
272 if (entries!=null && entries.hasMoreElements()) {
274 JarEntry el = (JarEntry) entries.nextElement();
275 if (!el.getName().equals(VAMSASDOC) && !el.getName().equals(VAMSASXML))
276 e.add(new String(el.getName())); // avoid references
277 } while (entries.hasMoreElements());
285 * @see java.util.jar.JarFile#getInputStream(java.util.zip.ZipEntry)
287 private InputStream getInputStream(ZipEntry ze) throws IOException {
289 return jfile.getInputStream(ze);
291 seekEntry(ze.getName());
292 return new AppDataInputStream(jstream);
298 * @see java.util.jar.JarFile#getJarEntry(java.lang.String)
300 private JarEntry getJarEntry(String name) {
302 return jfile.getJarEntry(name);
304 return seekEntry(name);