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