286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: FuncExtFunction.java,v 1.2.4.2 2005/09/14 20:18:43 jeffsuttor Exp $
286N/A */
286N/Apackage com.sun.org.apache.xpath.internal.functions;
286N/A
286N/Aimport java.util.Vector;
286N/A
286N/Aimport com.sun.org.apache.xalan.internal.res.XSLMessages;
286N/Aimport com.sun.org.apache.xpath.internal.Expression;
286N/Aimport com.sun.org.apache.xpath.internal.ExpressionNode;
286N/Aimport com.sun.org.apache.xpath.internal.ExpressionOwner;
286N/Aimport com.sun.org.apache.xpath.internal.ExtensionsProvider;
286N/Aimport com.sun.org.apache.xpath.internal.XPathContext;
286N/Aimport com.sun.org.apache.xpath.internal.XPathVisitor;
286N/Aimport com.sun.org.apache.xpath.internal.objects.XNull;
286N/Aimport com.sun.org.apache.xpath.internal.objects.XObject;
286N/Aimport com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
286N/Aimport com.sun.org.apache.xpath.internal.res.XPATHMessages;
286N/A
286N/A/**
286N/A * An object of this class represents an extension call expression. When
286N/A * the expression executes, it calls ExtensionsTable#extFunction, and then
286N/A * converts the result to the appropriate XObject.
286N/A * @xsl.usage advanced
286N/A */
286N/Apublic class FuncExtFunction extends Function
286N/A{
286N/A static final long serialVersionUID = 5196115554693708718L;
286N/A
286N/A /**
286N/A * The namespace for the extension function, which should not normally
286N/A * be null or empty.
286N/A * @serial
286N/A */
286N/A String m_namespace;
286N/A
286N/A /**
286N/A * The local name of the extension.
286N/A * @serial
286N/A */
286N/A String m_extensionName;
286N/A
286N/A /**
286N/A * Unique method key, which is passed to ExtensionsTable#extFunction in
286N/A * order to allow caching of the method.
286N/A * @serial
286N/A */
286N/A Object m_methodKey;
286N/A
286N/A /**
286N/A * Array of static expressions which represent the parameters to the
286N/A * function.
286N/A * @serial
286N/A */
286N/A Vector m_argVec = new Vector();
286N/A
286N/A /**
286N/A * This function is used to fixup variables from QNames to stack frame
286N/A * indexes at stylesheet build time.
286N/A * @param vars List of QNames that correspond to variables. This list
286N/A * should be searched backwards for the first qualified name that
286N/A * corresponds to the variable reference qname. The position of the
286N/A * QName in the vector from the start of the vector will be its position
286N/A * in the stack frame (but variables above the globalsTop value will need
286N/A * to be offset to the current stack frame).
286N/A * NEEDSDOC @param globalsSize
286N/A */
286N/A public void fixupVariables(java.util.Vector vars, int globalsSize)
286N/A {
286N/A
286N/A if (null != m_argVec)
286N/A {
286N/A int nArgs = m_argVec.size();
286N/A
286N/A for (int i = 0; i < nArgs; i++)
286N/A {
286N/A Expression arg = (Expression) m_argVec.elementAt(i);
286N/A
286N/A arg.fixupVariables(vars, globalsSize);
286N/A }
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Return the namespace of the extension function.
286N/A *
286N/A * @return The namespace of the extension function.
286N/A */
286N/A public String getNamespace()
286N/A {
286N/A return m_namespace;
286N/A }
286N/A
286N/A /**
286N/A * Return the name of the extension function.
286N/A *
286N/A * @return The name of the extension function.
286N/A */
286N/A public String getFunctionName()
286N/A {
286N/A return m_extensionName;
286N/A }
286N/A
286N/A /**
286N/A * Return the method key of the extension function.
286N/A *
286N/A * @return The method key of the extension function.
286N/A */
286N/A public Object getMethodKey()
286N/A {
286N/A return m_methodKey;
286N/A }
286N/A
286N/A /**
286N/A * Return the nth argument passed to the extension function.
286N/A *
286N/A * @param n The argument number index.
286N/A * @return The Expression object at the given index.
286N/A */
286N/A public Expression getArg(int n) {
286N/A if (n >= 0 && n < m_argVec.size())
286N/A return (Expression) m_argVec.elementAt(n);
286N/A else
286N/A return null;
286N/A }
286N/A
286N/A /**
286N/A * Return the number of arguments that were passed
286N/A * into this extension function.
286N/A *
286N/A * @return The number of arguments.
286N/A */
286N/A public int getArgCount() {
286N/A return m_argVec.size();
286N/A }
286N/A
286N/A /**
286N/A * Create a new FuncExtFunction based on the qualified name of the extension,
286N/A * and a unique method key.
286N/A *
286N/A * @param namespace The namespace for the extension function, which should
286N/A * not normally be null or empty.
286N/A * @param extensionName The local name of the extension.
286N/A * @param methodKey Unique method key, which is passed to
286N/A * ExtensionsTable#extFunction in order to allow caching
286N/A * of the method.
286N/A */
286N/A public FuncExtFunction(java.lang.String namespace,
286N/A java.lang.String extensionName, Object methodKey)
286N/A {
286N/A //try{throw new Exception("FuncExtFunction() " + namespace + " " + extensionName);} catch (Exception e){e.printStackTrace();}
286N/A m_namespace = namespace;
286N/A m_extensionName = extensionName;
286N/A m_methodKey = methodKey;
286N/A }
286N/A
286N/A /**
286N/A * Execute the function. The function must return
286N/A * a valid object.
286N/A * @param xctxt The current execution context.
286N/A * @return A valid XObject.
286N/A *
286N/A * @throws javax.xml.transform.TransformerException
286N/A */
286N/A public XObject execute(XPathContext xctxt)
286N/A throws javax.xml.transform.TransformerException
286N/A {
286N/A if (xctxt.isSecureProcessing())
286N/A throw new javax.xml.transform.TransformerException(
286N/A XPATHMessages.createXPATHMessage(
286N/A XPATHErrorResources.ER_EXTENSION_FUNCTION_CANNOT_BE_INVOKED,
286N/A new Object[] {toString()}));
286N/A
286N/A XObject result;
286N/A Vector argVec = new Vector();
286N/A int nArgs = m_argVec.size();
286N/A
286N/A for (int i = 0; i < nArgs; i++)
286N/A {
286N/A Expression arg = (Expression) m_argVec.elementAt(i);
286N/A
286N/A XObject xobj = arg.execute(xctxt);
286N/A /*
286N/A * Should cache the arguments for func:function
286N/A */
286N/A xobj.allowDetachToRelease(false);
286N/A argVec.addElement(xobj);
286N/A }
286N/A //dml
286N/A ExtensionsProvider extProvider = (ExtensionsProvider)xctxt.getOwnerObject();
286N/A Object val = extProvider.extFunction(this, argVec);
286N/A
286N/A if (null != val)
286N/A {
286N/A result = XObject.create(val, xctxt);
286N/A }
286N/A else
286N/A {
286N/A result = new XNull();
286N/A }
286N/A
286N/A return result;
286N/A }
286N/A
286N/A /**
286N/A * Set an argument expression for a function. This method is called by the
286N/A * XPath compiler.
286N/A *
286N/A * @param arg non-null expression that represents the argument.
286N/A * @param argNum The argument number index.
286N/A *
286N/A * @throws WrongNumberArgsException If the argNum parameter is beyond what
286N/A * is specified for this function.
286N/A */
286N/A public void setArg(Expression arg, int argNum)
286N/A throws WrongNumberArgsException
286N/A {
286N/A m_argVec.addElement(arg);
286N/A arg.exprSetParent(this);
286N/A }
286N/A
286N/A /**
286N/A * Check that the number of arguments passed to this function is correct.
286N/A *
286N/A *
286N/A * @param argNum The number of arguments that is being passed to the function.
286N/A *
286N/A * @throws WrongNumberArgsException
286N/A */
286N/A public void checkNumberArgs(int argNum) throws WrongNumberArgsException{}
286N/A
286N/A
286N/A class ArgExtOwner implements ExpressionOwner
286N/A {
286N/A
286N/A Expression m_exp;
286N/A
286N/A ArgExtOwner(Expression exp)
286N/A {
286N/A m_exp = exp;
286N/A }
286N/A
286N/A /**
286N/A * @see ExpressionOwner#getExpression()
286N/A */
286N/A public Expression getExpression()
286N/A {
286N/A return m_exp;
286N/A }
286N/A
286N/A
286N/A /**
286N/A * @see ExpressionOwner#setExpression(Expression)
286N/A */
286N/A public void setExpression(Expression exp)
286N/A {
286N/A exp.exprSetParent(FuncExtFunction.this);
286N/A m_exp = exp;
286N/A }
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Call the visitors for the function arguments.
286N/A */
286N/A public void callArgVisitors(XPathVisitor visitor)
286N/A {
286N/A for (int i = 0; i < m_argVec.size(); i++)
286N/A {
286N/A Expression exp = (Expression)m_argVec.elementAt(i);
286N/A exp.callVisitors(new ArgExtOwner(exp), visitor);
286N/A }
286N/A
286N/A }
286N/A
286N/A /**
286N/A * Set the parent node.
286N/A * For an extension function, we also need to set the parent
286N/A * node for all argument expressions.
286N/A *
286N/A * @param n The parent node
286N/A */
286N/A public void exprSetParent(ExpressionNode n)
286N/A {
286N/A
286N/A super.exprSetParent(n);
286N/A
286N/A int nArgs = m_argVec.size();
286N/A
286N/A for (int i = 0; i < nArgs; i++)
286N/A {
286N/A Expression arg = (Expression) m_argVec.elementAt(i);
286N/A
286N/A arg.exprSetParent(n);
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Constructs and throws a WrongNumberArgException with the appropriate
286N/A * message for this function object. This class supports an arbitrary
286N/A * number of arguments, so this method must never be called.
286N/A *
286N/A * @throws WrongNumberArgsException
286N/A */
286N/A protected void reportWrongNumberArgs() throws WrongNumberArgsException {
286N/A String fMsg = XSLMessages.createXPATHMessage(
286N/A XPATHErrorResources.ER_INCORRECT_PROGRAMMER_ASSERTION,
286N/A new Object[]{ "Programmer's assertion: the method FunctionMultiArgs.reportWrongNumberArgs() should never be called." });
286N/A
286N/A throw new RuntimeException(fMsg);
286N/A }
286N/A
286N/A /**
286N/A * Return the name of the extesion function in string format
286N/A */
286N/A public String toString()
286N/A {
286N/A if (m_namespace != null && m_namespace.length() > 0)
286N/A return "{" + m_namespace + "}" + m_extensionName;
286N/A else
286N/A return m_extensionName;
286N/A }
286N/A}