All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.saxon.style.XSLFunction Maven / Gradle / Ivy

Go to download

Provides a basic XSLT 2.0 and XQuery 1.0 processor (W3C Recommendations, January 2007). Command line interfaces and implementations of several Java APIs (DOM, XPath, s9api) are also included.

The newest version!
package net.sf.saxon.style;
import net.sf.saxon.Configuration;
import net.sf.saxon.expr.*;
import net.sf.saxon.instruct.*;
import net.sf.saxon.om.*;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.value.SequenceType;
import net.sf.saxon.value.Whitespace;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
* Handler for xsl:function elements in stylesheet (XSLT 2.0). 
* Attributes:
* name gives the name of the function * saxon:memo-function=yes|no indicates whether it acts as a memo function. */ public class XSLFunction extends StyleElement implements StylesheetProcedure { private String nameAtt = null; private String asAtt = null; private String overrideAtt = null; private SequenceType resultType; private String functionName; private SlotManager stackFrameMap; private boolean memoFunction = false; private boolean override = true; private int numberOfArguments = -1; // -1 means not yet known private UserFunction compiledFunction; // List of UserFunctionCall objects that reference this XSLFunction List references = new ArrayList(10); /** * Method called by UserFunctionCall to register the function call for * subsequent fixup. * @param ref the UserFunctionCall to be registered */ public void registerReference(UserFunctionCall ref) { references.add(ref); } public void prepareAttributes() throws XPathException { AttributeCollection atts = getAttributeList(); overrideAtt = "yes"; for (int a=0; a=0; i--) { Object child = toplevel.get(i); if (child instanceof XSLFunction && !(child == this) && ((XSLFunction)child).getObjectName().equals(getObjectName()) && ((XSLFunction)child).getNumberOfArguments() == numberOfArguments) { if (((XSLFunction)child).getPrecedence() == getPrecedence()) { isDuplicate = true; } if (((XSLFunction)child).getPrecedence() > getPrecedence()) { // it's not an error to have duplicates if there is another with higher precedence isDuplicate = false; break; } } } if (isDuplicate) { compileError("Duplicate function declaration", "XTSE0770"); } } /** * Compile the function definition to create an executable representation * @return an Instruction, or null. The instruction returned is actually * rather irrelevant; the compile() method has the side-effect of binding * all references to the function to the executable representation * (a UserFunction object) * @throws XPathException */ public Expression compile(Executable exec) throws XPathException { compileAsExpression(exec); return null; } /** * Compile the function into a UserFunction object, which treats the function * body as a single XPath expression. This involves recursively translating * xsl:variable declarations into let expressions, withe the action part of the * let expression containing the rest of the function body. * The UserFunction that is created will be linked from all calls to * this function, so nothing else needs to be done with the result. If there are * no calls to it, the compiled function will be garbage-collected away. * @param exec the Executable * @throws XPathException */ private void compileAsExpression(Executable exec) throws XPathException { //Block body = new Block(); //compileChildren(exec, body, false); Expression exp = compileSequenceConstructor(exec, iterateAxis(Axis.CHILD), false); if (exp == null) { exp = Literal.makeEmptySequence(); } //Expression exp = body; UserFunction fn = new UserFunction(); fn.setHostLanguage(Configuration.XSLT); fn.setBody(exp); fn.setFunctionName(getObjectName(getNamePool())); setParameterDefinitions(fn); //fn.setParameterDefinitions(getParameterDefinitions()); //fn.setArgumentTypes(getArgumentTypes()); fn.setResultType(getResultType()); fn.setLineNumber(getLineNumber()); fn.setSystemId(getSystemId()); fn.setStackFrameMap(stackFrameMap); fn.setMemoFunction(memoFunction); fn.setExecutable(exec); Expression exp2 = exp; ExpressionVisitor visitor = makeExpressionVisitor(); try { // We've already done the typecheck of each XPath expression, but it's worth doing again at this // level because we have more information now. exp2 = visitor.typeCheck(exp, null); if (resultType != null) { RoleLocator role = new RoleLocator(RoleLocator.FUNCTION_RESULT, functionName, 0, null); role.setSourceLocator(new ExpressionLocation(this)); role.setErrorCode("XTTE0780"); exp2 = TypeChecker.staticTypeCheck(exp2, resultType, false, role, visitor); } exp2 = exp2.optimize(visitor, null); } catch (XPathException err) { err.maybeSetLocation(this); compileError(err); } // Try to extract new global variables from the body of the function exp2 = getConfiguration().getOptimizer().promoteExpressionsToGlobal(exp2, visitor); // Add trace wrapper code if required if (getPreparedStylesheet().isCompileWithTracing()) { TraceWrapper trace = new TraceInstruction(exp2, this); trace.setLocationId(allocateLocationId(getSystemId(), getLineNumber())); exp2 = trace; } allocateSlots(exp2); if (exp2 != exp) { fn.setBody(exp2); } int tailCalls = ExpressionTool.markTailFunctionCalls(exp2, getObjectName(), getNumberOfArguments()); if (tailCalls != 0) { fn.setTailRecursive(tailCalls > 0, tailCalls > 1); fn.setBody(new TailCallLoop(fn)); } fixupInstruction(fn); compiledFunction = fn; fn.computeEvaluationMode(); if (isExplaining()) { exp2.explain(System.err); } } /** * Fixup all function references. * @param compiledFunction the Instruction representing this function in the compiled code * @throws XPathException if an error occurs. */ private void fixupInstruction(UserFunction compiledFunction) throws XPathException { ExpressionVisitor visitor = makeExpressionVisitor(); try { Iterator iter = references.iterator(); while (iter.hasNext()) { UserFunctionCall call = ((UserFunctionCall)iter.next()); call.setFunction(compiledFunction); call.checkFunctionCall(compiledFunction, visitor); call.computeArgumentEvaluationModes(); } } catch (XPathException err) { compileError(err); } } /** * Get associated Procedure (for details of stack frame). * @return the associated Procedure object */ public SlotManager getSlotManager() { return stackFrameMap; } /** * Get the type of value returned by this function * @return the declared result type, or the inferred result type * if this is more precise */ public SequenceType getResultType() { return resultType; } /** * Get the number of arguments declared by this function (that is, its arity). * @return the arity of the function */ public int getNumberOfArguments() { if (numberOfArguments == -1) { numberOfArguments = 0; AxisIterator kids = iterateAxis(Axis.CHILD); while (true) { Item child = kids.next(); if (child instanceof XSLParam) { numberOfArguments++; } else { return numberOfArguments; } } } return numberOfArguments; } /** * Set the definitions of the parameters in the compiled function, as an array. * @param fn the compiled object representing the user-written function */ public void setParameterDefinitions(UserFunction fn) { UserFunctionParameter[] params = new UserFunctionParameter[getNumberOfArguments()]; fn.setParameterDefinitions(params); int count = 0; AxisIterator kids = iterateAxis(Axis.CHILD); while (true) { NodeInfo node = (NodeInfo)kids.next(); if (node == null) { return; } if (node instanceof XSLParam) { UserFunctionParameter param = new UserFunctionParameter(); params[count++] = param; param.setRequiredType(((XSLParam)node).getRequiredType()); param.setVariableQName(((XSLParam)node).getVariableQName()); param.setSlotNumber(((XSLParam)node).getSlotNumber()); ((XSLParam)node).fixupBinding(param); int refs = ExpressionTool.getReferenceCount(fn.getBody(), param, false); param.setReferenceCount(refs); } } } /** * Get the compiled function * @return the object representing the compiled user-written function */ public UserFunction getCompiledFunction() { return compiledFunction; } /** * Get the type of construct. This will be a constant in * class {@link net.sf.saxon.trace.Location}. This method is part of the * {@link net.sf.saxon.trace.InstructionInfo} interface */ public int getConstructType() { return StandardNames.XSL_FUNCTION; } } // // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License"); // you may not use this file except in compliance with the License. You may obtain a copy of the // License at http://www.mozilla.org/MPL/ // // Software distributed under the License is distributed on an "AS IS" basis, // WITHOUT WARRANTY OF ANY KIND, either express or implied. // See the License for the specific language governing rights and limitations under the License. // // The Original Code is: all this file. // // The Initial Developer of the Original Code is Michael H. Kay. // // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved. // // Contributor(s): // Portions marked "e.g." are from Edwin Glaser ([email protected]) //




© 2015 - 2025 Weber Informatics LLC | Privacy Policy