1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Chris Waterson <waterson@netscape.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef nsTemplateRule_h__
40 #define nsTemplateRule_h__
44 #include "nsIRDFDataSource.h"
45 #include "nsIRDFResource.h"
46 #include "nsIContent.h"
47 #include "nsIDOMNode.h"
48 #include "nsVoidArray.h"
50 #include "nsIXULTemplateRuleFilter.h"
51 #include "nsCycleCollectionParticipant.h"
53 class nsIXULTemplateQueryProcessor
;
54 class nsTemplateQuerySet
;
56 class nsTemplateCondition
59 // relations that may be used in a rule. They may be negated with the
60 // negate flag. Less and Greater are used for numeric comparisons and
61 // Before and After are used for string comparisons. For Less, Greater,
62 // Before, After, Startswith, Endswith, and Contains, the source is
63 // conceptually on the left of the relation and the target is on the
64 // right. For example, if the relation is Contains, that means Match if
65 // the source contains the target.
66 enum ConditionRelation
{
78 nsTemplateCondition(nsIAtom
* aSourceVariable
,
79 const nsAString
& aRelation
,
80 nsIAtom
* aTargetVariable
,
84 nsTemplateCondition(nsIAtom
* aSourceVariable
,
85 const nsAString
& aRelation
,
86 const nsAString
& aTargets
,
91 nsTemplateCondition(const nsAString
& aSource
,
92 const nsAString
& aRelation
,
93 nsIAtom
* aTargetVariable
,
97 ~nsTemplateCondition() { MOZ_COUNT_DTOR(nsTemplateCondition
); }
99 nsTemplateCondition
* GetNext() { return mNext
; }
100 void SetNext(nsTemplateCondition
* aNext
) { mNext
= aNext
; }
102 void SetRelation(const nsAString
& aRelation
);
105 CheckMatch(nsIXULTemplateResult
* aResult
);
108 CheckMatchStrings(const nsAString
& aLeftString
,
109 const nsAString
& aRightString
);
112 nsCOMPtr
<nsIAtom
> mSourceVariable
;
114 ConditionRelation mRelation
;
115 nsCOMPtr
<nsIAtom
> mTargetVariable
;
116 nsStringArray mTargetList
;
117 PRPackedBool mIgnoreCase
;
118 PRPackedBool mNegate
;
120 nsTemplateCondition
* mNext
;
124 * A rule consists of:
126 * - Conditions, a set of unbound variables with consistency
127 * constraints that specify the values that each variable can
128 * assume. The conditions must be completely and consistently
129 * "bound" for the rule to be considered "matched".
131 * - Bindings, a set of unbound variables with consistency constraints
132 * that specify the values that each variable can assume. Unlike the
133 * conditions, the bindings need not be bound for the rule to be
134 * considered matched.
136 * - Content that should be constructed when the rule is "activated".
142 nsTemplateRule(nsIContent
* aRuleNode
,
144 nsTemplateQuerySet
* aQuerySet
);
149 * Return the <action> node that this rule was constructed from, or its
150 * logical equivalent for shorthand syntaxes. That is, the parent node of
151 * the content that should be generated for this rule.
153 nsIContent
* GetAction() const { return mAction
; }
156 * Return the <rule> content node that this rule was constructed from.
157 * @param aResult an out parameter, which will contain the rule node
158 * @return NS_OK if no errors occur.
160 nsresult
GetRuleNode(nsIDOMNode
** aResult
) const;
162 void SetVars(nsIAtom
* aRefVariable
, nsIAtom
* aMemberVariable
)
164 mRefVariable
= aRefVariable
;
165 mMemberVariable
= aMemberVariable
;
168 void SetRuleFilter(nsIXULTemplateRuleFilter
* aRuleFilter
)
170 mRuleFilter
= aRuleFilter
;
173 nsIAtom
* GetTag() { return mTag
; }
174 void SetTag(nsIAtom
* aTag
) { mTag
= aTag
; }
176 nsIAtom
* GetMemberVariable() { return mMemberVariable
; }
179 * Set the first condition for the rule. Other conditions are linked
180 * to it using the condition's SetNext method.
182 void SetCondition(nsTemplateCondition
* aConditions
);
185 * Check if the result matches the rule by first looking at the conditions.
186 * If the results is accepted by the conditions, the rule filter, if any
187 * was set, is checked. If either check rejects a result, a match cannot
188 * occur for this rule and result.
191 CheckMatch(nsIXULTemplateResult
* aResult
) const;
194 * Determine if the rule has the specified binding
197 HasBinding(nsIAtom
* aSourceVariable
,
199 nsIAtom
* aTargetVariable
) const;
202 * Add a binding to the rule. A binding consists of an already-bound
203 * source variable, and the RDF property that should be tested to
204 * generate a target value. The target value is bound to a target
207 * @param aSourceVariable the source variable that will be used in
209 * @param aExpr the expression that will be used in the query.
210 * @param aTargetVariable the variable whose value will be bound
211 * to the RDF node that is returned when querying the binding
212 * @return NS_OK if no errors occur.
214 nsresult
AddBinding(nsIAtom
* aSourceVariable
,
216 nsIAtom
* aTargetVariable
);
219 * Inform the query processor of the bindings that are set for a rule.
220 * This should be called after all the bindings for a rule are compiled.
223 AddBindingsToQueryProcessor(nsIXULTemplateQueryProcessor
* aProcessor
);
225 void Traverse(nsCycleCollectionTraversalCallback
&cb
) const
227 cb
.NoteXPCOMChild(mRuleNode
);
228 cb
.NoteXPCOMChild(mAction
);
234 nsCOMPtr
<nsIAtom
> mSourceVariable
;
235 nsCOMPtr
<nsIAtom
> mTargetVariable
;
241 // backreference to the query set which owns this rule
242 nsTemplateQuerySet
* mQuerySet
;
244 // the <rule> node, or the <template> node if there is no <rule>
245 nsCOMPtr
<nsIDOMNode
> mRuleNode
;
247 // the <action> node, or, if there is no <action>, the container node
248 // which contains the content to generate
249 nsCOMPtr
<nsIContent
> mAction
;
251 // the rule filter set by the builder's SetRuleFilter function
252 nsCOMPtr
<nsIXULTemplateRuleFilter
> mRuleFilter
;
254 // indicates that the rule will only match when generating content
255 // to be inserted into a container with this tag
256 nsCOMPtr
<nsIAtom
> mTag
;
258 // linked-list of the bindings for the rule, owned by the rule.
261 nsCOMPtr
<nsIAtom
> mRefVariable
;
262 nsCOMPtr
<nsIAtom
> mMemberVariable
;
264 nsTemplateCondition
* mConditions
; // owned by nsTemplateRule
267 /** nsTemplateQuerySet
269 * A single <queryset> which holds the query node and the rules for it.
270 * All builders have at least one queryset, which may be created with an
271 * explicit <queryset> tag or implied if the tag is not used.
273 * These queryset objects are created and owned by the builder in its
276 class nsTemplateQuerySet
279 nsVoidArray mRules
; // rules owned by nsTemplateQuerySet
281 // a number which increments for each successive queryset. It is stored so
282 // it can be used as an optimization when updating results so that it is
283 // known where to insert them into a match.
289 nsCOMPtr
<nsIContent
> mQueryNode
;
291 // compiled opaque query object returned by the query processor's
293 nsCOMPtr
<nsISupports
> mCompiledQuery
;
295 // indicates that the query will only generate content to be inserted into
296 // a container with this tag
297 nsCOMPtr
<nsIAtom
> mTag
;
299 nsTemplateQuerySet(PRInt32 aPriority
)
300 : mPriority(aPriority
)
302 MOZ_COUNT_CTOR(nsTemplateQuerySet
);
305 ~nsTemplateQuerySet()
307 MOZ_COUNT_DTOR(nsTemplateQuerySet
);
311 PRInt32
Priority() const
316 nsIAtom
* GetTag() { return mTag
; }
317 void SetTag(nsIAtom
* aTag
) { mTag
= aTag
; }
319 nsresult
AddRule(nsTemplateRule
*aChild
)
321 // nsTemplateMatch stores the index as a 16-bit value,
322 // so check to make sure for overflow
323 if (mRules
.Count() == PR_INT16_MAX
)
324 return NS_ERROR_FAILURE
;
326 if (!mRules
.AppendElement(aChild
))
327 return NS_ERROR_OUT_OF_MEMORY
;
331 PRInt16
RuleCount() const
333 return mRules
.Count();
336 nsTemplateRule
* GetRuleAt(PRInt16 aIndex
)
338 return static_cast<nsTemplateRule
*>(mRules
[aIndex
]);
343 for (PRInt32 r
= mRules
.Count() - 1; r
>= 0; r
--) {
344 nsTemplateRule
* rule
= static_cast<nsTemplateRule
*>(mRules
[r
]);
351 #endif // nsTemplateRule_h__