nsXULTemplateQueryProcessorXML.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #ifndef nsXULTemplateQueryProcessorXML_h__
  6. #define nsXULTemplateQueryProcessorXML_h__
  7. #include "nsIXULTemplateBuilder.h"
  8. #include "nsIXULTemplateQueryProcessor.h"
  9. #include "nsAutoPtr.h"
  10. #include "nsISimpleEnumerator.h"
  11. #include "nsString.h"
  12. #include "nsCOMArray.h"
  13. #include "nsRefPtrHashtable.h"
  14. #include "nsIDOMEventListener.h"
  15. #include "nsIDOMXPathEvaluator.h"
  16. #include "nsXMLBinding.h"
  17. #include "nsCycleCollectionParticipant.h"
  18. #include "nsIXMLHttpRequest.h"
  19. #include "mozilla/Attributes.h"
  20. #include "mozilla/dom/Element.h"
  21. #include "mozilla/dom/XPathEvaluator.h"
  22. #include "mozilla/dom/XPathResult.h"
  23. class nsXULTemplateQueryProcessorXML;
  24. #define NS_IXMLQUERY_IID \
  25. {0x0358d692, 0xccce, 0x4a97, \
  26. { 0xb2, 0x51, 0xba, 0x8f, 0x17, 0x0f, 0x3b, 0x6f }}
  27. class nsXMLQuery final : public nsISupports
  28. {
  29. public:
  30. NS_DECLARE_STATIC_IID_ACCESSOR(NS_IXMLQUERY_IID)
  31. NS_DECL_ISUPPORTS
  32. // return a weak reference to the processor the query was created from
  33. nsXULTemplateQueryProcessorXML* Processor() { return mProcessor; }
  34. // return a weak reference t the member variable for the query
  35. nsIAtom* GetMemberVariable() { return mMemberVariable; }
  36. // return a weak reference to the expression used to generate results
  37. mozilla::dom::XPathExpression* GetResultsExpression()
  38. { return mResultsExpr; }
  39. // return a weak reference to the additional required bindings
  40. nsXMLBindingSet* GetBindingSet() { return mRequiredBindings; }
  41. // add a required binding for the query
  42. void
  43. AddBinding(nsIAtom* aVar, nsAutoPtr<mozilla::dom::XPathExpression>&& aExpr)
  44. {
  45. if (!mRequiredBindings) {
  46. mRequiredBindings = new nsXMLBindingSet();
  47. }
  48. mRequiredBindings->AddBinding(aVar, mozilla::Move(aExpr));
  49. }
  50. nsXMLQuery(nsXULTemplateQueryProcessorXML* aProcessor,
  51. nsIAtom* aMemberVariable,
  52. nsAutoPtr<mozilla::dom::XPathExpression>&& aResultsExpr)
  53. : mProcessor(aProcessor),
  54. mMemberVariable(aMemberVariable),
  55. mResultsExpr(aResultsExpr)
  56. { }
  57. protected:
  58. ~nsXMLQuery() {}
  59. nsXULTemplateQueryProcessorXML* mProcessor;
  60. nsCOMPtr<nsIAtom> mMemberVariable;
  61. nsAutoPtr<mozilla::dom::XPathExpression> mResultsExpr;
  62. RefPtr<nsXMLBindingSet> mRequiredBindings;
  63. };
  64. NS_DEFINE_STATIC_IID_ACCESSOR(nsXMLQuery, NS_IXMLQUERY_IID)
  65. class nsXULTemplateResultSetXML final : public nsISimpleEnumerator
  66. {
  67. private:
  68. // reference back to the query
  69. nsCOMPtr<nsXMLQuery> mQuery;
  70. // the binding set created from <assign> nodes
  71. RefPtr<nsXMLBindingSet> mBindingSet;
  72. // set of results contained in this enumerator
  73. RefPtr<mozilla::dom::XPathResult> mResults;
  74. // current position within the list of results
  75. uint32_t mPosition;
  76. ~nsXULTemplateResultSetXML() {}
  77. public:
  78. // nsISupports interface
  79. NS_DECL_ISUPPORTS
  80. // nsISimpleEnumerator interface
  81. NS_DECL_NSISIMPLEENUMERATOR
  82. nsXULTemplateResultSetXML(nsXMLQuery* aQuery,
  83. already_AddRefed<mozilla::dom::XPathResult> aResults,
  84. nsXMLBindingSet* aBindingSet)
  85. : mQuery(aQuery),
  86. mBindingSet(aBindingSet),
  87. mResults(aResults),
  88. mPosition(0)
  89. {}
  90. };
  91. class nsXULTemplateQueryProcessorXML final : public nsIXULTemplateQueryProcessor,
  92. public nsIDOMEventListener
  93. {
  94. public:
  95. nsXULTemplateQueryProcessorXML()
  96. : mGenerationStarted(false)
  97. {}
  98. // nsISupports interface
  99. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  100. NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsXULTemplateQueryProcessorXML,
  101. nsIXULTemplateQueryProcessor)
  102. // nsIXULTemplateQueryProcessor interface
  103. NS_DECL_NSIXULTEMPLATEQUERYPROCESSOR
  104. // nsIDOMEventListener interface
  105. NS_DECL_NSIDOMEVENTLISTENER
  106. nsXMLBindingSet*
  107. GetOptionalBindingsForRule(nsIDOMNode* aRuleNode);
  108. // create an XPath expression from aExpr, using aNode for
  109. // resolving namespaces
  110. mozilla::dom::XPathExpression*
  111. CreateExpression(const nsAString& aExpr,
  112. nsINode* aNode,
  113. mozilla::ErrorResult& aRv);
  114. private:
  115. ~nsXULTemplateQueryProcessorXML() {}
  116. bool mGenerationStarted;
  117. nsRefPtrHashtable<nsISupportsHashKey, nsXMLBindingSet> mRuleToBindingsMap;
  118. nsCOMPtr<mozilla::dom::Element> mRoot;
  119. RefPtr<mozilla::dom::XPathEvaluator> mEvaluator;
  120. nsCOMPtr<nsIXULTemplateBuilder> mTemplateBuilder;
  121. nsCOMPtr<nsIXMLHttpRequest> mRequest;
  122. };
  123. #endif // nsXULTemplateQueryProcessorXML_h__