blob: 12db741e2fa323c65f5ba4793a50a9049f5d510e [file] [log] [blame]
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xalan" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 1999, Lotus
* Development Corporation., http://www.lotus.com. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.xpath.functions;
//import org.w3c.dom.Node;
import javax.xml.transform.TransformerException;
import org.apache.xalan.res.XSLMessages;
import org.apache.xpath.Expression;
import org.apache.xpath.ExpressionNode;
import org.apache.xpath.ExpressionOwner;
import org.apache.xpath.XPathContext;
import org.apache.xpath.XPathVisitor;
import org.apache.xpath.objects.XObject;
import org.apache.xpath.parser.Node;
import org.apache.xpath.parser.PathExpr;
/**
* <meta name="usage" content="advanced"/>
* This is a superclass of all XPath functions. This allows two
* ways for the class to be called. One method is that the
* super class processes the arguments and hands the results to
* the derived class, the other method is that the derived
* class may process it's own arguments, which is faster since
* the arguments don't have to be added to an array, but causes
* a larger code footprint.
*/
public abstract class Function extends Expression implements Cloneable
{
/**
* Set an argument expression for a function. This method is called by the
* XPath compiler.
*
* @param arg non-null expression that represents the argument.
* @param argNum The argument number index.
*
* @throws WrongNumberArgsException If the argNum parameter is beyond what
* is specified for this function.
*/
public void setArg(Expression arg, int argNum)
throws WrongNumberArgsException
{
// throw new WrongNumberArgsException(XSLMessages.createXPATHMessage("zero", null));
reportWrongNumberArgs();
}
/**
* @see ExpressionOwner#setExpression(Expression)
*/
public void jjtAddChild(Node exp, int argNum)
{
try
{
exp = fixupPrimarys(exp);
setArg((Expression)exp, argNum);
}
catch(WrongNumberArgsException wnae)
{
throw new org.apache.xml.utils.WrappedRuntimeException(wnae);
}
}
/**
* Check that the number of arguments passed to this function is correct.
* This method is meant to be overloaded by derived classes, to check for
* the number of arguments for a specific function type. This method is
* called by the compiler for static number of arguments checking.
*
* @param argNum The number of arguments that is being passed to the function.
*
* @throws WrongNumberArgsException
*/
public void checkNumberArgs(int argNum) throws WrongNumberArgsException
{
if (argNum != 0)
reportWrongNumberArgs();
}
/**
* Constructs and throws a WrongNumberArgException with the appropriate
* message for this function object. This method is meant to be overloaded
* by derived classes so that the message will be as specific as possible.
*
* @throws WrongNumberArgsException
*/
protected void reportWrongNumberArgs() throws WrongNumberArgsException {
throw new WrongNumberArgsException(XSLMessages.createXPATHMessage("zero", null));
}
/**
* Execute an XPath function object. The function must return
* a valid object.
* @param xctxt The execution current context.
* @return A valid XObject.
*
* @throws javax.xml.transform.TransformerException
*/
public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
{
// Programmer's assert. (And, no, I don't want the method to be abstract).
System.out.println("Error! Function.execute should not be called!");
return null;
}
/**
* Call the visitors for the function arguments.
*/
public void callArgVisitors(XPathVisitor visitor)
{
}
/**
* @see XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
*/
public void callVisitors(ExpressionOwner owner, XPathVisitor visitor)
{
if(visitor.visitFunction(owner, this))
{
callArgVisitors(visitor);
}
}
/**
* @see Expression#deepEquals(Expression)
*/
public boolean deepEquals(Expression expr)
{
if(!isSameClass(expr))
return false;
return true;
}
/**
* This function is currently only being used by Position()
* and Last(). See respective functions for more detail.
*/
public void postCompileStep(Compiler compiler)
{
// no default action
}
// public void jjtAddChild(org.apache.xpath.parser.Node n, int i) {
// // at least for now, just ignore this.
// }
/** This method tells the node to add its argument to the node's
list of children. */
public void exprAddChild(ExpressionNode n, int i)
{
try
{
setArg((Expression)n, i);
}
catch(WrongNumberArgsException wnae)
{
throw new RuntimeException(wnae.getMessage());
}
}
/**
* Tell if this node should have it's PathExpr ancestory reduced.
*/
public boolean isPathExprReduced()
{
Node pathOwner = jjtGetParent().jjtGetParent();
return pathOwner.jjtGetNumChildren() == 1 ? true : false;
}
}