Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #include "txExpr.h"
7 : #include "nsIAtom.h"
8 : #include "txIXPathContext.h"
9 : #include "txNodeSet.h"
10 :
11 : /**
12 : * This class represents a FunctionCall as defined by the XSL Working Draft
13 : **/
14 :
15 : //------------------/
16 : //- Public Methods -/
17 : //------------------/
18 :
19 : /*
20 : * Evaluates the given Expression and converts its result to a number.
21 : */
22 : // static
23 : nsresult
24 0 : FunctionCall::evaluateToNumber(Expr* aExpr, txIEvalContext* aContext,
25 : double* aResult)
26 : {
27 0 : NS_ASSERTION(aExpr, "missing expression");
28 0 : RefPtr<txAExprResult> exprResult;
29 0 : nsresult rv = aExpr->evaluate(aContext, getter_AddRefs(exprResult));
30 0 : NS_ENSURE_SUCCESS(rv, rv);
31 :
32 0 : *aResult = exprResult->numberValue();
33 :
34 0 : return NS_OK;
35 : }
36 :
37 : /*
38 : * Evaluates the given Expression and converts its result to a NodeSet.
39 : * If the result is not a NodeSet nullptr is returned.
40 : */
41 : nsresult
42 0 : FunctionCall::evaluateToNodeSet(Expr* aExpr, txIEvalContext* aContext,
43 : txNodeSet** aResult)
44 : {
45 0 : NS_ASSERTION(aExpr, "Missing expression to evaluate");
46 0 : *aResult = nullptr;
47 :
48 0 : RefPtr<txAExprResult> exprRes;
49 0 : nsresult rv = aExpr->evaluate(aContext, getter_AddRefs(exprRes));
50 0 : NS_ENSURE_SUCCESS(rv, rv);
51 :
52 0 : if (exprRes->getResultType() != txAExprResult::NODESET) {
53 0 : aContext->receiveError(NS_LITERAL_STRING("NodeSet expected as argument"), NS_ERROR_XSLT_NODESET_EXPECTED);
54 0 : return NS_ERROR_XSLT_NODESET_EXPECTED;
55 : }
56 :
57 0 : *aResult =
58 0 : static_cast<txNodeSet*>(static_cast<txAExprResult*>(exprRes));
59 0 : NS_ADDREF(*aResult);
60 :
61 0 : return NS_OK;
62 : }
63 :
64 0 : bool FunctionCall::requireParams(int32_t aParamCountMin,
65 : int32_t aParamCountMax,
66 : txIEvalContext* aContext)
67 : {
68 0 : int32_t argc = mParams.Length();
69 0 : if (argc < aParamCountMin ||
70 0 : (aParamCountMax > -1 && argc > aParamCountMax)) {
71 0 : nsAutoString err(NS_LITERAL_STRING("invalid number of parameters for function"));
72 : #ifdef TX_TO_STRING
73 0 : err.AppendLiteral(": ");
74 0 : toString(err);
75 : #endif
76 0 : aContext->receiveError(err, NS_ERROR_XPATH_INVALID_ARG);
77 :
78 0 : return false;
79 : }
80 :
81 0 : return true;
82 : }
83 :
84 : Expr*
85 0 : FunctionCall::getSubExprAt(uint32_t aPos)
86 : {
87 0 : return mParams.SafeElementAt(aPos);
88 : }
89 :
90 : void
91 0 : FunctionCall::setSubExprAt(uint32_t aPos, Expr* aExpr)
92 : {
93 0 : NS_ASSERTION(aPos < mParams.Length(),
94 : "setting bad subexpression index");
95 0 : mParams[aPos] = aExpr;
96 0 : }
97 :
98 : bool
99 0 : FunctionCall::argsSensitiveTo(ContextSensitivity aContext)
100 : {
101 0 : uint32_t i, len = mParams.Length();
102 0 : for (i = 0; i < len; ++i) {
103 0 : if (mParams[i]->isSensitiveTo(aContext)) {
104 0 : return true;
105 : }
106 : }
107 :
108 0 : return false;
109 : }
110 :
111 : #ifdef TX_TO_STRING
112 : void
113 0 : FunctionCall::toString(nsAString& aDest)
114 : {
115 0 : nsCOMPtr<nsIAtom> functionNameAtom;
116 0 : if (NS_FAILED(getNameAtom(getter_AddRefs(functionNameAtom)))) {
117 0 : NS_ERROR("Can't get function name.");
118 0 : return;
119 : }
120 :
121 :
122 :
123 0 : aDest.Append(nsDependentAtomString(functionNameAtom) +
124 0 : NS_LITERAL_STRING("("));
125 0 : for (uint32_t i = 0; i < mParams.Length(); ++i) {
126 0 : if (i != 0) {
127 0 : aDest.Append(char16_t(','));
128 : }
129 0 : mParams[i]->toString(aDest);
130 : }
131 0 : aDest.Append(char16_t(')'));
132 : }
133 : #endif
|