It works fine to pass a DOMSource to an XQuery using saxon:stream, as long as only
one of these streams is evaluated at a time. When processing multiple instances, not
even concurrently, there seems to be some interference, causing incorrect results or
exceptions being thrown.
 
Here is sample code that reproduces some of the observed behaviour:
 
 
        public static void main(String[] args) throws Exception {
                Processor processor = new Processor(configuration);
                XQueryCompiler compiler = processor.newXQueryCompiler();
                XQueryExecutable executable = compiler.compile("saxon:stream(doc('INPUT')/*/*)");
 
                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                documentBuilderFactory.setNamespaceAware(true);
                final Document document = documentBuilderFactory.newDocumentBuilder().parse(new InputSource(new StringReader("<x><y/></x>")));
                configuration.setURIResolver(new URIResolver() {
                        @Override
                        public Source resolve(String href, String base) throws XPathException {
                                return new DOMSource(document);
                        }
                });
 
                XQueryEvaluator evaluator1 = executable.load();        
                XQueryEvaluator evaluator2 = executable.load();        
               
                XdmSequenceIterator iterator1 = (XdmSequenceIterator) evaluator1.iterator();
                XdmSequenceIterator iterator2 = (XdmSequenceIterator) evaluator2.iterator();
               
                System.out.println(iterator1.hasNext());
                System.out.println(iterator2.hasNext());
        }
 
 
The expected result is:
 
true
true
 
It usually prints
 
false
false  
 
and sometimes also throws exceptions, two samples are shown below.
 
Apparently this only happens with the very first DOMSource being streamed
during the execution of a JVM - the above code seems to work as expected
when a single DOMSource has been streamed successfully before.
 
I can work around this by not using saxon:stream when facing a DOMSource,
but thought that it might be useful to report it.
 
Tested on Saxon-EE 9.5.1.6.
 
Best regards,
Gunther
 
 
java.lang.NullPointerException
        at com.sun.org.apache.xerces.internal.dom.DeferredElementNSImpl.synchronizeData(Unknown Source)
        at com.sun.org.apache.xerces.internal.dom.ElementImpl.getNodeName(Unknown Source)
        at net.sf.saxon.dom.DOMNodeWrapper.getPrefix(DOMNodeWrapper.java:523)
        at net.sf.saxon.om.NameOfNode.getPrefix(NameOfNode.java:27)
        at net.sf.saxon.om.NameOfNode.getNamespaceBinding(NameOfNode.java:93)
        at net.sf.saxon.event.StartTagBuffer.startElement(StartTagBuffer.java:136)
        at net.sf.saxon.event.Stripper.startElement(Stripper.java:109)
        at net.sf.saxon.tree.util.Navigator.copy(Navigator.java:602)
        at net.sf.saxon.dom.DOMNodeWrapper.copy(DOMNodeWrapper.java:762)
        at net.sf.saxon.tree.util.Navigator.copy(Navigator.java:592)
        at net.sf.saxon.dom.DOMNodeWrapper.copy(DOMNodeWrapper.java:762)
        at net.sf.saxon.event.Sender.sendDocumentInfo(Sender.java:259)
        at net.sf.saxon.event.Sender.send(Sender.java:124)
        at net.sf.saxon.functions.DocumentFn.sendDoc(DocumentFn.java:771)
        at net.sf.saxon.functions.Doc.sendDocument(Doc.java:232)
        at com.saxonica.stream.StreamingCopy.process(StreamingCopy.java:353)
        at com.saxonica.stream.SequenceExchanger$EvaluationThread.run(SequenceExchanger.java:216)
        at java.lang.Thread.run(Unknown Source)
Error
  java.lang.NullPointerException
Exception in thread "main" net.sf.saxon.s9api.SaxonApiUncheckedException: java.lang.NullPointerException
        at net.sf.saxon.s9api.XdmSequenceIterator.hasNext(XdmSequenceIterator.java:67)
        at saxon.test.StreamFromDomSource.main(StreamFromDomSource.java:48)
Caused by: net.sf.saxon.trans.XPathException: java.lang.NullPointerException
        at com.saxonica.stream.SequenceExchanger$EvaluationThread.run(SequenceExchanger.java:225)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
        at com.sun.org.apache.xerces.internal.dom.DeferredElementNSImpl.synchronizeData(Unknown Source)
        at com.sun.org.apache.xerces.internal.dom.ElementImpl.getNodeName(Unknown Source)
        at net.sf.saxon.dom.DOMNodeWrapper.getPrefix(DOMNodeWrapper.java:523)
        at net.sf.saxon.om.NameOfNode.getPrefix(NameOfNode.java:27)
        at net.sf.saxon.om.NameOfNode.getNamespaceBinding(NameOfNode.java:93)
        at net.sf.saxon.event.StartTagBuffer.startElement(StartTagBuffer.java:136)
        at net.sf.saxon.event.Stripper.startElement(Stripper.java:109)
        at net.sf.saxon.tree.util.Navigator.copy(Navigator.java:602)
        at net.sf.saxon.dom.DOMNodeWrapper.copy(DOMNodeWrapper.java:762)
        at net.sf.saxon.tree.util.Navigator.copy(Navigator.java:592)
        at net.sf.saxon.dom.DOMNodeWrapper.copy(DOMNodeWrapper.java:762)
        at net.sf.saxon.event.Sender.sendDocumentInfo(Sender.java:259)
        at net.sf.saxon.event.Sender.send(Sender.java:124)
        at net.sf.saxon.functions.DocumentFn.sendDoc(DocumentFn.java:771)
        at net.sf.saxon.functions.Doc.sendDocument(Doc.java:232)
        at com.saxonica.stream.StreamingCopy.process(StreamingCopy.java:353)
        at com.saxonica.stream.SequenceExchanger$EvaluationThread.run(SequenceExchanger.java:216)
        ... 1 more
 
 
java.lang.IllegalArgumentException: type: -1
        at com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl.getNodeObject(Unknown Source)
        at com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl.synchronizeChildren(Unknown Source)
        at com.sun.org.apache.xerces.internal.dom.DeferredElementNSImpl.synchronizeChildren(Unknown Source)
        at com.sun.org.apache.xerces.internal.dom.ParentNode.getChildNodes(Unknown Source)
        at net.sf.saxon.dom.DOMNodeWrapper$ChildEnumeration.<init>(DOMNodeWrapper.java:1099)
        at net.sf.saxon.dom.DOMNodeWrapper.iterateChildren(DOMNodeWrapper.java:658)
        at net.sf.saxon.tree.wrapper.AbstractNodeWrapper.iterateAxis(AbstractNodeWrapper.java:416)
        at net.sf.saxon.tree.util.Navigator.copy(Navigator.java:635)
        at net.sf.saxon.dom.DOMNodeWrapper.copy(DOMNodeWrapper.java:762)
        at net.sf.saxon.tree.util.Navigator.copy(Navigator.java:592)
        at net.sf.saxon.dom.DOMNodeWrapper.copy(DOMNodeWrapper.java:762)
        at net.sf.saxon.event.Sender.sendDocumentInfo(Sender.java:259)
        at net.sf.saxon.event.Sender.send(Sender.java:124)
        at net.sf.saxon.functions.DocumentFn.sendDoc(DocumentFn.java:771)
        at net.sf.saxon.functions.Doc.sendDocument(Doc.java:232)
        at com.saxonica.stream.StreamingCopy.process(StreamingCopy.java:353)
        at com.saxonica.stream.SequenceExchanger$EvaluationThread.run(SequenceExchanger.java:216)
        at java.lang.Thread.run(Unknown Source)
Error
  java.lang.IllegalArgumentException: type: -1
Exception in thread "main" net.sf.saxon.s9api.SaxonApiUncheckedException: java.lang.IllegalArgumentException: type: -1
        at net.sf.saxon.s9api.XdmSequenceIterator.hasNext(XdmSequenceIterator.java:67)
        at saxon.test.StreamFromDomSource.main(StreamFromDomSource.java:47)
Caused by: net.sf.saxon.trans.XPathException: java.lang.IllegalArgumentException: type: -1
        at com.saxonica.stream.SequenceExchanger$EvaluationThread.run(SequenceExchanger.java:225)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: type: -1
        at com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl.getNodeObject(Unknown Source)
        at com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl.synchronizeChildren(Unknown Source)
        at com.sun.org.apache.xerces.internal.dom.DeferredElementNSImpl.synchronizeChildren(Unknown Source)
        at com.sun.org.apache.xerces.internal.dom.ParentNode.getChildNodes(Unknown Source)
        at net.sf.saxon.dom.DOMNodeWrapper$ChildEnumeration.<init>(DOMNodeWrapper.java:1099)
        at net.sf.saxon.dom.DOMNodeWrapper.iterateChildren(DOMNodeWrapper.java:658)
        at net.sf.saxon.tree.wrapper.AbstractNodeWrapper.iterateAxis(AbstractNodeWrapper.java:416)
        at net.sf.saxon.tree.util.Navigator.copy(Navigator.java:635)
        at net.sf.saxon.dom.DOMNodeWrapper.copy(DOMNodeWrapper.java:762)
        at net.sf.saxon.tree.util.Navigator.copy(Navigator.java:592)
        at net.sf.saxon.dom.DOMNodeWrapper.copy(DOMNodeWrapper.java:762)
        at net.sf.saxon.event.Sender.sendDocumentInfo(Sender.java:259)
        at net.sf.saxon.event.Sender.send(Sender.java:124)
        at net.sf.saxon.functions.DocumentFn.sendDoc(DocumentFn.java:771)
        at net.sf.saxon.functions.Doc.sendDocument(Doc.java:232)
        at com.saxonica.stream.StreamingCopy.process(StreamingCopy.java:353)
        at com.saxonica.stream.SequenceExchanger$EvaluationThread.run(SequenceExchanger.java:216)
        ... 1 more
 

Software AG – Sitz/Registered office: Uhlandstraße 12, 64297 Darmstadt, Germany – Registergericht/Commercial register: Darmstadt HRB 1562 - Vorstand/Management Board: Karl-Heinz Streibich (Vorsitzender/Chairman), Dr. Wolfram Jost, Arnd Zinnhardt; - Aufsichtsratsvorsitzender/Chairman of the Supervisory Board: Dr. Andreas Bereczky - http://www.softwareag.com