001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: Function.java,v 1.15 2005/01/23 01:08:21 mcnamara Exp $
018: */
019: package org.apache.xpath.functions;
020:
021: import org.apache.xalan.res.XSLMessages;
022: import org.apache.xpath.Expression;
023: import org.apache.xpath.ExpressionOwner;
024: import org.apache.xpath.XPathContext;
025: import org.apache.xpath.XPathVisitor;
026: import org.apache.xpath.compiler.Compiler;
027: import org.apache.xpath.objects.XObject;
028:
029: /**
030: * This is a superclass of all XPath functions. This allows two
031: * ways for the class to be called. One method is that the
032: * super class processes the arguments and hands the results to
033: * the derived class, the other method is that the derived
034: * class may process it's own arguments, which is faster since
035: * the arguments don't have to be added to an array, but causes
036: * a larger code footprint.
037: * @xsl.usage advanced
038: */
039: public abstract class Function extends Expression {
040: static final long serialVersionUID = 6927661240854599768L;
041:
042: /**
043: * Set an argument expression for a function. This method is called by the
044: * XPath compiler.
045: *
046: * @param arg non-null expression that represents the argument.
047: * @param argNum The argument number index.
048: *
049: * @throws WrongNumberArgsException If the argNum parameter is beyond what
050: * is specified for this function.
051: */
052: public void setArg(Expression arg, int argNum)
053: throws WrongNumberArgsException {
054: // throw new WrongNumberArgsException(XSLMessages.createXPATHMessage("zero", null));
055: reportWrongNumberArgs();
056: }
057:
058: /**
059: * Check that the number of arguments passed to this function is correct.
060: * This method is meant to be overloaded by derived classes, to check for
061: * the number of arguments for a specific function type. This method is
062: * called by the compiler for static number of arguments checking.
063: *
064: * @param argNum The number of arguments that is being passed to the function.
065: *
066: * @throws WrongNumberArgsException
067: */
068: public void checkNumberArgs(int argNum)
069: throws WrongNumberArgsException {
070: if (argNum != 0)
071: reportWrongNumberArgs();
072: }
073:
074: /**
075: * Constructs and throws a WrongNumberArgException with the appropriate
076: * message for this function object. This method is meant to be overloaded
077: * by derived classes so that the message will be as specific as possible.
078: *
079: * @throws WrongNumberArgsException
080: */
081: protected void reportWrongNumberArgs()
082: throws WrongNumberArgsException {
083: throw new WrongNumberArgsException(XSLMessages
084: .createXPATHMessage("zero", null));
085: }
086:
087: /**
088: * Execute an XPath function object. The function must return
089: * a valid object.
090: * @param xctxt The execution current context.
091: * @return A valid XObject.
092: *
093: * @throws javax.xml.transform.TransformerException
094: */
095: public XObject execute(XPathContext xctxt)
096: throws javax.xml.transform.TransformerException {
097:
098: // Programmer's assert. (And, no, I don't want the method to be abstract).
099: System.out
100: .println("Error! Function.execute should not be called!");
101:
102: return null;
103: }
104:
105: /**
106: * Call the visitors for the function arguments.
107: */
108: public void callArgVisitors(XPathVisitor visitor) {
109: }
110:
111: /**
112: * @see org.apache.xpath.XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
113: */
114: public void callVisitors(ExpressionOwner owner, XPathVisitor visitor) {
115: if (visitor.visitFunction(owner, this )) {
116: callArgVisitors(visitor);
117: }
118: }
119:
120: /**
121: * @see Expression#deepEquals(Expression)
122: */
123: public boolean deepEquals(Expression expr) {
124: if (!isSameClass(expr))
125: return false;
126:
127: return true;
128: }
129:
130: /**
131: * This function is currently only being used by Position()
132: * and Last(). See respective functions for more detail.
133: */
134: public void postCompileStep(Compiler compiler) {
135: // no default action
136: }
137: }
|