org.xillium.data.persistence.xml.Data Maven / Gradle / Ivy
package org.xillium.data.persistence.xml;
import java.io.*;
import java.util.List;
import java.util.logging.*;
import org.xillium.data.*;
import org.xillium.base.beans.*;
/**
* This class models the top-level element of a sql-rs XML document. It also provides
* static methods to coalesce (deserialize) an sql-rs XML document from a stream.
*
* To coalesce a compliant sql-rs XML document, do the following.
*
* - Define an implementation of {@link org.xillium.data.DataObject DataObject} that matches the row structure of the XML document.
*
{@code
* public class R implements DataObject {
* ...
* }
* }
*
* - Define an implementation of {@link org.xillium.data.Collector Collector<R>} to process the row objects.
*
{@code
* public class P implements Collector {
* public boolean add(R row) {
* ...
* }
* ...
* }
* }
*
* -
{@code
* P proc = Data.coalesce(inputStream, R.class, new P());
* }
*
*
*
* If a simple {@link java.util.List List} is all that is desired, the second step can be skipped and the last step becomes
*
* -
{@code
* List list = Data.coalesce(inputStream, R.class);
* }
*
*
*/
public class Data> {
private static final Logger _logger = Logger.getLogger(Data.class.getName());
private final C _collector;
private final String _name;
/**
* Constructs a Data object.
*
* @param collector
* @param name - the name of the result set, collected from the XML document
*/
public Data(C collector, String name) {
_collector = collector;
_name = name;
}
/**
* Constructs a Data object.
*
* @param collector
*/
public Data(C collector) {
_collector = collector;
_name = null;
}
/**
* Invoked by an XMLBeanAssembler, passes the row object to the collector.
*/
public void add(Row row) throws Exception {
_collector.add(row.data);
}
/**
* Returns the collector.
*/
public C getCollector() {
return _collector;
}
@SuppressWarnings("unchecked")
public static > C coalesce(InputStream in, Class rtype, C collector) throws Exception {
BurnedInArgumentsObjectFactory factory = new BurnedInArgumentsObjectFactory();
factory.setBurnedIn(Data.class, collector);
factory.setBurnedIn(Row.class, rtype);
factory.setBurnedIn(Column.class, rtype);
try {
return ((Data)new XMLBeanAssembler(factory).build(in)).getCollector();
} finally {
in.close();
}
}
public static List coalesce(InputStream in, Class rtype) throws Exception {
return coalesce(in, rtype, new ArrayListCollector());
}
}