nsXMLBinding.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  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. #ifndef nsXMLBinding_h__
  6. #define nsXMLBinding_h__
  7. #include "nsAutoPtr.h"
  8. #include "nsIAtom.h"
  9. #include "mozilla/Attributes.h"
  10. #include "mozilla/dom/XPathExpression.h"
  11. class nsINode;
  12. class nsXULTemplateResultXML;
  13. class nsXMLBindingValues;
  14. namespace mozilla {
  15. namespace dom {
  16. class XPathResult;
  17. } // namespace dom
  18. } // namespace mozilla
  19. /**
  20. * Classes related to storing bindings for XML handling.
  21. */
  22. /**
  23. * a <binding> description
  24. */
  25. struct nsXMLBinding {
  26. nsCOMPtr<nsIAtom> mVar;
  27. nsAutoPtr<mozilla::dom::XPathExpression> mExpr;
  28. nsAutoPtr<nsXMLBinding> mNext;
  29. nsXMLBinding(nsIAtom* aVar, nsAutoPtr<mozilla::dom::XPathExpression>&& aExpr)
  30. : mVar(aVar), mExpr(aExpr), mNext(nullptr)
  31. {
  32. MOZ_COUNT_CTOR(nsXMLBinding);
  33. }
  34. ~nsXMLBinding()
  35. {
  36. MOZ_COUNT_DTOR(nsXMLBinding);
  37. }
  38. };
  39. /**
  40. * a collection of <binding> descriptors. This object is refcounted by
  41. * nsXMLBindingValues objects and the query processor.
  42. */
  43. class nsXMLBindingSet final
  44. {
  45. ~nsXMLBindingSet();
  46. public:
  47. // pointer to the first binding in a linked list
  48. nsAutoPtr<nsXMLBinding> mFirst;
  49. NS_INLINE_DECL_REFCOUNTING(nsXMLBindingSet);
  50. /**
  51. * Add a binding to the set
  52. */
  53. void
  54. AddBinding(nsIAtom* aVar, nsAutoPtr<mozilla::dom::XPathExpression>&& aExpr);
  55. /**
  56. * The nsXMLBindingValues class stores an array of values, one for each
  57. * target symbol that could be set by the bindings in the set.
  58. * LookupTargetIndex determines the index into the array for a given
  59. * target symbol.
  60. */
  61. int32_t
  62. LookupTargetIndex(nsIAtom* aTargetVariable, nsXMLBinding** aBinding);
  63. };
  64. /**
  65. * a set of values of bindings. This object is used once per result.
  66. */
  67. class nsXMLBindingValues
  68. {
  69. protected:
  70. // the binding set
  71. RefPtr<nsXMLBindingSet> mBindings;
  72. /**
  73. * A set of values for variable bindings. To look up a binding value,
  74. * scan through the binding set in mBindings for the right target atom.
  75. * Its index will correspond to the index in this array.
  76. */
  77. nsTArray<RefPtr<mozilla::dom::XPathResult> > mValues;
  78. public:
  79. nsXMLBindingValues() { MOZ_COUNT_CTOR(nsXMLBindingValues); }
  80. ~nsXMLBindingValues() { MOZ_COUNT_DTOR(nsXMLBindingValues); }
  81. nsXMLBindingSet* GetBindingSet() { return mBindings; }
  82. void SetBindingSet(nsXMLBindingSet* aBindings) { mBindings = aBindings; }
  83. int32_t
  84. LookupTargetIndex(nsIAtom* aTargetVariable, nsXMLBinding** aBinding)
  85. {
  86. return mBindings ?
  87. mBindings->LookupTargetIndex(aTargetVariable, aBinding) : -1;
  88. }
  89. /**
  90. * Retrieve the assignment for a particular variable
  91. *
  92. * aResult the result generated from the template
  93. * aBinding the binding looked up using LookupTargetIndex
  94. * aIndex the index of the assignment to retrieve
  95. * aType the type of result expected
  96. */
  97. mozilla::dom::XPathResult*
  98. GetAssignmentFor(nsXULTemplateResultXML* aResult,
  99. nsXMLBinding* aBinding,
  100. int32_t idx,
  101. uint16_t type);
  102. nsINode*
  103. GetNodeAssignmentFor(nsXULTemplateResultXML* aResult,
  104. nsXMLBinding* aBinding,
  105. int32_t idx);
  106. void
  107. GetStringAssignmentFor(nsXULTemplateResultXML* aResult,
  108. nsXMLBinding* aBinding,
  109. int32_t idx,
  110. nsAString& aValue);
  111. };
  112. #endif // nsXMLBinding_h__