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: FunctionOneArg.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.XPathVisitor;
025:
026: /**
027: * Base class for functions that accept one argument.
028: * @xsl.usage advanced
029: */
030: public class FunctionOneArg extends Function implements ExpressionOwner {
031: static final long serialVersionUID = -5180174180765609758L;
032:
033: /** The first argument passed to the function (at index 0).
034: * @serial */
035: Expression m_arg0;
036:
037: /**
038: * Return the first argument passed to the function (at index 0).
039: *
040: * @return An expression that represents the first argument passed to the
041: * function.
042: */
043: public Expression getArg0() {
044: return m_arg0;
045: }
046:
047: /**
048: * Set an argument expression for a function. This method is called by the
049: * XPath compiler.
050: *
051: * @param arg non-null expression that represents the argument.
052: * @param argNum The argument number index.
053: *
054: * @throws WrongNumberArgsException If the argNum parameter is greater than 0.
055: */
056: public void setArg(Expression arg, int argNum)
057: throws WrongNumberArgsException {
058:
059: if (0 == argNum) {
060: m_arg0 = arg;
061: arg.exprSetParent(this );
062: } else
063: reportWrongNumberArgs();
064: }
065:
066: /**
067: * Check that the number of arguments passed to this function is correct.
068: *
069: *
070: * @param argNum The number of arguments that is being passed to the function.
071: *
072: * @throws WrongNumberArgsException
073: */
074: public void checkNumberArgs(int argNum)
075: throws WrongNumberArgsException {
076: if (argNum != 1)
077: reportWrongNumberArgs();
078: }
079:
080: /**
081: * Constructs and throws a WrongNumberArgException with the appropriate
082: * message for this function object.
083: *
084: * @throws WrongNumberArgsException
085: */
086: protected void reportWrongNumberArgs()
087: throws WrongNumberArgsException {
088: throw new WrongNumberArgsException(XSLMessages
089: .createXPATHMessage("one", null));
090: }
091:
092: /**
093: * Tell if this expression or it's subexpressions can traverse outside
094: * the current subtree.
095: *
096: * @return true if traversal outside the context node's subtree can occur.
097: */
098: public boolean canTraverseOutsideSubtree() {
099: return m_arg0.canTraverseOutsideSubtree();
100: }
101:
102: /**
103: * This function is used to fixup variables from QNames to stack frame
104: * indexes at stylesheet build time.
105: * @param vars List of QNames that correspond to variables. This list
106: * should be searched backwards for the first qualified name that
107: * corresponds to the variable reference qname. The position of the
108: * QName in the vector from the start of the vector will be its position
109: * in the stack frame (but variables above the globalsTop value will need
110: * to be offset to the current stack frame).
111: */
112: public void fixupVariables(java.util.Vector vars, int globalsSize) {
113: if (null != m_arg0)
114: m_arg0.fixupVariables(vars, globalsSize);
115: }
116:
117: /**
118: * @see org.apache.xpath.XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
119: */
120: public void callArgVisitors(XPathVisitor visitor) {
121: if (null != m_arg0)
122: m_arg0.callVisitors(this , visitor);
123: }
124:
125: /**
126: * @see ExpressionOwner#getExpression()
127: */
128: public Expression getExpression() {
129: return m_arg0;
130: }
131:
132: /**
133: * @see ExpressionOwner#setExpression(Expression)
134: */
135: public void setExpression(Expression exp) {
136: exp.exprSetParent(this );
137: m_arg0 = exp;
138: }
139:
140: /**
141: * @see Expression#deepEquals(Expression)
142: */
143: public boolean deepEquals(Expression expr) {
144: if (!super .deepEquals(expr))
145: return false;
146:
147: if (null != m_arg0) {
148: if (null == ((FunctionOneArg) expr).m_arg0)
149: return false;
150:
151: if (!m_arg0.deepEquals(((FunctionOneArg) expr).m_arg0))
152: return false;
153: } else if (null != ((FunctionOneArg) expr).m_arg0)
154: return false;
155:
156: return true;
157: }
158:
159: }
|