Home | History | Annotate | Download | only in functions
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one
      3  * or more contributor license agreements. See the NOTICE file
      4  * distributed with this work for additional information
      5  * regarding copyright ownership. The ASF licenses this file
      6  * to you under the Apache License, Version 2.0 (the  "License");
      7  * you may not use this file except in compliance with the License.
      8  * You may obtain a copy of the License at
      9  *
     10  *     http://www.apache.org/licenses/LICENSE-2.0
     11  *
     12  * Unless required by applicable law or agreed to in writing, software
     13  * distributed under the License is distributed on an "AS IS" BASIS,
     14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15  * See the License for the specific language governing permissions and
     16  * limitations under the License.
     17  */
     18 /*
     19  * $Id: Function.java 468655 2006-10-28 07:12:06Z minchau $
     20  */
     21 package org.apache.xpath.functions;
     22 
     23 import org.apache.xalan.res.XSLMessages;
     24 import org.apache.xpath.Expression;
     25 import org.apache.xpath.ExpressionOwner;
     26 import org.apache.xpath.XPathContext;
     27 import org.apache.xpath.XPathVisitor;
     28 import org.apache.xpath.compiler.Compiler;
     29 import org.apache.xpath.objects.XObject;
     30 
     31 /**
     32  * This is a superclass of all XPath functions.  This allows two
     33  * ways for the class to be called. One method is that the
     34  * super class processes the arguments and hands the results to
     35  * the derived class, the other method is that the derived
     36  * class may process it's own arguments, which is faster since
     37  * the arguments don't have to be added to an array, but causes
     38  * a larger code footprint.
     39  * @xsl.usage advanced
     40  */
     41 public abstract class Function extends Expression
     42 {
     43     static final long serialVersionUID = 6927661240854599768L;
     44 
     45   /**
     46    * Set an argument expression for a function.  This method is called by the
     47    * XPath compiler.
     48    *
     49    * @param arg non-null expression that represents the argument.
     50    * @param argNum The argument number index.
     51    *
     52    * @throws WrongNumberArgsException If the argNum parameter is beyond what
     53    * is specified for this function.
     54    */
     55   public void setArg(Expression arg, int argNum)
     56           throws WrongNumberArgsException
     57   {
     58 			// throw new WrongNumberArgsException(XSLMessages.createXPATHMessage("zero", null));
     59       reportWrongNumberArgs();
     60   }
     61 
     62   /**
     63    * Check that the number of arguments passed to this function is correct.
     64    * This method is meant to be overloaded by derived classes, to check for
     65    * the number of arguments for a specific function type.  This method is
     66    * called by the compiler for static number of arguments checking.
     67    *
     68    * @param argNum The number of arguments that is being passed to the function.
     69    *
     70    * @throws WrongNumberArgsException
     71    */
     72   public void checkNumberArgs(int argNum) throws WrongNumberArgsException
     73   {
     74     if (argNum != 0)
     75       reportWrongNumberArgs();
     76   }
     77 
     78   /**
     79    * Constructs and throws a WrongNumberArgException with the appropriate
     80    * message for this function object.  This method is meant to be overloaded
     81    * by derived classes so that the message will be as specific as possible.
     82    *
     83    * @throws WrongNumberArgsException
     84    */
     85   protected void reportWrongNumberArgs() throws WrongNumberArgsException {
     86       throw new WrongNumberArgsException(XSLMessages.createXPATHMessage("zero", null));
     87   }
     88 
     89   /**
     90    * Execute an XPath function object.  The function must return
     91    * a valid object.
     92    * @param xctxt The execution current context.
     93    * @return A valid XObject.
     94    *
     95    * @throws javax.xml.transform.TransformerException
     96    */
     97   public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException
     98   {
     99 
    100     // Programmer's assert.  (And, no, I don't want the method to be abstract).
    101     System.out.println("Error! Function.execute should not be called!");
    102 
    103     return null;
    104   }
    105 
    106   /**
    107    * Call the visitors for the function arguments.
    108    */
    109   public void callArgVisitors(XPathVisitor visitor)
    110   {
    111   }
    112 
    113 
    114   /**
    115    * @see org.apache.xpath.XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
    116    */
    117   public void callVisitors(ExpressionOwner owner, XPathVisitor visitor)
    118   {
    119   	if(visitor.visitFunction(owner, this))
    120   	{
    121   		callArgVisitors(visitor);
    122   	}
    123   }
    124 
    125   /**
    126    * @see Expression#deepEquals(Expression)
    127    */
    128   public boolean deepEquals(Expression expr)
    129   {
    130   	if(!isSameClass(expr))
    131   		return false;
    132 
    133   	return true;
    134   }
    135 
    136   /**
    137    * This function is currently only being used by Position()
    138    * and Last(). See respective functions for more detail.
    139    */
    140   public void postCompileStep(Compiler compiler)
    141   {
    142     // no default action
    143   }
    144 }
    145