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