8f1e0eaede31f7a70a416760a3770c6da4194a17
[vamsas.git] / src / org / vamsas / client / simpleclient / VamsasArchiveReader.java
1 package org.vamsas.client.simpleclient;
2
3 import java.io.File;
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;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 /**
18  * Basic methods for accessing an existing Vamsas Archive, 
19  * and Jar entry names for creating new vamsas archives.
20  * 
21  * @author jimp
22  *
23  */
24 public class VamsasArchiveReader {
25   private static Log log = LogFactory.getLog(VamsasArchiveReader.class);
26   JarFile jfile;
27   boolean stream=false; // true if we are seeking on the stream.
28   RandomAccessFile rfile; 
29   JarInputStream jstream;
30   public VamsasArchiveReader(File vamsasfile) {
31     jfile=null;
32     if (vamsasfile.exists()) {
33       try {
34         jfile=new JarFile(vamsasfile);
35       }
36       catch (Exception e) {
37         log.debug("non-serious? couldn't open new JarFile on "+vamsasfile,e);
38         jfile=null;
39       }
40     }
41     
42   }
43   /**
44    * in an ideal world - this constructor will create a reader object
45    * for the locked file's random access stream.
46    * 
47    * @param vamsaslock
48    */
49   public VamsasArchiveReader(Lock vamsaslock) {
50     rfile = vamsaslock.rafile;
51     stream = true;
52     // TODO: Implement stream based JarFile access
53     log.error("NOT IMPLEMENTED STREAM-BASED JAR ACCESS");
54     throw new Error("Can't access a locked VamsasArchive file as a random access stream yet.");
55     // rfile.seek(0);
56     
57   }
58   /**
59    * the vamsas document version(s) handled by this Reader
60    */
61   final public static String DOCUMENT_VERSION="0.1"; 
62   /**
63    * name of the jarEntry containing a well formatted vamsas XML Document
64    */
65   
66   final public static String VAMSASDOC="vamsasDocument.xml";
67   
68   /**
69    * name of the jarEntry containing a root VAMSAS element, and containing a 
70    * random sequence of VAMSAS DataSet elements 
71    */
72   
73   final public static String VAMSASXML="vamsas.xml";
74   
75   /**
76    * 
77    * @return JarEntry for VamsasArchiveReader.VAMSASDOC
78    */
79   protected JarEntry getVamsasDocumentEntry() {
80     if (jfile!=null)
81       return jfile.getJarEntry(VAMSASDOC);
82     return null;
83   }
84   /**
85    * 
86    * @return JarEntry for VamsasArchiveReader.VAMSASXML
87    */
88   protected JarEntry getVamsasXmlEntry() {
89     if (jfile!=null)
90       return jfile.getJarEntry(VAMSASXML);
91     return null;
92   }
93   /**
94    * Test for valid vamsas document archive
95    * @return true if getVamsasDocumentStream will return a stream likely to contain valid XML
96    */
97   public boolean isValid() {
98     if (jfile!=null)
99       // 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
100       return (getVamsasDocumentEntry()!=null);
101     return false;   
102   }
103
104   
105   protected JarEntry getAppdataEntry(String AppdataRef) {
106     JarEntry entry;
107     if (jfile==null || !isValid() || (entry=jfile.getJarEntry(AppdataRef))==null)
108       return null;
109     return entry;
110   }
111   
112   public InputStream getAppdataStream(String AppdataRef) {
113     JarEntry entry=getAppdataEntry(AppdataRef);
114     try {
115       if (entry!=null)
116         return jfile.getInputStream(entry);
117     } catch (IOException e) {
118       System.err.println("Failed when opening AppdataStream for "+AppdataRef);
119       e.printStackTrace(System.err);
120     }
121     return null;
122   }
123   /**
124    * get the VamsasDocument input stream, if it exists.
125    * @return null or valid input stream
126    */
127   public InputStream getVamsasDocumentStream() {
128     InputStream vdoc;
129     if (jfile==null || !isValid())
130       return null;
131     try {
132       vdoc = jfile.getInputStream(getVamsasDocumentEntry());
133     } catch (IOException e) {
134       e.printStackTrace(System.err);
135       vdoc=null;
136     }
137     return vdoc;
138   }
139   
140   /**
141    * get the VamsasXML input stream, if it exists.
142    * Note: Deprecated beyond our prealpha testing.
143    * @return null or valid input stream.
144    */
145   
146   public InputStream getVamsasXmlStream() {
147     // log.warn("Deprecated call");
148     JarEntry xmle=getVamsasXmlEntry();
149     InputStream vdoc;
150     if (xmle==null)
151       return null;
152     try {
153       vdoc = jfile.getInputStream(xmle);
154     } catch (IOException e) {
155       e.printStackTrace(System.err);
156       vdoc=null;
157     }
158     return vdoc;
159   }
160   
161   /**
162    * silently close the jar file.
163    *
164    */
165   public void close() {
166     if (jfile!=null) {
167       try {
168         jfile.close();
169       } catch (IOException e) {
170         e.printStackTrace(System.err);
171       }
172     }
173   }
174   
175   /**
176    * returns all entries not matching the filespec of a vamsas xml entry
177    * @return array of entries.
178    */
179   public Vector getExtraEntries() {
180     if (jfile==null || !isValid())
181       return null;
182     Enumeration entries = jfile.entries();
183     if (entries!=null && entries.hasMoreElements()) {
184       Vector e = new Vector();
185       do {
186         JarEntry el = (JarEntry) entries.nextElement();
187         if (!el.getName().equals(VAMSASDOC) && !el.getName().equals(VAMSASXML))
188           e.add(new String(el.getName())); // avoid references
189       } while (entries.hasMoreElements());
190       return e;
191     }
192     return null;
193   }
194 }