X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fuk%2Fac%2Fvamsas%2Fclient%2FVorbaXmlBinder.java;h=94624943cf42597a0d8d697bc1d84e5507d3d08d;hb=0a868a9601b051aeaf439a8d73f6fc070044a02c;hp=330372a4c9621fbc4cb8382cd36aa72f6b3b2a1a;hpb=ea8b10ee787a2b1e2b35f2acb4dccaf12fd2a6fd;p=vamsas.git diff --git a/src/uk/ac/vamsas/client/VorbaXmlBinder.java b/src/uk/ac/vamsas/client/VorbaXmlBinder.java index 330372a..9462494 100644 --- a/src/uk/ac/vamsas/client/VorbaXmlBinder.java +++ b/src/uk/ac/vamsas/client/VorbaXmlBinder.java @@ -24,7 +24,8 @@ import org.exolab.castor.xml.Marshaller; import org.exolab.castor.xml.UnmarshalListener; import org.exolab.castor.xml.Unmarshaller; import org.exolab.castor.xml.ValidationException; -import org.vamsas.objects.core.VamsasDocument; + +import uk.ac.vamsas.objects.core.VamsasDocument; /** * Implements the Vamsas Vobject ID machinery for translating * between non-volatile XML IDs and Vobject references. Use the @@ -40,14 +41,13 @@ public class VorbaXmlBinder implements UnmarshalListener { private final Vector obj; private final Hashtable oldobjhashes; private final Hashtable objrefs; - private final Vector updatedobjs; - - public VorbaXmlBinder(IVorbaIdFactory vorbafactory, Vector obj, Hashtable objrefs, Hashtable oldobjhashes, Vector updatedobjs) { - this.vorbafactory = vorbafactory; - this.obj = obj; - this.objrefs = objrefs; + private final Vector updatedobjs; // not yet used elswhere ? + public VorbaXmlBinder(IVorbaIdFactory vorbafactory2, Vector unrefedObj, Hashtable objrefs2, Hashtable oldobjhashes, Vector updatedObj) { + this.vorbafactory = vorbafactory2; + this.obj = unrefedObj; + this.objrefs = objrefs2; this.oldobjhashes = oldobjhashes; - this.updatedobjs = updatedobjs; + this.updatedobjs = updatedObj; } /* @@ -56,6 +56,7 @@ public class VorbaXmlBinder implements UnmarshalListener { * @see org.exolab.castor.xml.UnmarshalListener#attributesProcessed(java.lang.Object) */ public void attributesProcessed(Object object) { + } /* @@ -79,8 +80,11 @@ public class VorbaXmlBinder implements UnmarshalListener { * @see org.exolab.castor.xml.UnmarshalListener#initialized(java.lang.Object) */ public void initialized(Object object) { + if (object instanceof Vobject) { + Vobject nobj = (Vobject) object; + } } - + /* * Check if the object has an 'id' field - if it does, copy the value into * the VorbaId field of Vobject, and add the Vobject to the VorbaId hash. @@ -101,7 +105,8 @@ public class VorbaXmlBinder implements UnmarshalListener { if (idstring!=null) { if (idstring.length() > 0) { nobj.setVorbaId(VorbaId.newId(idstring)); - if (objrefs.containsKey(nobj_id=nobj.getVorbaId()) && !objrefs.get(nobj.getVorbaId()).equals(nobj)) { + nobj_id=nobj.getVorbaId(); + if (objrefs.containsKey(nobj_id) && !objrefs.get(nobj_id).equals(nobj)) { System.err.println("Serious problem : duplicate id '"+idstring+"' found! expect badness."); // TODO: HANDLE duplicate XML ids correctly } @@ -114,23 +119,33 @@ public class VorbaXmlBinder implements UnmarshalListener { // TODO: add to list of objects without a valid vorbaId obj.add(nobj); } - - nobj.doHash(); - // check to see if new object was present in old object hash - if (oldobjhashes.containsKey(nobj.getVorbaId())) { - Vobjhash oldhash = (Vobjhash) oldobjhashes.get(nobj.getVorbaId()); - if (oldhash.isUpdated(nobj)) { - // mark the object as updated in this document read. - nobj.set__updated_since_last_read(true); + nobj.doHash(); // updates detected by comparing with last hash when marshalling + // check to see if new object was present in old object hash + if (nobj_id!=null) { + if (oldobjhashes.containsKey(nobj_id)) { + Vobjhash oldhash = (Vobjhash) oldobjhashes.get(nobj_id); + if (oldhash.isUpdated(nobj)) { + // mark the object as updated in this document read. + nobj.set__updated_since_last_read(true); + updatedobjs.addElement(nobj); + } + } else { + // object has no entry in the oldhashvalue list but + // there is a valid vorbaId so + nobj.set__added_since_last_read(true); + } + // and record the just-unmarshalled hash value oldobjhashes.put(nobj_id, new Vobjhash(nobj)); - updatedobjs.addElement(nobj); + } else { + // for objects yet to get a valid vorba_id + nobj.set__added_since_last_read(true); } } - } + } catch (Exception e) { return; }; - + } } @@ -145,15 +160,16 @@ public class VorbaXmlBinder implements UnmarshalListener { * @throws ValidationException */ public static void putVamsasDocument(PrintWriter outstream, VorbaIdFactory vorba, VamsasDocument doc) - throws IOException, MarshalException, ValidationException { + throws IOException, MarshalException, ValidationException { // Ensure references if (vorba==null) throw new Error("Null VorbaIdFactory Parameter"); if (doc.__vorba==null) doc.__vorba = vorba; doc.__ensure_instance_ids(); // this may take a while. Do we allow for cyclic references ? - doc.marshal(outstream); - + Marshaller mshl = new Marshaller(outstream); + mshl.marshal(doc); + } /** * creates new VorbaId references where necessary for newly unmarshalled objects @@ -181,61 +197,62 @@ public class VorbaXmlBinder implements UnmarshalListener { return sync; } /** - * Unmarshals a vamsasDocument Vobject from a stream, registers - * unregistered objects, records existing VorbaIds, and completes - * the uk.ac.vamsas.client.Vobject housekeeping fields. - * For a valid unmarshalling, the array of returned objects also includes - * a sync parameter which is true if new VorbaIds - * were created. If sync is false, then the caller should ensure that the - * vamsasDocument is written back to disk to propagate the new VorbaIds. - * TODO: ensure that provenance is correct for newly registered objects - * as getVamsasObjects but will detect updated objects based on differing hash values - * obtained from the VorbaIdFactory's VorbaId, Vobject.get__last_Hash() pairs (if any) - * @param instream - the XML input stream + * Unmarshals a vamsasDocument Vobject from a stream, registers + * unregistered objects, records existing VorbaIds, and completes + * the uk.ac.vamsas.client.Vobject housekeeping fields. + * For a valid unmarshalling, the array of returned objects also includes + * a sync parameter which is true if new VorbaIds + * were created. If sync is false, then the caller should ensure that the + * vamsasDocument is written back to disk to propagate the new VorbaIds. + * TODO: ensure that provenance is correct for newly registered objects + * as getVamsasObjects but will detect updated objects based on differing hash values + * obtained from the VorbaIdFactory's VorbaId, Vobject.get__last_Hash() pairs (if any) + * @param instream - the XML input stream * @param factory - the SimpleClient's properly configured VorbaId factory to make new references. - * @param root the root element's org.vamsas.objects.core Vobject. - * @return null or {(Object) VamsasDocument Vobject, (Object) Hashtable of Vobject references, (Object) Boolean(sync), (Object) Vector of updated objects in document } - */ + * @param root the root element's uk.ac.vamsas.objects.core Vobject. + * @return null or {(Object) VamsasDocument Vobject, (Object) Hashtable of Vobject references, (Object) Boolean(sync), (Object) Vector of updated objects in document } + */ public static Object[] getVamsasObjects(Reader instream, - VorbaIdFactory factory, Vobject root) { + VorbaIdFactory factory, Vobject root) { Unmarshaller unmarshaller = new Unmarshaller(root); - unmarshaller.setIDResolver(new IDResolver() { - public Object resolve(String id) { - VorbaXmlBinder.log.warn("Warning - id " + id - + " is not found in the Vamsas XML!"); + unmarshaller.setIDResolver(new IDResolver() { + public Object resolve(String id) { + // TODO: allow for external ID resolution + VorbaXmlBinder.log.warn("Warning - id " + id + + " is not found in the Vamsas XML! (TODO: Ignore if this is a forward reference!)"); + return null; + } + }); + final Hashtable objrefs = new Hashtable(); + if (factory.extanthashv==null) + factory.extanthashv=new Hashtable(); + final Hashtable oobjhashes=factory.extanthashv; + final VorbaIdFactory vorbafactory = factory; + final Vector unrefedObj = new Vector(); + final Vector updatedObj = new Vector(); + unmarshaller.setUnmarshalListener(new VorbaXmlBinder(vorbafactory, unrefedObj, objrefs, oobjhashes,updatedObj)); + // Call the unmarshaller. + try { + while (instream.ready()) { + // TODO: mark objects in oobjhash prior to unmarshalling, to detect when objects have been lost through an update. + //tohere + Object obj = unmarshaller.unmarshal(instream); + boolean sync=ensure_references(unrefedObj, objrefs); + if (!(obj instanceof Vobject)) return null; - } - }); - final Hashtable objrefs = new Hashtable(); - if (factory.extanthashv==null) - factory.extanthashv=new Hashtable(); - final Hashtable oobjhashes=factory.extanthashv; - final VorbaIdFactory vorbafactory = factory; - final Vector unrefedObj = new Vector(); - final Vector updatedObj = new Vector(); - unmarshaller.setUnmarshalListener(new VorbaXmlBinder(vorbafactory, unrefedObj, objrefs, oobjhashes, updatedObj)); - // Call the unmarshaller. - try { - while (instream.ready()) { - // TODO: mark objects in oobjhash prior to unmarshalling, to detect when objects have been lost through an update. - //tohere - Object obj = unmarshaller.unmarshal(instream); - boolean sync=ensure_references(unrefedObj, objrefs); - if (!(obj instanceof Vobject)) - return null; - vorbafactory.setNewIdHash(objrefs); // update the Document IO Handler's set of vorbaId<>Object bindings. - return new Object[] { obj, objrefs, new Boolean(sync),updatedObj}; - } - } catch (MarshalException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (ValidationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + vorbafactory.setNewIdHash(objrefs); // update the Document IO Handler's set of vorbaId<>Object bindings. + return new Object[] { obj, objrefs, new Boolean(sync),updatedObj}; } - return null; + } catch (MarshalException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ValidationException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } + return null; + } } \ No newline at end of file