1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * @LastModified: Oct 2017
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.xpath.internal.functions;
  23 
  24 import com.sun.org.apache.xalan.internal.res.XSLMessages;
  25 import com.sun.org.apache.xml.internal.utils.QName;
  26 import com.sun.org.apache.xpath.internal.Expression;
  27 import com.sun.org.apache.xpath.internal.ExpressionOwner;
  28 import com.sun.org.apache.xpath.internal.XPathVisitor;
  29 import java.util.List;
  30 
  31 /**
  32  * Base class for functions that accept one argument.
  33  * @xsl.usage advanced
  34  */
  35 public class FunctionOneArg extends Function implements ExpressionOwner
  36 {
  37     static final long serialVersionUID = -5180174180765609758L;
  38 
  39   /** The first argument passed to the function (at index 0).
  40    *  @serial  */
  41   Expression m_arg0;
  42 
  43   /**
  44    * Return the first argument passed to the function (at index 0).
  45    *
  46    * @return An expression that represents the first argument passed to the
  47    *         function.
  48    */
  49   public Expression getArg0()
  50   {
  51     return m_arg0;
  52   }
  53 
  54   /**
  55    * Set an argument expression for a function.  This method is called by the
  56    * XPath compiler.
  57    *
  58    * @param arg non-null expression that represents the argument.
  59    * @param argNum The argument number index.
  60    *
  61    * @throws WrongNumberArgsException If the argNum parameter is greater than 0.
  62    */
  63   public void setArg(Expression arg, int argNum)
  64           throws WrongNumberArgsException
  65   {
  66 
  67     if (0 == argNum)
  68     {
  69       m_arg0 = arg;
  70       arg.exprSetParent(this);
  71     }
  72     else
  73       reportWrongNumberArgs();
  74   }
  75 
  76   /**
  77    * Check that the number of arguments passed to this function is correct.
  78    *
  79    *
  80    * @param argNum The number of arguments that is being passed to the function.
  81    *
  82    * @throws WrongNumberArgsException
  83    */
  84   public void checkNumberArgs(int argNum) throws WrongNumberArgsException
  85   {
  86     if (argNum != 1)
  87       reportWrongNumberArgs();
  88   }
  89 
  90   /**
  91    * Constructs and throws a WrongNumberArgException with the appropriate
  92    * message for this function object.
  93    *
  94    * @throws WrongNumberArgsException
  95    */
  96   protected void reportWrongNumberArgs() throws WrongNumberArgsException {
  97       throw new WrongNumberArgsException(XSLMessages.createXPATHMessage("one", null));
  98   }
  99 
 100   /**
 101    * Tell if this expression or it's subexpressions can traverse outside
 102    * the current subtree.
 103    *
 104    * @return true if traversal outside the context node's subtree can occur.
 105    */
 106    public boolean canTraverseOutsideSubtree()
 107    {
 108     return m_arg0.canTraverseOutsideSubtree();
 109    }
 110 
 111   /**
 112    * This function is used to fixup variables from QNames to stack frame
 113    * indexes at stylesheet build time.
 114    * @param vars List of QNames that correspond to variables.  This list
 115    * should be searched backwards for the first qualified name that
 116    * corresponds to the variable reference qname.  The position of the
 117    * QName in the vector from the start of the vector will be its position
 118    * in the stack frame (but variables above the globalsTop value will need
 119    * to be offset to the current stack frame).
 120    */
 121   public void fixupVariables(List<QName> vars, int globalsSize)
 122   {
 123     if(null != m_arg0)
 124       m_arg0.fixupVariables(vars, globalsSize);
 125   }
 126 
 127   /**
 128    * @see com.sun.org.apache.xpath.internal.XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
 129    */
 130   public void callArgVisitors(XPathVisitor visitor)
 131   {
 132         if(null != m_arg0)
 133                 m_arg0.callVisitors(this, visitor);
 134   }
 135 
 136 
 137   /**
 138    * @see ExpressionOwner#getExpression()
 139    */
 140   public Expression getExpression()
 141   {
 142     return m_arg0;
 143   }
 144 
 145   /**
 146    * @see ExpressionOwner#setExpression(Expression)
 147    */
 148   public void setExpression(Expression exp)
 149   {
 150         exp.exprSetParent(this);
 151         m_arg0 = exp;
 152   }
 153 
 154   /**
 155    * @see Expression#deepEquals(Expression)
 156    */
 157   public boolean deepEquals(Expression expr)
 158   {
 159         if(!super.deepEquals(expr))
 160                 return false;
 161 
 162         if(null != m_arg0)
 163         {
 164                 if(null == ((FunctionOneArg)expr).m_arg0)
 165                         return false;
 166 
 167                 if(!m_arg0.deepEquals(((FunctionOneArg)expr).m_arg0))
 168                         return false;
 169         }
 170         else if(null != ((FunctionOneArg)expr).m_arg0)
 171                 return false;
 172 
 173         return true;
 174   }
 175 
 176 
 177 }