KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xpath > functions > Function2Args


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: Function2Args.java,v 1.13 2004/02/17 04:34:01 minchau Exp $
18  */

19 package org.apache.xpath.functions;
20
21 import org.apache.xalan.res.XSLMessages;
22 import org.apache.xpath.Expression;
23 import org.apache.xpath.ExpressionOwner;
24 import org.apache.xpath.XPathVisitor;
25
26 /**
27  * Base class for functions that accept two arguments.
28  * @xsl.usage advanced
29  */

30 public class Function2Args extends FunctionOneArg
31 {
32
33   /** The second argument passed to the function (at index 1).
34    * @serial */

35   Expression m_arg1;
36
37   /**
38    * Return the second argument passed to the function (at index 1).
39    *
40    * @return An expression that represents the second argument passed to the
41    * function.
42    */

43   public Expression getArg1()
44   {
45     return m_arg1;
46   }
47   
48   /**
49    * This function is used to fixup variables from QNames to stack frame
50    * indexes at stylesheet build time.
51    * @param vars List of QNames that correspond to variables. This list
52    * should be searched backwards for the first qualified name that
53    * corresponds to the variable reference qname. The position of the
54    * QName in the vector from the start of the vector will be its position
55    * in the stack frame (but variables above the globalsTop value will need
56    * to be offset to the current stack frame).
57    */

58   public void fixupVariables(java.util.Vector JavaDoc vars, int globalsSize)
59   {
60     super.fixupVariables(vars, globalsSize);
61     if(null != m_arg1)
62       m_arg1.fixupVariables(vars, globalsSize);
63   }
64
65
66   /**
67    * Set an argument expression for a function. This method is called by the
68    * XPath compiler.
69    *
70    * @param arg non-null expression that represents the argument.
71    * @param argNum The argument number index.
72    *
73    * @throws WrongNumberArgsException If the argNum parameter is greater than 1.
74    */

75   public void setArg(Expression arg, int argNum)
76           throws WrongNumberArgsException
77   {
78
79     // System.out.println("argNum: "+argNum);
80
if (argNum == 0)
81       super.setArg(arg, argNum);
82     else if (1 == argNum)
83     {
84       m_arg1 = arg;
85       arg.exprSetParent(this);
86     }
87     else
88           reportWrongNumberArgs();
89   }
90
91   /**
92    * Check that the number of arguments passed to this function is correct.
93    *
94    *
95    * @param argNum The number of arguments that is being passed to the function.
96    *
97    * @throws WrongNumberArgsException
98    */

99   public void checkNumberArgs(int argNum) throws WrongNumberArgsException
100   {
101     if (argNum != 2)
102       reportWrongNumberArgs();
103   }
104
105   /**
106    * Constructs and throws a WrongNumberArgException with the appropriate
107    * message for this function object.
108    *
109    * @throws WrongNumberArgsException
110    */

111   protected void reportWrongNumberArgs() throws WrongNumberArgsException {
112       throw new WrongNumberArgsException(XSLMessages.createXPATHMessage("two", null));
113   }
114   
115   /**
116    * Tell if this expression or it's subexpressions can traverse outside
117    * the current subtree.
118    *
119    * @return true if traversal outside the context node's subtree can occur.
120    */

121    public boolean canTraverseOutsideSubtree()
122    {
123     return super.canTraverseOutsideSubtree()
124     ? true : m_arg1.canTraverseOutsideSubtree();
125    }
126    
127   class Arg1Owner implements ExpressionOwner
128   {
129     /**
130      * @see ExpressionOwner#getExpression()
131      */

132     public Expression getExpression()
133     {
134       return m_arg1;
135     }
136
137
138     /**
139      * @see ExpressionOwner#setExpression(Expression)
140      */

141     public void setExpression(Expression exp)
142     {
143         exp.exprSetParent(Function2Args.this);
144         m_arg1 = exp;
145     }
146   }
147
148    
149   /**
150    * @see XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
151    */

152   public void callArgVisitors(XPathVisitor visitor)
153   {
154     super.callArgVisitors(visitor);
155     if(null != m_arg1)
156         m_arg1.callVisitors(new Arg1Owner(), visitor);
157   }
158
159   /**
160    * @see Expression#deepEquals(Expression)
161    */

162   public boolean deepEquals(Expression expr)
163   {
164     if(!super.deepEquals(expr))
165         return false;
166         
167     if(null != m_arg1)
168     {
169         if(null == ((Function2Args)expr).m_arg1)
170             return false;
171             
172         if(!m_arg1.deepEquals(((Function2Args)expr).m_arg1))
173             return false;
174     }
175     else if(null != ((Function2Args)expr).m_arg1)
176         return false;
177         
178     return true;
179   }
180
181 }
182
Popular Tags