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 : * Olivier Gerardin <ogerardin@vo.lu> (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 : /*
40 : * DocumentFunctionCall
41 : * A representation of the XSLT additional function: document()
42 : */
43 :
44 : #include "nsGkAtoms.h"
45 : #include "txIXPathContext.h"
46 : #include "txXSLTFunctions.h"
47 : #include "txExecutionState.h"
48 : #include "txURIUtils.h"
49 :
50 : /*
51 : * Creates a new DocumentFunctionCall.
52 : */
53 0 : DocumentFunctionCall::DocumentFunctionCall(const nsAString& aBaseURI)
54 0 : : mBaseURI(aBaseURI)
55 : {
56 0 : }
57 :
58 : static void
59 0 : retrieveNode(txExecutionState* aExecutionState, const nsAString& aUri,
60 : const nsAString& aBaseUri, txNodeSet* aNodeSet)
61 : {
62 0 : nsAutoString absUrl;
63 0 : URIUtils::resolveHref(aUri, aBaseUri, absUrl);
64 :
65 0 : PRInt32 hash = absUrl.RFindChar(PRUnichar('#'));
66 : PRUint32 urlEnd, fragStart, fragEnd;
67 0 : if (hash == kNotFound) {
68 0 : urlEnd = absUrl.Length();
69 0 : fragStart = 0;
70 0 : fragEnd = 0;
71 : }
72 : else {
73 0 : urlEnd = hash;
74 0 : fragStart = hash + 1;
75 0 : fragEnd = absUrl.Length();
76 : }
77 :
78 0 : nsDependentSubstring docUrl(absUrl, 0, urlEnd);
79 0 : nsDependentSubstring frag(absUrl, fragStart, fragEnd);
80 :
81 0 : const txXPathNode* loadNode = aExecutionState->retrieveDocument(docUrl);
82 0 : if (loadNode) {
83 0 : if (frag.IsEmpty()) {
84 0 : aNodeSet->add(*loadNode);
85 : }
86 : else {
87 0 : txXPathTreeWalker walker(*loadNode);
88 0 : if (walker.moveToElementById(frag)) {
89 0 : aNodeSet->add(walker.getCurrentPosition());
90 : }
91 : }
92 : }
93 0 : }
94 :
95 : /*
96 : * Evaluates this Expr based on the given context node and processor state
97 : * NOTE: the implementation is incomplete since it does not make use of the
98 : * second argument (base URI)
99 : * @param context the context node for evaluation of this Expr
100 : * @return the result of the evaluation
101 : */
102 : nsresult
103 0 : DocumentFunctionCall::evaluate(txIEvalContext* aContext,
104 : txAExprResult** aResult)
105 : {
106 0 : *aResult = nsnull;
107 : txExecutionState* es =
108 0 : static_cast<txExecutionState*>(aContext->getPrivateContext());
109 :
110 0 : nsRefPtr<txNodeSet> nodeSet;
111 0 : nsresult rv = aContext->recycler()->getNodeSet(getter_AddRefs(nodeSet));
112 0 : NS_ENSURE_SUCCESS(rv, rv);
113 :
114 : // document(object, node-set?)
115 0 : if (!requireParams(1, 2, aContext)) {
116 0 : return NS_ERROR_XPATH_BAD_ARGUMENT_COUNT;
117 : }
118 :
119 0 : nsRefPtr<txAExprResult> exprResult1;
120 0 : rv = mParams[0]->evaluate(aContext, getter_AddRefs(exprResult1));
121 0 : NS_ENSURE_SUCCESS(rv, rv);
122 :
123 0 : nsAutoString baseURI;
124 0 : bool baseURISet = false;
125 :
126 0 : if (mParams.Length() == 2) {
127 : // We have 2 arguments, get baseURI from the first node
128 : // in the resulting nodeset
129 0 : nsRefPtr<txNodeSet> nodeSet2;
130 0 : rv = evaluateToNodeSet(mParams[1],
131 0 : aContext, getter_AddRefs(nodeSet2));
132 0 : NS_ENSURE_SUCCESS(rv, rv);
133 :
134 : // Make this true, even if nodeSet2 is empty. For relative URLs,
135 : // we'll fail to load the document with an empty base URI, and for
136 : // absolute URLs, the base URI doesn't matter
137 0 : baseURISet = true;
138 :
139 0 : if (!nodeSet2->isEmpty()) {
140 0 : txXPathNodeUtils::getBaseURI(nodeSet2->get(0), baseURI);
141 : }
142 : }
143 :
144 0 : if (exprResult1->getResultType() == txAExprResult::NODESET) {
145 : // The first argument is a NodeSet, iterate on its nodes
146 : txNodeSet* nodeSet1 = static_cast<txNodeSet*>
147 : (static_cast<txAExprResult*>
148 0 : (exprResult1));
149 : PRInt32 i;
150 0 : for (i = 0; i < nodeSet1->size(); ++i) {
151 0 : const txXPathNode& node = nodeSet1->get(i);
152 0 : nsAutoString uriStr;
153 0 : txXPathNodeUtils::appendNodeValue(node, uriStr);
154 0 : if (!baseURISet) {
155 : // if the second argument wasn't specified, use
156 : // the baseUri of node itself
157 0 : txXPathNodeUtils::getBaseURI(node, baseURI);
158 : }
159 0 : retrieveNode(es, uriStr, baseURI, nodeSet);
160 : }
161 :
162 0 : NS_ADDREF(*aResult = nodeSet);
163 :
164 0 : return NS_OK;
165 : }
166 :
167 : // The first argument is not a NodeSet
168 0 : nsAutoString uriStr;
169 0 : exprResult1->stringValue(uriStr);
170 0 : const nsAString* base = baseURISet ? &baseURI : &mBaseURI;
171 0 : retrieveNode(es, uriStr, *base, nodeSet);
172 :
173 0 : NS_ADDREF(*aResult = nodeSet);
174 :
175 0 : return NS_OK;
176 : }
177 :
178 : Expr::ResultType
179 0 : DocumentFunctionCall::getReturnType()
180 : {
181 0 : return NODESET_RESULT;
182 : }
183 :
184 : bool
185 0 : DocumentFunctionCall::isSensitiveTo(ContextSensitivity aContext)
186 : {
187 0 : return (aContext & PRIVATE_CONTEXT) || argsSensitiveTo(aContext);
188 : }
189 :
190 : #ifdef TX_TO_STRING
191 : nsresult
192 0 : DocumentFunctionCall::getNameAtom(nsIAtom** aAtom)
193 : {
194 0 : *aAtom = nsGkAtoms::document;
195 0 : NS_ADDREF(*aAtom);
196 0 : return NS_OK;
197 : }
198 : #endif
|