e04e7c972ee28708fa37d1c2b4d83aa541b3968a
[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    * the vamsas document version(s) handled by this Reader
34    */
35   final public static String DOCUMENT_VERSION="0.1"; 
36   /**
37    * name of the jarEntry containing a well formatted vamsas XML Document
38    */
39   
40   final public static String VAMSASDOC="vamsasDocument.xml";
41   
42   /**
43    * name of the jarEntry containing a root VAMSAS element, and containing a 
44    * random sequence of VAMSAS DataSet elements 
45    */
46   
47   final public static String VAMSASXML="vamsas.xml";
48   
49   /**
50    * 
51    * @return JarEntry for VamsasArchiveReader.VAMSASDOC
52    */
53   protected JarEntry getVamsasDocumentEntry() {
54     if (jfile!=null)
55       return jfile.getJarEntry(VAMSASDOC);
56     return null;
57   }
58   /**
59    * 
60    * @return JarEntry for VamsasArchiveReader.VAMSASXML
61    */
62   protected JarEntry getVamsasXmlEntry() {
63     if (jfile!=null)
64       return jfile.getJarEntry(VAMSASXML);
65     return null;
66   }
67   /**
68    * Test for valid vamsas document archive
69    * @return true if getVamsasDocumentStream will return a stream likely to contain valid XML
70    */
71   public boolean isValid() {
72     if (jfile!=null)
73       // 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
74       return (getVamsasDocumentEntry()!=null);
75     return false;   
76   }
77
78   
79   protected JarEntry getAppdataEntry(String AppdataRef) {
80     JarEntry entry;
81     if (jfile==null || !isValid() || (entry=jfile.getJarEntry(AppdataRef))==null)
82       return null;
83     return entry;
84   }
85   
86   public InputStream getAppdataStream(String AppdataRef) {
87     JarEntry entry=getAppdataEntry(AppdataRef);
88     try {
89       if (entry!=null)
90         return jfile.getInputStream(entry);
91     } catch (IOException e) {
92       System.err.println("Failed when opening AppdataStream for "+AppdataRef);
93       e.printStackTrace(System.err);
94     }
95     return null;
96   }
97   /**
98    * get the VamsasDocument input stream, if it exists.
99    * @return null or valid input stream
100    */
101   public InputStream getVamsasDocumentStream() {
102     InputStream vdoc;
103     if (jfile==null || !isValid())
104       return null;
105     try {
106       vdoc = jfile.getInputStream(getVamsasDocumentEntry());
107     } catch (IOException e) {
108       e.printStackTrace(System.err);
109       vdoc=null;
110     }
111     return vdoc;
112   }
113   
114   /**
115    * get the VamsasXML input stream, if it exists.
116    * Note: Deprecated beyond our prealpha testing.
117    * @return null or valid input stream.
118    */
119   
120   public InputStream getVamsasXmlStream() {
121     JarEntry xmle=getVamsasXmlEntry();
122     InputStream vdoc;
123     if (xmle==null)
124       return null;
125     try {
126       vdoc = jfile.getInputStream(xmle);
127     } catch (IOException e) {
128       e.printStackTrace(System.err);
129       vdoc=null;
130     }
131     return vdoc;
132   }
133   
134   /**
135    * silently close the jar file.
136    *
137    */
138   public void close() {
139     if (jfile!=null) {
140       try {
141         jfile.close();
142       } catch (IOException e) {
143         e.printStackTrace(System.err);
144       }
145     }
146   }
147 }