synchronized AppData parsing stuff with new AppData entry (embedded/references)
[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
8 import org.vamsas.client.simpleclient.VamsasArchiveReader;
9 import org.vamsas.objects.core.ApplicationData;
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, VamsasArchiveReader archive) {
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       if (document.getApplicationDataCount()>0) {
19         System.out.print("There are "+document.getApplicationDataCount()+" ApplicationData references.\n");
20         ApplicationData appd[] = document.getApplicationData();
21         for (int i=0,j=appd.length; i<j; i++) {
22           System.out.print("Application "+i+": '"+appd[i].getName()+"'\nVersion '"+appd[i].getVersion()+"'\nURN: '"+appd[i].getUrn()+"'\n");
23           System.out.print("AppData is ");
24           if (appd[i].getAppDataChoice().getDataReference()!=null) {
25             String appData=appd[i].getAppDataChoice().getDataReference();
26             if (appData.length()>1) {
27               System.out.print("a reference ("+appData+")");
28               InputStream jstrm;
29               if ((jstrm=archive.getAppdataStream(appData))!=null)
30                 System.out.println(" which resolves to a JarEntry.");
31               else {
32                 System.out.println(" which does not resolve to a JarEntry.");
33                 System.err.println("Unresolved appdata reference '"+appData+"'");
34               }
35             } else {
36               System.out.println("an empty reference.");
37             }
38           } else {
39             System.out.println("an embedded chunk of "+appd[i].getAppDataChoice().getData().length+" bytes.");
40           }
41         }
42           
43       }
44       return true;
45     } else {
46       System.out.print("Document Object is null");
47     }
48     return false;
49   }
50   
51   public static boolean rootReport(VAMSAS[] roots) {
52     if (roots!=null) {
53       for (int i=0; i<roots.length; i++) {
54         VAMSAS r = roots[i];
55         System.out.print("Vamsas Root "+i+" (id="
56             +((r.getId()!=null) ? r.getId():"<none>")
57             +") contains "+r.getDataSetCount()+" DataSets, "
58             + r.getTreeCount()+" Global trees");
59       }
60       return true;
61     }
62     return false;
63   }
64   
65   public static void main(String args[]) {
66     
67     try {
68       File av = new File(args[0]);
69       VamsasArchiveReader var = new VamsasArchiveReader(av);
70       VAMSAS roots[]=null;
71       if (var.isValid()) {
72         InputStreamReader vdoc = new InputStreamReader(var.getVamsasDocumentStream());
73         VamsasDocument doc = VamsasDocument.unmarshal(vdoc);
74         if (reportDocument(doc, var)) {
75           roots = doc.getVAMSAS();
76         }
77       } else {
78         InputStream vxmlis = var.getVamsasXmlStream();
79         
80         if (vxmlis!=null) { // Might be an old vamsas file.
81           BufferedInputStream ixml = new BufferedInputStream(var.getVamsasXmlStream());
82           InputStreamReader vxml = new InputStreamReader(ixml);
83           VAMSAS root;
84           // unmarshal seems to always close the stream (should check this)
85           if ((root = VAMSAS.unmarshal(vxml))!=null) {
86             System.out.println("Read a root.");
87             roots = new VAMSAS[1];
88             roots[0] = root;
89           }
90         }
91       }
92       if (!rootReport(roots))
93         System.err.print(args[0]+" is not a valid vamsas archive.");
94     } catch (Exception e) {
95       e.printStackTrace(System.err);
96     }
97   }
98 }