dom/xslt/xpath/txFunctionCall.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/xslt/xpath/txFunctionCall.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,133 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "txExpr.h"
    1.10 +#include "nsIAtom.h"
    1.11 +#include "txIXPathContext.h"
    1.12 +#include "txNodeSet.h"
    1.13 +
    1.14 +/**
    1.15 + * This class represents a FunctionCall as defined by the XSL Working Draft
    1.16 +**/
    1.17 +
    1.18 +  //------------------/
    1.19 + //- Public Methods -/
    1.20 +//------------------/
    1.21 +
    1.22 +/*
    1.23 + * Evaluates the given Expression and converts its result to a number.
    1.24 + */
    1.25 +// static
    1.26 +nsresult
    1.27 +FunctionCall::evaluateToNumber(Expr* aExpr, txIEvalContext* aContext,
    1.28 +                               double* aResult)
    1.29 +{
    1.30 +    NS_ASSERTION(aExpr, "missing expression");
    1.31 +    nsRefPtr<txAExprResult> exprResult;
    1.32 +    nsresult rv = aExpr->evaluate(aContext, getter_AddRefs(exprResult));
    1.33 +    NS_ENSURE_SUCCESS(rv, rv);
    1.34 +
    1.35 +    *aResult = exprResult->numberValue();
    1.36 +
    1.37 +    return NS_OK;
    1.38 +}
    1.39 +
    1.40 +/*
    1.41 + * Evaluates the given Expression and converts its result to a NodeSet.
    1.42 + * If the result is not a NodeSet nullptr is returned.
    1.43 + */
    1.44 +nsresult
    1.45 +FunctionCall::evaluateToNodeSet(Expr* aExpr, txIEvalContext* aContext,
    1.46 +                                txNodeSet** aResult)
    1.47 +{
    1.48 +    NS_ASSERTION(aExpr, "Missing expression to evaluate");
    1.49 +    *aResult = nullptr;
    1.50 +
    1.51 +    nsRefPtr<txAExprResult> exprRes;
    1.52 +    nsresult rv = aExpr->evaluate(aContext, getter_AddRefs(exprRes));
    1.53 +    NS_ENSURE_SUCCESS(rv, rv);
    1.54 +
    1.55 +    if (exprRes->getResultType() != txAExprResult::NODESET) {
    1.56 +        aContext->receiveError(NS_LITERAL_STRING("NodeSet expected as argument"), NS_ERROR_XSLT_NODESET_EXPECTED);
    1.57 +        return NS_ERROR_XSLT_NODESET_EXPECTED;
    1.58 +    }
    1.59 +
    1.60 +    *aResult =
    1.61 +        static_cast<txNodeSet*>(static_cast<txAExprResult*>(exprRes));
    1.62 +    NS_ADDREF(*aResult);
    1.63 +
    1.64 +    return NS_OK;
    1.65 +}
    1.66 +
    1.67 +bool FunctionCall::requireParams(int32_t aParamCountMin,
    1.68 +                                   int32_t aParamCountMax,
    1.69 +                                   txIEvalContext* aContext)
    1.70 +{
    1.71 +    int32_t argc = mParams.Length();
    1.72 +    if (argc < aParamCountMin ||
    1.73 +        (aParamCountMax > -1 && argc > aParamCountMax)) {
    1.74 +        nsAutoString err(NS_LITERAL_STRING("invalid number of parameters for function"));
    1.75 +#ifdef TX_TO_STRING
    1.76 +        err.AppendLiteral(": ");
    1.77 +        toString(err);
    1.78 +#endif
    1.79 +        aContext->receiveError(err, NS_ERROR_XPATH_INVALID_ARG);
    1.80 +
    1.81 +        return false;
    1.82 +    }
    1.83 +
    1.84 +    return true;
    1.85 +}
    1.86 +
    1.87 +Expr*
    1.88 +FunctionCall::getSubExprAt(uint32_t aPos)
    1.89 +{
    1.90 +    return mParams.SafeElementAt(aPos);
    1.91 +}
    1.92 +
    1.93 +void
    1.94 +FunctionCall::setSubExprAt(uint32_t aPos, Expr* aExpr)
    1.95 +{
    1.96 +    NS_ASSERTION(aPos < mParams.Length(),
    1.97 +                 "setting bad subexpression index");
    1.98 +    mParams[aPos] = aExpr;
    1.99 +}
   1.100 +
   1.101 +bool
   1.102 +FunctionCall::argsSensitiveTo(ContextSensitivity aContext)
   1.103 +{
   1.104 +    uint32_t i, len = mParams.Length();
   1.105 +    for (i = 0; i < len; ++i) {
   1.106 +        if (mParams[i]->isSensitiveTo(aContext)) {
   1.107 +            return true;
   1.108 +        }
   1.109 +    }
   1.110 +
   1.111 +    return false;
   1.112 +}
   1.113 +
   1.114 +#ifdef TX_TO_STRING
   1.115 +void
   1.116 +FunctionCall::toString(nsAString& aDest)
   1.117 +{
   1.118 +    nsCOMPtr<nsIAtom> functionNameAtom;
   1.119 +    if (NS_FAILED(getNameAtom(getter_AddRefs(functionNameAtom)))) {
   1.120 +        NS_ERROR("Can't get function name.");
   1.121 +        return;
   1.122 +    }
   1.123 +
   1.124 +
   1.125 +
   1.126 +    aDest.Append(nsDependentAtomString(functionNameAtom) +
   1.127 +                 NS_LITERAL_STRING("("));
   1.128 +    for (uint32_t i = 0; i < mParams.Length(); ++i) {
   1.129 +        if (i != 0) {
   1.130 +            aDest.Append(char16_t(','));
   1.131 +        }
   1.132 +        mParams[i]->toString(aDest);
   1.133 +    }
   1.134 +    aDest.Append(char16_t(')'));
   1.135 +}
   1.136 +#endif

mercurial