3b0313705db8dd687af2c98835147304024265dc
[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         jfile=null;
38       }
39     }
40     
41   }
42   /**
43    * in an ideal world - this constructor will create a reader object
44    * for the locked file's random access stream.
45    * 
46    * @param vamsaslock
47    */
48   public VamsasArchiveReader(Lock vamsaslock) {
49     rfile = vamsaslock.rafile;
50     stream = true;
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.");
54     // rfile.seek(0);
55     
56   }
57   /**
58    * the vamsas document version(s) handled by this Reader
59    */
60   final public static String DOCUMENT_VERSION="0.1"; 
61   /**
62    * name of the jarEntry containing a well formatted vamsas XML Document
63    */
64   
65   final public static String VAMSASDOC="vamsasDocument.xml";
66   
67   /**
68    * name of the jarEntry containing a root VAMSAS element, and containing a 
69    * random sequence of VAMSAS DataSet elements 
70    */
71   
72   final public static String VAMSASXML="vamsas.xml";
73   
74   /**
75    * 
76    * @return JarEntry for VamsasArchiveReader.VAMSASDOC
77    */
78   protected JarEntry getVamsasDocumentEntry() {
79     if (jfile!=null)
80       return jfile.getJarEntry(VAMSASDOC);
81     return null;
82   }
83   /**
84    * 
85    * @return JarEntry for VamsasArchiveReader.VAMSASXML
86    */
87   protected JarEntry getVamsasXmlEntry() {
88     if (jfile!=null)
89       return jfile.getJarEntry(VAMSASXML);
90     return null;
91   }
92   /**
93    * Test for valid vamsas document archive
94    * @return true if getVamsasDocumentStream will return a stream likely to contain valid XML
95    */
96   public boolean isValid() {
97     if (jfile!=null)
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);
100     return false;   
101   }
102
103   
104   protected JarEntry getAppdataEntry(String AppdataRef) {
105     JarEntry entry;
106     if (jfile==null || !isValid() || (entry=jfile.getJarEntry(AppdataRef))==null)
107       return null;
108     return entry;
109   }
110   
111   public InputStream getAppdataStream(String AppdataRef) {
112     JarEntry entry=getAppdataEntry(AppdataRef);
113     try {
114       if (entry!=null)
115         return jfile.getInputStream(entry);
116     } catch (IOException e) {
117       System.err.println("Failed when opening AppdataStream for "+AppdataRef);
118       e.printStackTrace(System.err);
119     }
120     return null;
121   }
122   /**
123    * get the VamsasDocument input stream, if it exists.
124    * @return null or valid input stream
125    */
126   public InputStream getVamsasDocumentStream() {
127     InputStream vdoc;
128     if (jfile==null || !isValid())
129       return null;
130     try {
131       vdoc = jfile.getInputStream(getVamsasDocumentEntry());
132     } catch (IOException e) {
133       e.printStackTrace(System.err);
134       vdoc=null;
135     }
136     return vdoc;
137   }
138   
139   /**
140    * get the VamsasXML input stream, if it exists.
141    * Note: Deprecated beyond our prealpha testing.
142    * @return null or valid input stream.
143    */
144   
145   public InputStream getVamsasXmlStream() {
146     // log.warn("Deprecated call");
147     JarEntry xmle=getVamsasXmlEntry();
148     InputStream vdoc;
149     if (xmle==null)
150       return null;
151     try {
152       vdoc = jfile.getInputStream(xmle);
153     } catch (IOException e) {
154       e.printStackTrace(System.err);
155       vdoc=null;
156     }
157     return vdoc;
158   }
159   
160   /**
161    * silently close the jar file.
162    *
163    */
164   public void close() {
165     if (jfile!=null) {
166       try {
167         jfile.close();
168       } catch (IOException e) {
169         e.printStackTrace(System.err);
170       }
171     }
172   }
173   
174   /**
175    * returns all entries not matching the filespec of a vamsas xml entry
176    * @return array of entries.
177    */
178   public Vector getExtraEntries() {
179     if (jfile==null || !isValid())
180       return null;
181     Enumeration entries = jfile.entries();
182     if (entries!=null && entries.hasMoreElements()) {
183       Vector e = new Vector();
184       do {
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());
189       return e;
190     }
191     return null;
192   }
193 }