KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > expr > Function


1 package com.icl.saxon.expr;
2 import com.icl.saxon.*;
3
4
5 import java.util.*;
6
7
8 /**
9 * Abstract superclass for system-defined and user-defined functions
10 */

11
12 public abstract class Function extends Expression {
13
14     protected Expression[] argument = new Expression[6];
15     private int numberOfArguments = 0;
16
17     /**
18     * Method to add an argument during function definition.
19     */

20
21     public void addArgument(Expression expr) {
22         if (numberOfArguments>=argument.length) {
23             Expression[] arg2 = new Expression[argument.length*2];
24             System.arraycopy(argument, 0, arg2, 0, numberOfArguments);
25             argument = arg2;
26         }
27         argument[numberOfArguments++] = expr;
28     }
29
30     /**
31     * Determine the number of actual arguments supplied in the function call
32     */

33
34     public int getNumberOfArguments() {
35         return numberOfArguments;
36     }
37
38     /**
39     * Get the name of the function.
40     * This method must be implemented in all subclasses.
41     * @return the name of the function, as used in XSL expressions, but excluding
42     * its namespace prefix
43     */

44
45     public abstract String JavaDoc getName();
46
47     /**
48     * Check number of arguments. <BR>
49     * A convenience routine for use in subclasses.
50     * @param min the minimum number of arguments allowed
51     * @param max the maximum number of arguments allowed
52     * @return the actual number of arguments
53     * @throws XPathException if the number of arguments is out of range
54     */

55
56     protected int checkArgumentCount(int min, int max) throws XPathException {
57         int numArgs = numberOfArguments;
58         if (min==max && numArgs != min) {
59             throw new XPathException("Function " + getName() + " must have " + min + pluralArguments(min));
60         }
61         if (numArgs < min) {
62             throw new XPathException("Function " + getName() + " must have at least " + min + pluralArguments(min));
63         }
64         if (numArgs > max) {
65             throw new XPathException("Function " + getName() + " must have no more than " + max + pluralArguments(max));
66         }
67         return numArgs;
68     }
69
70     /**
71     * Utility routine used in constructing error messages
72     */

73
74     private String JavaDoc pluralArguments(int num) {
75         if (num==1) return " argument";
76         return " arguments";
77     }
78
79     /**
80     * Diagnostic print of expression structure
81     */

82     
83     public void display(int level) {
84         System.err.println(indent(level) + "function " + getName());
85         for (int a=0; a<numberOfArguments; a++) {
86             argument[a].display(level+1);
87         }
88     }
89     
90 }
91
92 //
93
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
94
// you may not use this file except in compliance with the License. You may obtain a copy of the
95
// License at http://www.mozilla.org/MPL/
96
//
97
// Software distributed under the License is distributed on an "AS IS" basis,
98
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
99
// See the License for the specific language governing rights and limitations under the License.
100
//
101
// The Original Code is: all this file.
102
//
103
// The Initial Developer of the Original Code is
104
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
105
//
106
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
107
//
108
// Contributor(s): none.
109
//
110
Popular Tags