package com.javaeye.jert.service.impl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.javaeye.jert.domain.Database;
import com.javaeye.jert.domain.ParameterDefinition;
import com.javaeye.jert.domain.ReportDefinition;
import com.javaeye.jert.service.ExportImportService;
import com.thoughtworks.xstream.XStream;
/**
* @author Quake Wang
* @since 2005-1-12
* @version $Revision: 1.2 $
*/
public class ExportImportServiceXStreamImpl implements ExportImportService {
private XStream xstream;
public ExportImportServiceXStreamImpl() {
xstream = new XStream();
xstream.alias("reportdefinition", ReportDefinition.class);
xstream.alias("parameterdefinition", ParameterDefinition.class);
}
public String exportToXml(ReportDefinition rd) {
return xstream.toXML(new ReportDefinition(rd));
}
public String exportAllToXml(Database database) {
List rds = new ArrayList(database.getReportDefinitions().size());
for (Iterator iter = database.getReportDefinitions().iterator(); iter.hasNext();) {
ReportDefinition rd = (ReportDefinition) iter.next();
rds.add(new ReportDefinition(rd));
}
return xstream.toXML(rds);
}
public ReportDefinition[] importFromXml(String xml) {
Object imported = xstream.fromXML(xml);
if (imported instanceof List) {
List result = (List) imported;
return (ReportDefinition[]) result.toArray(new ReportDefinition[result.size()]);
} else {
return new ReportDefinition[] { (ReportDefinition) imported };
}
}
}