* Write the given collection of {@link ObjectData}s to the given file.
*/
private void write(Collection datas, FileWriter fw)
throws Exception {
// create an XML pretty printer to write out the objects
Writer out = new XMLWriter(fw);
// start the file; the root node is an "extent"
out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.write("<extent>");
// run through each object in the collection
for (Iterator itr = datas.iterator(); itr.hasNext();) {
ObjectData obj = (ObjectData) itr.next();
ClassMetaData meta = obj.getMetaData();
// write out the "object" element start
out.write("<object class=\"");
out.write(meta.getDescribedType().getName());
out.write("\" oid=\"");
out.write(obj.getId().toString());
out.write("\" version=\"");
out.write(obj.getVersion().toString());
out.write("\">");
// run through each field writing out the value
FieldMetaData[] fmds = meta.getFields();
for (int i = 0; i < fmds.length; i++) {
if (fmds[i].getManagement() != fmds[i].MANAGE_PERSISTENT)
continue;
out.write("<field name=\"");
out.write(fmds[i].getName());
out.write("\">");
// write out the field data depending upon type
switch (fmds[i].getTypeCode()) {
case JavaTypes.COLLECTION:
case JavaTypes.ARRAY:
Collection c = (Collection) obj.getField(i);
if (c == null)
break;
// write out each of the elements
int elemType = fmds[i].getElement().getTypeCode();
for (Iterator ci = c.iterator(); ci.hasNext();) {
out.write("<element>");
writeDataValue(out, elemType, ci.next());
out.write("</element>");
}
break;
case JavaTypes.MAP:
Map m = (Map) obj.getField(i);
if (m == null)
break;
// write out each of the map entries
Collection entries = m.entrySet();
int keyType = fmds[i].getKey().getTypeCode();
int valueType = fmds[i].getElement().getTypeCode();
for (Iterator ei = entries.iterator(); ei.hasNext();) {
Map.Entry e = (Map.Entry) ei.next();
out.write("<key>");
writeDataValue(out, keyType, e.getKey());
out.write("</key>");
out.write("<value>");
writeDataValue(out, valueType, e.getValue());
out.write("</value>");
}
break;
default:
writeDataValue(out, fmds[i].getTypeCode(),
obj.getField(i));
}
out.write("</field>");
}
out.write("</object>");
}
out.write("</extent>");
}