incubator-hise-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r.@apache.org
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 GMT
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<Object> contextObjectTL = new ThreadLocal<Object>()
;
-    
+
     private Map<QName, Object> vars = new HashMap<QName, Object>();
     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);
     }
 



Mime
View raw message