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: FunctionOneArg.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.XPathVisitor;
     27 
     28 /**
     29  * Base class for functions that accept one argument.
     30  * @xsl.usage advanced
     31  */
     32 public class FunctionOneArg extends Function implements ExpressionOwner
     33 {
     34     static final long serialVersionUID = -5180174180765609758L;
     35 
     36   /** The first argument passed to the function (at index 0).
     37    *  @serial  */
     38   Expression m_arg0;
     39 
     40   /**
     41    * Return the first argument passed to the function (at index 0).
     42    *
     43    * @return An expression that represents the first argument passed to the
     44    *         function.
     45    */
     46   public Expression getArg0()
     47   {
     48     return m_arg0;
     49   }
     50 
     51   /**
     52    * Set an argument expression for a function.  This method is called by the
     53    * XPath compiler.
     54    *
     55    * @param arg non-null expression that represents the argument.
     56    * @param argNum The argument number index.
     57    *
     58    * @throws WrongNumberArgsException If the argNum parameter is greater than 0.
     59    */
     60   public void setArg(Expression arg, int argNum)
     61           throws WrongNumberArgsException
     62   {
     63 
     64     if (0 == argNum)
     65     {
     66       m_arg0 = arg;
     67       arg.exprSetParent(this);
     68     }
     69     else
     70       reportWrongNumberArgs();
     71   }
     72 
     73   /**
     74    * Check that the number of arguments passed to this function is correct.
     75    *
     76    *
     77    * @param argNum The number of arguments that is being passed to the function.
     78    *
     79    * @throws WrongNumberArgsException
     80    */
     81   public void checkNumberArgs(int argNum) throws WrongNumberArgsException
     82   {
     83     if (argNum != 1)
     84       reportWrongNumberArgs();
     85   }
     86 
     87   /**
     88    * Constructs and throws a WrongNumberArgException with the appropriate
     89    * message for this function object.
     90    *
     91    * @throws WrongNumberArgsException
     92    */
     93   protected void reportWrongNumberArgs() throws WrongNumberArgsException {
     94       throw new WrongNumberArgsException(XSLMessages.createXPATHMessage("one", null));
     95   }
     96 
     97   /**
     98    * Tell if this expression or it's subexpressions can traverse outside
     99    * the current subtree.
    100    *
    101    * @return true if traversal outside the context node's subtree can occur.
    102    */
    103    public boolean canTraverseOutsideSubtree()
    104    {
    105     return m_arg0.canTraverseOutsideSubtree();
    106    }
    107 
    108   /**
    109    * This function is used to fixup variables from QNames to stack frame
    110    * indexes at stylesheet build time.
    111    * @param vars List of QNames that correspond to variables.  This list
    112    * should be searched backwards for the first qualified name that
    113    * corresponds to the variable reference qname.  The position of the
    114    * QName in the vector from the start of the vector will be its position
    115    * in the stack frame (but variables above the globalsTop value will need
    116    * to be offset to the current stack frame).
    117    */
    118   public void fixupVariables(java.util.Vector vars, int globalsSize)
    119   {
    120     if(null != m_arg0)
    121       m_arg0.fixupVariables(vars, globalsSize);
    122   }
    123 
    124   /**
    125    * @see org.apache.xpath.XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
    126    */
    127   public void callArgVisitors(XPathVisitor visitor)
    128   {
    129   	if(null != m_arg0)
    130   		m_arg0.callVisitors(this, visitor);
    131   }
    132 
    133 
    134   /**
    135    * @see ExpressionOwner#getExpression()
    136    */
    137   public Expression getExpression()
    138   {
    139     return m_arg0;
    140   }
    141 
    142   /**
    143    * @see ExpressionOwner#setExpression(Expression)
    144    */
    145   public void setExpression(Expression exp)
    146   {
    147   	exp.exprSetParent(this);
    148   	m_arg0 = exp;
    149   }
    150 
    151   /**
    152    * @see Expression#deepEquals(Expression)
    153    */
    154   public boolean deepEquals(Expression expr)
    155   {
    156   	if(!super.deepEquals(expr))
    157   		return false;
    158 
    159   	if(null != m_arg0)
    160   	{
    161   		if(null == ((FunctionOneArg)expr).m_arg0)
    162   			return false;
    163 
    164   		if(!m_arg0.deepEquals(((FunctionOneArg)expr).m_arg0))
    165   			return false;
    166   	}
    167   	else if(null != ((FunctionOneArg)expr).m_arg0)
    168   		return false;
    169 
    170   	return true;
    171   }
    172 
    173 
    174 }
    175