LCOV - code coverage report
Current view: top level - dom/xslt/xslt - txKeyFunctionCall.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 0 164 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 15 0.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13