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