|
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 |
|
6 #include "nsIServiceManager.h" |
|
7 #include "nsIDOMNode.h" |
|
8 #include "nsIDOMElement.h" |
|
9 #include "nsIContent.h" |
|
10 |
|
11 #include "nsIRDFService.h" |
|
12 |
|
13 #include "nsXULTemplateResultXML.h" |
|
14 #include "nsXMLBinding.h" |
|
15 |
|
16 static uint32_t sTemplateId = 0; |
|
17 |
|
18 NS_IMPL_ISUPPORTS(nsXULTemplateResultXML, nsIXULTemplateResult) |
|
19 |
|
20 nsXULTemplateResultXML::nsXULTemplateResultXML(nsXMLQuery* aQuery, |
|
21 nsIDOMNode* aNode, |
|
22 nsXMLBindingSet* aBindings) |
|
23 : mQuery(aQuery), mNode(aNode) |
|
24 { |
|
25 nsCOMPtr<nsIContent> content = do_QueryInterface(mNode); |
|
26 |
|
27 // If the node has an id, create the uri from it. Otherwise, there isn't |
|
28 // anything to identify the node with so just use a somewhat random number. |
|
29 nsCOMPtr<nsIAtom> id = content->GetID(); |
|
30 if (id) { |
|
31 nsCOMPtr<nsIURI> uri = content->GetBaseURI(); |
|
32 nsAutoCString spec; |
|
33 uri->GetSpec(spec); |
|
34 |
|
35 mId = NS_ConvertUTF8toUTF16(spec); |
|
36 |
|
37 nsAutoString idstr; |
|
38 id->ToString(idstr); |
|
39 mId += NS_LITERAL_STRING("#") + idstr; |
|
40 } |
|
41 else { |
|
42 nsAutoString rowid(NS_LITERAL_STRING("row")); |
|
43 rowid.AppendInt(++sTemplateId); |
|
44 mId.Assign(rowid); |
|
45 } |
|
46 |
|
47 if (aBindings) |
|
48 mRequiredValues.SetBindingSet(aBindings); |
|
49 } |
|
50 |
|
51 NS_IMETHODIMP |
|
52 nsXULTemplateResultXML::GetIsContainer(bool* aIsContainer) |
|
53 { |
|
54 // a node is considered a container if it has children |
|
55 if (mNode) |
|
56 mNode->HasChildNodes(aIsContainer); |
|
57 else |
|
58 *aIsContainer = false; |
|
59 return NS_OK; |
|
60 } |
|
61 |
|
62 NS_IMETHODIMP |
|
63 nsXULTemplateResultXML::GetIsEmpty(bool* aIsEmpty) |
|
64 { |
|
65 // a node is considered empty if it has no elements as children |
|
66 nsCOMPtr<nsIContent> content = do_QueryInterface(mNode); |
|
67 if (content) { |
|
68 for (nsIContent* child = content->GetFirstChild(); |
|
69 child; |
|
70 child = child->GetNextSibling()) { |
|
71 if (child->IsElement()) { |
|
72 *aIsEmpty = false; |
|
73 return NS_OK; |
|
74 } |
|
75 } |
|
76 } |
|
77 |
|
78 *aIsEmpty = true; |
|
79 return NS_OK; |
|
80 } |
|
81 |
|
82 NS_IMETHODIMP |
|
83 nsXULTemplateResultXML::GetMayProcessChildren(bool* aMayProcessChildren) |
|
84 { |
|
85 *aMayProcessChildren = true; |
|
86 return NS_OK; |
|
87 } |
|
88 |
|
89 NS_IMETHODIMP |
|
90 nsXULTemplateResultXML::GetId(nsAString& aId) |
|
91 { |
|
92 aId = mId; |
|
93 return NS_OK; |
|
94 } |
|
95 |
|
96 NS_IMETHODIMP |
|
97 nsXULTemplateResultXML::GetResource(nsIRDFResource** aResource) |
|
98 { |
|
99 *aResource = nullptr; |
|
100 return NS_OK; |
|
101 } |
|
102 |
|
103 NS_IMETHODIMP |
|
104 nsXULTemplateResultXML::GetType(nsAString& aType) |
|
105 { |
|
106 aType.Truncate(); |
|
107 return NS_OK; |
|
108 } |
|
109 |
|
110 NS_IMETHODIMP |
|
111 nsXULTemplateResultXML::GetBindingFor(nsIAtom* aVar, nsAString& aValue) |
|
112 { |
|
113 NS_ENSURE_ARG_POINTER(aVar); |
|
114 |
|
115 // get the position of the atom in the variables table |
|
116 nsXMLBinding* binding; |
|
117 |
|
118 int32_t idx = mRequiredValues.LookupTargetIndex(aVar, &binding); |
|
119 if (idx >= 0) { |
|
120 mRequiredValues.GetStringAssignmentFor(this, binding, idx, aValue); |
|
121 return NS_OK; |
|
122 } |
|
123 |
|
124 idx = mOptionalValues.LookupTargetIndex(aVar, &binding); |
|
125 if (idx >= 0) { |
|
126 mOptionalValues.GetStringAssignmentFor(this, binding, idx, aValue); |
|
127 return NS_OK; |
|
128 } |
|
129 |
|
130 // if the variable is not bound, just use the variable name as the name of |
|
131 // an attribute to retrieve |
|
132 nsAutoString attr; |
|
133 aVar->ToString(attr); |
|
134 |
|
135 if (attr.Length() > 1) { |
|
136 nsCOMPtr<nsIDOMElement> element = do_QueryInterface(mNode); |
|
137 if (element) |
|
138 return element->GetAttribute(Substring(attr, 1), aValue); |
|
139 } |
|
140 |
|
141 aValue.Truncate(); |
|
142 return NS_OK; |
|
143 } |
|
144 |
|
145 NS_IMETHODIMP |
|
146 nsXULTemplateResultXML::GetBindingObjectFor(nsIAtom* aVar, nsISupports** aValue) |
|
147 { |
|
148 NS_ENSURE_ARG_POINTER(aVar); |
|
149 |
|
150 nsXMLBinding* binding; |
|
151 nsCOMPtr<nsIDOMNode> node; |
|
152 |
|
153 if (mQuery && aVar == mQuery->GetMemberVariable()) { |
|
154 node = mNode; |
|
155 } |
|
156 else { |
|
157 int32_t idx = mRequiredValues.LookupTargetIndex(aVar, &binding); |
|
158 if (idx > 0) { |
|
159 mRequiredValues.GetNodeAssignmentFor(this, binding, idx, |
|
160 getter_AddRefs(node)); |
|
161 } |
|
162 else { |
|
163 idx = mOptionalValues.LookupTargetIndex(aVar, &binding); |
|
164 if (idx > 0) { |
|
165 mOptionalValues.GetNodeAssignmentFor(this, binding, idx, |
|
166 getter_AddRefs(node)); |
|
167 } |
|
168 } |
|
169 } |
|
170 |
|
171 *aValue = node; |
|
172 NS_IF_ADDREF(*aValue); |
|
173 return NS_OK; |
|
174 } |
|
175 |
|
176 NS_IMETHODIMP |
|
177 nsXULTemplateResultXML::RuleMatched(nsISupports* aQueryNode, |
|
178 nsIDOMNode* aRuleNode) |
|
179 { |
|
180 // when a rule matches, set the bindings that must be used. |
|
181 nsXULTemplateQueryProcessorXML* processor = mQuery ? mQuery->Processor() : |
|
182 nullptr; |
|
183 if (processor) { |
|
184 nsXMLBindingSet* bindings = |
|
185 processor->GetOptionalBindingsForRule(aRuleNode); |
|
186 if (bindings) |
|
187 mOptionalValues.SetBindingSet(bindings); |
|
188 } |
|
189 |
|
190 return NS_OK; |
|
191 } |
|
192 |
|
193 NS_IMETHODIMP |
|
194 nsXULTemplateResultXML::HasBeenRemoved() |
|
195 { |
|
196 return NS_OK; |
|
197 } |
|
198 |
|
199 void |
|
200 nsXULTemplateResultXML::GetNode(nsIDOMNode** aNode) |
|
201 { |
|
202 *aNode = mNode; |
|
203 NS_IF_ADDREF(*aNode); |
|
204 } |