Dear Florent,
thanks again for your blog entry. In the following, I have listed two quick alternatives for evaluating XQuery expressions in BaseX. The first version directly communicates with the XQuery processor of BaseX and caches the serialized byte stream (bypassing the string conversion):
import org.basex.core.Context; import org.basex.data.Result; import org.basex.io.serial.Serializer; import org.basex.query.QueryException; import org.basex.query.QueryProcessor; ...
@Override public void run() throws SaxonApiException { super.run();
XdmNode query_doc = mySource.read(); String query_txt = query_doc.getStringValue();
ByteArrayOutputStream baos = new ByteArrayOutputStream(); Context ctx = new Context(); QueryProcessor qp = new QueryProcessor(query_txt, ctx); try { Serializer ser = qp.getSerializer(baos); Result res = qp.execute(); res.serialize(ser); } catch(QueryException ex) { throw new XProcException(ex); } catch(IOException ex) { throw new XProcException(ex); } Source src = new StreamSource(new ByteArrayInputStream(baos.toByteArray()));
DocumentBuilder builder = runtime.getProcessor().newDocumentBuilder(); XdmNode doc = builder.build(src); myResult.write(doc); }
The second variant communicates with the client/server architecture of BaseX:
import org.basex.core.BaseXException; import org.basex.server.ClientSession; ...
@Override public void run() throws SaxonApiException { super.run();
XdmNode query_doc = mySource.read(); String query_txt = query_doc.getStringValue();
try { ClientSession cs = new ClientSession("localhost", 1984, "admin", "admin"); final String result = cs.query(query_txt).execute(); Source src = new StreamSource(new StringReader(result));
DocumentBuilder builder = runtime.getProcessor().newDocumentBuilder(); XdmNode doc = builder.build(src); myResult.write(doc); } catch (IOException ex) { throw new XProcException(ex); } catch (BaseXException ex) { throw new XProcException(ex); } }
In both variants, the result is completely serialized before it is passed on to Saxon's node builder. If the intermediate result gets very large, we could try in a second step to merge the serializer and input stream.
Christian ___________________________
On Mon, Sep 5, 2011 at 11:54 AM, Christian Grün christian.gruen@gmail.com wrote:
I've just published a blog post at [1] about writing an extension step in Java for Calabash to use BaseX. Using BaseX is just an excuse, and the post shows more how to write an extension and how to glue the various parts together, but I think it is enough to give the big picture.
..that's great news! I'll have a closer look at both your blog entry and the MarkLogic extension today.
Christian