package org.vamsas.client;
import java.io.IOException;
+import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.Vector;
+import org.exolab.castor.mapping.FieldHandler;
+import org.exolab.castor.mapping.GeneralizedFieldHandler;
+import org.exolab.castor.mapping.ValidityException;
import org.exolab.castor.xml.IDResolver;
import org.exolab.castor.xml.MarshalException;
+import org.exolab.castor.xml.MarshalListener;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.UnmarshalListener;
import org.exolab.castor.xml.Unmarshaller;
*/
public void initialized(Object object) {
}
-
+
/*
* Check if the object has an 'id' field - if it does, copy the value into
* the VorbaId field of object, and add the object to the VorbaId hash.
if (nobj.isRegisterable()) {
// look for the id field (should be an NCName string)
nobj.__vorba = vorbafactory;
- fd = nobj.getClass().getField("id");
+ fd = nobj.getClass().getDeclaredField("_id");
String idstring;
if (fd.get(nobj) != null) {
idstring = (String) fd.get(nobj);
* writes the VamsasDocument to the given stream.
* TODO: ensure that (at least) default provenance entries are written for objects.
* @param outstream
+ * @param vorba valid VorbaIdFactory to construct any missing IDs
* @param doc
* @throws IOException
* @throws MarshalException
* @throws ValidationException
*/
- private static void setVamsasDocument(Writer outstream, VamsasDocument doc)
+ public static void putVamsasDocument(PrintWriter outstream, VorbaIdFactory vorba, VamsasDocument doc)
throws IOException, MarshalException, ValidationException {
- Marshaller marshaller = new Marshaller(outstream);
- marshaller.marshal(doc);
+ // Ensure references
+ if (vorba==null)
+ throw new Error("Null VorbaIdVactory 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);
}
+ private static boolean ensure_references(Vector unrefed, Hashtable objrefs) {
+ boolean sync=true;
+ if (unrefed.size()>0) {
+ sync=false; // document is out of sync - ids have been created.
+ java.util.Iterator newobj = unrefed.listIterator();
+ while (newobj.hasNext()) {
+ object o = (object) newobj.next();
+ // forces registration and id field update.
+ VorbaId id = o.getVorbaId();
+ if (!objrefs.containsKey(id)) {
+ objrefs.put(id.id, o);
+ } else {
+ throw new Error("Serious! Duplicate reference made by vorbaIdFactory!");
+ }
+ }
+ }
+ return sync;
+ }
/**
* Unmarshals a vamsasDocument object from a stream, registers
* unregistered objects, records existing VorbaIds, and completes
* vamsasDocument is written back to disk to propagate the new VorbaIds.
* TODO: ensure that provenance is correct for newly registered objects
* @param instream - the XML input stream
- * @param factory - the SimpleClient's properly configured VorbaId factory to make new references.
+ * @param factory - the SimpleClient's properly configured VorbaId factory to make new references.
+ * @param root the root element's org.vamsas.objects.core object.
* @return null or {(Object) VamsasDocument object, (Object) Hashtable of object references, (Object) Boolean(sync) }
*/
- public static Object[] getVamsasDocument(Reader instream,
- IVorbaIdFactory factory) {
- Unmarshaller unmarshaller = new Unmarshaller(instream);
+ public static Object[] getVamsasObjects(Reader instream,
+ IVorbaIdFactory factory, object root) {
+ Unmarshaller unmarshaller = new Unmarshaller(root);
unmarshaller.setIDResolver(new IDResolver() {
public Object resolve(String id) {
System.err.println("Warning - id " + id
try {
while (instream.ready()) {
Object obj = unmarshaller.unmarshal(instream);
- boolean sync=true;
- if (obj instanceof VamsasDocument) {
- if (unrefed.size()>0) {
- sync=false; // document is out of sync - ids have been created.
- java.util.Iterator newobj = unrefed.listIterator();
- while (newobj.hasNext()) {
- object o = (object) newobj.next();
- // forces registration and id field update.
- VorbaId id = o.getVorbaId();
- if (!objrefs.containsKey(id)) {
- objrefs.put(id.id, o);
- } else {
- throw new Error("Serious! Duplicate reference made by vorbaIdFactory!");
- }
- }
- }
- return new Object[] { obj, objrefs, new Boolean(sync)};
+ boolean sync=ensure_references(unrefed, objrefs);
+ if (!(obj instanceof object))
+ return null;
+ return new Object[] { obj, objrefs, new Boolean(sync)};
}
- }
} catch (MarshalException e) {
// TODO Auto-generated catch block
e.printStackTrace();