Line data Source code
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 :
6 : #include "nsXULTemplateQueryProcessorXML.h"
7 : #include "nsXULTemplateResultXML.h"
8 : #include "nsXMLBinding.h"
9 : #include "mozilla/ErrorResult.h"
10 : #include "mozilla/dom/XPathResult.h"
11 :
12 : using namespace mozilla;
13 : using namespace mozilla::dom;
14 :
15 0 : nsXMLBindingSet::~nsXMLBindingSet()
16 0 : {}
17 :
18 : void
19 0 : nsXMLBindingSet::AddBinding(nsIAtom* aVar, nsAutoPtr<XPathExpression>&& aExpr)
20 : {
21 0 : nsAutoPtr<nsXMLBinding> newbinding(new nsXMLBinding(aVar, Move(aExpr)));
22 :
23 0 : if (mFirst) {
24 0 : nsXMLBinding* binding = mFirst;
25 :
26 0 : while (binding) {
27 : // if the target variable is already used in a binding, ignore it
28 : // since it won't be useful for anything
29 0 : if (binding->mVar == aVar)
30 0 : return;
31 :
32 : // add the binding at the end of the list
33 0 : if (!binding->mNext) {
34 0 : binding->mNext = newbinding;
35 0 : return;
36 : }
37 :
38 0 : binding = binding->mNext;
39 : }
40 : }
41 : else {
42 0 : mFirst = newbinding;
43 : }
44 : }
45 :
46 : int32_t
47 0 : nsXMLBindingSet::LookupTargetIndex(nsIAtom* aTargetVariable,
48 : nsXMLBinding** aBinding)
49 : {
50 0 : int32_t idx = 0;
51 0 : nsXMLBinding* binding = mFirst;
52 :
53 0 : while (binding) {
54 0 : if (binding->mVar == aTargetVariable) {
55 0 : *aBinding = binding;
56 0 : return idx;
57 : }
58 0 : idx++;
59 0 : binding = binding->mNext;
60 : }
61 :
62 0 : *aBinding = nullptr;
63 0 : return -1;
64 : }
65 :
66 : XPathResult*
67 0 : nsXMLBindingValues::GetAssignmentFor(nsXULTemplateResultXML* aResult,
68 : nsXMLBinding* aBinding,
69 : int32_t aIndex,
70 : uint16_t aType)
71 : {
72 0 : XPathResult* value = mValues.SafeElementAt(aIndex);
73 0 : if (value) {
74 0 : return value;
75 : }
76 :
77 0 : nsINode* contextNode = aResult->Node();
78 0 : if (!contextNode) {
79 0 : return nullptr;
80 : }
81 :
82 0 : mValues.EnsureLengthAtLeast(aIndex + 1);
83 :
84 0 : ErrorResult ignored;
85 0 : mValues[aIndex] = aBinding->mExpr->Evaluate(*contextNode, aType, nullptr,
86 0 : ignored);
87 :
88 0 : return mValues[aIndex];
89 : }
90 :
91 : nsINode*
92 0 : nsXMLBindingValues::GetNodeAssignmentFor(nsXULTemplateResultXML* aResult,
93 : nsXMLBinding* aBinding,
94 : int32_t aIndex)
95 : {
96 : XPathResult* result = GetAssignmentFor(aResult, aBinding, aIndex,
97 0 : XPathResult::FIRST_ORDERED_NODE_TYPE);
98 :
99 0 : ErrorResult rv;
100 0 : return result ? result->GetSingleNodeValue(rv) : nullptr;
101 : }
102 :
103 : void
104 0 : nsXMLBindingValues::GetStringAssignmentFor(nsXULTemplateResultXML* aResult,
105 : nsXMLBinding* aBinding,
106 : int32_t aIndex,
107 : nsAString& aValue)
108 : {
109 : XPathResult* result = GetAssignmentFor(aResult, aBinding, aIndex,
110 0 : XPathResult::STRING_TYPE);
111 :
112 0 : if (result) {
113 0 : ErrorResult rv;
114 0 : result->GetStringValue(aValue, rv);
115 : } else {
116 0 : aValue.Truncate();
117 : }
118 0 : }
|