Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2018-2023 Saxonica Limited
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package net.sf.saxon.query;
import net.sf.saxon.Configuration;
import net.sf.saxon.Controller;
import net.sf.saxon.event.ComplexContentOutputter;
import net.sf.saxon.event.Outputter;
import net.sf.saxon.event.PipelineConfiguration;
import net.sf.saxon.event.Receiver;
import net.sf.saxon.expr.*;
import net.sf.saxon.expr.elab.PullEvaluator;
import net.sf.saxon.expr.elab.PushEvaluator;
import net.sf.saxon.expr.instruct.Executable;
import net.sf.saxon.expr.instruct.GlobalContextRequirement;
import net.sf.saxon.expr.instruct.GlobalVariable;
import net.sf.saxon.expr.instruct.SlotManager;
import net.sf.saxon.expr.parser.*;
import net.sf.saxon.lib.ErrorReporter;
import net.sf.saxon.lib.SerializerFactory;
import net.sf.saxon.lib.TraceListener;
import net.sf.saxon.om.*;
import net.sf.saxon.s9api.HostLanguage;
import net.sf.saxon.s9api.Location;
import net.sf.saxon.serialize.SerializationProperties;
import net.sf.saxon.trace.ExpressionPresenter;
import net.sf.saxon.trace.TraceableComponent;
import net.sf.saxon.trans.SaxonErrorCode;
import net.sf.saxon.trans.UncheckedXPathException;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.trans.XmlProcessingException;
import net.sf.saxon.tree.iter.GroundedIterator;
import net.sf.saxon.tree.iter.ManualIterator;
import net.sf.saxon.type.AnyItemType;
import net.sf.saxon.type.ItemType;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;
/**
* XQueryExpression represents a compiled query. This object is immutable and thread-safe,
* the same compiled query may be executed many times in series or in parallel. The object
* is intended to be created only by using the compileQuery method of the QueryProcessor class.
*
Various methods are provided for evaluating the query, with different options for
* delivery of the results.
*/
public class XQueryExpression implements Location, ExpressionOwner, TraceableComponent {
protected Expression expression;
protected SlotManager stackFrameMap;
protected Executable executable;
protected QueryModule mainModule;
protected PullEvaluator pullEvaluator = null;
protected PushEvaluator pushEvaluator = null;
/**
* The constructor should only be
* called using the compileQuery() methods of StaticQueryContext
* @param exp an expression to be wrapped as an XQueryExpression
* @param mainModule the static context of the main module
* @param streaming true if streamed execution is requested
* @throws XPathException if an error occurs
*/
public XQueryExpression(Expression exp, QueryModule mainModule, boolean streaming)
throws XPathException {
Executable exec = mainModule.getExecutable();
Configuration config = mainModule.getConfiguration();
stackFrameMap = config.makeSlotManager();
executable = exec;
this.mainModule = mainModule;
exp.setRetainedStaticContext(mainModule.makeRetainedStaticContext());
try {
ExpressionVisitor visitor = ExpressionVisitor.make(mainModule);
Optimizer optimizer = visitor.obtainOptimizer();
visitor.setOptimizeForStreaming(streaming);
exp = exp.simplify();
exp.checkForUpdatingSubexpressions();
GlobalContextRequirement contextReq = exec.getGlobalContextRequirement();
ItemType req = contextReq == null ? AnyItemType.getInstance() : contextReq.getRequiredItemType();
ContextItemStaticInfo cit = config.makeContextItemStaticInfo(req, true);
Expression e2 = exp.typeCheck(visitor, cit);
if (e2 != exp) {
e2.setRetainedStaticContext(exp.getRetainedStaticContext());
e2.setParentExpression(null);
exp = e2;
}
if (optimizer.isOptionSet(OptimizerOptions.MISCELLANEOUS)) {
e2 = exp.optimize(visitor, cit);
if (e2 != exp) {
e2.setRetainedStaticContext(exp.getRetainedStaticContext());
e2.setParentExpression(null);
exp = e2;
}
}
if (optimizer.isOptionSet(OptimizerOptions.LOOP_LIFTING)) {
e2 = LoopLifter.process(exp, visitor, cit);
if (e2 != exp) {
e2.setRetainedStaticContext(exp.getRetainedStaticContext());
e2.setParentExpression(null);
exp = e2;
}
}
} catch (XPathException err) {
//err.printStackTrace();
mainModule.reportStaticError(err);
throw err;
}
ExpressionTool.allocateSlots(exp, 0, stackFrameMap);
ExpressionTool.computeEvaluationModesForUserFunctionCalls(exp);
for (GlobalVariable var : getPackageData().getGlobalVariableList()) {
Expression top = var.getBody();
if (top != null) {
ExpressionTool.computeEvaluationModesForUserFunctionCalls(top);
}
}
expression = exp;
executable.setConfiguration(config);
}
/**
* Get the expression wrapped in this XQueryExpression object
*
* @return the underlying expression
*/
public Expression getExpression() {
return expression;
}
@Override
public Expression getBody() {
return getExpression();
}
@Override
public Expression getChildExpression() {
return expression;
}
@Override
public void setBody(Expression expression) {
setChildExpression(expression);
}
/**
* Get a name identifying the object of the expression, for example a function name, template name,
* variable name, key name, element name, etc. This is used only where the name is known statically.
*
* @return the QName of the object declared or manipulated by this instruction or expression
*/
@Override
public StructuredQName getObjectName() {
return null;
}
@Override
public String getTracingTag() {
return "query";
}
@Override
public Location getLocation() {
return this;
}
/**
* Get data about the unit of compilation (XQuery module, XSLT package) to which this
* container belongs
* @return the package information
*/
public PackageData getPackageData() {
return mainModule.getPackageData();
}
/**
* Get the Configuration to which this Container belongs
*
* @return the Configuration
*/
public Configuration getConfiguration() {
return mainModule.getConfiguration();
}
/**
* Ask whether this query uses the context item
*
* @return true if the context item is referenced either in the query body or in the initializer
* of any global variable
*/
public boolean usesContextItem() {
if (ExpressionTool.dependsOnFocus(expression)) {
return true;
}
List map = getPackageData().getGlobalVariableList();
if (map != null) {
for (GlobalVariable var : map) {
Expression select = var.getBody();
if (select != null && ExpressionTool.dependsOnFocus(select)) {
return true;
}
}
}
return false;
}
/**
* Ask whether this is an update query
*
* @return true if the body of the query is an updating expression
* (as defined by the XQuery Update specification). Note that a query can use Update syntax
* (notably the copy-modify syntax) without being an updating expression.
*/
public boolean isUpdateQuery() {
return false;
}
/**
* Get the stack frame map used for the outermost level of this query
*
* @return the stack frame map
*/
public SlotManager getStackFrameMap() {
return stackFrameMap;
}
/**
* Output the path map of the query for diagnostics
*/
public void explainPathMap() {
// No action (requires Saxon-EE)
}
/**
* Get the static context in which this expression was compiled. This is essentially an internal
* copy of the original user-created StaticQueryContext object, augmented with information obtained
* from the query prolog of the main query module, and with information about functions and variables
* imported from other library modules. The user-created StaticQueryContext object is not modified
* by Saxon, whereas the QueryModule object includes additional information found in the query prolog.
*
* @return the QueryModule object representing the static context of the main module of the query.
* This is available for inspection, but must not be modified or reused by the application.
*/
public QueryModule getMainModule() {
return mainModule;
}
/**
* Get a list containing the names of the external variables in the query.
*
Changed in Saxon 9.0 to return an array of StructuredQName objects rather than
* integer fingerprints.
*
* @return an array of StructuredQName objects, representing the names of external variables defined
* in the query
*/
/*@NotNull*/
public StructuredQName[] getExternalVariableNames() {
List list = stackFrameMap.getVariableMap();
StructuredQName[] names = new StructuredQName[stackFrameMap.getNumberOfVariables()];
for (int i = 0; i < names.length; i++) {
names[i] = list.get(i);
}
return names;
}
/**
* Execute a the compiled Query, returning the results as a List of objects
* obtained by converting the items in the query result to the nearest appropriate
* Java type
*
* @param env Provides the dynamic query evaluation context
* @return The results of the expression, as a List. The List represents the sequence
* of items returned by the expression. Each item in the list will either be an
* object representing a node, or an object representing an atomic value.
* For the types of Java object that may be returned, see the description of the
* {@link net.sf.saxon.xpath.XPathEvaluator#evaluate} method
* of class XPathProcessor
* @throws XPathException if a dynamic error occurs during query evaluation
*/
/*@NotNull*/
public List