Back to index

lightning-sunbird  0.9+nobinonly
FunctionCall.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is TransforMiiX XSLT processor code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * The MITRE Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Keith Visco <kvisco@ziplink.net> (Original Author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "FunctionLib.h"
00040 #include "ExprResult.h"
00041 #include "nsIAtom.h"
00042 #include "txIXPathContext.h"
00043 #include "txNodeSet.h"
00044 
00049 FunctionCall::FunctionCall()
00050 {
00051 }
00052 
00056 FunctionCall::~FunctionCall()
00057 {
00058     txListIterator iter(&params);
00059     while (iter.hasNext()) {
00060         delete (Expr*)iter.next();
00061     }
00062 } //-- ~FunctionCall
00063 
00064   //------------------/
00065  //- Public Methods -/
00066 //------------------/
00067 
00072 nsresult
00073 FunctionCall::addParam(Expr* aExpr)
00074 {
00075     NS_ASSERTION(aExpr, "missing expression");
00076     nsresult rv = params.add(aExpr);
00077     if (NS_FAILED(rv)) {
00078         delete aExpr;
00079     }
00080     return rv;
00081 } //-- addParam
00082 
00083 /*
00084  * Evaluates the given Expression and converts its result to a String.
00085  * The value is appended to the given destination String
00086  */
00087 void FunctionCall::evaluateToString(Expr* aExpr, txIEvalContext* aContext,
00088                                     nsAString& aDest)
00089 {
00090     NS_ASSERTION(aExpr, "missing expression");
00091     nsRefPtr<txAExprResult> exprResult;
00092     nsresult rv = aExpr->evaluate(aContext, getter_AddRefs(exprResult));
00093     if (NS_FAILED(rv))
00094         return;
00095 
00096     exprResult->stringValue(aDest);
00097 }
00098 
00099 /*
00100  * Evaluates the given Expression and converts its result to a number.
00101  */
00102 double FunctionCall::evaluateToNumber(Expr* aExpr, txIEvalContext* aContext)
00103 {
00104     NS_ASSERTION(aExpr, "missing expression");
00105     nsRefPtr<txAExprResult> exprResult;
00106     nsresult rv = aExpr->evaluate(aContext, getter_AddRefs(exprResult));
00107     if (NS_FAILED(rv))
00108         return Double::NaN;
00109 
00110     return exprResult->numberValue();
00111 }
00112 
00113 /*
00114  * Evaluates the given Expression and converts its result to a boolean.
00115  */
00116 MBool FunctionCall::evaluateToBoolean(Expr* aExpr, txIEvalContext* aContext)
00117 {
00118     NS_ASSERTION(aExpr, "missing expression");
00119     nsRefPtr<txAExprResult> exprResult;
00120     nsresult rv = aExpr->evaluate(aContext, getter_AddRefs(exprResult));
00121     if (NS_FAILED(rv))
00122         return PR_FALSE;
00123 
00124     return exprResult->booleanValue();
00125 }
00126 
00127 /*
00128  * Evaluates the given Expression and converts its result to a NodeSet.
00129  * If the result is not a NodeSet NULL is returned.
00130  */
00131 nsresult
00132 FunctionCall::evaluateToNodeSet(Expr* aExpr, txIEvalContext* aContext,
00133                                 txNodeSet** aResult)
00134 {
00135     NS_ASSERTION(aExpr, "Missing expression to evaluate");
00136     *aResult = nsnull;
00137 
00138     nsRefPtr<txAExprResult> exprRes;
00139     nsresult rv = aExpr->evaluate(aContext, getter_AddRefs(exprRes));
00140     NS_ENSURE_SUCCESS(rv, rv);
00141 
00142     if (exprRes->getResultType() != txAExprResult::NODESET) {
00143         aContext->receiveError(NS_LITERAL_STRING("NodeSet expected as argument"), NS_ERROR_XSLT_NODESET_EXPECTED);
00144         return NS_ERROR_XSLT_NODESET_EXPECTED;
00145     }
00146 
00147     *aResult =
00148         NS_STATIC_CAST(txNodeSet*, NS_STATIC_CAST(txAExprResult*, exprRes));
00149     NS_ADDREF(*aResult);
00150 
00151     return NS_OK;
00152 }
00153 
00154 PRBool FunctionCall::requireParams(PRInt32 aParamCountMin,
00155                                    PRInt32 aParamCountMax,
00156                                    txIEvalContext* aContext)
00157 {
00158     PRInt32 argc = params.getLength();
00159     if (argc < aParamCountMin ||
00160         (aParamCountMax > -1 && argc > aParamCountMax)) {
00161         nsAutoString err(NS_LITERAL_STRING("invalid number of parameters for function"));
00162 #ifdef TX_TO_STRING
00163         err.AppendLiteral(": ");
00164         toString(err);
00165 #endif
00166         aContext->receiveError(err, NS_ERROR_XPATH_INVALID_ARG);
00167 
00168         return PR_FALSE;
00169     }
00170 
00171     return PR_TRUE;
00172 }
00173 
00174 #ifdef TX_TO_STRING
00175 void
00176 FunctionCall::toString(nsAString& aDest)
00177 {
00178     nsCOMPtr<nsIAtom> functionNameAtom;
00179     nsAutoString functionName;
00180     if (NS_FAILED(getNameAtom(getter_AddRefs(functionNameAtom))) ||
00181         NS_FAILED(functionNameAtom->ToString(functionName))) {
00182         NS_ASSERTION(0, "Can't get function name.");
00183         return;
00184     }
00185 
00186     aDest.Append(functionName);
00187     aDest.Append(PRUnichar('('));
00188     txListIterator iter(&params);
00189     MBool addComma = MB_FALSE;
00190     while (iter.hasNext()) {
00191         if (addComma) {
00192             aDest.Append(PRUnichar(','));
00193         }
00194         addComma = MB_TRUE;
00195         Expr* expr = (Expr*)iter.next();
00196         expr->toString(aDest);
00197     }
00198     aDest.Append(PRUnichar(')'));
00199 }
00200 #endif