{
final ItemDestination dest = new WriterItemDestination(new OutputStreamWriter(os,"UTF-8"),"UTF-8");
final ItemConstructor constructor = InfosetFactory.getDefaultInfoset().createItemConstructor();
try {
context.execute(new QueryContext.ResultListener() {
DocumentLoader loader = new SAXDocumentLoader();
public void onStart() throws QueryException {
try {
dest.send(constructor.createDocument());
dest.send(constructor.createElement(AtomResource.FEED_NAME));
dest.send(constructor.createCharacters("\n"));
link(constructor,dest,"self",getRequest().getResourceRef().toString());
dest.send(constructor.createCharacters("\n"));
text(constructor,dest,AtomResource.TITLE_NAME,"Term Query");
dest.send(constructor.createCharacters("\n"));
text(constructor,dest,AtomResource.ID_NAME,UUID.randomUUID().toString());
dest.send(constructor.createCharacters("\n"));
text(constructor,dest,AtomResource.UPDATED_NAME,AtomResource.toXSDDate(new Date()));
} catch (XMLException ex) {
throw new QueryException("Exception during feed start.",ex);
}
}
public void onEnd() throws QueryException {
try {
dest.send(constructor.createCharacters("\n"));
dest.send(constructor.createElementEnd(AtomResource.FEED_NAME));
dest.send(constructor.createDocumentEnd());
} catch (XMLException ex) {
throw new QueryException("Exception during feed end.",ex);
}
}
public void onEntry(Entry entry) throws QueryException {
try {
Feed feed = entry.getFeed();
String feedPath = feed.getPath();
String feedBaseURI = resourceBase.toString()+feedPath;
dest.send(constructor.createCharacters("\n"));
// get the entry representation
Representation rep = app.getStorage().getEntry(feedBaseURI,feedPath,feed.getUUID(),entry.getUUID());
// avoid thread creation because reading an output representation requires a thread
StringWriter sw = new StringWriter();
rep.write(sw);
rep.release();
// TODO: optimize by giving item destination to storage
loader.generate(new StringReader(sw.toString()), new RemoveDocumentFilter(dest));
} catch (Exception ex) {
throw new QueryException("Exception during feed entry generation.",ex);
}
}