1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6  *
7  *
8  * This Original Code has been modified by IBM Corporation.
9  * Modifications made by IBM described herein are
10  * Copyright (c) International Business Machines
11  * Corporation, 2000
12  *
13  * Modifications to Mozilla code or documentation
14  * identified per MPL Section 3.3
15  *
16  * Date         Modified by     Description of modification
17  * 03/27/2000   IBM Corp.       Added PR_CALLBACK for Optlink
18  *                               use in OS2
19  */
20 
21 
22 /*
23 
24   A package of routines shared by the XUL content code.
25 
26  */
27 
28 #include "mozilla/ArrayUtils.h"
29 
30 #include "nsCOMPtr.h"
31 #include "nsIContent.h"
32 #include "nsIDocument.h"
33 #include "nsIDOMElement.h"
34 #include "nsIDOMXULCommandDispatcher.h"
35 #include "nsIDOMXULDocument.h"
36 #include "nsIRDFNode.h"
37 #include "nsIRDFService.h"
38 #include "nsIServiceManager.h"
39 #include "nsIURL.h"
40 #include "nsXULContentUtils.h"
41 #include "nsLayoutCID.h"
42 #include "nsNameSpaceManager.h"
43 #include "nsRDFCID.h"
44 #include "nsString.h"
45 #include "nsXPIDLString.h"
46 #include "nsGkAtoms.h"
47 #include "mozilla/Logging.h"
48 #include "prtime.h"
49 #include "rdf.h"
50 #include "nsContentUtils.h"
51 #include "nsIDateTimeFormat.h"
52 #include "nsIScriptableDateFormat.h"
53 #include "nsICollation.h"
54 #include "nsCollationCID.h"
55 #include "nsILocale.h"
56 #include "nsILocaleService.h"
57 #include "nsIConsoleService.h"
58 #include "nsEscape.h"
59 
60 using namespace mozilla;
61 
62 //------------------------------------------------------------------------
63 
64 nsIRDFService* nsXULContentUtils::gRDF;
65 nsIDateTimeFormat* nsXULContentUtils::gFormat;
66 nsICollation *nsXULContentUtils::gCollation;
67 
68 extern LazyLogModule gXULTemplateLog;
69 
70 #define XUL_RESOURCE(ident, uri) nsIRDFResource* nsXULContentUtils::ident
71 #define XUL_LITERAL(ident, val) nsIRDFLiteral* nsXULContentUtils::ident
72 #include "nsXULResourceList.h"
73 #undef XUL_RESOURCE
74 #undef XUL_LITERAL
75 
76 //------------------------------------------------------------------------
77 // Constructors n' stuff
78 //
79 
80 nsresult
Init()81 nsXULContentUtils::Init()
82 {
83     static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
84     nsresult rv = CallGetService(kRDFServiceCID, &gRDF);
85     if (NS_FAILED(rv)) {
86         return rv;
87     }
88 
89 #define XUL_RESOURCE(ident, uri)                              \
90   PR_BEGIN_MACRO                                              \
91    rv = gRDF->GetResource(NS_LITERAL_CSTRING(uri), &(ident)); \
92    if (NS_FAILED(rv)) return rv;                              \
93   PR_END_MACRO
94 
95 #define XUL_LITERAL(ident, val)                                   \
96   PR_BEGIN_MACRO                                                  \
97    rv = gRDF->GetLiteral(val, &(ident));                          \
98    if (NS_FAILED(rv)) return rv;                                  \
99   PR_END_MACRO
100 
101 #include "nsXULResourceList.h"
102 #undef XUL_RESOURCE
103 #undef XUL_LITERAL
104 
105     gFormat = nsIDateTimeFormat::Create().take();
106     if (!gFormat) {
107         return NS_ERROR_FAILURE;
108     }
109 
110     return NS_OK;
111 }
112 
113 
114 nsresult
Finish()115 nsXULContentUtils::Finish()
116 {
117     NS_IF_RELEASE(gRDF);
118 
119 #define XUL_RESOURCE(ident, uri) NS_IF_RELEASE(ident)
120 #define XUL_LITERAL(ident, val) NS_IF_RELEASE(ident)
121 #include "nsXULResourceList.h"
122 #undef XUL_RESOURCE
123 #undef XUL_LITERAL
124 
125     NS_IF_RELEASE(gFormat);
126     NS_IF_RELEASE(gCollation);
127 
128     return NS_OK;
129 }
130 
131 nsICollation*
GetCollation()132 nsXULContentUtils::GetCollation()
133 {
134     if (!gCollation) {
135         nsresult rv;
136 
137         // get a locale service
138         nsCOMPtr<nsILocaleService> localeService =
139             do_GetService(NS_LOCALESERVICE_CONTRACTID, &rv);
140         if (NS_SUCCEEDED(rv)) {
141             nsCOMPtr<nsILocale> locale;
142             rv = localeService->GetApplicationLocale(getter_AddRefs(locale));
143             if (NS_SUCCEEDED(rv) && locale) {
144                 nsCOMPtr<nsICollationFactory> colFactory =
145                     do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID);
146                 if (colFactory) {
147                     rv = colFactory->CreateCollation(locale, &gCollation);
148                     NS_ASSERTION(NS_SUCCEEDED(rv),
149                                  "couldn't create collation instance");
150                 } else
151                     NS_ERROR("couldn't create instance of collation factory");
152             } else
153                 NS_ERROR("unable to get application locale");
154         } else
155             NS_ERROR("couldn't get locale factory");
156     }
157 
158     return gCollation;
159 }
160 
161 //------------------------------------------------------------------------
162 
163 nsresult
FindChildByTag(nsIContent * aElement,int32_t aNameSpaceID,nsIAtom * aTag,nsIContent ** aResult)164 nsXULContentUtils::FindChildByTag(nsIContent* aElement,
165                                   int32_t aNameSpaceID,
166                                   nsIAtom* aTag,
167                                   nsIContent** aResult)
168 {
169     for (nsIContent* child = aElement->GetFirstChild();
170          child;
171          child = child->GetNextSibling()) {
172 
173         if (child->NodeInfo()->Equals(aTag, aNameSpaceID)) {
174             NS_ADDREF(*aResult = child);
175 
176             return NS_OK;
177         }
178     }
179 
180     *aResult = nullptr;
181     return NS_RDF_NO_VALUE; // not found
182 }
183 
184 
185 /*
186 	Note: this routine is similar, yet distinctly different from, nsBookmarksService::GetTextForNode
187 */
188 
189 nsresult
GetTextForNode(nsIRDFNode * aNode,nsAString & aResult)190 nsXULContentUtils::GetTextForNode(nsIRDFNode* aNode, nsAString& aResult)
191 {
192     if (! aNode) {
193         aResult.Truncate();
194         return NS_OK;
195     }
196 
197     nsresult rv;
198 
199     // Literals are the most common, so try these first.
200     nsCOMPtr<nsIRDFLiteral> literal = do_QueryInterface(aNode);
201     if (literal) {
202         const char16_t* p;
203         rv = literal->GetValueConst(&p);
204         if (NS_FAILED(rv)) return rv;
205 
206         aResult = p;
207         return NS_OK;
208     }
209 
210     nsCOMPtr<nsIRDFDate> dateLiteral = do_QueryInterface(aNode);
211     if (dateLiteral) {
212         PRTime value;
213         rv = dateLiteral->GetValue(&value);
214         if (NS_FAILED(rv)) return rv;
215 
216         nsAutoString str;
217         rv = gFormat->FormatPRTime(nullptr /* nsILocale* locale */,
218                                   kDateFormatShort,
219                                   kTimeFormatSeconds,
220                                   value,
221                                   str);
222         aResult.Assign(str);
223 
224         if (NS_FAILED(rv)) return rv;
225 
226         return NS_OK;
227     }
228 
229     nsCOMPtr<nsIRDFInt> intLiteral = do_QueryInterface(aNode);
230     if (intLiteral) {
231         int32_t	value;
232         rv = intLiteral->GetValue(&value);
233         if (NS_FAILED(rv)) return rv;
234 
235         aResult.Truncate();
236         nsAutoString intStr;
237         intStr.AppendInt(value, 10);
238         aResult.Append(intStr);
239         return NS_OK;
240     }
241 
242 
243     nsCOMPtr<nsIRDFResource> resource = do_QueryInterface(aNode);
244     if (resource) {
245         const char* p;
246         rv = resource->GetValueConst(&p);
247         if (NS_FAILED(rv)) return rv;
248         CopyUTF8toUTF16(p, aResult);
249         return NS_OK;
250     }
251 
252     NS_ERROR("not a resource or a literal");
253     return NS_ERROR_UNEXPECTED;
254 }
255 
256 nsresult
GetResource(int32_t aNameSpaceID,nsIAtom * aAttribute,nsIRDFResource ** aResult)257 nsXULContentUtils::GetResource(int32_t aNameSpaceID, nsIAtom* aAttribute, nsIRDFResource** aResult)
258 {
259     // construct a fully-qualified URI from the namespace/tag pair.
260     NS_PRECONDITION(aAttribute != nullptr, "null ptr");
261     if (! aAttribute)
262         return NS_ERROR_NULL_POINTER;
263 
264     return GetResource(aNameSpaceID, nsDependentAtomString(aAttribute),
265                        aResult);
266 }
267 
268 
269 nsresult
GetResource(int32_t aNameSpaceID,const nsAString & aAttribute,nsIRDFResource ** aResult)270 nsXULContentUtils::GetResource(int32_t aNameSpaceID, const nsAString& aAttribute, nsIRDFResource** aResult)
271 {
272     // construct a fully-qualified URI from the namespace/tag pair.
273 
274     // XXX should we allow nodes with no namespace???
275     //NS_PRECONDITION(aNameSpaceID != kNameSpaceID_Unknown, "no namespace");
276     //if (aNameSpaceID == kNameSpaceID_Unknown)
277     //    return NS_ERROR_UNEXPECTED;
278 
279     nsresult rv;
280 
281     char16_t buf[256];
282     nsFixedString uri(buf, ArrayLength(buf), 0);
283     if (aNameSpaceID != kNameSpaceID_Unknown && aNameSpaceID != kNameSpaceID_None) {
284         rv = nsContentUtils::NameSpaceManager()->GetNameSpaceURI(aNameSpaceID, uri);
285         // XXX ignore failure; treat as "no namespace"
286     }
287 
288     // XXX check to see if we need to insert a '/' or a '#'. Oy.
289     if (!uri.IsEmpty()  && uri.Last() != '#' && uri.Last() != '/' && aAttribute.First() != '#')
290         uri.Append(char16_t('#'));
291 
292     uri.Append(aAttribute);
293 
294     rv = gRDF->GetUnicodeResource(uri, aResult);
295     NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get resource");
296     if (NS_FAILED(rv)) return rv;
297 
298     return NS_OK;
299 }
300 
301 
302 nsresult
SetCommandUpdater(nsIDocument * aDocument,nsIContent * aElement)303 nsXULContentUtils::SetCommandUpdater(nsIDocument* aDocument, nsIContent* aElement)
304 {
305     // Deal with setting up a 'commandupdater'. Pulls the 'events' and
306     // 'targets' attributes off of aElement, and adds it to the
307     // document's command dispatcher.
308     NS_PRECONDITION(aDocument != nullptr, "null ptr");
309     if (! aDocument)
310         return NS_ERROR_NULL_POINTER;
311 
312     NS_PRECONDITION(aElement != nullptr, "null ptr");
313     if (! aElement)
314         return NS_ERROR_NULL_POINTER;
315 
316     nsresult rv;
317 
318     nsCOMPtr<nsIDOMXULDocument> xuldoc = do_QueryInterface(aDocument);
319     NS_ASSERTION(xuldoc != nullptr, "not a xul document");
320     if (! xuldoc)
321         return NS_ERROR_UNEXPECTED;
322 
323     nsCOMPtr<nsIDOMXULCommandDispatcher> dispatcher;
324     rv = xuldoc->GetCommandDispatcher(getter_AddRefs(dispatcher));
325     NS_ASSERTION(NS_SUCCEEDED(rv), "unable to get dispatcher");
326     if (NS_FAILED(rv)) return rv;
327 
328     NS_ASSERTION(dispatcher != nullptr, "no dispatcher");
329     if (! dispatcher)
330         return NS_ERROR_UNEXPECTED;
331 
332     nsAutoString events;
333     aElement->GetAttr(kNameSpaceID_None, nsGkAtoms::events, events);
334     if (events.IsEmpty())
335         events.Assign('*');
336 
337     nsAutoString targets;
338     aElement->GetAttr(kNameSpaceID_None, nsGkAtoms::targets, targets);
339 
340     if (targets.IsEmpty())
341         targets.Assign('*');
342 
343     nsCOMPtr<nsIDOMElement> domelement = do_QueryInterface(aElement);
344     NS_ASSERTION(domelement != nullptr, "not a DOM element");
345     if (! domelement)
346         return NS_ERROR_UNEXPECTED;
347 
348     rv = dispatcher->AddCommandUpdater(domelement, events, targets);
349     if (NS_FAILED(rv)) return rv;
350 
351     return NS_OK;
352 }
353 
354 void
LogTemplateError(const char * aStr)355 nsXULContentUtils::LogTemplateError(const char* aStr)
356 {
357   nsAutoString message;
358   message.AssignLiteral("Error parsing template: ");
359   message.Append(NS_ConvertUTF8toUTF16(aStr).get());
360 
361   nsCOMPtr<nsIConsoleService> cs = do_GetService(NS_CONSOLESERVICE_CONTRACTID);
362   if (cs) {
363     cs->LogStringMessage(message.get());
364     MOZ_LOG(gXULTemplateLog, LogLevel::Info, ("Error parsing template: %s", aStr));
365   }
366 }
367