nsInstantiationNode.cpp 3.1 KB

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