nsIXULTemplateQueryProcessor.idl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. #include "domstubs.idl"
  6. interface nsIAtom;
  7. interface nsIArray;
  8. interface nsISimpleEnumerator;
  9. interface nsIXULTemplateResult;
  10. interface nsIXULTemplateRuleFilter;
  11. interface nsIXULTemplateBuilder;
  12. /**
  13. * A query processor takes a template query and generates results for it given
  14. * a datasource and a reference point. There is a one-to-one relationship
  15. * between a template builder and a query processor. The template builder
  16. * creates the query processor, and there is no other means to retrieve it.
  17. *
  18. * A template query is the contents inside a <query> element within the
  19. * template. The actual syntax is opaque to the template builder and defined
  20. * by a query processor. The query is expected to consist of either text or
  21. * DOM nodes that, when executed by a call to the generateResults method, will
  22. * allow the generation of a list of results.
  23. *
  24. * The template builder will supply two variables, the reference variable and
  25. * the member variable to further indicate what part of the datasource is to
  26. * be examined in addition to the query itself. The reference is always
  27. * a placeholder for the starting point and the member is always a placeholder
  28. * for the end points (the results).
  29. *
  30. * The reference point is important when generating output recursively, as
  31. * the query will be the same for each iteration, however, the reference point
  32. * will differ.
  33. *
  34. * For instance, when examining an XML source, an XML query processor might
  35. * begin at the node referred by the reference variable and end at a list of
  36. * that node's children.
  37. *
  38. * Some queries may not need the reference variable if the syntax or the form
  39. * of the data implies the value. For instance, a datasource that holds a
  40. * table that can only produce one set of results.
  41. *
  42. * The reference variable may be specified in a template by setting the
  43. * "container" attribute on the <template> element to the variable to use. The
  44. * member variable may be specified in a similar way using the "member"
  45. * attribute, or it may be specified in the first <action> body in the
  46. * template as the value of a uri attribute on an element. A breadth-first
  47. * search of the first action is performed to find this element.
  48. *
  49. * If unspecified, the default value of the reference variable is ?uri.
  50. *
  51. * For example, a query might have the following syntax:
  52. *
  53. * (?id, ?name, ?url) from Bookmarks where parentfolder = ?start
  54. *
  55. * This query might generate a result for each bookmark within a given folder.
  56. * The variable ?start would be the reference variable, while the variable ?id
  57. * would be the member variable, since it is the unique value that identifies
  58. * a result. Each result will have the four variables referred to defined for
  59. * it and the values may be retrieved using the result's getBindingFor and
  60. * getBindingObjectFor methods.
  61. *
  62. * The template builder must call initializeForBuilding before the other
  63. * methods, except for translateRef. The builder will then call compileQuery
  64. * for each query in the template to compile the queries. When results need
  65. * to be generated, the builder will call generateResults. The
  66. * initializeForBuilding, compileQuery and addBinding methods may not be
  67. * called after generateResults has been called until the builder indicates
  68. * that the generated output is being removed by calling the done method.
  69. *
  70. * Currently, the datasource supplied to the methods will always be an
  71. * nsIRDFDataSource or a DOM node, and will always be the same one in between
  72. * calls to initializeForBuilding and done.
  73. */
  74. [scriptable, uuid(C257573F-444F-468A-BA27-DE979DC55FE4)]
  75. interface nsIXULTemplateQueryProcessor : nsISupports
  76. {
  77. /**
  78. * Retrieve the datasource to use for the query processor. The list of
  79. * datasources in a template is specified using the datasources attribute as
  80. * a space separated list of URIs. This list is processed by the builder and
  81. * supplied to the query processor in the aDataSources array as a list of
  82. * nsIURI objects or nsIDOMNode objects. This method may return an object
  83. * corresponding to these URIs and the builder will supply this object to
  84. * other query processor methods. For example, for an XML source, the
  85. * datasource might be an nsIDOMNode.
  86. *
  87. * All of these URIs are checked by the builder so it is safe to use them,
  88. * however note that a URI that redirects may still needs to be checked to
  89. * ensure that the document containing aRootNode may access it. This is the
  90. * responsibility of the query processor if it needs to load the content of
  91. * the URI.
  92. *
  93. * If the query processor needs to load the datasource asynchronously, it
  94. * may set the aShouldDelayBuilding returned parameter to true to delay
  95. * building the template content, and call the builder's Rebuild method when
  96. * the data is available.
  97. *
  98. * @param aDataSources the list of nsIURI objects and/or nsIDOMNode objects
  99. * @param aRootNode the root node the builder is attached to
  100. * @param aIsTrusted true if the template is in a trusted document
  101. * @param aBuilder the template builder
  102. * @param aShouldDelayBuilding [out] whether the builder should wait to
  103. * build the content or not
  104. * @returns a datasource object
  105. */
  106. nsISupports getDatasource(in nsIArray aDataSources,
  107. in nsIDOMNode aRootNode,
  108. in boolean aIsTrusted,
  109. in nsIXULTemplateBuilder aBuilder,
  110. out boolean aShouldDelayBuilding);
  111. /**
  112. * Initialize for query generation. This will be called before the rules are
  113. * processed and whenever the template is rebuilt. This method must be
  114. * called once before any of the other query processor methods except for
  115. * translateRef.
  116. *
  117. * @param aDatasource datasource for the data
  118. * @param aBuilder the template builder
  119. * @param aRootNode the root node the builder is attached to
  120. *
  121. * @throws NS_ERROR_INVALID_ARG if the datasource is not supported or
  122. * NS_ERROR_UNEXPECTED if generateResults has already been called.
  123. */
  124. void initializeForBuilding(in nsISupports aDatasource,
  125. in nsIXULTemplateBuilder aBuilder,
  126. in nsIDOMNode aRootNode);
  127. /**
  128. * Called when the template builder is being destroyed so that the query
  129. * processor can clean up any state. The query processor should remove as
  130. * much state as possible, such as results or references to the builder.
  131. * This method will also be called when the template is going to be rebuilt.
  132. */
  133. void done();
  134. /**
  135. * Compile a query from a node. The result of this function will later be
  136. * passed to generateResults for result generation. If null is returned,
  137. * the query will be ignored.
  138. *
  139. * The template builder will call this method once for each query within
  140. * the template, before any results can be generated using generateResults,
  141. * but after initializeForBuilding has been called. This method should not
  142. * be called again for the same query unless the template is rebuilt.
  143. *
  144. * The reference variable may be used by the query processor as a
  145. * placeholder for the reference point, or starting point in the query.
  146. *
  147. * The member variable is determined from the member attribute on the
  148. * template, or from the uri in the first action's rule if that attribute is
  149. * not present. A rule processor may use the member variable as a hint to
  150. * indicate what variable is expected to contain the results.
  151. *
  152. * @param aBuilder the template builder
  153. * @param aQuery <query> node to compile
  154. * @param aRefVariable the reference variable
  155. * @param aMemberVariable the member variable
  156. *
  157. * @returns a compiled query object
  158. */
  159. nsISupports compileQuery(in nsIXULTemplateBuilder aBuilder,
  160. in nsIDOMNode aQuery,
  161. in nsIAtom aRefVariable,
  162. in nsIAtom aMemberVariable);
  163. /**
  164. * Generate the results of a query and return them in an enumerator. The
  165. * enumerator must contain nsIXULTemplateResult objects. If there are no
  166. * results, an empty enumerator must be returned.
  167. *
  168. * The datasource will be the same as the one passed to the earlier
  169. * initializeForBuilding method. The context reference (aRef) is a reference
  170. * point used when calculating results.
  171. *
  172. * The value of aQuery must be the result of a previous call to compileQuery
  173. * from this query processor. This method may be called multiple times,
  174. * typically with different values for aRef.
  175. *
  176. * @param aDatasource datasource for the data
  177. * @param aRef context reference value used as a starting point
  178. * @param aQuery the compiled query returned from query compilation
  179. *
  180. * @returns an enumerator of nsIXULTemplateResult objects as the results
  181. *
  182. * @throws NS_ERROR_INVALID_ARG if aQuery is invalid
  183. */
  184. nsISimpleEnumerator generateResults(in nsISupports aDatasource,
  185. in nsIXULTemplateResult aRef,
  186. in nsISupports aQuery);
  187. /**
  188. * Add a variable binding for a particular rule. A binding allows an
  189. * additional variable to be set for a result, outside of those defined
  190. * within the query. These bindings are always optional, in that they will
  191. * never affect the results generated.
  192. *
  193. * This function will never be called after generateResults. Any bindings
  194. * that were added should be applied to each result when the result's
  195. * ruleMatched method is called, since the bindings are different for each
  196. * rule.
  197. *
  198. * The reference aRef may be used to determine the reference when
  199. * calculating the value for the binding, for example when a value should
  200. * depend on the value of another variable.
  201. *
  202. * The syntax of the expression aExpr is defined by the query processor. If
  203. * the syntax is invalid, the binding should be ignored. Only fatal errors
  204. * should be thrown, or NS_ERROR_UNEXPECTED if generateResults has already
  205. * been called.
  206. *
  207. * As an example, if the reference aRef is the variable '?count' which
  208. * holds the value 5, and the expression aExpr is the string '+2', the value
  209. * of the variable aVar would be 7, assuming the query processor considers
  210. * the syntax '+2' to mean add two to the reference.
  211. *
  212. * @param aRuleNode rule to add the binding to
  213. * @param aVar variable that will be bound
  214. * @param aRef variable that holds reference value
  215. * @param aExpr expression used to compute the value to assign
  216. */
  217. void addBinding(in nsIDOMNode aRuleNode,
  218. in nsIAtom aVar,
  219. in nsIAtom aRef,
  220. in AString aExpr);
  221. /**
  222. * Translate a ref attribute string into a result. This is used as the
  223. * reference point by the template builder when generating the first level
  224. * of content. For recursive generation, the result from the parent
  225. * generation phase will be used directly as the reference so a translation
  226. * is not needed. This allows all levels to be generated using objects that
  227. * all implement the nsIXULTemplateResult interface.
  228. *
  229. * This method may be called before initializeForBuilding, so the
  230. * implementation may use the supplied datasource if it is needed to
  231. * translate the reference.
  232. *
  233. * @param aDatasource datasource for the data
  234. * @param aRefString the ref attribute string
  235. *
  236. * @return the translated ref
  237. */
  238. nsIXULTemplateResult translateRef(in nsISupports aDatasource,
  239. in AString aRefString);
  240. /**
  241. * Compare two results to determine their order, used when sorting results.
  242. * This method should return -1 when the left result is less than the right,
  243. * 0 if both are equivalent, and 1 if the left is greater than the right.
  244. * The comparison should only consider the values for the specified
  245. * variable.
  246. *
  247. * If the comparison variable is null, the results may be
  248. * sorted in a natural order, for instance, based on the order the data in
  249. * stored in the datasource.
  250. *
  251. * The sort hints are the flags in nsIXULSortService.
  252. *
  253. * This method must only be called with results that were created by this
  254. * query processor.
  255. *
  256. * @param aLeft the left result to compare
  257. * @param aRight the right result to compare
  258. * @param aVar variable to compare
  259. *
  260. * @param returns -1 if less, 0 if equal, or 1 if greater
  261. */
  262. int32_t compareResults(in nsIXULTemplateResult aLeft,
  263. in nsIXULTemplateResult aRight,
  264. in nsIAtom aVar,
  265. in unsigned long aSortHints);
  266. };