/** * Make an expression visitor * @return the expression visitor */ public ExpressionVisitor makeExpressionVisitor() { ExpressionVisitor visitor = ExpressionVisitor.make(staticContext); visitor.setExecutable(getExecutable()); return visitor; }
/** * Make an expression visitor * @return the expression visitor */ public ExpressionVisitor makeExpressionVisitor() { ExpressionVisitor visitor = ExpressionVisitor.make(staticContext); visitor.setExecutable(getExecutable()); return visitor; }
/** * Perform type checking on global variables. * <p>This method is intended for internal use</p> * @param compiledVars a list of {@link GlobalVariableDefinition} objects to be checked */ public void typeCheckGlobalVariables(List compiledVars) throws XPathException { ExpressionVisitor visitor = ExpressionVisitor.make(this); visitor.setExecutable(getExecutable()); Iterator iter = compiledVars.iterator(); //Stack stack = new Stack(); while (iter.hasNext()) { GlobalVariableDefinition gvd = (GlobalVariableDefinition)iter.next(); //GlobalVariable gv = gvd.getCompiledVariable(); //gv.lookForCycles(stack); gvd.typeCheck(visitor); } }
/** * Perform type checking on global variables. * <p>This method is intended for internal use</p> * @param compiledVars a list of {@link GlobalVariableDefinition} objects to be checked */ public void typeCheckGlobalVariables(List compiledVars) throws XPathException { ExpressionVisitor visitor = ExpressionVisitor.make(this); visitor.setExecutable(getExecutable()); Iterator iter = compiledVars.iterator(); //Stack stack = new Stack(); while (iter.hasNext()) { GlobalVariableDefinition gvd = (GlobalVariableDefinition)iter.next(); //GlobalVariable gv = gvd.getCompiledVariable(); //gv.lookForCycles(stack); gvd.typeCheck(visitor); } }
/** * Fixup all references to global functions. This method is called * on completion of query parsing. Each XQueryFunction is required to * bind all references to that function to the object representing the run-time * executable code of the function. * <p> * This method is for internal use. * @param env the static context for the main query body. */ protected void fixupGlobalFunctions(QueryModule env) throws XPathException { ExpressionVisitor visitor = ExpressionVisitor.make(env); Iterator iter = functions.values().iterator(); while (iter.hasNext()) { XQueryFunction fn = (XQueryFunction)iter.next(); fn.compile(); } iter = functions.values().iterator(); while (iter.hasNext()) { XQueryFunction fn = (XQueryFunction)iter.next(); visitor.setExecutable(fn.getExecutable()); fn.checkReferences(visitor); } }
/** * Fixup all references to global functions. This method is called * on completion of query parsing. Each XQueryFunction is required to * bind all references to that function to the object representing the run-time * executable code of the function. * <p> * This method is for internal use. * @param env the static context for the main query body. */ protected void fixupGlobalFunctions(QueryModule env) throws XPathException { ExpressionVisitor visitor = ExpressionVisitor.make(env); Iterator iter = functions.values().iterator(); while (iter.hasNext()) { XQueryFunction fn = (XQueryFunction)iter.next(); fn.compile(); } iter = functions.values().iterator(); while (iter.hasNext()) { XQueryFunction fn = (XQueryFunction)iter.next(); visitor.setExecutable(fn.getExecutable()); fn.checkReferences(visitor); } }
/** * Convert an expression so it generates a space-separated sequence of strings * * @param exp the expression that calculates the content * @param noNodeIfEmpty if true, no node is produced when the value of the content * expression is an empty sequence. If false, the effect of supplying an empty sequence * is that a node is created whose string-value is a zero-length string. Set to true for * text node constructors, false for other kinds of node. * @return an expression that computes the content and converts the result to a character string */ private Expression stringify(Expression exp, boolean noNodeIfEmpty) throws XPathException { return ExpressionVisitor.make(env).simplify(new QuerySimpleContentConstructor( exp, new StringLiteral(StringValue.SINGLE_SPACE), noNodeIfEmpty)); }
/** * Convert an expression so it generates a space-separated sequence of strings * * @param exp the expression that calculates the content * @param noNodeIfEmpty if true, no node is produced when the value of the content * expression is an empty sequence. If false, the effect of supplying an empty sequence * is that a node is created whose string-value is a zero-length string. Set to true for * text node constructors, false for other kinds of node. * @return an expression that computes the content and converts the result to a character string */ private Expression stringify(Expression exp, boolean noNodeIfEmpty) throws XPathException { return ExpressionVisitor.make(env).simplify(new QuerySimpleContentConstructor( exp, new StringLiteral(StringValue.SINGLE_SPACE), noNodeIfEmpty)); }
/** * Parse an expression. This performs the basic analysis of the expression against the * grammar, it binds variable references and function calls to variable definitions and * function definitions, and it performs context-independent expression rewriting for * optimization purposes. * * @param expression The expression (as a character string) * @param env An object giving information about the compile-time * context of the expression * @param start position of the first significant character in the expression * @param terminator The token that marks the end of this expression; typically * Tokenizer.EOF, but may for example be a right curly brace * @param lineNumber the line number of the start of the expression * @param compileWithTracing true if diagnostic tracing during expression parsing is required * @return an object of type Expression * @throws XPathException if the expression contains a static error */ public static Expression make(String expression, StaticContext env, int start, int terminator, int lineNumber, boolean compileWithTracing) throws XPathException { ExpressionParser parser = new ExpressionParser(); parser.setCompileWithTracing(compileWithTracing); if (terminator == -1) { terminator = Token.EOF; } Expression exp = parser.parse(expression, start, terminator, lineNumber, env); exp = ExpressionVisitor.make(env).simplify(exp); return exp; }
/** * Prepare (compile) an XSLT pattern for subsequent evaluation. The result is an XPathExpression * object representing a (pseudo-) expression that when evaluated returns a boolean result: true * if the context node matches the pattern, false if it does not. * @param pattern the XSLT pattern to be compiled, supplied as a string * @return an XPathExpression object representing the pattern, wrapped as an expression * @throws XPathException if the syntax of the expression is wrong, or if it references namespaces, * variables, or functions that have not been declared. * @since 9.1 */ public XPathExpression createPattern(String pattern) throws XPathException { Pattern pat = Pattern.make(pattern, staticContext, staticContext.getExecutable()); ExpressionVisitor visitor = ExpressionVisitor.make(staticContext); pat.analyze(visitor, Type.NODE_TYPE); SlotManager map = staticContext.getStackFrameMap(); int slots = map.getNumberOfVariables(); slots = pat.allocateSlots(staticContext, map, slots); PatternSponsor sponsor = new PatternSponsor(pat); XPathExpression xpe = new XPathExpression(this, sponsor); xpe.setStackFrameMap(map, slots); return xpe; }
/** * Static method to make a Pattern by parsing a String. <br> * @param pattern The pattern text as a String * @param env An object defining the compile-time context for the expression * @param exec The executable containing this pattern * @return The pattern object */ public static Pattern make(String pattern, StaticContext env, Executable exec) throws XPathException { Pattern pat = (new ExpressionParser()).parsePattern(pattern, env); pat.setSystemId(env.getSystemId()); pat.setLineNumber(env.getLineNumber()); // System.err.println("Simplified [" + pattern + "] to " + pat.getClass() + " default prio = " + pat.getDefaultPriority()); // set the pattern text for use in diagnostics pat.setOriginalText(pattern); pat.setExecutable(exec); ExpressionVisitor visitor = ExpressionVisitor.make(env); visitor.setExecutable(exec); pat = pat.simplify(visitor); return pat; }
/** * Prepare (compile) an XPath expression for subsequent evaluation. * @param expression The XPath expression to be compiled, supplied as a string. * @return an XPathExpression object representing the prepared expression * @throws XPathException if the syntax of the expression is wrong, or if it references namespaces, * variables, or functions that have not been declared. */ public XPathExpression createExpression(String expression) throws XPathException { Expression exp = ExpressionTool.make(expression, staticContext, 0, -1, 1, false); exp.setContainer(staticContext); ExpressionVisitor visitor = ExpressionVisitor.make(staticContext); visitor.setExecutable(getExecutable()); exp = visitor.typeCheck(exp, Type.ITEM_TYPE); SlotManager map = staticContext.getStackFrameMap(); int numberOfExternalVariables = map.getNumberOfVariables(); ExpressionTool.allocateSlots(exp, numberOfExternalVariables, map); XPathExpression xpe = new XPathExpression(this, exp); xpe.setStackFrameMap(map, numberOfExternalVariables); return xpe; }
/** * Static method to make a Pattern by parsing a String. <br> * @param pattern The pattern text as a String * @param env An object defining the compile-time context for the expression * @param exec The executable containing this pattern * @return The pattern object */ public static Pattern make(String pattern, StaticContext env, Executable exec) throws XPathException { Pattern pat = (new ExpressionParser()).parsePattern(pattern, env); pat.setSystemId(env.getSystemId()); pat.setLineNumber(env.getLineNumber()); // System.err.println("Simplified [" + pattern + "] to " + pat.getClass() + " default prio = " + pat.getDefaultPriority()); // set the pattern text for use in diagnostics pat.setOriginalText(pattern); pat.setExecutable(exec); ExpressionVisitor visitor = ExpressionVisitor.make(env); visitor.setExecutable(exec); pat = pat.simplify(visitor); return pat; }
/** * Static method to make a Pattern by parsing a String. <br> * @param pattern The pattern text as a String * @param env An object defining the compile-time context for the expression * @param exec The executable containing this pattern * @return The pattern object */ public static Pattern make(String pattern, StaticContext env, Executable exec) throws XPathException { Pattern pat = (new ExpressionParser()).parsePattern(pattern, env); pat.setSystemId(env.getSystemId()); pat.setLineNumber(env.getLineNumber()); // System.err.println("Simplified [" + pattern + "] to " + pat.getClass() + " default prio = " + pat.getDefaultPriority()); // set the pattern text for use in diagnostics pat.setOriginalText(pattern); pat.setExecutable(exec); ExpressionVisitor visitor = ExpressionVisitor.make(env); visitor.setExecutable(exec); pat = pat.simplify(visitor); return pat; }
private XPathExpressionImpl createExpressionInternal(String expression) throws net.sf.saxon.trans.XPathException { Expression exp = ExpressionTool.make(expression, staticContext, 0, -1, 1, false); ExpressionVisitor visitor = ExpressionVisitor.make(staticContext); visitor.setExecutable(getExecutable()); exp = visitor.typeCheck(exp, Type.ITEM_TYPE); SlotManager map = staticContext.getConfiguration().makeSlotManager(); ExpressionTool.allocateSlots(exp, 0, map); exp.setContainer(staticContext); XPathExpressionImpl xpe = new XPathExpressionImpl(exp, getExecutable()); xpe.setStackFrameMap(map); if (contextNode != null) { xpe.privatelySetContextNode(contextNode); } return xpe; }
/** * Prepare (compile) an XPath expression for subsequent evaluation. * @param expression The XPath expression to be compiled, supplied as a string. * @return an XPathExpression object representing the prepared expression * @throws XPathException if the syntax of the expression is wrong, or if it references namespaces, * variables, or functions that have not been declared. */ public XPathExpression createExpression(String expression) throws XPathException { Expression exp = ExpressionTool.make(expression, staticContext, 0, -1, 1, false); exp.setContainer(staticContext); ExpressionVisitor visitor = ExpressionVisitor.make(staticContext); visitor.setExecutable(getExecutable()); exp = visitor.typeCheck(exp, Type.ITEM_TYPE); exp = visitor.optimize(exp, Type.ITEM_TYPE); SlotManager map = staticContext.getStackFrameMap(); int numberOfExternalVariables = map.getNumberOfVariables(); ExpressionTool.allocateSlots(exp, numberOfExternalVariables, map); XPathExpression xpe = new XPathExpression(this, exp); xpe.setStackFrameMap(map, numberOfExternalVariables); return xpe; }
/** * Optimize the body of this function */ public void optimize() throws XPathException { ExpressionVisitor visitor = ExpressionVisitor.make(staticContext); visitor.setExecutable(getExecutable()); // body = visitor.simplify(body); // body = visitor.typeCheck(body, null); body = visitor.optimize(body, null); // Try to extract new global variables from the body of the function body = staticContext.getConfiguration().getOptimizer().promoteExpressionsToGlobal(body, visitor); // mark tail calls within the function body int arity = arguments.size(); int tailCalls = ExpressionTool.markTailFunctionCalls(body, functionName, arity); if (tailCalls != 0) { compiledFunction.setBody(body); compiledFunction.setTailRecursive(tailCalls > 0, tailCalls > 1); body = new TailCallLoop(compiledFunction); } body.setContainer(this); compiledFunction.setBody(body); compiledFunction.computeEvaluationMode(); ExpressionTool.allocateSlots(body, arity, compiledFunction.getStackFrameMap()); }
/** * Parse the expression (inside a string literal) used to define default values * for external variables. This requires instantiating a nested XPath parser. * @param exp holds the expression used to define a default value * @return the compiled expression that computes the default value */ public Expression setDefaultValue(String exp) { try { IndependentContext ic = new IndependentContext(env.getConfiguration()); ic.setNamespaceResolver(env.getNamespaceResolver()); Expression expr = ExpressionTool.make(exp, ic, 0, Token.EOF, 1, false); ItemType contextItemType = Type.ITEM_TYPE; ExpressionVisitor visitor = ExpressionVisitor.make(ic); expr = visitor.typeCheck(expr, contextItemType); expr = visitor.optimize(expr, contextItemType); SlotManager stackFrameMap = ic.getStackFrameMap(); ExpressionTool.allocateSlots(expr, stackFrameMap.getNumberOfVariables(), stackFrameMap); return expr; } catch (XPathException e) { try { warning("Invalid expression for default value: " + e.getMessage() + " (ignored)"); } catch (XPathException staticError) { // } return null; } }
/** * Parse the expression (inside a string literal) used to define default values * for external variables. This requires instantiating a nested XPath parser. * @param exp holds the expression used to define a default value * @return the compiled expression that computes the default value */ public Expression setDefaultValue(String exp) { try { IndependentContext ic = new IndependentContext(env.getConfiguration()); ic.setNamespaceResolver(env.getNamespaceResolver()); Expression expr = ExpressionTool.make(exp, ic, 0, Token.EOF, 1, false); ItemType contextItemType = Type.ITEM_TYPE; ExpressionVisitor visitor = ExpressionVisitor.make(ic); expr = visitor.typeCheck(expr, contextItemType); expr = visitor.optimize(expr, contextItemType); SlotManager stackFrameMap = ic.getStackFrameMap(); ExpressionTool.allocateSlots(expr, stackFrameMap.getNumberOfVariables(), stackFrameMap); return expr; } catch (XPathException e) { try { warning("Invalid expression for default value: " + e.getMessage() + " (ignored)"); } catch (XPathException staticError) { // } return null; } }
/** * Prepare and execute an XPath expression, supplied as a string, and returning the first * item in the result. This is useful where it is known that the expression will only return * a singleton value (for example, a single node, or a boolean). * @param expression The XPath expression to be evaluated, supplied as a string. * @return The first item in the sequence returned by the expression. If the expression * returns an empty sequence, this method returns null. Otherwise, it returns the first * item in the result sequence, represented as a Java object using the same mapping as for * the evaluate() method * @deprecated since Saxon 8.9 - use the methods defined in the JAXP interface */ public Object evaluateSingle(String expression) throws net.sf.saxon.trans.XPathException { Expression exp = ExpressionTool.make(expression, staticContext,0,-1,1, false); ExpressionVisitor visitor = ExpressionVisitor.make(staticContext); visitor.setExecutable(getExecutable()); exp = visitor.typeCheck(exp, Type.ITEM_TYPE); SlotManager map = staticContext.getConfiguration().makeSlotManager(); ExpressionTool.allocateSlots(exp, 0, map); XPathContextMajor context = new XPathContextMajor(contextNode, staticContext.getExecutable()); context.openStackFrame(map); SequenceIterator iterator = exp.iterate(context); Item item = iterator.next(); if (item == null) { return null; } else { return Value.convertToJava(item); } }