KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xpath > internal > functions > FunctionMultiArgs


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: FunctionMultiArgs.java,v 1.14 2004/02/17 04:34:01 minchau Exp $
18  */

19 package com.sun.org.apache.xpath.internal.functions;
20
21 import com.sun.org.apache.xalan.internal.res.XSLMessages;
22 import com.sun.org.apache.xpath.internal.Expression;
23 import com.sun.org.apache.xpath.internal.ExpressionOwner;
24 import com.sun.org.apache.xpath.internal.XPathVisitor;
25 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
26
27 /**
28  * Base class for functions that accept an undetermined number of multiple
29  * arguments.
30  * @xsl.usage advanced
31  */

32 public class FunctionMultiArgs extends Function3Args
33 {
34
35   /** Argument expressions that are at index 3 or greater.
36    * @serial */

37   Expression[] m_args;
38   
39   /**
40    * Return an expression array containing arguments at index 3 or greater.
41    *
42    * @return An array that contains the arguments at index 3 or greater.
43    */

44   public Expression[] getArgs()
45   {
46     return m_args;
47   }
48
49   /**
50    * Set an argument expression for a function. This method is called by the
51    * XPath compiler.
52    *
53    * @param arg non-null expression that represents the argument.
54    * @param argNum The argument number index.
55    *
56    * @throws WrongNumberArgsException If a derived class determines that the
57    * number of arguments is incorrect.
58    */

59   public void setArg(Expression arg, int argNum)
60           throws WrongNumberArgsException
61   {
62
63     if (argNum < 3)
64       super.setArg(arg, argNum);
65     else
66     {
67       if (null == m_args)
68       {
69         m_args = new Expression[1];
70         m_args[0] = arg;
71       }
72       else
73       {
74
75         // Slow but space conservative.
76
Expression[] args = new Expression[m_args.length + 1];
77
78         System.arraycopy(m_args, 0, args, 0, m_args.length);
79
80         args[m_args.length] = arg;
81         m_args = args;
82       }
83       arg.exprSetParent(this);
84     }
85   }
86   
87   /**
88    * This function is used to fixup variables from QNames to stack frame
89    * indexes at stylesheet build time.
90    * @param vars List of QNames that correspond to variables. This list
91    * should be searched backwards for the first qualified name that
92    * corresponds to the variable reference qname. The position of the
93    * QName in the vector from the start of the vector will be its position
94    * in the stack frame (but variables above the globalsTop value will need
95    * to be offset to the current stack frame).
96    */

97   public void fixupVariables(java.util.Vector JavaDoc vars, int globalsSize)
98   {
99     super.fixupVariables(vars, globalsSize);
100     if(null != m_args)
101     {
102       for (int i = 0; i < m_args.length; i++)
103       {
104         m_args[i].fixupVariables(vars, globalsSize);
105       }
106     }
107   }
108
109   /**
110    * Check that the number of arguments passed to this function is correct.
111    *
112    *
113    * @param argNum The number of arguments that is being passed to the function.
114    *
115    * @throws WrongNumberArgsException
116    */

117   public void checkNumberArgs(int argNum) throws WrongNumberArgsException{}
118
119   /**
120    * Constructs and throws a WrongNumberArgException with the appropriate
121    * message for this function object. This class supports an arbitrary
122    * number of arguments, so this method must never be called.
123    *
124    * @throws WrongNumberArgsException
125    */

126   protected void reportWrongNumberArgs() throws WrongNumberArgsException {
127     String JavaDoc fMsg = XSLMessages.createXPATHMessage(
128         XPATHErrorResources.ER_INCORRECT_PROGRAMMER_ASSERTION,
129         new Object JavaDoc[]{ "Programmer's assertion: the method FunctionMultiArgs.reportWrongNumberArgs() should never be called." });
130
131     throw new RuntimeException JavaDoc(fMsg);
132   }
133
134   /**
135    * Tell if this expression or it's subexpressions can traverse outside
136    * the current subtree.
137    *
138    * @return true if traversal outside the context node's subtree can occur.
139    */

140   public boolean canTraverseOutsideSubtree()
141   {
142
143     if (super.canTraverseOutsideSubtree())
144       return true;
145     else
146     {
147       int n = m_args.length;
148
149       for (int i = 0; i < n; i++)
150       {
151         if (m_args[i].canTraverseOutsideSubtree())
152           return true;
153       }
154
155       return false;
156     }
157   }
158   
159   class ArgMultiOwner implements ExpressionOwner
160   {
161     int m_argIndex;
162     
163     ArgMultiOwner(int index)
164     {
165         m_argIndex = index;
166     }
167     
168     /**
169      * @see ExpressionOwner#getExpression()
170      */

171     public Expression getExpression()
172     {
173       return m_args[m_argIndex];
174     }
175
176
177     /**
178      * @see ExpressionOwner#setExpression(Expression)
179      */

180     public void setExpression(Expression exp)
181     {
182         exp.exprSetParent(FunctionMultiArgs.this);
183         m_args[m_argIndex] = exp;
184     }
185   }
186
187    
188     /**
189      * @see XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
190      */

191     public void callArgVisitors(XPathVisitor visitor)
192     {
193       super.callArgVisitors(visitor);
194       if (null != m_args)
195       {
196         int n = m_args.length;
197         for (int i = 0; i < n; i++)
198         {
199           m_args[i].callVisitors(new ArgMultiOwner(i), visitor);
200         }
201       }
202     }
203     
204     /**
205      * @see Expression#deepEquals(Expression)
206      */

207     public boolean deepEquals(Expression expr)
208     {
209       if (!super.deepEquals(expr))
210             return false;
211
212       FunctionMultiArgs fma = (FunctionMultiArgs) expr;
213       if (null != m_args)
214       {
215         int n = m_args.length;
216         if ((null == fma) || (fma.m_args.length != n))
217               return false;
218
219         for (int i = 0; i < n; i++)
220         {
221           if (!m_args[i].deepEquals(fma.m_args[i]))
222                 return false;
223         }
224
225       }
226       else if (null != fma.m_args)
227       {
228           return false;
229       }
230
231       return true;
232     }
233 }
234
Popular Tags