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 "nsInstantiationNode.h"
7 : #include "nsTemplateRule.h"
8 : #include "nsXULTemplateQueryProcessorRDF.h"
9 :
10 : #include "mozilla/Logging.h"
11 : extern mozilla::LazyLogModule gXULTemplateLog;
12 :
13 0 : nsInstantiationNode::nsInstantiationNode(nsXULTemplateQueryProcessorRDF* aProcessor,
14 0 : nsRDFQuery* aQuery)
15 : : mProcessor(aProcessor),
16 0 : mQuery(aQuery)
17 : {
18 0 : MOZ_LOG(gXULTemplateLog, LogLevel::Debug,
19 : ("nsInstantiationNode[%p] query=%p", this, aQuery));
20 :
21 0 : MOZ_COUNT_CTOR(nsInstantiationNode);
22 0 : }
23 :
24 :
25 0 : nsInstantiationNode::~nsInstantiationNode()
26 : {
27 0 : MOZ_COUNT_DTOR(nsInstantiationNode);
28 0 : }
29 :
30 : nsresult
31 0 : nsInstantiationNode::Propagate(InstantiationSet& aInstantiations,
32 : bool aIsUpdate, bool& aTakenInstantiations)
33 : {
34 : // In update mode, iterate through the results and call the template
35 : // builder to update them. In non-update mode, cache them in the processor
36 : // to be used during processing. The results are cached in the processor
37 : // so that the simple rules are only computed once. In this situation, all
38 : // data for all queries are calculated at once.
39 0 : nsresult rv = NS_OK;
40 :
41 0 : aTakenInstantiations = false;
42 :
43 0 : if (aIsUpdate) {
44 : // Iterate through newly added keys to determine which rules fired.
45 : //
46 : // XXXwaterson Unfortunately, this could also lead to retractions;
47 : // e.g., (container ?a ^empty false) could become "unmatched". How
48 : // to track those?
49 0 : nsCOMPtr<nsIDOMNode> querynode;
50 0 : mQuery->GetQueryNode(getter_AddRefs(querynode));
51 :
52 0 : InstantiationSet::ConstIterator last = aInstantiations.Last();
53 0 : for (InstantiationSet::ConstIterator inst = aInstantiations.First(); inst != last; ++inst) {
54 0 : nsAssignmentSet assignments = inst->mAssignments;
55 :
56 0 : nsCOMPtr<nsIRDFNode> node;
57 0 : assignments.GetAssignmentFor(mQuery->mMemberVariable,
58 0 : getter_AddRefs(node));
59 0 : if (node) {
60 0 : nsCOMPtr<nsIRDFResource> resource = do_QueryInterface(node);
61 0 : if (resource) {
62 : RefPtr<nsXULTemplateResultRDF> nextresult =
63 0 : new nsXULTemplateResultRDF(mQuery, *inst, resource);
64 0 : if (! nextresult)
65 0 : return NS_ERROR_OUT_OF_MEMORY;
66 :
67 0 : rv = mProcessor->AddMemoryElements(*inst, nextresult);
68 0 : if (NS_FAILED(rv))
69 0 : return rv;
70 :
71 0 : mProcessor->GetBuilder()->AddResult(nextresult, querynode);
72 : }
73 : }
74 : }
75 : }
76 : else {
77 0 : nsresult rv = mQuery->SetCachedResults(mProcessor, aInstantiations);
78 0 : if (NS_SUCCEEDED(rv))
79 0 : aTakenInstantiations = true;
80 : }
81 :
82 0 : return rv;
83 : }
|