1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/xul/templates/src/nsInstantiationNode.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,87 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsInstantiationNode.h" 1.10 +#include "nsTemplateRule.h" 1.11 +#include "nsXULTemplateQueryProcessorRDF.h" 1.12 + 1.13 +#include "prlog.h" 1.14 +#ifdef PR_LOGGING 1.15 +extern PRLogModuleInfo* gXULTemplateLog; 1.16 +#endif 1.17 + 1.18 +nsInstantiationNode::nsInstantiationNode(nsXULTemplateQueryProcessorRDF* aProcessor, 1.19 + nsRDFQuery* aQuery) 1.20 + : mProcessor(aProcessor), 1.21 + mQuery(aQuery) 1.22 +{ 1.23 +#ifdef PR_LOGGING 1.24 + PR_LOG(gXULTemplateLog, PR_LOG_DEBUG, 1.25 + ("nsInstantiationNode[%p] query=%p", this, aQuery)); 1.26 +#endif 1.27 + 1.28 + MOZ_COUNT_CTOR(nsInstantiationNode); 1.29 +} 1.30 + 1.31 + 1.32 +nsInstantiationNode::~nsInstantiationNode() 1.33 +{ 1.34 + MOZ_COUNT_DTOR(nsInstantiationNode); 1.35 +} 1.36 + 1.37 +nsresult 1.38 +nsInstantiationNode::Propagate(InstantiationSet& aInstantiations, 1.39 + bool aIsUpdate, bool& aTakenInstantiations) 1.40 +{ 1.41 + // In update mode, iterate through the results and call the template 1.42 + // builder to update them. In non-update mode, cache them in the processor 1.43 + // to be used during processing. The results are cached in the processor 1.44 + // so that the simple rules are only computed once. In this situation, all 1.45 + // data for all queries are calculated at once. 1.46 + nsresult rv = NS_OK; 1.47 + 1.48 + aTakenInstantiations = false; 1.49 + 1.50 + if (aIsUpdate) { 1.51 + // Iterate through newly added keys to determine which rules fired. 1.52 + // 1.53 + // XXXwaterson Unfortunately, this could also lead to retractions; 1.54 + // e.g., (container ?a ^empty false) could become "unmatched". How 1.55 + // to track those? 1.56 + nsCOMPtr<nsIDOMNode> querynode; 1.57 + mQuery->GetQueryNode(getter_AddRefs(querynode)); 1.58 + 1.59 + InstantiationSet::ConstIterator last = aInstantiations.Last(); 1.60 + for (InstantiationSet::ConstIterator inst = aInstantiations.First(); inst != last; ++inst) { 1.61 + nsAssignmentSet assignments = inst->mAssignments; 1.62 + 1.63 + nsCOMPtr<nsIRDFNode> node; 1.64 + assignments.GetAssignmentFor(mQuery->mMemberVariable, 1.65 + getter_AddRefs(node)); 1.66 + if (node) { 1.67 + nsCOMPtr<nsIRDFResource> resource = do_QueryInterface(node); 1.68 + if (resource) { 1.69 + nsRefPtr<nsXULTemplateResultRDF> nextresult = 1.70 + new nsXULTemplateResultRDF(mQuery, *inst, resource); 1.71 + if (! nextresult) 1.72 + return NS_ERROR_OUT_OF_MEMORY; 1.73 + 1.74 + rv = mProcessor->AddMemoryElements(*inst, nextresult); 1.75 + if (NS_FAILED(rv)) 1.76 + return rv; 1.77 + 1.78 + mProcessor->GetBuilder()->AddResult(nextresult, querynode); 1.79 + } 1.80 + } 1.81 + } 1.82 + } 1.83 + else { 1.84 + nsresult rv = mQuery->SetCachedResults(mProcessor, aInstantiations); 1.85 + if (NS_SUCCEEDED(rv)) 1.86 + aTakenInstantiations = true; 1.87 + } 1.88 + 1.89 + return rv; 1.90 +}