KickJava   Java API By Example, From Geeks To Geeks.

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


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: Function.java,v 1.13 2004/02/17 04:34:00 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.XPathContext;
25 import org.apache.xpath.XPathVisitor;
26 import org.apache.xpath.compiler.Compiler;
27 import org.apache.xpath.objects.XObject;
28
29 /**
30  * This is a superclass of all XPath functions. This allows two
31  * ways for the class to be called. One method is that the
32  * super class processes the arguments and hands the results to
33  * the derived class, the other method is that the derived
34  * class may process it's own arguments, which is faster since
35  * the arguments don't have to be added to an array, but causes
36  * a larger code footprint.
37  * @xsl.usage advanced
38  */

39 public abstract class Function extends Expression
40 {
41
42   /**
43    * Set an argument expression for a function. This method is called by the
44    * XPath compiler.
45    *
46    * @param arg non-null expression that represents the argument.
47    * @param argNum The argument number index.
48    *
49    * @throws WrongNumberArgsException If the argNum parameter is beyond what
50    * is specified for this function.
51    */

52   public void setArg(Expression arg, int argNum)
53           throws WrongNumberArgsException
54   {
55             // throw new WrongNumberArgsException(XSLMessages.createXPATHMessage("zero", null));
56
reportWrongNumberArgs();
57   }
58
59   /**
60    * Check that the number of arguments passed to this function is correct.
61    * This method is meant to be overloaded by derived classes, to check for
62    * the number of arguments for a specific function type. This method is
63    * called by the compiler for static number of arguments checking.
64    *
65    * @param argNum The number of arguments that is being passed to the function.
66    *
67    * @throws WrongNumberArgsException
68    */

69   public void checkNumberArgs(int argNum) throws WrongNumberArgsException
70   {
71     if (argNum != 0)
72       reportWrongNumberArgs();
73   }
74
75   /**
76    * Constructs and throws a WrongNumberArgException with the appropriate
77    * message for this function object. This method is meant to be overloaded
78    * by derived classes so that the message will be as specific as possible.
79    *
80    * @throws WrongNumberArgsException
81    */

82   protected void reportWrongNumberArgs() throws WrongNumberArgsException {
83       throw new WrongNumberArgsException(XSLMessages.createXPATHMessage("zero", null));
84   }
85
86   /**
87    * Execute an XPath function object. The function must return
88    * a valid object.
89    * @param xctxt The execution current context.
90    * @return A valid XObject.
91    *
92    * @throws javax.xml.transform.TransformerException
93    */

94   public XObject execute(XPathContext xctxt) throws javax.xml.transform.TransformerException JavaDoc
95   {
96
97     // Programmer's assert. (And, no, I don't want the method to be abstract).
98
System.out.println("Error! Function.execute should not be called!");
99
100     return null;
101   }
102   
103   /**
104    * Call the visitors for the function arguments.
105    */

106   public void callArgVisitors(XPathVisitor visitor)
107   {
108   }
109
110   
111   /**
112    * @see XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
113    */

114   public void callVisitors(ExpressionOwner owner, XPathVisitor visitor)
115   {
116     if(visitor.visitFunction(owner, this))
117     {
118         callArgVisitors(visitor);
119     }
120   }
121   
122   /**
123    * @see Expression#deepEquals(Expression)
124    */

125   public boolean deepEquals(Expression expr)
126   {
127     if(!isSameClass(expr))
128         return false;
129         
130     return true;
131   }
132
133   /**
134    * This function is currently only being used by Position()
135    * and Last(). See respective functions for more detail.
136    */

137   public void postCompileStep(Compiler JavaDoc compiler)
138   {
139     // no default action
140
}
141 }
142
Popular Tags