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 : * Jonas Sicking.
19 : * Portions created by the Initial Developer are Copyright (C) 2001
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Jonas Sicking <sicking@bigfoot.com> (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 "txExecutionState.h"
40 : #include "nsGkAtoms.h"
41 : #include "txSingleNodeContext.h"
42 : #include "txXSLTFunctions.h"
43 : #include "nsReadableUtils.h"
44 : #include "txKey.h"
45 : #include "txXSLTPatterns.h"
46 : #include "txNamespaceMap.h"
47 : #include "mozilla/HashFunctions.h"
48 :
49 : using namespace mozilla;
50 :
51 : /*
52 : * txKeyFunctionCall
53 : * A representation of the XSLT additional function: key()
54 : */
55 :
56 : /*
57 : * Creates a new key function call
58 : */
59 0 : txKeyFunctionCall::txKeyFunctionCall(txNamespaceMap* aMappings)
60 0 : : mMappings(aMappings)
61 : {
62 0 : }
63 :
64 : /*
65 : * Evaluates a key() xslt-function call. First argument is name of key
66 : * to use, second argument is value to look up.
67 : * @param aContext the context node for evaluation of this Expr
68 : * @param aCs the ContextState containing the stack information needed
69 : * for evaluation
70 : * @return the result of the evaluation
71 : */
72 : nsresult
73 0 : txKeyFunctionCall::evaluate(txIEvalContext* aContext, txAExprResult** aResult)
74 : {
75 0 : if (!aContext || !requireParams(2, 2, aContext))
76 0 : return NS_ERROR_XPATH_BAD_ARGUMENT_COUNT;
77 :
78 : txExecutionState* es =
79 0 : static_cast<txExecutionState*>(aContext->getPrivateContext());
80 :
81 0 : nsAutoString keyQName;
82 0 : nsresult rv = mParams[0]->evaluateToString(aContext, keyQName);
83 0 : NS_ENSURE_SUCCESS(rv, rv);
84 :
85 0 : txExpandedName keyName;
86 0 : rv = keyName.init(keyQName, mMappings, false);
87 0 : NS_ENSURE_SUCCESS(rv, rv);
88 :
89 0 : nsRefPtr<txAExprResult> exprResult;
90 0 : rv = mParams[1]->evaluate(aContext, getter_AddRefs(exprResult));
91 0 : NS_ENSURE_SUCCESS(rv, rv);
92 :
93 0 : txXPathTreeWalker walker(aContext->getContextNode());
94 0 : walker.moveToRoot();
95 :
96 0 : nsRefPtr<txNodeSet> res;
97 : txNodeSet* nodeSet;
98 0 : if (exprResult->getResultType() == txAExprResult::NODESET &&
99 : (nodeSet = static_cast<txNodeSet*>
100 : (static_cast<txAExprResult*>
101 0 : (exprResult)))->size() > 1) {
102 0 : rv = aContext->recycler()->getNodeSet(getter_AddRefs(res));
103 0 : NS_ENSURE_SUCCESS(rv, rv);
104 :
105 : PRInt32 i;
106 0 : for (i = 0; i < nodeSet->size(); ++i) {
107 0 : nsAutoString val;
108 0 : txXPathNodeUtils::appendNodeValue(nodeSet->get(i), val);
109 :
110 0 : nsRefPtr<txNodeSet> nodes;
111 0 : rv = es->getKeyNodes(keyName, walker.getCurrentPosition(), val,
112 0 : i == 0, getter_AddRefs(nodes));
113 0 : NS_ENSURE_SUCCESS(rv, rv);
114 :
115 0 : res->add(*nodes);
116 : }
117 : }
118 : else {
119 0 : nsAutoString val;
120 0 : exprResult->stringValue(val);
121 0 : rv = es->getKeyNodes(keyName, walker.getCurrentPosition(), val,
122 0 : true, getter_AddRefs(res));
123 0 : NS_ENSURE_SUCCESS(rv, rv);
124 : }
125 :
126 0 : *aResult = res;
127 0 : NS_ADDREF(*aResult);
128 :
129 0 : return NS_OK;
130 : }
131 :
132 : Expr::ResultType
133 0 : txKeyFunctionCall::getReturnType()
134 : {
135 0 : return NODESET_RESULT;
136 : }
137 :
138 : bool
139 0 : txKeyFunctionCall::isSensitiveTo(ContextSensitivity aContext)
140 : {
141 0 : return (aContext & NODE_CONTEXT) || argsSensitiveTo(aContext);
142 : }
143 :
144 : #ifdef TX_TO_STRING
145 : nsresult
146 0 : txKeyFunctionCall::getNameAtom(nsIAtom** aAtom)
147 : {
148 0 : *aAtom = nsGkAtoms::key;
149 0 : NS_ADDREF(*aAtom);
150 0 : return NS_OK;
151 : }
152 : #endif
153 :
154 : /**
155 : * Hash functions
156 : */
157 :
158 : bool
159 0 : txKeyValueHashEntry::KeyEquals(KeyTypePointer aKey) const
160 : {
161 0 : return mKey.mKeyName == aKey->mKeyName &&
162 : mKey.mRootIdentifier == aKey->mRootIdentifier &&
163 0 : mKey.mKeyValue.Equals(aKey->mKeyValue);
164 : }
165 :
166 : PLDHashNumber
167 0 : txKeyValueHashEntry::HashKey(KeyTypePointer aKey)
168 : {
169 : const txKeyValueHashKey* key =
170 0 : static_cast<const txKeyValueHashKey*>(aKey);
171 :
172 : return AddToHash(HashString(key->mKeyValue),
173 : key->mKeyName.mNamespaceID,
174 : key->mRootIdentifier,
175 0 : key->mKeyName.mLocalName.get());
176 : }
177 :
178 : bool
179 0 : txIndexedKeyHashEntry::KeyEquals(KeyTypePointer aKey) const
180 : {
181 0 : return mKey.mKeyName == aKey->mKeyName &&
182 0 : mKey.mRootIdentifier == aKey->mRootIdentifier;
183 : }
184 :
185 : PLDHashNumber
186 0 : txIndexedKeyHashEntry::HashKey(KeyTypePointer aKey)
187 : {
188 : const txIndexedKeyHashKey* key =
189 0 : static_cast<const txIndexedKeyHashKey*>(aKey);
190 : return HashGeneric(key->mKeyName.mNamespaceID,
191 : key->mRootIdentifier,
192 0 : key->mKeyName.mLocalName.get());
193 : }
194 :
195 : /*
196 : * Class managing XSLT-keys
197 : */
198 :
199 : nsresult
200 0 : txKeyHash::getKeyNodes(const txExpandedName& aKeyName,
201 : const txXPathNode& aRoot,
202 : const nsAString& aKeyValue,
203 : bool aIndexIfNotFound,
204 : txExecutionState& aEs,
205 : txNodeSet** aResult)
206 : {
207 0 : *aResult = nsnull;
208 :
209 0 : PRInt32 identifier = txXPathNodeUtils::getUniqueIdentifier(aRoot);
210 :
211 0 : txKeyValueHashKey valueKey(aKeyName, identifier, aKeyValue);
212 0 : txKeyValueHashEntry* valueEntry = mKeyValues.GetEntry(valueKey);
213 0 : if (valueEntry) {
214 0 : *aResult = valueEntry->mNodeSet;
215 0 : NS_ADDREF(*aResult);
216 :
217 0 : return NS_OK;
218 : }
219 :
220 : // We didn't find a value. This could either mean that that key has no
221 : // nodes with that value or that the key hasn't been indexed using this
222 : // document.
223 :
224 0 : if (!aIndexIfNotFound) {
225 : // If aIndexIfNotFound is set then the caller knows this key is
226 : // indexed, so don't bother investigating.
227 0 : *aResult = mEmptyNodeSet;
228 0 : NS_ADDREF(*aResult);
229 :
230 0 : return NS_OK;
231 : }
232 :
233 0 : txIndexedKeyHashKey indexKey(aKeyName, identifier);
234 0 : txIndexedKeyHashEntry* indexEntry = mIndexedKeys.PutEntry(indexKey);
235 0 : NS_ENSURE_TRUE(indexEntry, NS_ERROR_OUT_OF_MEMORY);
236 :
237 0 : if (indexEntry->mIndexed) {
238 : // The key was indexed and apparently didn't contain this value so
239 : // return the empty nodeset.
240 0 : *aResult = mEmptyNodeSet;
241 0 : NS_ADDREF(*aResult);
242 :
243 0 : return NS_OK;
244 : }
245 :
246 : // The key needs to be indexed.
247 0 : txXSLKey* xslKey = mKeys.get(aKeyName);
248 0 : if (!xslKey) {
249 : // The key didn't exist, so bail.
250 0 : return NS_ERROR_INVALID_ARG;
251 : }
252 :
253 0 : nsresult rv = xslKey->indexSubtreeRoot(aRoot, mKeyValues, aEs);
254 0 : NS_ENSURE_SUCCESS(rv, rv);
255 :
256 0 : indexEntry->mIndexed = true;
257 :
258 : // Now that the key is indexed we can get its value.
259 0 : valueEntry = mKeyValues.GetEntry(valueKey);
260 0 : if (valueEntry) {
261 0 : *aResult = valueEntry->mNodeSet;
262 0 : NS_ADDREF(*aResult);
263 : }
264 : else {
265 0 : *aResult = mEmptyNodeSet;
266 0 : NS_ADDREF(*aResult);
267 : }
268 :
269 0 : return NS_OK;
270 : }
271 :
272 : nsresult
273 0 : txKeyHash::init()
274 : {
275 0 : nsresult rv = mKeyValues.Init(8);
276 0 : NS_ENSURE_SUCCESS(rv, rv);
277 :
278 0 : rv = mIndexedKeys.Init(1);
279 0 : NS_ENSURE_SUCCESS(rv, rv);
280 :
281 0 : mEmptyNodeSet = new txNodeSet(nsnull);
282 0 : NS_ENSURE_TRUE(mEmptyNodeSet, NS_ERROR_OUT_OF_MEMORY);
283 :
284 0 : return NS_OK;
285 : }
286 :
287 :
288 : /**
289 : * Adds a match/use pair.
290 : * @param aMatch match-pattern
291 : * @param aUse use-expression
292 : * @return false if an error occurred, true otherwise
293 : */
294 0 : bool txXSLKey::addKey(nsAutoPtr<txPattern> aMatch, nsAutoPtr<Expr> aUse)
295 : {
296 0 : if (!aMatch || !aUse)
297 0 : return false;
298 :
299 0 : Key* key = mKeys.AppendElement();
300 0 : if (!key)
301 0 : return false;
302 :
303 0 : key->matchPattern = aMatch;
304 0 : key->useExpr = aUse;
305 :
306 0 : return true;
307 : }
308 :
309 : /**
310 : * Indexes a document and adds it to the hash of key values
311 : * @param aRoot Subtree root to index and add
312 : * @param aKeyValueHash Hash to add values to
313 : * @param aEs txExecutionState to use for XPath evaluation
314 : */
315 0 : nsresult txXSLKey::indexSubtreeRoot(const txXPathNode& aRoot,
316 : txKeyValueHash& aKeyValueHash,
317 : txExecutionState& aEs)
318 : {
319 : txKeyValueHashKey key(mName,
320 : txXPathNodeUtils::getUniqueIdentifier(aRoot),
321 0 : EmptyString());
322 0 : return indexTree(aRoot, key, aKeyValueHash, aEs);
323 : }
324 :
325 : /**
326 : * Recursively searches a node, its attributes and its subtree for
327 : * nodes matching any of the keys match-patterns.
328 : * @param aNode Node to search
329 : * @param aKey Key to use when adding into the hash
330 : * @param aKeyValueHash Hash to add values to
331 : * @param aEs txExecutionState to use for XPath evaluation
332 : */
333 0 : nsresult txXSLKey::indexTree(const txXPathNode& aNode,
334 : txKeyValueHashKey& aKey,
335 : txKeyValueHash& aKeyValueHash,
336 : txExecutionState& aEs)
337 : {
338 0 : nsresult rv = testNode(aNode, aKey, aKeyValueHash, aEs);
339 0 : NS_ENSURE_SUCCESS(rv, rv);
340 :
341 : // check if the node's attributes match
342 0 : txXPathTreeWalker walker(aNode);
343 0 : if (walker.moveToFirstAttribute()) {
344 0 : do {
345 0 : rv = testNode(walker.getCurrentPosition(), aKey, aKeyValueHash,
346 0 : aEs);
347 0 : NS_ENSURE_SUCCESS(rv, rv);
348 : } while (walker.moveToNextAttribute());
349 0 : walker.moveToParent();
350 : }
351 :
352 : // check if the node's descendants match
353 0 : if (walker.moveToFirstChild()) {
354 0 : do {
355 0 : rv = indexTree(walker.getCurrentPosition(), aKey, aKeyValueHash,
356 0 : aEs);
357 0 : NS_ENSURE_SUCCESS(rv, rv);
358 : } while (walker.moveToNextSibling());
359 : }
360 :
361 0 : return NS_OK;
362 : }
363 :
364 : /**
365 : * Tests one node if it matches any of the keys match-patterns. If
366 : * the node matches its values are added to the index.
367 : * @param aNode Node to test
368 : * @param aKey Key to use when adding into the hash
369 : * @param aKeyValueHash Hash to add values to
370 : * @param aEs txExecutionState to use for XPath evaluation
371 : */
372 0 : nsresult txXSLKey::testNode(const txXPathNode& aNode,
373 : txKeyValueHashKey& aKey,
374 : txKeyValueHash& aKeyValueHash,
375 : txExecutionState& aEs)
376 : {
377 0 : nsAutoString val;
378 0 : PRUint32 currKey, numKeys = mKeys.Length();
379 0 : for (currKey = 0; currKey < numKeys; ++currKey) {
380 0 : if (mKeys[currKey].matchPattern->matches(aNode, &aEs)) {
381 : txSingleNodeContext *evalContext =
382 0 : new txSingleNodeContext(aNode, &aEs);
383 0 : NS_ENSURE_TRUE(evalContext, NS_ERROR_OUT_OF_MEMORY);
384 :
385 0 : nsresult rv = aEs.pushEvalContext(evalContext);
386 0 : NS_ENSURE_SUCCESS(rv, rv);
387 :
388 0 : nsRefPtr<txAExprResult> exprResult;
389 0 : rv = mKeys[currKey].useExpr->evaluate(evalContext,
390 0 : getter_AddRefs(exprResult));
391 :
392 0 : delete aEs.popEvalContext();
393 0 : NS_ENSURE_SUCCESS(rv, rv);
394 :
395 0 : if (exprResult->getResultType() == txAExprResult::NODESET) {
396 : txNodeSet* res = static_cast<txNodeSet*>
397 : (static_cast<txAExprResult*>
398 0 : (exprResult));
399 : PRInt32 i;
400 0 : for (i = 0; i < res->size(); ++i) {
401 0 : val.Truncate();
402 0 : txXPathNodeUtils::appendNodeValue(res->get(i), val);
403 :
404 0 : aKey.mKeyValue.Assign(val);
405 0 : txKeyValueHashEntry* entry = aKeyValueHash.PutEntry(aKey);
406 0 : NS_ENSURE_TRUE(entry && entry->mNodeSet,
407 : NS_ERROR_OUT_OF_MEMORY);
408 :
409 0 : if (entry->mNodeSet->isEmpty() ||
410 0 : entry->mNodeSet->get(entry->mNodeSet->size() - 1) !=
411 0 : aNode) {
412 0 : entry->mNodeSet->append(aNode);
413 : }
414 : }
415 : }
416 : else {
417 0 : exprResult->stringValue(val);
418 :
419 0 : aKey.mKeyValue.Assign(val);
420 0 : txKeyValueHashEntry* entry = aKeyValueHash.PutEntry(aKey);
421 0 : NS_ENSURE_TRUE(entry && entry->mNodeSet,
422 : NS_ERROR_OUT_OF_MEMORY);
423 :
424 0 : if (entry->mNodeSet->isEmpty() ||
425 0 : entry->mNodeSet->get(entry->mNodeSet->size() - 1) !=
426 0 : aNode) {
427 0 : entry->mNodeSet->append(aNode);
428 : }
429 : }
430 : }
431 : }
432 :
433 0 : return NS_OK;
434 : }
|