nsRDFBinding.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 "nsXULTemplateQueryProcessorRDF.h"
  6. #include "nsXULTemplateResultRDF.h"
  7. #include "nsRDFBinding.h"
  8. #ifdef DEBUG
  9. #include "nsXULContentUtils.h"
  10. #endif
  11. RDFBindingSet::~RDFBindingSet()
  12. {
  13. while (mFirst) {
  14. RDFBinding* doomed = mFirst;
  15. mFirst = mFirst->mNext;
  16. delete doomed;
  17. }
  18. MOZ_COUNT_DTOR(RDFBindingSet);
  19. }
  20. nsresult
  21. RDFBindingSet::AddBinding(nsIAtom* aVar, nsIAtom* aRef, nsIRDFResource* aPredicate)
  22. {
  23. RDFBinding* newbinding = new RDFBinding(aRef, aPredicate, aVar);
  24. if (mFirst) {
  25. RDFBinding* binding = mFirst;
  26. while (binding) {
  27. // the binding is dependant on the calculation of a previous binding
  28. if (binding->mSubjectVariable == aVar)
  29. newbinding->mHasDependency = true;
  30. // if the target variable is already used in a binding, ignore it
  31. // since it won't be useful for anything
  32. if (binding->mTargetVariable == aVar) {
  33. delete newbinding;
  34. return NS_OK;
  35. }
  36. // add the binding at the end of the list
  37. if (! binding->mNext) {
  38. binding->mNext = newbinding;
  39. break;
  40. }
  41. binding = binding->mNext;
  42. }
  43. }
  44. else {
  45. mFirst = newbinding;
  46. }
  47. mCount++;
  48. return NS_OK;
  49. }
  50. bool
  51. RDFBindingSet::SyncAssignments(nsIRDFResource* aSubject,
  52. nsIRDFResource* aPredicate,
  53. nsIRDFNode* aTarget,
  54. nsIAtom* aMemberVariable,
  55. nsXULTemplateResultRDF* aResult,
  56. nsBindingValues& aBindingValues)
  57. {
  58. NS_ASSERTION(aBindingValues.GetBindingSet() == this,
  59. "nsBindingValues not for this RDFBindingSet");
  60. NS_PRECONDITION(aResult, "Must have result");
  61. bool needSync = false;
  62. nsCOMPtr<nsIRDFNode>* valuesArray = aBindingValues.ValuesArray();
  63. if (!valuesArray)
  64. return false;
  65. RDFBinding* binding = mFirst;
  66. int32_t count = 0;
  67. // QI for proper comparisons just to be safe
  68. nsCOMPtr<nsIRDFNode> subjectnode = do_QueryInterface(aSubject);
  69. // iterate through the bindings looking for ones that would match the RDF
  70. // nodes that were involved in a change
  71. nsCOMPtr<nsIRDFNode> value;
  72. while (binding) {
  73. if (aPredicate == binding->mPredicate) {
  74. // if the source of the binding is the member variable, optimize
  75. if (binding->mSubjectVariable == aMemberVariable) {
  76. valuesArray[count] = aTarget;
  77. needSync = true;
  78. }
  79. else {
  80. aResult->GetAssignment(binding->mSubjectVariable, getter_AddRefs(value));
  81. if (value == subjectnode) {
  82. valuesArray[count] = aTarget;
  83. needSync = true;
  84. }
  85. }
  86. }
  87. binding = binding->mNext;
  88. count++;
  89. }
  90. return needSync;
  91. }
  92. void
  93. RDFBindingSet::AddDependencies(nsIRDFResource* aSubject,
  94. nsXULTemplateResultRDF* aResult)
  95. {
  96. NS_PRECONDITION(aResult, "Must have result");
  97. // iterate through the bindings and add binding dependencies to the
  98. // processor
  99. nsXULTemplateQueryProcessorRDF* processor = aResult->GetProcessor();
  100. if (! processor)
  101. return;
  102. nsCOMPtr<nsIRDFNode> value;
  103. RDFBinding* binding = mFirst;
  104. while (binding) {
  105. aResult->GetAssignment(binding->mSubjectVariable, getter_AddRefs(value));
  106. nsCOMPtr<nsIRDFResource> valueres = do_QueryInterface(value);
  107. if (valueres)
  108. processor->AddBindingDependency(aResult, valueres);
  109. binding = binding->mNext;
  110. }
  111. }
  112. void
  113. RDFBindingSet::RemoveDependencies(nsIRDFResource* aSubject,
  114. nsXULTemplateResultRDF* aResult)
  115. {
  116. NS_PRECONDITION(aResult, "Must have result");
  117. // iterate through the bindings and remove binding dependencies from the
  118. // processor
  119. nsXULTemplateQueryProcessorRDF* processor = aResult->GetProcessor();
  120. if (! processor)
  121. return;
  122. nsCOMPtr<nsIRDFNode> value;
  123. RDFBinding* binding = mFirst;
  124. while (binding) {
  125. aResult->GetAssignment(binding->mSubjectVariable, getter_AddRefs(value));
  126. nsCOMPtr<nsIRDFResource> valueres = do_QueryInterface(value);
  127. if (valueres)
  128. processor->RemoveBindingDependency(aResult, valueres);
  129. binding = binding->mNext;
  130. }
  131. }
  132. int32_t
  133. RDFBindingSet::LookupTargetIndex(nsIAtom* aTargetVariable, RDFBinding** aBinding)
  134. {
  135. int32_t idx = 0;
  136. RDFBinding* binding = mFirst;
  137. while (binding) {
  138. if (binding->mTargetVariable == aTargetVariable) {
  139. *aBinding = binding;
  140. return idx;
  141. }
  142. idx++;
  143. binding = binding->mNext;
  144. }
  145. return -1;
  146. }
  147. nsBindingValues::~nsBindingValues()
  148. {
  149. ClearBindingSet();
  150. MOZ_COUNT_DTOR(nsBindingValues);
  151. }
  152. void
  153. nsBindingValues::ClearBindingSet()
  154. {
  155. if (mBindings && mValues) {
  156. delete [] mValues;
  157. mValues = nullptr;
  158. }
  159. mBindings = nullptr;
  160. }
  161. nsresult
  162. nsBindingValues::SetBindingSet(RDFBindingSet* aBindings)
  163. {
  164. ClearBindingSet();
  165. int32_t count = aBindings->Count();
  166. if (count) {
  167. mValues = new nsCOMPtr<nsIRDFNode>[count];
  168. mBindings = aBindings;
  169. }
  170. else {
  171. mValues = nullptr;
  172. }
  173. return NS_OK;
  174. }
  175. void
  176. nsBindingValues::GetAssignmentFor(nsXULTemplateResultRDF* aResult,
  177. nsIAtom* aVar,
  178. nsIRDFNode** aValue)
  179. {
  180. *aValue = nullptr;
  181. // assignments are calculated lazily when asked for. The only issue is
  182. // when a binding has no value in the RDF graph, it will be checked again
  183. // every time.
  184. if (mBindings && mValues) {
  185. RDFBinding* binding;
  186. int32_t idx = mBindings->LookupTargetIndex(aVar, &binding);
  187. if (idx >= 0) {
  188. *aValue = mValues[idx];
  189. if (*aValue) {
  190. NS_ADDREF(*aValue);
  191. }
  192. else {
  193. nsXULTemplateQueryProcessorRDF* processor = aResult->GetProcessor();
  194. if (! processor)
  195. return;
  196. nsIRDFDataSource* ds = processor->GetDataSource();
  197. if (! ds)
  198. return;
  199. nsCOMPtr<nsIRDFNode> subjectValue;
  200. aResult->GetAssignment(binding->mSubjectVariable,
  201. getter_AddRefs(subjectValue));
  202. if (subjectValue) {
  203. nsCOMPtr<nsIRDFResource> subject = do_QueryInterface(subjectValue);
  204. ds->GetTarget(subject, binding->mPredicate, true, aValue);
  205. if (*aValue)
  206. mValues[idx] = *aValue;
  207. }
  208. }
  209. }
  210. }
  211. }
  212. void
  213. nsBindingValues::RemoveDependencies(nsIRDFResource* aSubject,
  214. nsXULTemplateResultRDF* aResult)
  215. {
  216. if (mBindings)
  217. mBindings->RemoveDependencies(aSubject, aResult);
  218. }