2 * This file is part of the Vamsas Client version 0.1.
\r
3 * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite,
\r
4 * Andrew Waterhouse and Dominik Lindner.
\r
6 * Earlier versions have also been incorporated into Jalview version 2.4
\r
7 * since 2008, and TOPALi version 2 since 2007.
\r
9 * The Vamsas Client is free software: you can redistribute it and/or modify
\r
10 * it under the terms of the GNU Lesser General Public License as published by
\r
11 * the Free Software Foundation, either version 3 of the License, or
\r
12 * (at your option) any later version.
\r
14 * The Vamsas Client is distributed in the hope that it will be useful,
\r
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
17 * GNU Lesser General Public License for more details.
\r
19 * You should have received a copy of the GNU Lesser General Public License
\r
20 * along with the Vamsas Client. If not, see <http://www.gnu.org/licenses/>.
\r
22 package uk.ac.vamsas.test.simpleclient;
\r
24 import java.io.File;
\r
25 import java.io.InputStream;
\r
26 import java.io.InputStreamReader;
\r
27 import java.io.PrintWriter;
\r
28 import java.text.DateFormat;
\r
29 import java.util.Date;
\r
30 import java.util.Hashtable;
\r
31 import java.util.Vector;
\r
33 import org.apache.commons.logging.Log;
\r
34 import org.apache.commons.logging.LogFactory;
\r
35 import uk.ac.vamsas.objects.core.Alignment;
\r
36 import uk.ac.vamsas.objects.core.ApplicationData;
\r
37 import uk.ac.vamsas.objects.core.Entry;
\r
38 import uk.ac.vamsas.objects.core.Instance;
\r
39 import uk.ac.vamsas.objects.core.Provenance;
\r
40 import uk.ac.vamsas.objects.core.VAMSAS;
\r
41 import uk.ac.vamsas.objects.core.VamsasDocument;
\r
42 import uk.ac.vamsas.objects.utils.ProvenanceStuff;
\r
43 import uk.ac.vamsas.client.simpleclient.VamsasArchive;
\r
44 import uk.ac.vamsas.client.simpleclient.VamsasArchiveReader;
\r
46 public class ArchiveWriter {
\r
49 * Test program for writing archive files. form is ArchiveWriter new/modified
\r
50 * argive command list
\r
53 static Log log = LogFactory.getLog(ArchiveWriter.class);
\r
55 private static void mergeVecs(Object[] destvec, Object[] svec1, Object[] svec2) {
\r
57 for (i = 0; i < svec1.length; i++)
\r
58 destvec[i] = svec1[i];
\r
59 for (int j = 0; j < svec2.length; i++, j++)
\r
60 destvec[i] = svec2[j];
\r
63 // Merge appDataReferences require transfer of jar entries, perhaps with a
\r
64 // renaming of the entry.
\r
65 // Merge appDatas require eventually unique URNS
\r
66 // TODO: merging global appdata from different documents where same app has
\r
67 // written them causes conflict
\r
69 public static Hashtable hashOfAppDatas(Hashtable ht, Instance[] appdatas) {
\r
71 ht = new Hashtable();
\r
72 for (int i = 0, j = appdatas.length; i < j; i++) {
\r
73 if (!ht.containsKey(appdatas[i].getUrn())) {
\r
74 Hashtable aphash = new Hashtable();
\r
75 ht.put(appdatas[i].getUrn(), aphash);
\r
76 aphash.put(appdatas[i], appdatas[i].getDataReference());
\r
78 // ensure urns and references are unique
\r
87 * safely copies an appData from one archive to another.
\r
90 * destination archive
\r
92 * destination document Vobject
\r
94 * source archive reader
\r
96 * application data to be copied from source archive
\r
98 public static void addAppDataEntry(VamsasArchive darc, VamsasDocument dest,
\r
99 VamsasArchiveReader sarc, ApplicationData entry) {
\r
100 // TODO: fix instances
\r
101 // check uniqueness of instance's[] entry.urn amongst
\r
102 // dest.ApplicationData[].getInstances[].urn
\r
103 // check uniqueness of entry.user[].urn amongst
\r
104 // dest.ApplicationData[].user[].urn
\r
105 // check uniqueness of entry.user
\r
106 // entry.getAppDataChoice().getData() or getDataReference is unique
\r
107 ApplicationData newo = new ApplicationData();
\r
108 for (int i = 0, j = dest.getApplicationDataCount(); i < j; i++) {
\r
109 ApplicationData o = dest.getApplicationData()[i];
\r
110 // ensure new urn is really unique
\r
111 // String urn = entry.getUrn();
\r
113 // while (o.getUrn().equals(urn)) {
\r
114 // urn = entry.getUrn()+v++;
\r
116 // uniqueness of urn
\r
117 // check each user ApplicationData
\r
118 // uniqueness (again)
\r
119 // copy over valid objects
\r
125 * Copy new datasets and appdatas from one vamsas document to another.
\r
131 * @return true if merge was successful.
\r
133 public static boolean mergeDocs(VamsasArchive darc, VamsasDocument dest,
\r
134 VamsasArchiveReader sarc, VamsasDocument source) {
\r
135 log.debug("mergeDocs entered.");
\r
136 // search for appDatas in cdoc
\r
137 VAMSAS[] newr = new VAMSAS[dest.getVAMSASCount() + source.getVAMSASCount()];
\r
138 mergeVecs(newr, dest.getVAMSAS(), source.getVAMSAS());
\r
139 dest.setVAMSAS(newr);
\r
141 * TODO: LATER: should verify that all ids really are unique in newly merged
\r
142 * document. If not then what ? investigate possibility of having an id
\r
143 * translation between appDatas and the core document - the mapping is
\r
144 * stored when an external application performs a merge, but when the owning
\r
145 * Application accesses the Vobject, the vorba_id is updated to the new one
\r
146 * when it writes its references in to its appdata again
\r
148 if (source.getApplicationDataCount() > 0) {
\r
149 ApplicationData[] newdat = new ApplicationData[source
\r
150 .getApplicationDataCount()
\r
151 + dest.getApplicationDataCount()];
\r
152 ApplicationData[] sappd = source.getApplicationData();
\r
153 // check refs and update/modify if necessary
\r
154 for (int i = 0; i < sappd.length; i++) {
\r
155 addAppDataEntry(darc, dest, sarc, sappd[i]);
\r
160 return true; // success
\r
163 private static CommandProcessor cproc;
\r
165 cproc.addCommand("new", 0, "no args");
\r
166 cproc.addCommand("add", 1,
\r
167 "Need another vamsas document archive filename as argument.");
\r
168 cproc.addCommand("repair", 0, "no args");
\r
169 cproc.addCommand("list", 0, "no args");
\r
170 cproc.addCommand("monitor", 0, "no args");
\r
173 public static void main(String argv[]) {
\r
175 * TODO: switches for setting user identities for writing to vamsas document
\r
177 if (argv.length < 1) {
\r
178 log.fatal("Usage : <archive to create> [(commands)]");
\r
181 File newarch = new File(argv[0]);
\r
184 // test fully fledged doc construction
\r
185 VamsasArchive varc = new VamsasArchive(newarch, true);
\r
186 VamsasDocument docroot;
\r
187 docroot = new VamsasDocument();
\r
188 docroot.setProvenance(ProvenanceStuff.newProvenance("ArchiveWriter",
\r
189 "user", "Created new Vamsas Document"));
\r
190 while (++argpos < argv.length) {
\r
191 File archive = new File(argv[argpos]);
\r
192 InputStream istream;
\r
193 if (archive.exists()) {
\r
194 VamsasArchiveReader vdoc = new VamsasArchiveReader(archive);
\r
195 if (vdoc.isValid()) {
\r
196 istream = vdoc.getVamsasDocumentStream();
\r
197 if (istream != null) {
\r
198 VamsasDocument cdocroot = VamsasDocument
\r
199 .unmarshal(new InputStreamReader(istream));
\r
200 if (cdocroot != null)
\r
201 mergeDocs(varc, docroot, vdoc, cdocroot);
\r
204 .warn("Unexpectedly null document stream from existing document "
\r
207 // updating an oldformat stream ?
\r
208 if ((istream = vdoc.getVamsasXmlStream()) != null) {
\r
209 // make a new vamsas document from the vamsas.xml entry
\r
210 VAMSAS root = VAMSAS.unmarshal(new InputStreamReader(istream)); // TODO:
\r
219 docroot.getProvenance().addEntry(
\r
220 ProvenanceStuff.newProvenanceEntry("ArchiveWriter", "user",
\r
221 "added vamsas.xml from " + argv[argpos - 1]));
\r
222 docroot.addVAMSAS(root);
\r
226 // Begin a new vamsas document
\r
227 PrintWriter docwriter = varc.getDocumentOutputStream();
\r
230 } catch (Exception e) {
\r
231 log.error("Whilst manipulating " + argv[0], e);
\r