2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "txExpr.h"
|
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "txIXPathContext.h"
|
|
|
|
#include "txNodeSet.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class represents a FunctionCall as defined by the XSL Working Draft
|
|
|
|
**/
|
|
|
|
|
|
|
|
//------------------/
|
|
|
|
//- Public Methods -/
|
|
|
|
//------------------/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Evaluates the given Expression and converts its result to a number.
|
|
|
|
*/
|
2009-11-15 02:57:49 -08:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
FunctionCall::evaluateToNumber(Expr* aExpr, txIEvalContext* aContext,
|
|
|
|
double* aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aExpr, "missing expression");
|
|
|
|
nsRefPtr<txAExprResult> exprResult;
|
|
|
|
nsresult rv = aExpr->evaluate(aContext, getter_AddRefs(exprResult));
|
2009-11-15 02:57:49 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
*aResult = exprResult->numberValue();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-11-15 02:57:49 -08:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Evaluates the given Expression and converts its result to a NodeSet.
|
2013-04-02 18:14:24 -07:00
|
|
|
* If the result is not a NodeSet nullptr is returned.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
|
|
|
nsresult
|
|
|
|
FunctionCall::evaluateToNodeSet(Expr* aExpr, txIEvalContext* aContext,
|
|
|
|
txNodeSet** aResult)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aExpr, "Missing expression to evaluate");
|
2012-07-30 07:20:58 -07:00
|
|
|
*aResult = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsRefPtr<txAExprResult> exprRes;
|
|
|
|
nsresult rv = aExpr->evaluate(aContext, getter_AddRefs(exprRes));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (exprRes->getResultType() != txAExprResult::NODESET) {
|
|
|
|
aContext->receiveError(NS_LITERAL_STRING("NodeSet expected as argument"), NS_ERROR_XSLT_NODESET_EXPECTED);
|
|
|
|
return NS_ERROR_XSLT_NODESET_EXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aResult =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<txNodeSet*>(static_cast<txAExprResult*>(exprRes));
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool FunctionCall::requireParams(int32_t aParamCountMin,
|
|
|
|
int32_t aParamCountMax,
|
2007-03-22 10:30:00 -07:00
|
|
|
txIEvalContext* aContext)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t argc = mParams.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (argc < aParamCountMin ||
|
|
|
|
(aParamCountMax > -1 && argc > aParamCountMax)) {
|
|
|
|
nsAutoString err(NS_LITERAL_STRING("invalid number of parameters for function"));
|
|
|
|
#ifdef TX_TO_STRING
|
|
|
|
err.AppendLiteral(": ");
|
|
|
|
toString(err);
|
|
|
|
#endif
|
|
|
|
aContext->receiveError(err, NS_ERROR_XPATH_INVALID_ARG);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Expr*
|
2012-08-22 08:56:38 -07:00
|
|
|
FunctionCall::getSubExprAt(uint32_t aPos)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
return mParams.SafeElementAt(aPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
FunctionCall::setSubExprAt(uint32_t aPos, Expr* aExpr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aPos < mParams.Length(),
|
|
|
|
"setting bad subexpression index");
|
|
|
|
mParams[aPos] = aExpr;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
FunctionCall::argsSensitiveTo(ContextSensitivity aContext)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i, len = mParams.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < len; ++i) {
|
|
|
|
if (mParams[i]->isSensitiveTo(aContext)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TX_TO_STRING
|
|
|
|
void
|
|
|
|
FunctionCall::toString(nsAString& aDest)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAtom> functionNameAtom;
|
2010-03-08 07:45:00 -08:00
|
|
|
if (NS_FAILED(getNameAtom(getter_AddRefs(functionNameAtom)))) {
|
2009-08-14 07:09:00 -07:00
|
|
|
NS_ERROR("Can't get function name.");
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-03-08 07:45:00 -08:00
|
|
|
|
|
|
|
|
|
|
|
aDest.Append(nsDependentAtomString(functionNameAtom) +
|
|
|
|
NS_LITERAL_STRING("("));
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mParams.Length(); ++i) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (i != 0) {
|
2014-01-04 07:02:17 -08:00
|
|
|
aDest.Append(char16_t(','));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
mParams[i]->toString(aDest);
|
|
|
|
}
|
2014-01-04 07:02:17 -08:00
|
|
|
aDest.Append(char16_t(')'));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif
|