if (getOption(_wrapper) != null) {
wrapper = getOption(_wrapper).getQName();
}
XdmNode queryDocument = source.read();
String queryString = queryDocument.getStringValue();
ContentSource contentSource;
try {
if ("".equals(contentBase)) {
contentSource = ContentSourceFactory.newContentSource(host, port, user, password);
} else {
contentSource = ContentSourceFactory.newContentSource(host, port, user, password, contentBase);
}
} catch (Exception e) {
throw new XProcException(e);
}
if ("basic".equals(getOption(_auth_method, ""))) {
contentSource.setAuthenticationPreemptive(true);
}
Session session;
try {
session = contentSource.newSession ();
Request request = session.newAdhocQuery (queryString);
for (QName name : params.keySet()) {
XSString value = ValueFactory.newXSString (params.get(name));
XName xname = new XName(name.getNamespaceURI(), name.getLocalName());
XdmVariable myVariable = ValueFactory.newVariable (xname, value);
request.setVariable (myVariable);
}
ResultSequence rs = session.submitRequest (request);
while (rs.hasNext()) {
ResultItem rsItem = rs.next();
XdmItem item = rsItem.getItem();
// FIXME: This needs work...
if (item instanceof XdmDocument || item instanceof XdmElement) {
XdmNode xccXML = parseString(item.asString());
result.write(xccXML);
} else if (item instanceof XdmBinary) {
String base64 = Base64.encodeBytes(((XdmBinary) item).asBinaryData());
TreeWriter treeWriter = new TreeWriter(runtime);
treeWriter.startDocument(step.getNode().getBaseURI());
treeWriter.addStartElement(wrapper);
if (XProcConstants.NS_XPROC_STEP.equals(wrapper.getNamespaceURI())) {
treeWriter.addAttribute(_encoding, "base64");
} else {
treeWriter.addAttribute(c_encoding, "base64");
}
treeWriter.startContent();
treeWriter.addText(base64);
treeWriter.addEndElement();
treeWriter.endDocument();
XdmNode node = treeWriter.getResult();
result.write(node);
} else {
String text = item.asString();
TreeWriter treeWriter = new TreeWriter(runtime);
treeWriter.startDocument(step.getNode().getBaseURI());
treeWriter.addStartElement(wrapper);
treeWriter.startContent();
treeWriter.addText(text);
treeWriter.addEndElement();
treeWriter.endDocument();
XdmNode node = treeWriter.getResult();
result.write(node);
}
}
session.close();