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
4 : *
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/
9 : *
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
13 : * License.
14 : *
15 : * The Original Code is Mozilla Communicator client code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : * Chris Waterson <waterson@netscape.com>
24 : *
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.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsRDFPropertyTestNode.h"
40 : #include "nsString.h"
41 : #include "nsXULContentUtils.h"
42 :
43 : #include "prlog.h"
44 : #ifdef PR_LOGGING
45 : extern PRLogModuleInfo* gXULTemplateLog;
46 : #include "nsIRDFLiteral.h"
47 : #endif
48 :
49 0 : nsRDFPropertyTestNode::nsRDFPropertyTestNode(TestNode* aParent,
50 : nsXULTemplateQueryProcessorRDF* aProcessor,
51 : nsIAtom* aSourceVariable,
52 : nsIRDFResource* aProperty,
53 : nsIAtom* aTargetVariable)
54 : : nsRDFTestNode(aParent),
55 : mProcessor(aProcessor),
56 : mSourceVariable(aSourceVariable),
57 : mSource(nsnull),
58 : mProperty(aProperty),
59 : mTargetVariable(aTargetVariable),
60 0 : mTarget(nsnull)
61 : {
62 : #ifdef PR_LOGGING
63 0 : if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
64 0 : const char* prop = "(null)";
65 0 : if (aProperty)
66 0 : aProperty->GetValueConst(&prop);
67 :
68 0 : nsAutoString svar(NS_LITERAL_STRING("(none)"));
69 0 : if (mSourceVariable)
70 0 : mSourceVariable->ToString(svar);
71 :
72 0 : nsAutoString tvar(NS_LITERAL_STRING("(none)"));
73 0 : if (mTargetVariable)
74 0 : mTargetVariable->ToString(tvar);
75 :
76 0 : PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
77 : ("nsRDFPropertyTestNode[%p]: parent=%p source=%s property=%s target=%s",
78 : this, aParent, NS_ConvertUTF16toUTF8(svar).get(), prop, NS_ConvertUTF16toUTF8(tvar).get()));
79 : }
80 : #endif
81 0 : }
82 :
83 :
84 0 : nsRDFPropertyTestNode::nsRDFPropertyTestNode(TestNode* aParent,
85 : nsXULTemplateQueryProcessorRDF* aProcessor,
86 : nsIRDFResource* aSource,
87 : nsIRDFResource* aProperty,
88 : nsIAtom* aTargetVariable)
89 : : nsRDFTestNode(aParent),
90 : mProcessor(aProcessor),
91 : mSourceVariable(0),
92 : mSource(aSource),
93 : mProperty(aProperty),
94 : mTargetVariable(aTargetVariable),
95 0 : mTarget(nsnull)
96 : {
97 : #ifdef PR_LOGGING
98 0 : if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
99 0 : const char* source = "(null)";
100 0 : if (aSource)
101 0 : aSource->GetValueConst(&source);
102 :
103 0 : const char* prop = "(null)";
104 0 : if (aProperty)
105 0 : aProperty->GetValueConst(&prop);
106 :
107 0 : nsAutoString tvar(NS_LITERAL_STRING("(none)"));
108 0 : if (mTargetVariable)
109 0 : mTargetVariable->ToString(tvar);
110 :
111 0 : PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
112 : ("nsRDFPropertyTestNode[%p]: parent=%p source=%s property=%s target=%s",
113 : this, aParent, source, prop, NS_ConvertUTF16toUTF8(tvar).get()));
114 : }
115 : #endif
116 0 : }
117 :
118 :
119 0 : nsRDFPropertyTestNode::nsRDFPropertyTestNode(TestNode* aParent,
120 : nsXULTemplateQueryProcessorRDF* aProcessor,
121 : nsIAtom* aSourceVariable,
122 : nsIRDFResource* aProperty,
123 : nsIRDFNode* aTarget)
124 : : nsRDFTestNode(aParent),
125 : mProcessor(aProcessor),
126 : mSourceVariable(aSourceVariable),
127 : mSource(nsnull),
128 : mProperty(aProperty),
129 : mTargetVariable(0),
130 0 : mTarget(aTarget)
131 : {
132 : #ifdef PR_LOGGING
133 0 : if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
134 0 : nsAutoString svar(NS_LITERAL_STRING("(none)"));
135 0 : if (mSourceVariable)
136 0 : mSourceVariable->ToString(svar);
137 :
138 0 : const char* prop = "(null)";
139 0 : if (aProperty)
140 0 : aProperty->GetValueConst(&prop);
141 :
142 0 : nsAutoString target;
143 0 : nsXULContentUtils::GetTextForNode(aTarget, target);
144 :
145 0 : PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
146 : ("nsRDFPropertyTestNode[%p]: parent=%p source=%s property=%s target=%s",
147 : this, aParent, NS_ConvertUTF16toUTF8(svar).get(), prop, NS_ConvertUTF16toUTF8(target).get()));
148 : }
149 : #endif
150 0 : }
151 :
152 :
153 : nsresult
154 0 : nsRDFPropertyTestNode::FilterInstantiations(InstantiationSet& aInstantiations,
155 : bool* aCantHandleYet) const
156 : {
157 : nsresult rv;
158 :
159 0 : if (aCantHandleYet)
160 0 : *aCantHandleYet = false;
161 :
162 0 : nsIRDFDataSource* ds = mProcessor->GetDataSource();
163 :
164 0 : InstantiationSet::Iterator last = aInstantiations.Last();
165 0 : for (InstantiationSet::Iterator inst = aInstantiations.First(); inst != last; ++inst) {
166 : bool hasSourceBinding;
167 0 : nsCOMPtr<nsIRDFResource> sourceRes;
168 :
169 0 : if (mSource) {
170 0 : hasSourceBinding = true;
171 0 : sourceRes = mSource;
172 : }
173 : else {
174 0 : nsCOMPtr<nsIRDFNode> sourceValue;
175 0 : hasSourceBinding = inst->mAssignments.GetAssignmentFor(mSourceVariable,
176 0 : getter_AddRefs(sourceValue));
177 0 : sourceRes = do_QueryInterface(sourceValue);
178 : }
179 :
180 : bool hasTargetBinding;
181 0 : nsCOMPtr<nsIRDFNode> targetValue;
182 :
183 0 : if (mTarget) {
184 0 : hasTargetBinding = true;
185 0 : targetValue = mTarget;
186 : }
187 : else {
188 0 : hasTargetBinding = inst->mAssignments.GetAssignmentFor(mTargetVariable,
189 0 : getter_AddRefs(targetValue));
190 : }
191 :
192 : #ifdef PR_LOGGING
193 0 : if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
194 0 : const char* source = "(unbound)";
195 0 : if (hasSourceBinding)
196 0 : sourceRes->GetValueConst(&source);
197 :
198 0 : nsAutoString target(NS_LITERAL_STRING("(unbound)"));
199 0 : if (hasTargetBinding)
200 0 : nsXULContentUtils::GetTextForNode(targetValue, target);
201 :
202 0 : PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
203 : ("nsRDFPropertyTestNode[%p]: FilterInstantiations() source=[%s] target=[%s]",
204 : this, source, NS_ConvertUTF16toUTF8(target).get()));
205 : }
206 : #endif
207 :
208 0 : if (hasSourceBinding && hasTargetBinding) {
209 : // it's a consistency check. see if we have a assignment that is consistent
210 : bool hasAssertion;
211 : rv = ds->HasAssertion(sourceRes, mProperty, targetValue,
212 0 : true, &hasAssertion);
213 0 : if (NS_FAILED(rv)) return rv;
214 :
215 : #ifdef PR_LOGGING
216 0 : PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
217 : (" consistency check => %s", hasAssertion ? "passed" : "failed"));
218 : #endif
219 :
220 0 : if (hasAssertion) {
221 : // it's consistent.
222 : Element* element =
223 : nsRDFPropertyTestNode::Element::Create(sourceRes,
224 : mProperty,
225 0 : targetValue);
226 :
227 0 : if (! element)
228 0 : return NS_ERROR_OUT_OF_MEMORY;
229 :
230 0 : inst->AddSupportingElement(element);
231 : }
232 : else {
233 : // it's inconsistent. remove it.
234 0 : aInstantiations.Erase(inst--);
235 0 : }
236 : }
237 0 : else if ((hasSourceBinding && ! hasTargetBinding) ||
238 0 : (! hasSourceBinding && hasTargetBinding)) {
239 : // it's an open ended query on the source or
240 : // target. figure out what matches and add as a
241 : // cross-product.
242 0 : nsCOMPtr<nsISimpleEnumerator> results;
243 0 : if (hasSourceBinding) {
244 : rv = ds->GetTargets(sourceRes,
245 : mProperty,
246 : true,
247 0 : getter_AddRefs(results));
248 : }
249 : else {
250 : rv = ds->GetSources(mProperty,
251 : targetValue,
252 : true,
253 0 : getter_AddRefs(results));
254 0 : if (NS_FAILED(rv)) return rv;
255 : }
256 :
257 0 : while (1) {
258 : bool hasMore;
259 0 : rv = results->HasMoreElements(&hasMore);
260 0 : if (NS_FAILED(rv)) return rv;
261 :
262 0 : if (! hasMore)
263 : break;
264 :
265 0 : nsCOMPtr<nsISupports> isupports;
266 0 : rv = results->GetNext(getter_AddRefs(isupports));
267 0 : if (NS_FAILED(rv)) return rv;
268 :
269 : nsIAtom* variable;
270 0 : nsCOMPtr<nsIRDFNode> value;
271 :
272 0 : if (hasSourceBinding) {
273 0 : variable = mTargetVariable;
274 :
275 0 : value = do_QueryInterface(isupports);
276 0 : NS_ASSERTION(value != nsnull, "target is not an nsIRDFNode");
277 :
278 : #ifdef PR_LOGGING
279 0 : if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
280 0 : nsAutoString s(NS_LITERAL_STRING("(none found)"));
281 0 : if (value)
282 0 : nsXULContentUtils::GetTextForNode(value, s);
283 :
284 0 : PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
285 : (" target => %s", NS_ConvertUTF16toUTF8(s).get()));
286 : }
287 : #endif
288 :
289 0 : if (! value) continue;
290 :
291 0 : targetValue = value;
292 : }
293 : else {
294 0 : variable = mSourceVariable;
295 :
296 0 : nsCOMPtr<nsIRDFResource> source = do_QueryInterface(isupports);
297 0 : NS_ASSERTION(source != nsnull, "source is not an nsIRDFResource");
298 :
299 : #ifdef PR_LOGGING
300 0 : if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
301 0 : const char* s = "(none found)";
302 0 : if (source)
303 0 : source->GetValueConst(&s);
304 :
305 0 : PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
306 : (" source => %s", s));
307 : }
308 : #endif
309 :
310 0 : if (! source) continue;
311 :
312 0 : value = sourceRes = source;
313 : }
314 :
315 : // Copy the original instantiation, and add it to the
316 : // instantiation set with the new assignment that we've
317 : // introduced. Ownership will be transferred to the
318 0 : Instantiation newinst = *inst;
319 0 : newinst.AddAssignment(variable, value);
320 :
321 : Element* element =
322 : nsRDFPropertyTestNode::Element::Create(sourceRes,
323 : mProperty,
324 0 : targetValue);
325 :
326 0 : if (! element)
327 0 : return NS_ERROR_OUT_OF_MEMORY;
328 :
329 0 : newinst.AddSupportingElement(element);
330 :
331 0 : aInstantiations.Insert(inst, newinst);
332 : }
333 :
334 : // finally, remove the "under specified" instantiation.
335 0 : aInstantiations.Erase(inst--);
336 : }
337 : else {
338 0 : if (!aCantHandleYet) {
339 0 : nsXULContentUtils::LogTemplateError(ERROR_TEMPLATE_TRIPLE_UNBOUND);
340 : // Neither source nor target assignment!
341 0 : return NS_ERROR_UNEXPECTED;
342 : }
343 :
344 0 : *aCantHandleYet = true;
345 0 : return NS_OK;
346 : }
347 : }
348 :
349 0 : return NS_OK;
350 : }
351 :
352 : bool
353 0 : nsRDFPropertyTestNode::CanPropagate(nsIRDFResource* aSource,
354 : nsIRDFResource* aProperty,
355 : nsIRDFNode* aTarget,
356 : Instantiation& aInitialBindings) const
357 : {
358 : bool result;
359 :
360 0 : if ((mProperty.get() != aProperty) ||
361 0 : (mSource && mSource.get() != aSource) ||
362 0 : (mTarget && mTarget.get() != aTarget)) {
363 0 : result = false;
364 : }
365 : else {
366 0 : if (mSourceVariable)
367 0 : aInitialBindings.AddAssignment(mSourceVariable, aSource);
368 :
369 0 : if (mTargetVariable)
370 0 : aInitialBindings.AddAssignment(mTargetVariable, aTarget);
371 :
372 0 : result = true;
373 : }
374 :
375 : #ifdef PR_LOGGING
376 0 : if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
377 : const char* source;
378 0 : aSource->GetValueConst(&source);
379 :
380 : const char* property;
381 0 : aProperty->GetValueConst(&property);
382 :
383 0 : nsAutoString target;
384 0 : nsXULContentUtils::GetTextForNode(aTarget, target);
385 :
386 0 : PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
387 : ("nsRDFPropertyTestNode[%p]: CanPropagate([%s]==[%s]=>[%s]) => %s",
388 : this, source, property, NS_ConvertUTF16toUTF8(target).get(),
389 : result ? "true" : "false"));
390 : }
391 : #endif
392 :
393 0 : return result;
394 : }
395 :
396 : void
397 0 : nsRDFPropertyTestNode::Retract(nsIRDFResource* aSource,
398 : nsIRDFResource* aProperty,
399 : nsIRDFNode* aTarget) const
400 : {
401 0 : if (aProperty == mProperty.get()) {
402 : #ifdef PR_LOGGING
403 0 : if (PR_LOG_TEST(gXULTemplateLog, PR_LOG_DEBUG)) {
404 : const char* source;
405 0 : aSource->GetValueConst(&source);
406 :
407 : const char* property;
408 0 : aProperty->GetValueConst(&property);
409 :
410 0 : nsAutoString target;
411 0 : nsXULContentUtils::GetTextForNode(aTarget, target);
412 :
413 0 : PR_LOG(gXULTemplateLog, PR_LOG_DEBUG,
414 : ("nsRDFPropertyTestNode[%p]: Retract([%s]==[%s]=>[%s])",
415 : this, source, property, NS_ConvertUTF16toUTF8(target).get()));
416 : }
417 : #endif
418 :
419 0 : mProcessor->RetractElement(Element(aSource, aProperty, aTarget));
420 : }
421 0 : }
422 :
|