1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is TransforMiiX XSLT processor code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * The MITRE Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1999
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Keith Visco <kvisco@ziplink.net> (Original Author)
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "txExpr.h"
40 : #include "nsIAtom.h"
41 : #include "txIXPathContext.h"
42 : #include "txNodeSet.h"
43 :
44 : /**
45 : * This class represents a FunctionCall as defined by the XSL Working Draft
46 : **/
47 :
48 : //------------------/
49 : //- Public Methods -/
50 : //------------------/
51 :
52 : /*
53 : * Evaluates the given Expression and converts its result to a number.
54 : */
55 : // static
56 : nsresult
57 0 : FunctionCall::evaluateToNumber(Expr* aExpr, txIEvalContext* aContext,
58 : double* aResult)
59 : {
60 0 : NS_ASSERTION(aExpr, "missing expression");
61 0 : nsRefPtr<txAExprResult> exprResult;
62 0 : nsresult rv = aExpr->evaluate(aContext, getter_AddRefs(exprResult));
63 0 : NS_ENSURE_SUCCESS(rv, rv);
64 :
65 0 : *aResult = exprResult->numberValue();
66 :
67 0 : return NS_OK;
68 : }
69 :
70 : /*
71 : * Evaluates the given Expression and converts its result to a NodeSet.
72 : * If the result is not a NodeSet NULL is returned.
73 : */
74 : nsresult
75 0 : FunctionCall::evaluateToNodeSet(Expr* aExpr, txIEvalContext* aContext,
76 : txNodeSet** aResult)
77 : {
78 0 : NS_ASSERTION(aExpr, "Missing expression to evaluate");
79 0 : *aResult = nsnull;
80 :
81 0 : nsRefPtr<txAExprResult> exprRes;
82 0 : nsresult rv = aExpr->evaluate(aContext, getter_AddRefs(exprRes));
83 0 : NS_ENSURE_SUCCESS(rv, rv);
84 :
85 0 : if (exprRes->getResultType() != txAExprResult::NODESET) {
86 0 : aContext->receiveError(NS_LITERAL_STRING("NodeSet expected as argument"), NS_ERROR_XSLT_NODESET_EXPECTED);
87 0 : return NS_ERROR_XSLT_NODESET_EXPECTED;
88 : }
89 :
90 : *aResult =
91 0 : static_cast<txNodeSet*>(static_cast<txAExprResult*>(exprRes));
92 0 : NS_ADDREF(*aResult);
93 :
94 0 : return NS_OK;
95 : }
96 :
97 0 : bool FunctionCall::requireParams(PRInt32 aParamCountMin,
98 : PRInt32 aParamCountMax,
99 : txIEvalContext* aContext)
100 : {
101 0 : PRInt32 argc = mParams.Length();
102 0 : if (argc < aParamCountMin ||
103 : (aParamCountMax > -1 && argc > aParamCountMax)) {
104 0 : nsAutoString err(NS_LITERAL_STRING("invalid number of parameters for function"));
105 : #ifdef TX_TO_STRING
106 0 : err.AppendLiteral(": ");
107 0 : toString(err);
108 : #endif
109 0 : aContext->receiveError(err, NS_ERROR_XPATH_INVALID_ARG);
110 :
111 0 : return false;
112 : }
113 :
114 0 : return true;
115 : }
116 :
117 : Expr*
118 0 : FunctionCall::getSubExprAt(PRUint32 aPos)
119 : {
120 0 : return mParams.SafeElementAt(aPos);
121 : }
122 :
123 : void
124 0 : FunctionCall::setSubExprAt(PRUint32 aPos, Expr* aExpr)
125 : {
126 0 : NS_ASSERTION(aPos < mParams.Length(),
127 : "setting bad subexpression index");
128 0 : mParams[aPos] = aExpr;
129 0 : }
130 :
131 : bool
132 0 : FunctionCall::argsSensitiveTo(ContextSensitivity aContext)
133 : {
134 0 : PRUint32 i, len = mParams.Length();
135 0 : for (i = 0; i < len; ++i) {
136 0 : if (mParams[i]->isSensitiveTo(aContext)) {
137 0 : return true;
138 : }
139 : }
140 :
141 0 : return false;
142 : }
143 :
144 : #ifdef TX_TO_STRING
145 : void
146 0 : FunctionCall::toString(nsAString& aDest)
147 : {
148 0 : nsCOMPtr<nsIAtom> functionNameAtom;
149 0 : if (NS_FAILED(getNameAtom(getter_AddRefs(functionNameAtom)))) {
150 0 : NS_ERROR("Can't get function name.");
151 : return;
152 : }
153 :
154 :
155 :
156 0 : aDest.Append(nsDependentAtomString(functionNameAtom) +
157 0 : NS_LITERAL_STRING("("));
158 0 : for (PRUint32 i = 0; i < mParams.Length(); ++i) {
159 0 : if (i != 0) {
160 0 : aDest.Append(PRUnichar(','));
161 : }
162 0 : mParams[i]->toString(aDest);
163 : }
164 0 : aDest.Append(PRUnichar(')'));
165 : }
166 : #endif
|