286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: FunctionDef1Arg.java,v 1.2.4.1 2005/09/14 20:18:42 jeffsuttor Exp $
286N/A */
286N/Apackage com.sun.org.apache.xpath.internal.functions;
286N/A
286N/Aimport com.sun.org.apache.xalan.internal.res.XSLMessages;
286N/Aimport com.sun.org.apache.xml.internal.dtm.DTM;
286N/Aimport com.sun.org.apache.xml.internal.utils.XMLString;
286N/Aimport com.sun.org.apache.xpath.internal.XPathContext;
286N/Aimport com.sun.org.apache.xpath.internal.objects.XString;
286N/Aimport com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
286N/A
286N/A/**
286N/A * Base class for functions that accept one argument that can be defaulted if
286N/A * not specified.
286N/A * @xsl.usage advanced
286N/A */
286N/Apublic class FunctionDef1Arg extends FunctionOneArg
286N/A{
286N/A static final long serialVersionUID = 2325189412814149264L;
286N/A
286N/A /**
286N/A * Execute the first argument expression that is expected to return a
286N/A * nodeset. If the argument is null, then return the current context node.
286N/A *
286N/A * @param xctxt Runtime XPath context.
286N/A *
286N/A * @return The first node of the executed nodeset, or the current context
286N/A * node if the first argument is null.
286N/A *
286N/A * @throws javax.xml.transform.TransformerException if an error occurs while
286N/A * executing the argument expression.
286N/A */
286N/A protected int getArg0AsNode(XPathContext xctxt)
286N/A throws javax.xml.transform.TransformerException
286N/A {
286N/A
286N/A return (null == m_arg0)
286N/A ? xctxt.getCurrentNode() : m_arg0.asNode(xctxt);
286N/A }
286N/A
286N/A /**
286N/A * Tell if the expression is a nodeset expression.
286N/A * @return true if the expression can be represented as a nodeset.
286N/A */
286N/A public boolean Arg0IsNodesetExpr()
286N/A {
286N/A return (null == m_arg0) ? true : m_arg0.isNodesetExpr();
286N/A }
286N/A
286N/A /**
286N/A * Execute the first argument expression that is expected to return a
286N/A * string. If the argument is null, then get the string value from the
286N/A * current context node.
286N/A *
286N/A * @param xctxt Runtime XPath context.
286N/A *
286N/A * @return The string value of the first argument, or the string value of the
286N/A * current context node if the first argument is null.
286N/A *
286N/A * @throws javax.xml.transform.TransformerException if an error occurs while
286N/A * executing the argument expression.
286N/A */
286N/A protected XMLString getArg0AsString(XPathContext xctxt)
286N/A throws javax.xml.transform.TransformerException
286N/A {
286N/A if(null == m_arg0)
286N/A {
286N/A int currentNode = xctxt.getCurrentNode();
286N/A if(DTM.NULL == currentNode)
286N/A return XString.EMPTYSTRING;
286N/A else
286N/A {
286N/A DTM dtm = xctxt.getDTM(currentNode);
286N/A return dtm.getStringValue(currentNode);
286N/A }
286N/A
286N/A }
286N/A else
286N/A return m_arg0.execute(xctxt).xstr();
286N/A }
286N/A
286N/A /**
286N/A * Execute the first argument expression that is expected to return a
286N/A * number. If the argument is null, then get the number value from the
286N/A * current context node.
286N/A *
286N/A * @param xctxt Runtime XPath context.
286N/A *
286N/A * @return The number value of the first argument, or the number value of the
286N/A * current context node if the first argument is null.
286N/A *
286N/A * @throws javax.xml.transform.TransformerException if an error occurs while
286N/A * executing the argument expression.
286N/A */
286N/A protected double getArg0AsNumber(XPathContext xctxt)
286N/A throws javax.xml.transform.TransformerException
286N/A {
286N/A
286N/A if(null == m_arg0)
286N/A {
286N/A int currentNode = xctxt.getCurrentNode();
286N/A if(DTM.NULL == currentNode)
286N/A return 0;
286N/A else
286N/A {
286N/A DTM dtm = xctxt.getDTM(currentNode);
286N/A XMLString str = dtm.getStringValue(currentNode);
286N/A return str.toDouble();
286N/A }
286N/A
286N/A }
286N/A else
286N/A return m_arg0.execute(xctxt).num();
286N/A }
286N/A
286N/A /**
286N/A * Check that the number of arguments passed to this function is correct.
286N/A *
286N/A * @param argNum The number of arguments that is being passed to the function.
286N/A *
286N/A * @throws WrongNumberArgsException if the number of arguments is not 0 or 1.
286N/A */
286N/A public void checkNumberArgs(int argNum) throws WrongNumberArgsException
286N/A {
286N/A if (argNum > 1)
286N/A reportWrongNumberArgs();
286N/A }
286N/A
286N/A /**
286N/A * Constructs and throws a WrongNumberArgException with the appropriate
286N/A * message for this function object.
286N/A *
286N/A * @throws WrongNumberArgsException
286N/A */
286N/A protected void reportWrongNumberArgs() throws WrongNumberArgsException {
286N/A throw new WrongNumberArgsException(XSLMessages.createXPATHMessage(XPATHErrorResources.ER_ZERO_OR_ONE, null)); //"0 or 1");
286N/A }
286N/A
286N/A /**
286N/A * Tell if this expression or it's subexpressions can traverse outside
286N/A * the current subtree.
286N/A *
286N/A * @return true if traversal outside the context node's subtree can occur.
286N/A */
286N/A public boolean canTraverseOutsideSubtree()
286N/A {
286N/A return (null == m_arg0) ? false : super.canTraverseOutsideSubtree();
286N/A }
286N/A}