From hise-commits-return-253-apmail-incubator-hise-commits-archive=incubator.apache.org@incubator.apache.org Mon Aug 16 09:50:00 2010 Return-Path: Delivered-To: apmail-incubator-hise-commits-archive@minotaur.apache.org Received: (qmail 66481 invoked from network); 16 Aug 2010 09:50:00 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 16 Aug 2010 09:50:00 -0000 Received: (qmail 72373 invoked by uid 500); 16 Aug 2010 09:50:00 -0000 Delivered-To: apmail-incubator-hise-commits-archive@incubator.apache.org Received: (qmail 72340 invoked by uid 500); 16 Aug 2010 09:49:59 -0000 Mailing-List: contact hise-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: hise-dev@incubator.apache.org Delivered-To: mailing list hise-commits@incubator.apache.org Received: (qmail 72310 invoked by uid 99); 16 Aug 2010 09:49:59 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 16 Aug 2010 09:49:59 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 16 Aug 2010 09:49:56 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 4D23423888D2; Mon, 16 Aug 2010 09:48:38 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: svn commit: r985847 - /incubator/hise/trunk/hise-services/src/main/java/org/apache/hise/utils/XQueryEvaluator.java Date: Mon, 16 Aug 2010 09:48:38 -0000 To: hise-commits@incubator.apache.org From: rr@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100816094838.4D23423888D2@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: rr Date: Mon Aug 16 09:48:38 2010 New Revision: 985847 URL: http://svn.apache.org/viewvc?rev=985847&view=rev Log: HISE-79: avoid conversion dom->string->dom in XQueryEvaluator (Thanks to Piotr Zagórski) Modified: incubator/hise/trunk/hise-services/src/main/java/org/apache/hise/utils/XQueryEvaluator.java Modified: incubator/hise/trunk/hise-services/src/main/java/org/apache/hise/utils/XQueryEvaluator.java URL: http://svn.apache.org/viewvc/incubator/hise/trunk/hise-services/src/main/java/org/apache/hise/utils/XQueryEvaluator.java?rev=985847&r1=985846&r2=985847&view=diff ============================================================================== --- incubator/hise/trunk/hise-services/src/main/java/org/apache/hise/utils/XQueryEvaluator.java (original) +++ incubator/hise/trunk/hise-services/src/main/java/org/apache/hise/utils/XQueryEvaluator.java Mon Aug 16 09:48:38 2010 @@ -27,8 +27,11 @@ import java.util.Map; import javax.xml.namespace.QName; import net.sf.saxon.Configuration; +import net.sf.saxon.dom.DOMSender; +import net.sf.saxon.dom.DOMWriter; import net.sf.saxon.dom.DocumentWrapper; import net.sf.saxon.dom.NodeOverNodeInfo; +import net.sf.saxon.event.PipelineConfiguration; import net.sf.saxon.expr.JPConverter; import net.sf.saxon.functions.FunctionLibraryList; import net.sf.saxon.functions.JavaExtensionLibrary; @@ -46,10 +49,11 @@ import org.apache.commons.logging.LogFac import org.apache.ws.commons.schema.utils.NodeNamespaceContext; import org.w3c.dom.Document; import org.w3c.dom.DocumentFragment; +import org.w3c.dom.Element; import org.w3c.dom.Node; /** - * + * * @author Rafał Rusin */ public class XQueryEvaluator { @@ -57,25 +61,25 @@ public class XQueryEvaluator { private Log __log = LogFactory.getLog(XQueryEvaluator.class); public static ThreadLocal contextObjectTL = new ThreadLocal() ; - + private Map vars = new HashMap(); private Configuration config = Configuration.makeConfiguration(null, null); private JavaExtensionLibrary jel = new JavaExtensionLibrary(config); private StaticQueryContext sqc; - + private Object contextObject; - - - + + + public XQueryEvaluator() { super(); sqc = new StaticQueryContext(config); } public void bindVariable(QName var, Object value) { - vars.put(var, value); - } - + vars.put(var, value); + } + public void declareJavaClass(String uri, Class clazz) { jel.declareJavaClass(uri, clazz); } @@ -87,18 +91,18 @@ public class XQueryEvaluator { public ValueRepresentation convertJavaToSaxon(Object obj) { try { if (obj == null) obj = ""; - + if (obj instanceof Node) { NodeInfo v = new DocumentWrapper((Node) obj, "", config).getRoot(); return v; - } else { + } else { return JPConverter.allocate(obj.getClass(), null).convert(obj, null); } } catch (XPathException e) { throw new RuntimeException("", e); } } - + public List evaluateExpression(String expr, org.w3c.dom.Node contextNode) { //String expr=new String(xqueryNamespace+_expr); try { @@ -109,26 +113,22 @@ public class XQueryEvaluator { config.setExtensionBinder("java", fll); } - + sqc.clearDeclaredGlobalVariables(); for (QName var : vars.keySet()) { sqc.declareGlobalVariable(StructuredQName.fromClarkName(var.toString()), SequenceType.SINGLE_ITEM, convertJavaToSaxon(vars.get(var)) , false); } DynamicQueryContext dqc = new DynamicQueryContext(config); - + XQueryExpression e = sqc.compileQuery(expr); - + if (contextNode != null) { - if (!(contextNode instanceof Document || contextNode instanceof DocumentFragment) ) { - try { - contextNode = DOMUtils.parse(DOMUtils.domToString(contextNode)); - } catch (Exception e1) { - throw new RuntimeException("", e1); - } -// DocumentFragment frag = contextNode.getOwnerDocument().createDocumentFragment(); -// frag.appendChild(contextNode); -// contextNode = frag; + if (!(contextNode instanceof Document || contextNode instanceof DocumentFragment)) { + DocumentFragment df = contextNode.getOwnerDocument().createDocumentFragment(); + df.appendChild(contextNode.cloneNode(true)); + contextNode = df; } - dqc.setContextItem(new DocumentWrapper(contextNode, "", config)); + DocumentWrapper documentWrapper = new DocumentWrapper(contextNode, "", config); + dqc.setContextItem(documentWrapper); } List value = e.evaluate(dqc); @@ -136,9 +136,23 @@ public class XQueryEvaluator { for (Object o : value) { Object o2 = o; if (o2 instanceof NodeInfo) { - try { - Node o3 = DOMUtils.parse(DOMUtils.domToString(NodeOverNodeInfo.wrap((NodeInfo) o2))).getDocumentElement(); - o2 = o3; + try { //saxon tiny tree dom to java dom conversion + + Document destDoc = DOMUtils.createEmptyDomDocument(); + Element destElement = destDoc.createElement("result"); + DOMWriter writer = new DOMWriter(); + writer.setNode(destElement); + NodeInfo sourceNodeInfo = (NodeInfo) o2; + DOMSender doms = new DOMSender(); + doms.setReceiver(writer); + doms.setStartNode(NodeOverNodeInfo.wrap(sourceNodeInfo)); + PipelineConfiguration pc = new PipelineConfiguration(); + pc.setConfiguration(config); + doms.setPipelineConfiguration(pc); + doms.send(); + + destDoc.appendChild(destElement.getFirstChild()); + o2 = destDoc.getDocumentElement(); } catch (Exception e1) { throw new RuntimeException("Error converting result", e1); } @@ -155,9 +169,9 @@ public class XQueryEvaluator { contextObjectTL.set(null); } } - + public void declareNamespace(String prefix, String uri){ - + sqc.declareNamespace(prefix, uri); }