* Adds additional meta data to the query results,
* and turns it into a DOM document.
* @param expandSource if true, source meta attributes will be added
*/
public static Document queryResultsToDOM(NodeSet nodeSet, boolean expandSource) {
DocumentImpl doc = new DocumentImpl();
Element root = doc.createElement("result");
doc.appendChild(root);
int count = 0;
while (nodeSet != null && nodeSet.hasMoreNodes()) {
final Object element = nodeSet.getNextNode();
if (element instanceof Attr) {
Attr n = (Attr) element;
Element holder = doc.createElementNS(XindiceCollection.QUERY_NS, "xq:result");
holder.setAttribute(NodeImpl.XMLNS_PREFIX + ":xq", XindiceCollection.QUERY_NS);
holder.setAttributeNode((Attr) doc.importNode(n, true));
if (expandSource && n instanceof DBNode) {
((DBNode) holder).setSource(((DBNode) n).getSource());
((DBNode) holder).expandSource();
}
root.appendChild(holder);
} else if (element instanceof Text || element instanceof Comment) {
Node n = (Node) element;
Element holder = doc.createElementNS(XindiceCollection.QUERY_NS, "xq:result");
holder.setAttribute(NodeImpl.XMLNS_PREFIX + ":xq", XindiceCollection.QUERY_NS);
holder.appendChild(doc.importNode(n, true));
if (expandSource && n instanceof DBNode) {
((DBNode) holder).setSource(((DBNode) n).getSource());
((DBNode) holder).expandSource();
}
root.appendChild(holder);
} else if (element instanceof ProcessingInstruction) {
if (log.isWarnEnabled()) {
log.warn("XPath query with ProcessingInstruction result is not supported");
}
} else if (element instanceof Node) {
Node n = (Node) element;
if (n.getNodeType() == Node.DOCUMENT_NODE) {
n = ((Document) n).getDocumentElement();
}
if (expandSource && n instanceof DBNode) {
((DBNode) n).expandSource();
}
root.appendChild(doc.importNode(n, true));
} else {
throw new XindiceRuntimeException("Unknown result type (" + element.getClass().getName() + ") in nodeset");
}
count++;