/*
* Copyright 1999-2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* CVS $Id: QueryUtil.java,v 1.7 2004/03/04 13:18:49 vgritsenko Exp $
*/
package org.apache.xindice.core.query;
import org.apache.xindice.client.xmldb.XindiceCollection;
import org.apache.xindice.core.data.NodeSet;
import org.apache.xindice.util.XindiceRuntimeException;
import org.apache.xindice.xml.NamespaceMap;
import org.apache.xindice.xml.TextWriter;
import org.apache.xindice.xml.dom.DBNode;
import org.apache.xindice.xml.dom.DocumentImpl;
import org.apache.xindice.xml.dom.NodeImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import org.w3c.dom.Comment;
import org.w3c.dom.ProcessingInstruction;
import java.util.Hashtable;
/**
* Helper to convert NodeSet query result into the Document
*
* @version CVS $Revision: 1.7 $, $Date: 2004/03/04 13:18:49 $
*/
public class QueryUtil {
private static final Log log = LogFactory.getLog(QueryUtil.class);
/**
* Maps a Hashtable containing namespace definitions into a Xindice
* NamespaceMap.
*/
public static NamespaceMap mapNamespaces(Hashtable namespaces) {
if (namespaces != null && namespaces.size() > 0) {
return new NamespaceMap(namespaces);
}
return null;
}
/**
* 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++;
}
root.setAttribute("count", Integer.toString(count));
return doc;
}
/**
* Adds additional meta data to the query results,
* and turns it into a DOM document.
*/
public static Document queryResultsToDOM(NodeSet nodeSet) {
return queryResultsToDOM(nodeSet, true);
}
/**
* Adds additional meta data to the query results,
* and turns it into a String containing XML document.
* @param expandSource if true, source meta attributes will be added
*/
public static String queryResultsToString(NodeSet nodeSet, boolean expandSource) {
return TextWriter.toString(queryResultsToDOM(nodeSet, expandSource));
}
/**
* Adds additional meta data to the query results,
* and turns it into a String containing XML document.
*/
public static String queryResultsToString(NodeSet nodeSet) {
return TextWriter.toString(queryResultsToDOM(nodeSet, true));
}
}