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.org code.
16 : *
17 : * The Initial Developer of the Original Code is Neil Deakin
18 : * Portions created by the Initial Developer are Copyright (C) 2006
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either of the GNU General Public License Version 2 or later (the "GPL"),
25 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 : * in which case the provisions of the GPL or the LGPL are applicable instead
27 : * of those above. If you wish to allow use of your version of this file only
28 : * under the terms of either the GPL or the LGPL, and not to allow others to
29 : * use your version of this file under the terms of the MPL, indicate your
30 : * decision by deleting the provisions above and replace them with the notice
31 : * and other provisions required by the GPL or the LGPL. If you do not delete
32 : * the provisions above, a recipient may use your version of this file under
33 : * the terms of any one of the MPL, the GPL or the LGPL.
34 : *
35 : * ***** END LICENSE BLOCK ***** */
36 :
37 : #include "nsIServiceManager.h"
38 : #include "nsIDOMNode.h"
39 : #include "nsIDOMElement.h"
40 : #include "nsIContent.h"
41 :
42 : #include "nsIRDFService.h"
43 :
44 : #include "nsXULTemplateResultXML.h"
45 : #include "nsXMLBinding.h"
46 :
47 : static PRUint32 sTemplateId = 0;
48 :
49 0 : NS_IMPL_ISUPPORTS1(nsXULTemplateResultXML, nsIXULTemplateResult)
50 :
51 0 : nsXULTemplateResultXML::nsXULTemplateResultXML(nsXMLQuery* aQuery,
52 : nsIDOMNode* aNode,
53 : nsXMLBindingSet* aBindings)
54 0 : : mQuery(aQuery), mNode(aNode)
55 : {
56 0 : nsCOMPtr<nsIContent> content = do_QueryInterface(mNode);
57 :
58 : // If the node has an id, create the uri from it. Otherwise, there isn't
59 : // anything to identify the node with so just use a somewhat random number.
60 0 : nsCOMPtr<nsIAtom> id = content->GetID();
61 0 : if (id) {
62 0 : nsCOMPtr<nsIURI> uri = content->GetBaseURI();
63 0 : nsCAutoString spec;
64 0 : uri->GetSpec(spec);
65 :
66 0 : mId = NS_ConvertUTF8toUTF16(spec);
67 :
68 0 : nsAutoString idstr;
69 0 : id->ToString(idstr);
70 0 : mId += NS_LITERAL_STRING("#") + idstr;
71 : }
72 : else {
73 0 : nsAutoString rowid(NS_LITERAL_STRING("row"));
74 0 : rowid.AppendInt(++sTemplateId);
75 0 : mId.Assign(rowid);
76 : }
77 :
78 0 : if (aBindings)
79 0 : mRequiredValues.SetBindingSet(aBindings);
80 0 : }
81 :
82 : NS_IMETHODIMP
83 0 : nsXULTemplateResultXML::GetIsContainer(bool* aIsContainer)
84 : {
85 : // a node is considered a container if it has children
86 0 : if (mNode)
87 0 : mNode->HasChildNodes(aIsContainer);
88 : else
89 0 : *aIsContainer = false;
90 0 : return NS_OK;
91 : }
92 :
93 : NS_IMETHODIMP
94 0 : nsXULTemplateResultXML::GetIsEmpty(bool* aIsEmpty)
95 : {
96 : // a node is considered empty if it has no elements as children
97 0 : nsCOMPtr<nsIContent> content = do_QueryInterface(mNode);
98 0 : if (content) {
99 0 : for (nsIContent* child = content->GetFirstChild();
100 : child;
101 0 : child = child->GetNextSibling()) {
102 0 : if (child->IsElement()) {
103 0 : *aIsEmpty = false;
104 0 : return NS_OK;
105 : }
106 : }
107 : }
108 :
109 0 : *aIsEmpty = true;
110 0 : return NS_OK;
111 : }
112 :
113 : NS_IMETHODIMP
114 0 : nsXULTemplateResultXML::GetMayProcessChildren(bool* aMayProcessChildren)
115 : {
116 0 : *aMayProcessChildren = true;
117 0 : return NS_OK;
118 : }
119 :
120 : NS_IMETHODIMP
121 0 : nsXULTemplateResultXML::GetId(nsAString& aId)
122 : {
123 0 : aId = mId;
124 0 : return NS_OK;
125 : }
126 :
127 : NS_IMETHODIMP
128 0 : nsXULTemplateResultXML::GetResource(nsIRDFResource** aResource)
129 : {
130 0 : *aResource = nsnull;
131 0 : return NS_OK;
132 : }
133 :
134 : NS_IMETHODIMP
135 0 : nsXULTemplateResultXML::GetType(nsAString& aType)
136 : {
137 0 : aType.Truncate();
138 0 : return NS_OK;
139 : }
140 :
141 : NS_IMETHODIMP
142 0 : nsXULTemplateResultXML::GetBindingFor(nsIAtom* aVar, nsAString& aValue)
143 : {
144 0 : NS_ENSURE_ARG_POINTER(aVar);
145 :
146 : // get the position of the atom in the variables table
147 : nsXMLBinding* binding;
148 :
149 0 : PRInt32 idx = mRequiredValues.LookupTargetIndex(aVar, &binding);
150 0 : if (idx >= 0) {
151 0 : mRequiredValues.GetStringAssignmentFor(this, binding, idx, aValue);
152 0 : return NS_OK;
153 : }
154 :
155 0 : idx = mOptionalValues.LookupTargetIndex(aVar, &binding);
156 0 : if (idx >= 0) {
157 0 : mOptionalValues.GetStringAssignmentFor(this, binding, idx, aValue);
158 0 : return NS_OK;
159 : }
160 :
161 : // if the variable is not bound, just use the variable name as the name of
162 : // an attribute to retrieve
163 0 : nsAutoString attr;
164 0 : aVar->ToString(attr);
165 :
166 0 : if (attr.Length() > 1) {
167 0 : nsCOMPtr<nsIDOMElement> element = do_QueryInterface(mNode);
168 0 : if (element)
169 0 : return element->GetAttribute(Substring(attr, 1), aValue);
170 : }
171 :
172 0 : aValue.Truncate();
173 0 : return NS_OK;
174 : }
175 :
176 : NS_IMETHODIMP
177 0 : nsXULTemplateResultXML::GetBindingObjectFor(nsIAtom* aVar, nsISupports** aValue)
178 : {
179 0 : NS_ENSURE_ARG_POINTER(aVar);
180 :
181 : nsXMLBinding* binding;
182 0 : nsCOMPtr<nsIDOMNode> node;
183 :
184 0 : if (mQuery && aVar == mQuery->GetMemberVariable()) {
185 0 : node = mNode;
186 : }
187 : else {
188 0 : PRInt32 idx = mRequiredValues.LookupTargetIndex(aVar, &binding);
189 0 : if (idx > 0) {
190 : mRequiredValues.GetNodeAssignmentFor(this, binding, idx,
191 0 : getter_AddRefs(node));
192 : }
193 : else {
194 0 : idx = mOptionalValues.LookupTargetIndex(aVar, &binding);
195 0 : if (idx > 0) {
196 : mOptionalValues.GetNodeAssignmentFor(this, binding, idx,
197 0 : getter_AddRefs(node));
198 : }
199 : }
200 : }
201 :
202 0 : *aValue = node;
203 0 : NS_IF_ADDREF(*aValue);
204 0 : return NS_OK;
205 : }
206 :
207 : NS_IMETHODIMP
208 0 : nsXULTemplateResultXML::RuleMatched(nsISupports* aQueryNode,
209 : nsIDOMNode* aRuleNode)
210 : {
211 : // when a rule matches, set the bindings that must be used.
212 0 : nsXULTemplateQueryProcessorXML* processor = mQuery ? mQuery->Processor() :
213 0 : nsnull;
214 0 : if (processor) {
215 : nsXMLBindingSet* bindings =
216 0 : processor->GetOptionalBindingsForRule(aRuleNode);
217 0 : if (bindings)
218 0 : mOptionalValues.SetBindingSet(bindings);
219 : }
220 :
221 0 : return NS_OK;
222 : }
223 :
224 : NS_IMETHODIMP
225 0 : nsXULTemplateResultXML::HasBeenRemoved()
226 : {
227 0 : return NS_OK;
228 : }
229 :
230 : void
231 0 : nsXULTemplateResultXML::GetNode(nsIDOMNode** aNode)
232 : {
233 0 : *aNode = mNode;
234 0 : NS_IF_ADDREF(*aNode);
235 0 : }
|