Vamsas Archive Reader and simple test class.
[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  * Basic methods for accessing an existing Vamsas Archive, 
13  * and Jar entry names for creating new vamsas archives.
14  * 
15  * @author jimp
16  *
17  */
18 public class VamsasArchiveReader {
19   JarFile jfile;
20   public VamsasArchiveReader(File vamsasfile) {
21     jfile=null;
22     if (vamsasfile.exists()) {
23       try {
24         jfile=new JarFile(vamsasfile);
25       }
26       catch (Exception e) {
27         jfile=null;
28       }
29     }
30     
31   }
32   
33   /**
34    * name of the jarEntry containing a well formatted vamsas XML Document
35    */
36   
37   final public static String VAMSASDOC="vamsasDocument.xml";
38   
39   /**
40    * name of the jarEntry containing a root VAMSAS element, and containing a 
41    * random sequence of VAMSAS DataSet elements 
42    */
43   
44   final public static String VAMSASXML="vamsas.xml";
45   
46   /**
47    * 
48    * @return JarEntry for VamsasArchiveReader.VAMSASDOC
49    */
50   protected JarEntry getVamsasDocumentEntry() {
51     if (jfile!=null)
52       return jfile.getJarEntry(VAMSASDOC);
53     return null;
54   }
55   /**
56    * 
57    * @return JarEntry for VamsasArchiveReader.VAMSASXML
58    */
59   protected JarEntry getVamsasXmlEntry() {
60     if (jfile!=null)
61       return jfile.getJarEntry(VAMSASXML);
62     return null;
63   }
64   /**
65    * Test for valid vamsas document archive
66    * @return true if getVamsasDocumentStream will return a stream likely to contain valid XML
67    */
68   public boolean isValid() {
69     if (jfile!=null)
70       // 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
71       return (getVamsasDocumentEntry()!=null);
72     return false;   
73   }
74
75   /**
76    * get the VamsasDocument input stream, if it exists.
77    * @return null or valid input stream
78    */
79   public InputStream getVamsasDocumentStream() {
80     InputStream vdoc;
81     if (jfile==null || !isValid())
82       return null;
83     try {
84       vdoc = jfile.getInputStream(getVamsasDocumentEntry());
85     } catch (IOException e) {
86       e.printStackTrace(System.err);
87       vdoc=null;
88     }
89     return vdoc;
90   }
91   
92   /**
93    * get the VamsasXML input stream, if it exists.
94    * Note: Deprecated beyond our prealpha testing.
95    * @return null or valid input stream.
96    */
97   
98   public InputStream getVamsasXmlStream() {
99     JarEntry xmle=getVamsasXmlEntry();
100     InputStream vdoc;
101     if (xmle==null)
102       return null;
103     try {
104       vdoc = jfile.getInputStream(xmle);
105     } catch (IOException e) {
106       e.printStackTrace(System.err);
107       vdoc=null;
108     }
109     return vdoc;
110   }
111   
112   /**
113    * silently close the jar file.
114    *
115    */
116   public void close() {
117     if (jfile!=null) {
118       try {
119         jfile.close();
120       } catch (IOException e) {
121         e.printStackTrace(System.err);
122       }
123     }
124   }
125 }