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 : * Keith Visco.
19 : * Portions created by the Initial Developer are Copyright (C) 1999
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsGkAtoms.h"
39 : #include "txIXPathContext.h"
40 : #include "txNodeSet.h"
41 : #include "txXPathTreeWalker.h"
42 : #include "txXSLTFunctions.h"
43 : #include "txExecutionState.h"
44 :
45 : /*
46 : Implementation of XSLT 1.0 extension function: generate-id
47 : */
48 :
49 : /**
50 : * Creates a new generate-id function call
51 : **/
52 0 : GenerateIdFunctionCall::GenerateIdFunctionCall()
53 : {
54 0 : }
55 :
56 : /**
57 : * Evaluates this Expr based on the given context node and processor state
58 : * @param context the context node for evaluation of this Expr
59 : * @param ps the ContextState containing the stack information needed
60 : * for evaluation
61 : * @return the result of the evaluation
62 : * @see FunctionCall.h
63 : **/
64 : nsresult
65 0 : GenerateIdFunctionCall::evaluate(txIEvalContext* aContext,
66 : txAExprResult** aResult)
67 : {
68 0 : *aResult = nsnull;
69 0 : if (!requireParams(0, 1, aContext))
70 0 : return NS_ERROR_XPATH_BAD_ARGUMENT_COUNT;
71 :
72 : txExecutionState* es =
73 0 : static_cast<txExecutionState*>(aContext->getPrivateContext());
74 0 : if (!es) {
75 : NS_ERROR(
76 0 : "called xslt extension function \"current\" with wrong context");
77 0 : return NS_ERROR_UNEXPECTED;
78 : }
79 :
80 0 : nsresult rv = NS_OK;
81 0 : if (mParams.IsEmpty()) {
82 : StringResult* strRes;
83 0 : rv = aContext->recycler()->getStringResult(&strRes);
84 0 : NS_ENSURE_SUCCESS(rv, rv);
85 :
86 0 : txXPathNodeUtils::getXSLTId(aContext->getContextNode(),
87 0 : es->getSourceDocument(),
88 0 : strRes->mValue);
89 :
90 0 : *aResult = strRes;
91 :
92 0 : return NS_OK;
93 : }
94 :
95 0 : nsRefPtr<txNodeSet> nodes;
96 0 : rv = evaluateToNodeSet(mParams[0], aContext,
97 0 : getter_AddRefs(nodes));
98 0 : NS_ENSURE_SUCCESS(rv, rv);
99 :
100 0 : if (nodes->isEmpty()) {
101 0 : aContext->recycler()->getEmptyStringResult(aResult);
102 :
103 0 : return NS_OK;
104 : }
105 :
106 : StringResult* strRes;
107 0 : rv = aContext->recycler()->getStringResult(&strRes);
108 0 : NS_ENSURE_SUCCESS(rv, rv);
109 :
110 0 : txXPathNodeUtils::getXSLTId(nodes->get(0), es->getSourceDocument(),
111 0 : strRes->mValue);
112 :
113 0 : *aResult = strRes;
114 :
115 0 : return NS_OK;
116 : }
117 :
118 : Expr::ResultType
119 0 : GenerateIdFunctionCall::getReturnType()
120 : {
121 0 : return STRING_RESULT;
122 : }
123 :
124 : bool
125 0 : GenerateIdFunctionCall::isSensitiveTo(ContextSensitivity aContext)
126 : {
127 0 : if (mParams.IsEmpty()) {
128 0 : return !!(aContext & NODE_CONTEXT);
129 : }
130 :
131 0 : return argsSensitiveTo(aContext);
132 : }
133 :
134 : #ifdef TX_TO_STRING
135 : nsresult
136 0 : GenerateIdFunctionCall::getNameAtom(nsIAtom** aAtom)
137 : {
138 0 : *aAtom = nsGkAtoms::generateId;
139 0 : NS_ADDREF(*aAtom);
140 0 : return NS_OK;
141 : }
142 : #endif
|