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: FunctionDef1Arg.java,v 1.13 2004/08/17 19:25:37 jycli Exp $
018: */
019: package org.apache.xpath.functions;
020:
021: import org.apache.xalan.res.XSLMessages;
022: import org.apache.xml.dtm.DTM;
023: import org.apache.xml.utils.XMLString;
024: import org.apache.xpath.XPathContext;
025: import org.apache.xpath.objects.XString;
026: import org.apache.xpath.res.XPATHErrorResources;
027:
028: /**
029: * Base class for functions that accept one argument that can be defaulted if
030: * not specified.
031: * @xsl.usage advanced
032: */
033: public class FunctionDef1Arg extends FunctionOneArg {
034: static final long serialVersionUID = 2325189412814149264L;
035:
036: /**
037: * Execute the first argument expression that is expected to return a
038: * nodeset. If the argument is null, then return the current context node.
039: *
040: * @param xctxt Runtime XPath context.
041: *
042: * @return The first node of the executed nodeset, or the current context
043: * node if the first argument is null.
044: *
045: * @throws javax.xml.transform.TransformerException if an error occurs while
046: * executing the argument expression.
047: */
048: protected int getArg0AsNode(XPathContext xctxt)
049: throws javax.xml.transform.TransformerException {
050:
051: return (null == m_arg0) ? xctxt.getCurrentNode() : m_arg0
052: .asNode(xctxt);
053: }
054:
055: /**
056: * Tell if the expression is a nodeset expression.
057: * @return true if the expression can be represented as a nodeset.
058: */
059: public boolean Arg0IsNodesetExpr() {
060: return (null == m_arg0) ? true : m_arg0.isNodesetExpr();
061: }
062:
063: /**
064: * Execute the first argument expression that is expected to return a
065: * string. If the argument is null, then get the string value from the
066: * current context node.
067: *
068: * @param xctxt Runtime XPath context.
069: *
070: * @return The string value of the first argument, or the string value of the
071: * current context node if the first argument is null.
072: *
073: * @throws javax.xml.transform.TransformerException if an error occurs while
074: * executing the argument expression.
075: */
076: protected XMLString getArg0AsString(XPathContext xctxt)
077: throws javax.xml.transform.TransformerException {
078: if (null == m_arg0) {
079: int currentNode = xctxt.getCurrentNode();
080: if (DTM.NULL == currentNode)
081: return XString.EMPTYSTRING;
082: else {
083: DTM dtm = xctxt.getDTM(currentNode);
084: return dtm.getStringValue(currentNode);
085: }
086:
087: } else
088: return m_arg0.execute(xctxt).xstr();
089: }
090:
091: /**
092: * Execute the first argument expression that is expected to return a
093: * number. If the argument is null, then get the number value from the
094: * current context node.
095: *
096: * @param xctxt Runtime XPath context.
097: *
098: * @return The number value of the first argument, or the number value of the
099: * current context node if the first argument is null.
100: *
101: * @throws javax.xml.transform.TransformerException if an error occurs while
102: * executing the argument expression.
103: */
104: protected double getArg0AsNumber(XPathContext xctxt)
105: throws javax.xml.transform.TransformerException {
106:
107: if (null == m_arg0) {
108: int currentNode = xctxt.getCurrentNode();
109: if (DTM.NULL == currentNode)
110: return 0;
111: else {
112: DTM dtm = xctxt.getDTM(currentNode);
113: XMLString str = dtm.getStringValue(currentNode);
114: return str.toDouble();
115: }
116:
117: } else
118: return m_arg0.execute(xctxt).num();
119: }
120:
121: /**
122: * Check that the number of arguments passed to this function is correct.
123: *
124: * @param argNum The number of arguments that is being passed to the function.
125: *
126: * @throws WrongNumberArgsException if the number of arguments is not 0 or 1.
127: */
128: public void checkNumberArgs(int argNum)
129: throws WrongNumberArgsException {
130: if (argNum > 1)
131: reportWrongNumberArgs();
132: }
133:
134: /**
135: * Constructs and throws a WrongNumberArgException with the appropriate
136: * message for this function object.
137: *
138: * @throws WrongNumberArgsException
139: */
140: protected void reportWrongNumberArgs()
141: throws WrongNumberArgsException {
142: throw new WrongNumberArgsException(XSLMessages
143: .createXPATHMessage(XPATHErrorResources.ER_ZERO_OR_ONE,
144: null)); //"0 or 1");
145: }
146:
147: /**
148: * Tell if this expression or it's subexpressions can traverse outside
149: * the current subtree.
150: *
151: * @return true if traversal outside the context node's subtree can occur.
152: */
153: public boolean canTraverseOutsideSubtree() {
154: return (null == m_arg0) ? false : super
155: .canTraverseOutsideSubtree();
156: }
157: }
|