reorganising vamsasArchive (write to temp file, rather than write to original filename)
[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.util.Iterator;
7 import java.util.jar.JarEntry;
8 import java.util.jar.JarFile;
9 import java.util.jar.JarInputStream;
10 import java.util.jar.JarOutputStream;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14 /**
15  * Basic methods for accessing an existing Vamsas Archive, 
16  * and Jar entry names for creating new vamsas archives.
17  * 
18  * @author jimp
19  *
20  */
21 public class VamsasArchiveReader {
22   private static Log log = LogFactory.getLog(VamsasArchiveReader.class);
23   JarFile jfile;
24   public VamsasArchiveReader(File vamsasfile) {
25     jfile=null;
26     if (vamsasfile.exists()) {
27       try {
28         jfile=new JarFile(vamsasfile);
29       }
30       catch (Exception e) {
31         jfile=null;
32       }
33     }
34     
35   }
36   /**
37    * the vamsas document version(s) handled by this Reader
38    */
39   final public static String DOCUMENT_VERSION="0.1"; 
40   /**
41    * name of the jarEntry containing a well formatted vamsas XML Document
42    */
43   
44   final public static String VAMSASDOC="vamsasDocument.xml";
45   
46   /**
47    * name of the jarEntry containing a root VAMSAS element, and containing a 
48    * random sequence of VAMSAS DataSet elements 
49    */
50   
51   final public static String VAMSASXML="vamsas.xml";
52   
53   /**
54    * 
55    * @return JarEntry for VamsasArchiveReader.VAMSASDOC
56    */
57   protected JarEntry getVamsasDocumentEntry() {
58     if (jfile!=null)
59       return jfile.getJarEntry(VAMSASDOC);
60     return null;
61   }
62   /**
63    * 
64    * @return JarEntry for VamsasArchiveReader.VAMSASXML
65    */
66   protected JarEntry getVamsasXmlEntry() {
67     if (jfile!=null)
68       return jfile.getJarEntry(VAMSASXML);
69     return null;
70   }
71   /**
72    * Test for valid vamsas document archive
73    * @return true if getVamsasDocumentStream will return a stream likely to contain valid XML
74    */
75   public boolean isValid() {
76     if (jfile!=null)
77       // 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
78       return (getVamsasDocumentEntry()!=null);
79     return false;   
80   }
81
82   
83   protected JarEntry getAppdataEntry(String AppdataRef) {
84     JarEntry entry;
85     if (jfile==null || !isValid() || (entry=jfile.getJarEntry(AppdataRef))==null)
86       return null;
87     return entry;
88   }
89   
90   public InputStream getAppdataStream(String AppdataRef) {
91     JarEntry entry=getAppdataEntry(AppdataRef);
92     try {
93       if (entry!=null)
94         return jfile.getInputStream(entry);
95     } catch (IOException e) {
96       System.err.println("Failed when opening AppdataStream for "+AppdataRef);
97       e.printStackTrace(System.err);
98     }
99     return null;
100   }
101   /**
102    * get the VamsasDocument input stream, if it exists.
103    * @return null or valid input stream
104    */
105   public InputStream getVamsasDocumentStream() {
106     InputStream vdoc;
107     if (jfile==null || !isValid())
108       return null;
109     try {
110       vdoc = jfile.getInputStream(getVamsasDocumentEntry());
111     } catch (IOException e) {
112       e.printStackTrace(System.err);
113       vdoc=null;
114     }
115     return vdoc;
116   }
117   
118   /**
119    * get the VamsasXML input stream, if it exists.
120    * Note: Deprecated beyond our prealpha testing.
121    * @return null or valid input stream.
122    */
123   
124   public InputStream getVamsasXmlStream() {
125     // log.warn("Deprecated call");
126     JarEntry xmle=getVamsasXmlEntry();
127     InputStream vdoc;
128     if (xmle==null)
129       return null;
130     try {
131       vdoc = jfile.getInputStream(xmle);
132     } catch (IOException e) {
133       e.printStackTrace(System.err);
134       vdoc=null;
135     }
136     return vdoc;
137   }
138   
139   /**
140    * silently close the jar file.
141    *
142    */
143   public void close() {
144     if (jfile!=null) {
145       try {
146         jfile.close();
147       } catch (IOException e) {
148         e.printStackTrace(System.err);
149       }
150     }
151   }
152 }