2156c51e964bf4bd7704603b2447b563cb5e980e
[vamsas.git] / src / org / vamsas / test / simpleclient / ArchiveReader.java
1 package org.vamsas.test.simpleclient;
2
3 import java.io.BufferedInputStream;
4 import java.io.File;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.util.Vector;
8
9 import org.vamsas.client.simpleclient.VamsasArchiveReader;
10 import org.vamsas.objects.core.VAMSAS;
11 import org.vamsas.objects.core.VamsasDocument;
12
13 public class ArchiveReader {
14   public static boolean reportDocument(VamsasDocument document) {
15     if (document!=null) {
16       System.out.print("Vamsas Document version '"+document.getVersion()+"'");
17       System.out.print("Document contains "+document.getVAMSASCount()+" VAMSAS Elements and "+document.getApplicationDataCount()+" elements.\n");
18       return true;
19     } else {
20       System.out.print("Document Object is null");
21     }
22     return false;
23   }
24   
25   public static boolean rootReport(VAMSAS[] roots) {
26     if (roots!=null) {
27       for (int i=0; i<roots.length; i++) {
28         VAMSAS r = roots[i];
29         System.out.print("Vamsas Root "+i+" (id="
30             +((r.getId()!=null) ? r.getId():"<none>")
31             +") contains "+r.getDataSetCount()+" DataSets, "
32             + r.getTreeCount()+" Global trees");
33       }
34       return true;
35     }
36     return false;
37   }
38   
39   public static void main(String args[]) {
40     
41     try {
42       File av = new File(args[0]);
43       VamsasArchiveReader var = new VamsasArchiveReader(av);
44       VAMSAS roots[]=null;
45       if (var.isValid()) {
46         InputStreamReader vdoc = new InputStreamReader(var.getVamsasDocumentStream());
47         VamsasDocument doc = VamsasDocument.unmarshal(vdoc);
48         if (reportDocument(doc)) {
49           roots = doc.getVAMSAS();
50         }
51       } else {
52         InputStream vxmlis = var.getVamsasXmlStream();
53         
54         if (vxmlis!=null) { // Might be an old vamsas file.
55           BufferedInputStream ixml = new BufferedInputStream(var.getVamsasXmlStream());
56           InputStreamReader vxml = new InputStreamReader(ixml);
57           VAMSAS root;
58           // unmarshal seems to always close the stream (should check this)
59           if ((root = VAMSAS.unmarshal(vxml))!=null) {
60             System.out.println("Read a root.");
61             roots = new VAMSAS[1];
62             roots[0] = root;
63           }
64         }
65       }
66       if (!rootReport(roots))
67         System.err.print(args[0]+" is not a valid vamsas archive.");
68     } catch (Exception e) {
69       e.printStackTrace(System.err);
70     }
71   }
72 }