nsXULTemplateResultXML.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 "nsIServiceManager.h"
  6. #include "nsIDOMNode.h"
  7. #include "nsIDOMElement.h"
  8. #include "nsIContent.h"
  9. #include "nsIRDFService.h"
  10. #include "nsXULTemplateResultXML.h"
  11. #include "nsXMLBinding.h"
  12. static uint32_t sTemplateId = 0;
  13. NS_IMPL_ISUPPORTS(nsXULTemplateResultXML, nsIXULTemplateResult)
  14. nsXULTemplateResultXML::nsXULTemplateResultXML(nsXMLQuery* aQuery,
  15. nsIContent* aNode,
  16. nsXMLBindingSet* aBindings)
  17. : mQuery(aQuery), mNode(aNode)
  18. {
  19. // If the node has an id, create the uri from it. Otherwise, there isn't
  20. // anything to identify the node with so just use a somewhat random number.
  21. nsCOMPtr<nsIAtom> id = mNode->GetID();
  22. if (id) {
  23. nsCOMPtr<nsIURI> uri = mNode->GetBaseURI();
  24. nsAutoCString spec;
  25. uri->GetSpec(spec);
  26. mId = NS_ConvertUTF8toUTF16(spec);
  27. nsAutoString idstr;
  28. id->ToString(idstr);
  29. mId += NS_LITERAL_STRING("#") + idstr;
  30. }
  31. else {
  32. nsAutoString rowid(NS_LITERAL_STRING("row"));
  33. rowid.AppendInt(++sTemplateId);
  34. mId.Assign(rowid);
  35. }
  36. if (aBindings)
  37. mRequiredValues.SetBindingSet(aBindings);
  38. }
  39. NS_IMETHODIMP
  40. nsXULTemplateResultXML::GetIsContainer(bool* aIsContainer)
  41. {
  42. // a node is considered a container if it has children
  43. *aIsContainer = mNode && mNode->HasChildNodes();
  44. return NS_OK;
  45. }
  46. NS_IMETHODIMP
  47. nsXULTemplateResultXML::GetIsEmpty(bool* aIsEmpty)
  48. {
  49. // a node is considered empty if it has no elements as children
  50. nsCOMPtr<nsIContent> content = do_QueryInterface(mNode);
  51. if (content) {
  52. for (nsIContent* child = content->GetFirstChild();
  53. child;
  54. child = child->GetNextSibling()) {
  55. if (child->IsElement()) {
  56. *aIsEmpty = false;
  57. return NS_OK;
  58. }
  59. }
  60. }
  61. *aIsEmpty = true;
  62. return NS_OK;
  63. }
  64. NS_IMETHODIMP
  65. nsXULTemplateResultXML::GetMayProcessChildren(bool* aMayProcessChildren)
  66. {
  67. *aMayProcessChildren = true;
  68. return NS_OK;
  69. }
  70. NS_IMETHODIMP
  71. nsXULTemplateResultXML::GetId(nsAString& aId)
  72. {
  73. aId = mId;
  74. return NS_OK;
  75. }
  76. NS_IMETHODIMP
  77. nsXULTemplateResultXML::GetResource(nsIRDFResource** aResource)
  78. {
  79. *aResource = nullptr;
  80. return NS_OK;
  81. }
  82. NS_IMETHODIMP
  83. nsXULTemplateResultXML::GetType(nsAString& aType)
  84. {
  85. aType.Truncate();
  86. return NS_OK;
  87. }
  88. NS_IMETHODIMP
  89. nsXULTemplateResultXML::GetBindingFor(nsIAtom* aVar, nsAString& aValue)
  90. {
  91. NS_ENSURE_ARG_POINTER(aVar);
  92. // get the position of the atom in the variables table
  93. nsXMLBinding* binding;
  94. int32_t idx = mRequiredValues.LookupTargetIndex(aVar, &binding);
  95. if (idx >= 0) {
  96. mRequiredValues.GetStringAssignmentFor(this, binding, idx, aValue);
  97. return NS_OK;
  98. }
  99. idx = mOptionalValues.LookupTargetIndex(aVar, &binding);
  100. if (idx >= 0) {
  101. mOptionalValues.GetStringAssignmentFor(this, binding, idx, aValue);
  102. return NS_OK;
  103. }
  104. // if the variable is not bound, just use the variable name as the name of
  105. // an attribute to retrieve
  106. nsAutoString attr;
  107. aVar->ToString(attr);
  108. if (attr.Length() > 1) {
  109. nsCOMPtr<nsIDOMElement> element = do_QueryInterface(mNode);
  110. if (element)
  111. return element->GetAttribute(Substring(attr, 1), aValue);
  112. }
  113. aValue.Truncate();
  114. return NS_OK;
  115. }
  116. NS_IMETHODIMP
  117. nsXULTemplateResultXML::GetBindingObjectFor(nsIAtom* aVar, nsISupports** aValue)
  118. {
  119. NS_ENSURE_ARG_POINTER(aVar);
  120. nsXMLBinding* binding;
  121. nsCOMPtr<nsISupports> node;
  122. if (mQuery && aVar == mQuery->GetMemberVariable()) {
  123. node = mNode;
  124. }
  125. else {
  126. int32_t idx = mRequiredValues.LookupTargetIndex(aVar, &binding);
  127. if (idx > 0) {
  128. node = mRequiredValues.GetNodeAssignmentFor(this, binding, idx);
  129. }
  130. else {
  131. idx = mOptionalValues.LookupTargetIndex(aVar, &binding);
  132. if (idx > 0) {
  133. node = mOptionalValues.GetNodeAssignmentFor(this, binding, idx);
  134. }
  135. }
  136. }
  137. node.forget(aValue);
  138. return NS_OK;
  139. }
  140. NS_IMETHODIMP
  141. nsXULTemplateResultXML::RuleMatched(nsISupports* aQueryNode,
  142. nsIDOMNode* aRuleNode)
  143. {
  144. // when a rule matches, set the bindings that must be used.
  145. nsXULTemplateQueryProcessorXML* processor = mQuery ? mQuery->Processor() :
  146. nullptr;
  147. if (processor) {
  148. nsXMLBindingSet* bindings =
  149. processor->GetOptionalBindingsForRule(aRuleNode);
  150. if (bindings)
  151. mOptionalValues.SetBindingSet(bindings);
  152. }
  153. return NS_OK;
  154. }
  155. NS_IMETHODIMP
  156. nsXULTemplateResultXML::HasBeenRemoved()
  157. {
  158. return NS_OK;
  159. }