Author: shawkins
Date: 2011-08-04 13:08:29 -0400 (Thu, 04 Aug 2011)
New Revision: 3362
Added:
branches/7.4.x/engine/src/main/java/org/teiid/query/xquery/saxon/XQueryEvaluator.java
Modified:
branches/7.4.x/engine/src/main/java/org/teiid/query/eval/Evaluator.java
branches/7.4.x/engine/src/main/java/org/teiid/query/xquery/saxon/SaxonXQueryExpression.java
branches/7.4.x/engine/src/main/java/org/teiid/query/xquery/saxon/StreamingUtils.java
Log:
TEIID-1695 isolating xom/nux so that designer doesn't need the dependencies.
Modified: branches/7.4.x/engine/src/main/java/org/teiid/query/eval/Evaluator.java
===================================================================
--- branches/7.4.x/engine/src/main/java/org/teiid/query/eval/Evaluator.java 2011-08-04
15:31:44 UTC (rev 3361)
+++ branches/7.4.x/engine/src/main/java/org/teiid/query/eval/Evaluator.java 2011-08-04
17:08:29 UTC (rev 3362)
@@ -118,6 +118,7 @@
import org.teiid.query.sql.util.VariableContext;
import org.teiid.query.util.CommandContext;
import org.teiid.query.xquery.saxon.SaxonXQueryExpression;
+import org.teiid.query.xquery.saxon.XQueryEvaluator;
import org.teiid.query.xquery.saxon.SaxonXQueryExpression.Result;
import org.teiid.query.xquery.saxon.SaxonXQueryExpression.RowProcessor;
import org.teiid.translator.WSConnection.Util;
@@ -923,7 +924,7 @@
parameters.put(passing.getAlias(), value);
}
}
- return xquery.evaluateXQuery(contextItem, parameters, processor, context);
+ return XQueryEvaluator.evaluateXQuery(xquery, contextItem, parameters, processor,
context);
}
private Evaluator.NameValuePair<Object>[] getNameValuePairs(List<?> tuple,
List<DerivedColumn> args, boolean xmlNames)
Modified:
branches/7.4.x/engine/src/main/java/org/teiid/query/xquery/saxon/SaxonXQueryExpression.java
===================================================================
---
branches/7.4.x/engine/src/main/java/org/teiid/query/xquery/saxon/SaxonXQueryExpression.java 2011-08-04
15:31:44 UTC (rev 3361)
+++
branches/7.4.x/engine/src/main/java/org/teiid/query/xquery/saxon/SaxonXQueryExpression.java 2011-08-04
17:08:29 UTC (rev 3362)
@@ -23,9 +23,7 @@
package org.teiid.query.xquery.saxon;
import java.io.IOException;
-import java.io.InputStream;
import java.io.Writer;
-import java.sql.SQLXML;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
@@ -40,9 +38,7 @@
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
-import net.sf.saxon.AugmentedSource;
import net.sf.saxon.Configuration;
-import net.sf.saxon.event.ProxyReceiver;
import net.sf.saxon.expr.AxisExpression;
import net.sf.saxon.expr.ContextItemExpression;
import net.sf.saxon.expr.Expression;
@@ -53,14 +49,12 @@
import net.sf.saxon.expr.PathMap.PathMapNodeSet;
import net.sf.saxon.expr.PathMap.PathMapRoot;
import net.sf.saxon.om.Axis;
-import net.sf.saxon.om.DocumentInfo;
import net.sf.saxon.om.Item;
import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.om.SequenceIterator;
import net.sf.saxon.om.StructuredQName;
import net.sf.saxon.pattern.AnyNodeTest;
import net.sf.saxon.pattern.NodeKindTest;
-import net.sf.saxon.query.DynamicQueryContext;
import net.sf.saxon.query.QueryResult;
import net.sf.saxon.query.StaticQueryContext;
import net.sf.saxon.query.XQueryExpression;
@@ -72,12 +66,6 @@
import net.sf.saxon.type.ItemType;
import net.sf.saxon.type.TypeHierarchy;
import net.sf.saxon.value.SequenceType;
-import nu.xom.Builder;
-import nu.xom.Element;
-import nu.xom.Nodes;
-import nu.xom.ParsingException;
-import nux.xom.xquery.StreamingPathFilter;
-import nux.xom.xquery.StreamingTransform;
import org.teiid.api.exception.query.QueryResolverException;
import org.teiid.common.buffer.BufferManager;
@@ -89,9 +77,6 @@
import org.teiid.core.types.XMLTranslator;
import org.teiid.core.types.XMLType;
import org.teiid.core.types.XMLType.Type;
-import org.teiid.logging.LogConstants;
-import org.teiid.logging.LogManager;
-import org.teiid.logging.MessageLevel;
import org.teiid.query.QueryPlugin;
import org.teiid.query.analysis.AnalysisRecord;
import org.teiid.query.function.source.XMLSystemFunctions;
@@ -100,7 +85,6 @@
import org.teiid.query.sql.symbol.DerivedColumn;
import org.teiid.query.sql.symbol.XMLNamespaces;
import org.teiid.query.sql.symbol.XMLNamespaces.NamespaceItem;
-import org.teiid.query.util.CommandContext;
import org.teiid.translator.WSConnection.Util;
@SuppressWarnings("serial")
@@ -113,15 +97,6 @@
DEFAULT_OUTPUT_PROPERTIES.setProperty(OutputKeys.OMIT_XML_DECLARATION,
"yes"); //$NON-NLS-1$
}
- private static Nodes NONE = new Nodes();
- private static InputStream FAKE_IS = new InputStream() {
-
- @Override
- public int read() throws IOException {
- return 0;
- }
- };
-
public interface RowProcessor {
void processRow(NodeInfo row);
@@ -182,12 +157,12 @@
}
};
- private XQueryExpression xQuery;
- private String xQueryString;
- private Map<String, String> namespaceMap = new HashMap<String, String>();
- private Configuration config = new Configuration();
- private PathMapRoot contextRoot;
- private StreamingPathFilter streamingPathFilter;
+ XQueryExpression xQuery;
+ String xQueryString;
+ Map<String, String> namespaceMap = new HashMap<String, String>();
+ Configuration config = new Configuration();
+ PathMapRoot contextRoot;
+ String streamingPath;
public SaxonXQueryExpression(String xQueryString, XMLNamespaces namespaces,
List<DerivedColumn> passing, List<XMLTable.XMLColumn> columns)
throws QueryResolverException {
@@ -246,7 +221,7 @@
clone.config = config;
clone.contextRoot = contextRoot;
clone.namespaceMap = namespaceMap;
- clone.streamingPathFilter = streamingPathFilter;
+ clone.streamingPath = streamingPath;
return clone;
}
@@ -256,7 +231,7 @@
public void useDocumentProjection(List<XMLTable.XMLColumn> columns, AnalysisRecord
record) {
try {
- streamingPathFilter = StreamingUtils.getStreamingPathFilter(xQueryString,
namespaceMap);
+ streamingPath = StreamingUtils.getStreamingPath(xQueryString, namespaceMap);
} catch (IllegalArgumentException e) {
if (record.recordDebug()) {
record.println("Document streaming will not be used: " + e.getMessage());
//$NON-NLS-1$
@@ -362,8 +337,8 @@
continue;
}
for (PathMapArc arc : subContextRoot.getArcs()) {
- if (streamingPathFilter != null && !validateColumnForStreaming(record,
xmlColumn, arc)) {
- streamingPathFilter = null;
+ if (streamingPath != null && !validateColumnForStreaming(record,
xmlColumn, arc)) {
+ streamingPath = null;
}
finalNode.createArc(arc.getStep(), arc.getTarget());
}
@@ -491,90 +466,7 @@
}
}
- public Result evaluateXQuery(Object context, Map<String, Object>
parameterValues, final RowProcessor processor, CommandContext commandContext) throws
TeiidProcessingException {
- DynamicQueryContext dynamicContext = new DynamicQueryContext(config);
-
- Result result = new Result();
- try {
- try {
- for (Map.Entry<String, Object> entry : parameterValues.entrySet()) {
- Object value = entry.getValue();
- if(value instanceof SQLXML) {
- value = XMLSystemFunctions.convertToSource(value);
- result.sources.add((Source)value);
- } else if (value instanceof java.util.Date) {
- value = XMLSystemFunctions.convertToAtomicValue(value);
- }
- dynamicContext.setParameter(entry.getKey(), value);
- }
- } catch (TransformerException e) {
- throw new TeiidProcessingException(e);
- }
- if (context != null) {
- Source source = XMLSystemFunctions.convertToSource(context);
- result.sources.add(source);
- if (contextRoot != null) {
- //create our own filter as this logic is not provided in the free saxon
- ProxyReceiver filter = new PathMapFilter(contextRoot);
- AugmentedSource sourceInput =
AugmentedSource.makeAugmentedSource(source);
- sourceInput.addFilter(filter);
- source = sourceInput;
-
- //use streamable processing instead
- if (streamingPathFilter != null && processor != null) {
- if (LogManager.isMessageToBeRecorded(LogConstants.CTX_DQP,
MessageLevel.DETAIL)) {
- LogManager.logDetail(LogConstants.CTX_DQP, "Using stream
processing for evaluation of", this.xQueryString); //$NON-NLS-1$
- }
- //set to non-blocking in case default expression evaluation blocks
- boolean isNonBlocking = commandContext.isNonBlocking();
- commandContext.setNonBlocking(true);
-
- final StreamingTransform myTransform = new StreamingTransform() {
- public Nodes transform(Element elem) {
- processor.processRow(StreamingUtils.wrap(elem, config));
- return NONE;
- }
- };
-
- Builder builder = new Builder(new SaxonReader(config, sourceInput), false,
- streamingPathFilter.createNodeFactory(null, myTransform));
- try {
- //the builder is hard wired to parse the source, but the api will throw an
exception if the stream is null
- builder.build(FAKE_IS);
- return result;
- } catch (ParsingException e) {
- throw new TeiidProcessingException(e,
QueryPlugin.Util.getString("SaxonXQueryExpression.bad_context")); //$NON-NLS-1$
- } catch (IOException e) {
- throw new TeiidProcessingException(e,
QueryPlugin.Util.getString("SaxonXQueryExpression.bad_context")); //$NON-NLS-1$
- } finally {
- if (!isNonBlocking) {
- commandContext.setNonBlocking(false);
- }
- }
- }
- }
- DocumentInfo doc;
- try {
- doc = config.buildDocument(source);
- } catch (XPathException e) {
- throw new TeiidProcessingException(e,
QueryPlugin.Util.getString("SaxonXQueryExpression.bad_context")); //$NON-NLS-1$
- }
- dynamicContext.setContextItem(doc);
- }
- try {
- result.iter = xQuery.iterator(dynamicContext);
- return result;
- } catch (TransformerException e) {
- throw new TeiidProcessingException(e,
QueryPlugin.Util.getString("SaxonXQueryExpression.bad_xquery")); //$NON-NLS-1$
- }
- } finally {
- if (result.iter == null) {
- result.close();
- }
- }
- }
-
- public XMLType createXMLType(final SequenceIterator iter, BufferManager bufferManager,
boolean emptyOnEmpty) throws XPathException, TeiidComponentException,
TeiidProcessingException {
+ public XMLType createXMLType(final SequenceIterator iter, BufferManager
bufferManager, boolean emptyOnEmpty) throws XPathException, TeiidComponentException,
TeiidProcessingException {
Item item = iter.next();
if (item == null && !emptyOnEmpty) {
return null;
@@ -634,7 +526,7 @@
}
public boolean isStreaming() {
- return streamingPathFilter != null;
+ return streamingPath != null;
}
}
Modified:
branches/7.4.x/engine/src/main/java/org/teiid/query/xquery/saxon/StreamingUtils.java
===================================================================
---
branches/7.4.x/engine/src/main/java/org/teiid/query/xquery/saxon/StreamingUtils.java 2011-08-04
15:31:44 UTC (rev 3361)
+++
branches/7.4.x/engine/src/main/java/org/teiid/query/xquery/saxon/StreamingUtils.java 2011-08-04
17:08:29 UTC (rev 3362)
@@ -32,11 +32,7 @@
import net.sf.saxon.event.ProxyReceiver;
import net.sf.saxon.event.Receiver;
import net.sf.saxon.om.Name11Checker;
-import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.trans.XPathException;
-import nu.xom.DocType;
-import nu.xom.Node;
-import nux.xom.xquery.StreamingPathFilter;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
@@ -51,28 +47,6 @@
final class StreamingUtils {
/**
- * Converts a xom node into something readable by Saxon
- * @param node
- * @param config
- * @return
- */
- static NodeInfo wrap(Node node, Configuration config) {
- if (node == null)
- throw new IllegalArgumentException("node must not be null"); //$NON-NLS-1$
- if (node instanceof DocType)
- throw new IllegalArgumentException("DocType can't be queried by
XQuery/XPath"); //$NON-NLS-1$
-
- Node root = node;
- while (root.getParent() != null) {
- root = root.getParent();
- }
-
- DocumentWrapper docWrapper = new DocumentWrapper(root, root.getBaseURI(), config);
-
- return docWrapper.wrap(node);
- }
-
- /**
* Pre-parser that adds validation and handles a default name space
*
* TODO: add support for more general paths including node tests
@@ -83,7 +57,7 @@
* @param prefixMap
* @return
*/
- public static StreamingPathFilter getStreamingPathFilter(String locationPath,
Map<String, String> prefixMap) {
+ public static String getStreamingPath(String locationPath, Map<String, String>
prefixMap) {
if (locationPath.indexOf("//") >= 0) //$NON-NLS-1$
throw new IllegalArgumentException("DESCENDANT axis is not supported");
//$NON-NLS-1$
@@ -124,7 +98,7 @@
}
fixedPath += localNames[i];
}
- return new StreamingPathFilter(fixedPath, prefixMap);
+ return fixedPath;
}
}
Added:
branches/7.4.x/engine/src/main/java/org/teiid/query/xquery/saxon/XQueryEvaluator.java
===================================================================
--- branches/7.4.x/engine/src/main/java/org/teiid/query/xquery/saxon/XQueryEvaluator.java
(rev 0)
+++
branches/7.4.x/engine/src/main/java/org/teiid/query/xquery/saxon/XQueryEvaluator.java 2011-08-04
17:08:29 UTC (rev 3362)
@@ -0,0 +1,177 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * See the COPYRIGHT.txt file distributed with this work for information
+ * regarding copyright ownership. Some portions may be licensed
+ * to Red Hat, Inc. under one or more contributor license agreements.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA.
+ */
+
+package org.teiid.query.xquery.saxon;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.sql.SQLXML;
+import java.util.Map;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
+
+import net.sf.saxon.AugmentedSource;
+import net.sf.saxon.Configuration;
+import net.sf.saxon.event.ProxyReceiver;
+import net.sf.saxon.om.DocumentInfo;
+import net.sf.saxon.om.NodeInfo;
+import net.sf.saxon.query.DynamicQueryContext;
+import net.sf.saxon.trans.XPathException;
+import nu.xom.Builder;
+import nu.xom.DocType;
+import nu.xom.Element;
+import nu.xom.Node;
+import nu.xom.Nodes;
+import nu.xom.ParsingException;
+import nux.xom.xquery.StreamingPathFilter;
+import nux.xom.xquery.StreamingTransform;
+
+import org.teiid.core.TeiidProcessingException;
+import org.teiid.logging.LogConstants;
+import org.teiid.logging.LogManager;
+import org.teiid.logging.MessageLevel;
+import org.teiid.query.QueryPlugin;
+import org.teiid.query.function.source.XMLSystemFunctions;
+import org.teiid.query.util.CommandContext;
+import org.teiid.query.xquery.saxon.SaxonXQueryExpression.RowProcessor;
+
+/**
+ * Used to isolate the xom/nux dependency and to better isolate the saxon processing
logic.
+ */
+public class XQueryEvaluator {
+
+ private static Nodes NONE = new Nodes();
+ private static InputStream FAKE_IS = new InputStream() {
+
+ @Override
+ public int read() throws IOException {
+ return 0;
+ }
+ };
+
+ public static SaxonXQueryExpression.Result evaluateXQuery(final SaxonXQueryExpression
xquery, Object context, Map<String, Object> parameterValues, final RowProcessor
processor, CommandContext commandContext) throws TeiidProcessingException {
+ DynamicQueryContext dynamicContext = new DynamicQueryContext(xquery.config);
+
+ SaxonXQueryExpression.Result result = new SaxonXQueryExpression.Result();
+ try {
+ try {
+ for (Map.Entry<String, Object> entry : parameterValues.entrySet()) {
+ Object value = entry.getValue();
+ if(value instanceof SQLXML) {
+ value = XMLSystemFunctions.convertToSource(value);
+ result.sources.add((Source)value);
+ } else if (value instanceof java.util.Date) {
+ value = XMLSystemFunctions.convertToAtomicValue(value);
+ }
+ dynamicContext.setParameter(entry.getKey(), value);
+ }
+ } catch (TransformerException e) {
+ throw new TeiidProcessingException(e);
+ }
+ if (context != null) {
+ Source source = XMLSystemFunctions.convertToSource(context);
+ result.sources.add(source);
+ if (xquery.contextRoot != null) {
+ //create our own filter as this logic is not provided in the free saxon
+ ProxyReceiver filter = new PathMapFilter(xquery.contextRoot);
+ AugmentedSource sourceInput =
AugmentedSource.makeAugmentedSource(source);
+ sourceInput.addFilter(filter);
+ source = sourceInput;
+
+ //use streamable processing instead
+ if (xquery.streamingPath != null && processor != null) {
+ if (LogManager.isMessageToBeRecorded(LogConstants.CTX_DQP,
MessageLevel.DETAIL)) {
+ LogManager.logDetail(LogConstants.CTX_DQP, "Using stream
processing for evaluation of", xquery.xQueryString); //$NON-NLS-1$
+ }
+ //set to non-blocking in case default expression evaluation blocks
+ boolean isNonBlocking = commandContext.isNonBlocking();
+ commandContext.setNonBlocking(true);
+
+ final StreamingTransform myTransform = new StreamingTransform() {
+ public Nodes transform(Element elem) {
+ processor.processRow(XQueryEvaluator.wrap(elem, xquery.config));
+ return NONE;
+ }
+ };
+
+ Builder builder = new Builder(new SaxonReader(xquery.config, sourceInput), false,
+ new StreamingPathFilter(xquery.streamingPath,
xquery.namespaceMap).createNodeFactory(null, myTransform));
+ try {
+ //the builder is hard wired to parse the source, but the api will throw an
exception if the stream is null
+ builder.build(FAKE_IS);
+ return result;
+ } catch (ParsingException e) {
+ throw new TeiidProcessingException(e,
QueryPlugin.Util.getString("SaxonXQueryExpression.bad_context")); //$NON-NLS-1$
+ } catch (IOException e) {
+ throw new TeiidProcessingException(e,
QueryPlugin.Util.getString("SaxonXQueryExpression.bad_context")); //$NON-NLS-1$
+ } finally {
+ if (!isNonBlocking) {
+ commandContext.setNonBlocking(false);
+ }
+ }
+ }
+ }
+ DocumentInfo doc;
+ try {
+ doc = xquery.config.buildDocument(source);
+ } catch (XPathException e) {
+ throw new TeiidProcessingException(e,
QueryPlugin.Util.getString("SaxonXQueryExpression.bad_context")); //$NON-NLS-1$
+ }
+ dynamicContext.setContextItem(doc);
+ }
+ try {
+ result.iter = xquery.xQuery.iterator(dynamicContext);
+ return result;
+ } catch (TransformerException e) {
+ throw new TeiidProcessingException(e,
QueryPlugin.Util.getString("SaxonXQueryExpression.bad_xquery")); //$NON-NLS-1$
+ }
+ } finally {
+ if (result.iter == null) {
+ result.close();
+ }
+ }
+ }
+
+ /**
+ * Converts a xom node into something readable by Saxon
+ * @param node
+ * @param config
+ * @return
+ */
+ static NodeInfo wrap(Node node, Configuration config) {
+ if (node == null)
+ throw new IllegalArgumentException("node must not be null"); //$NON-NLS-1$
+ if (node instanceof DocType)
+ throw new IllegalArgumentException("DocType can't be queried by
XQuery/XPath"); //$NON-NLS-1$
+
+ Node root = node;
+ while (root.getParent() != null) {
+ root = root.getParent();
+ }
+
+ DocumentWrapper docWrapper = new DocumentWrapper(root, root.getBaseURI(), config);
+
+ return docWrapper.wrap(node);
+ }
+
+}
Property changes on:
branches/7.4.x/engine/src/main/java/org/teiid/query/xquery/saxon/XQueryEvaluator.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain