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) 2013 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.*;
import net.sf.saxon.evpull.ComplexContentProcessor;
import net.sf.saxon.evpull.EventIterator;
import net.sf.saxon.evpull.EventIteratorToReceiver;
import net.sf.saxon.expr.*;
import net.sf.saxon.expr.instruct.Executable;
import net.sf.saxon.expr.instruct.GlobalParam;
import net.sf.saxon.expr.instruct.GlobalVariable;
import net.sf.saxon.expr.instruct.SlotManager;
import net.sf.saxon.expr.parser.ExpressionTool;
import net.sf.saxon.expr.parser.ExpressionVisitor;
import net.sf.saxon.expr.parser.Optimizer;
import net.sf.saxon.expr.parser.PathMap;
import net.sf.saxon.lib.FeatureKeys;
import net.sf.saxon.lib.SaxonOutputKeys;
import net.sf.saxon.lib.SerializerFactory;
import net.sf.saxon.lib.TraceListener;
import net.sf.saxon.om.*;
import net.sf.saxon.trace.ExpressionPresenter;
import net.sf.saxon.trans.SaxonErrorCode;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.tree.iter.SingletonIterator;
import net.sf.saxon.tree.iter.UnfailingIterator;
import javax.xml.transform.ErrorListener;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;
import java.io.OutputStream;
import java.util.*;
/**
* 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
* can 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 Container {
private Expression expression;
private SlotManager stackFrameMap;
private Executable executable;
private QueryModule staticContext;
private PathMap pathMap;
private boolean allowDocumentProjection;
private boolean isUpdating;
/**
* The constructor is protected, to ensure that instances can only be
* created using the compileQuery() methods of StaticQueryContext
*
* @param exp an expression to be wrapped as an XQueryExpression
* @param exec the executable
* @param mainModule the static context of the main module
* @param config the configuration
* @throws XPathException if an error occurs
*/
protected XQueryExpression(/*@NotNull*/ Expression exp, Executable exec, /*@NotNull*/ QueryModule mainModule, /*@NotNull*/ Configuration config)
throws XPathException {
stackFrameMap = config.makeSlotManager();
executable = exec;
exp.setContainer(this);
Optimizer optimizer = config.obtainOptimizer();
try {
ExpressionVisitor visitor = ExpressionVisitor.make(mainModule, exec);
visitor.setExecutable(exec);
exp = visitor.simplify(exp);
exp.checkForUpdatingSubexpressions();
ExpressionVisitor.ContextItemType cit = new ExpressionVisitor.ContextItemType(mainModule.getUserQueryContext().getRequiredContextItemType(), true);
exp = visitor.typeCheck(exp, cit);
// ExpressionPresenter presenter = new ExpressionPresenter(config,
// ExpressionPresenter.defaultDestination(config, new FileOutputStream("c:/projects/montreal/before50.xml")));
// exp.explain(presenter);
// presenter.close();
if (optimizer.getOptimizationLevel() != Optimizer.NO_OPTIMIZATION) {
exp = exp.optimize(visitor, cit);
}
} catch (XPathException err) {
//err.printStackTrace();
mainModule.reportFatalError(err);
throw err;
}
ExpressionTool.allocateSlots(exp, 0, stackFrameMap);
if ((Boolean)config.getConfigurationProperty(FeatureKeys.GENERATE_BYTE_CODE)
&& config.isLicensedFeature(Configuration.LicenseFeature.ENTERPRISE_XQUERY)) {
if (config.isTiming()) {
config.getStandardErrorOutput().println("Generating byte code...");
}
Expression cexp = optimizer.compileToByteCode(exp, "main",
Expression.PROCESS_METHOD | Expression.ITERATE_METHOD);
if (cexp != null) {
exp = cexp;
}
}
expression = exp;
executable.setConfiguration(config);
executable.setCollationMap(mainModule.getUserQueryContext().getAllCollations());
staticContext = mainModule;
isUpdating = exp.isUpdatingExpression();
}
/**
* Get the expression wrapped in this XQueryExpression object
*
* @return the underlying expression
*/
public Expression getExpression() {
return expression;
}
/**
* Get the granularity of the container.
* @return 0 for a temporary container created during parsing; 1 for a container
* that operates at the level of an XPath expression; 2 for a container at the level
* of a global function or template
*/
public int getContainerGranularity() {
return 2;
}
/**
* 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() {
StructuredQName contextVar = getExecutable().getInitialContextItemVariableName();
Binding[] binding = null;
if (contextVar != null) {
GlobalVariable cvar = getExecutable().getGlobalVariable(contextVar);
binding = new Binding[]{cvar};
}
if (ExpressionTool.dependsOnFocus(expression)) {
return true;
}
if (binding != null && ExpressionTool.dependsOnVariable(expression, binding)) {
return true;
}
HashMap map = executable.getCompiledGlobalVariables();
if (map != null) {
for (GlobalVariable var : map.values()) {
Expression select = var.getSelectExpression();
if (select != null && ExpressionTool.dependsOnFocus(select)) {
return true;
}
if (select != null && binding != null && ExpressionTool.dependsOnVariable(select, binding)) {
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 isUpdating;
}
/**
* Get the stack frame map used for the outermost level of this query
*
* @return the stack frame map
*/
public SlotManager getStackFrameMap() {
return stackFrameMap;
}
/**
* Replace one subexpression by a replacement subexpression. For internal use only
*
* @param original the original subexpression
* @param replacement the replacement subexpression
* @return true if the original subexpression is found
*/
// public boolean replaceSubExpression(Expression original, Expression replacement) {
// boolean found = false;
// if (expression == original) {
// expression = replacement;
// found = true;
// }
// return found;
// }
/**
* 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 getStaticContext() {
return staticContext;
}
/**
* 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] = (StructuredQName)list.get(i);
}
return names;
}
/**
* Execute a the compiled Query, returning the results as a List.
*
* @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 evaluate} method
* of class XPathProcessor
* @throws XPathException if a dynamic error occurs during query evaluation
*/
/*@NotNull*/ public List