Back to index

lightning-sunbird  0.9+nobinonly
XSLTProcessorModule.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is TransforMiiX XSLT processor code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Peter Van der Beken.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Peter Van der Beken <peterv@propagandism.org>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsICategoryManager.h"
00040 #include "nsIDOMClassInfo.h"
00041 #include "nsIDOMNSXPathExpression.h"
00042 #include "nsIDOMXPathExpression.h"
00043 #include "nsIDOMXPathNSResolver.h"
00044 #include "nsIDOMXPathResult.h"
00045 #include "nsIErrorService.h"
00046 #include "nsIExceptionService.h"
00047 #include "nsIGenericFactory.h"
00048 #include "nsIParserService.h"
00049 #include "nsIScriptNameSpaceManager.h"
00050 #include "nsIServiceManager.h"
00051 #include "nsXPathEvaluator.h"
00052 #include "nsXPathException.h"
00053 #include "nsXPIDLString.h"
00054 #include "txAtoms.h"
00055 #include "txMozillaXSLTProcessor.h"
00056 #include "TxLog.h"
00057 #include "nsCRT.h"
00058 #include "nsIScriptSecurityManager.h"
00059 #include "txURIUtils.h"
00060 #include "txXSLTProcessor.h"
00061 #include "nsXPath1Scheme.h"
00062 
00063 #ifndef DISABLE_XFORMS_HOOKS
00064 #include "nsXFormsXPathEvaluator.h"
00065 #endif
00066 
00067 NS_GENERIC_FACTORY_CONSTRUCTOR(nsXPath1SchemeProcessor)
00068 
00069 /* 1c1a3c01-14f6-11d6-a7f2-ea502af815dc */
00070 #define TRANSFORMIIX_DOMCI_EXTENSION_CID   \
00071 { 0x1c1a3c01, 0x14f6, 0x11d6, {0xa7, 0xf2, 0xea, 0x50, 0x2a, 0xf8, 0x15, 0xdc} }
00072 
00073 /* {0C351177-0159-4500-86B0-A219DFDE4258} */
00074 #define TRANSFORMIIX_XPATH1_SCHEME_CID \
00075 { 0xc351177, 0x159, 0x4500, { 0x86, 0xb0, 0xa2, 0x19, 0xdf, 0xde, 0x42, 0x58 } }
00076 
00077 #define TRANSFORMIIX_DOMCI_EXTENSION_CONTRACTID \
00078 "@mozilla.org/transformiix-domci-extender;1"
00079 
00080 NS_DOMCI_EXTENSION(Transformiix)
00081     static NS_DEFINE_CID(kXSLTProcessorCID, TRANSFORMIIX_XSLT_PROCESSOR_CID);
00082     NS_DOMCI_EXTENSION_ENTRY_BEGIN(XSLTProcessor)
00083         NS_DOMCI_EXTENSION_ENTRY_INTERFACE(nsIXSLTProcessor)
00084         NS_DOMCI_EXTENSION_ENTRY_INTERFACE(nsIXSLTProcessorObsolete) // XXX DEPRECATED
00085         NS_DOMCI_EXTENSION_ENTRY_INTERFACE(nsIXSLTProcessorPrivate)
00086     NS_DOMCI_EXTENSION_ENTRY_END(XSLTProcessor, nsIXSLTProcessor, PR_TRUE,
00087                                  &kXSLTProcessorCID)
00088 
00089     static NS_DEFINE_CID(kXPathEvaluatorCID, TRANSFORMIIX_XPATH_EVALUATOR_CID);
00090     NS_DOMCI_EXTENSION_ENTRY_BEGIN(XPathEvaluator)
00091         NS_DOMCI_EXTENSION_ENTRY_INTERFACE(nsIDOMXPathEvaluator)
00092     NS_DOMCI_EXTENSION_ENTRY_END(XPathEvaluator, nsIDOMXPathEvaluator, PR_TRUE,
00093                                  &kXPathEvaluatorCID)
00094 
00095     NS_DOMCI_EXTENSION_ENTRY_BEGIN(XPathException)
00096         NS_DOMCI_EXTENSION_ENTRY_INTERFACE(nsIDOMXPathException)
00097         NS_DOMCI_EXTENSION_ENTRY_INTERFACE(nsIException)
00098     NS_DOMCI_EXTENSION_ENTRY_END(XPathException, nsIDOMXPathException, PR_TRUE,
00099                                  nsnull)
00100 
00101     NS_DOMCI_EXTENSION_ENTRY_BEGIN(XPathExpression)
00102         NS_DOMCI_EXTENSION_ENTRY_INTERFACE(nsIDOMXPathExpression)
00103         NS_DOMCI_EXTENSION_ENTRY_INTERFACE(nsIDOMNSXPathExpression)
00104     NS_DOMCI_EXTENSION_ENTRY_END(XPathExpression, nsIDOMXPathExpression,
00105                                  PR_TRUE, nsnull)
00106 
00107     NS_DOMCI_EXTENSION_ENTRY_BEGIN(XPathNSResolver)
00108         NS_DOMCI_EXTENSION_ENTRY_INTERFACE(nsIDOMXPathNSResolver)
00109     NS_DOMCI_EXTENSION_ENTRY_END(XPathNSResolver, nsIDOMXPathNSResolver,
00110                                  PR_TRUE, nsnull)
00111 
00112     NS_DOMCI_EXTENSION_ENTRY_BEGIN(XPathResult)
00113         NS_DOMCI_EXTENSION_ENTRY_INTERFACE(nsIDOMXPathResult)
00114     NS_DOMCI_EXTENSION_ENTRY_END(XPathResult, nsIDOMXPathResult, PR_TRUE,
00115                                  nsnull)
00116 NS_DOMCI_EXTENSION_END
00117 
00118 // Factory Constructor
00119 NS_GENERIC_FACTORY_CONSTRUCTOR(txMozillaXSLTProcessor)
00120 NS_GENERIC_FACTORY_CONSTRUCTOR(nsXPathEvaluator)
00121 #ifndef DISABLE_XFORMS_HOOKS
00122 NS_GENERIC_FACTORY_CONSTRUCTOR(nsXFormsXPathEvaluator)
00123 #endif
00124 
00125 NS_DECL_DOM_CLASSINFO(XSLTProcessor)
00126 NS_DECL_DOM_CLASSINFO(XPathEvaluator)
00127 NS_DECL_DOM_CLASSINFO(XPathException)
00128 NS_DECL_DOM_CLASSINFO(XPathExpression)
00129 NS_DECL_DOM_CLASSINFO(XPathNSResolver)
00130 NS_DECL_DOM_CLASSINFO(XPathResult)
00131 
00132 static NS_METHOD 
00133 RegisterTransformiix(nsIComponentManager *aCompMgr,
00134                      nsIFile *aPath,
00135                      const char *registryLocation,
00136                      const char *componentType,
00137                      const nsModuleComponentInfo *info)
00138 {
00139     nsresult rv = NS_OK;
00140 
00141     nsCOMPtr<nsICategoryManager> catman =
00142         do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
00143 
00144     if (NS_FAILED(rv))
00145         return rv;
00146 
00147     nsXPIDLCString previous;
00148     rv = catman->AddCategoryEntry(JAVASCRIPT_DOM_CLASS,
00149                                   "XSLTProcessor",
00150                                   TRANSFORMIIX_DOMCI_EXTENSION_CONTRACTID,
00151                                   PR_TRUE, PR_TRUE, getter_Copies(previous));
00152     NS_ENSURE_SUCCESS(rv, rv);
00153 
00154     rv = catman->AddCategoryEntry(JAVASCRIPT_DOM_CLASS,
00155                                   "XPathEvaluator",
00156                                   TRANSFORMIIX_DOMCI_EXTENSION_CONTRACTID,
00157                                   PR_TRUE, PR_TRUE, getter_Copies(previous));
00158     NS_ENSURE_SUCCESS(rv, rv);
00159 
00160     rv = catman->AddCategoryEntry(JAVASCRIPT_DOM_CLASS,
00161                                   "XPathException",
00162                                   TRANSFORMIIX_DOMCI_EXTENSION_CONTRACTID,
00163                                   PR_TRUE, PR_TRUE, getter_Copies(previous));
00164     NS_ENSURE_SUCCESS(rv, rv);
00165 
00166     rv = catman->AddCategoryEntry(JAVASCRIPT_DOM_CLASS,
00167                                   "XPathExpression",
00168                                   TRANSFORMIIX_DOMCI_EXTENSION_CONTRACTID,
00169                                   PR_TRUE, PR_TRUE, getter_Copies(previous));
00170     NS_ENSURE_SUCCESS(rv, rv);
00171 
00172     rv = catman->AddCategoryEntry(JAVASCRIPT_DOM_CLASS,
00173                                   "XPathNSResolver",
00174                                   TRANSFORMIIX_DOMCI_EXTENSION_CONTRACTID,
00175                                   PR_TRUE, PR_TRUE, getter_Copies(previous));
00176     NS_ENSURE_SUCCESS(rv, rv);
00177 
00178     rv = catman->AddCategoryEntry(JAVASCRIPT_DOM_CLASS,
00179                                   "XPathResult",
00180                                   TRANSFORMIIX_DOMCI_EXTENSION_CONTRACTID,
00181                                   PR_TRUE, PR_TRUE, getter_Copies(previous));
00182     NS_ENSURE_SUCCESS(rv, rv);
00183 
00184     char* iidString = NS_GET_IID(nsIXSLTProcessorObsolete).ToString();
00185     if (!iidString)
00186       return NS_ERROR_OUT_OF_MEMORY;
00187     rv = catman->AddCategoryEntry(JAVASCRIPT_DOM_INTERFACE,
00188                                   "nsIXSLTProcessorObsolete",
00189                                   iidString,
00190                                   PR_TRUE, PR_TRUE, getter_Copies(previous));
00191     nsCRT::free(iidString);
00192     NS_ENSURE_SUCCESS(rv, rv);
00193 
00194     iidString = NS_GET_IID(nsIXSLTProcessor).ToString();
00195     if (!iidString)
00196       return NS_ERROR_OUT_OF_MEMORY;
00197     rv = catman->AddCategoryEntry(JAVASCRIPT_DOM_INTERFACE,
00198                                   "nsIXSLTProcessor",
00199                                   iidString,
00200                                   PR_TRUE, PR_TRUE, getter_Copies(previous));
00201     nsCRT::free(iidString);
00202 
00203     return rv;
00204 }
00205 
00206 static PRBool gInitialized = PR_FALSE;
00207 static nsIExceptionProvider *gXPathExceptionProvider = 0;
00208 nsINameSpaceManager *gTxNameSpaceManager = 0;
00209 nsIParserService *gTxParserService = 0;
00210 
00211 // Perform our one-time intialization for this module
00212 PR_STATIC_CALLBACK(nsresult)
00213 Initialize(nsIModule* aSelf)
00214 {
00215     NS_PRECONDITION(!gInitialized, "module already initialized");
00216     if (gInitialized)
00217         return NS_OK;
00218 
00219     gInitialized = PR_TRUE;
00220 
00221     gXPathExceptionProvider = new nsXPathExceptionProvider();
00222     if (!gXPathExceptionProvider)
00223         return NS_ERROR_OUT_OF_MEMORY;
00224     NS_ADDREF(gXPathExceptionProvider);
00225     nsCOMPtr<nsIExceptionService> xs =
00226         do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID);
00227     if (xs)
00228         xs->RegisterExceptionProvider(gXPathExceptionProvider,
00229                                       NS_ERROR_MODULE_DOM_XPATH);
00230 
00231     if (!txXSLTProcessor::init()) {
00232         return NS_ERROR_OUT_OF_MEMORY;
00233     }
00234 
00235     nsresult rv = CallGetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID,
00236                                  &gTxSecurityManager);
00237     if (NS_FAILED(rv)) {
00238         gTxSecurityManager = nsnull;
00239         return rv;
00240     }
00241 
00242     rv = CallGetService(NS_NAMESPACEMANAGER_CONTRACTID, &gTxNameSpaceManager);
00243     if (NS_FAILED(rv)) {
00244         gTxNameSpaceManager = nsnull;
00245         return rv;
00246     }
00247 
00248     rv = CallGetService("@mozilla.org/parser/parser-service;1",
00249                         &gTxParserService);
00250     if (NS_FAILED(rv)) {
00251         gTxParserService = nsnull;
00252         return rv;
00253     }
00254 
00255     nsCOMPtr<nsIErrorService> errorService =
00256         do_GetService(NS_ERRORSERVICE_CONTRACTID);
00257     if (errorService) {
00258         errorService->RegisterErrorStringBundle(NS_ERROR_MODULE_XSLT,
00259                                                 XSLT_MSGS_URL);
00260     }
00261 
00262     return NS_OK;
00263 }
00264 
00265 // Shutdown this module, releasing all of the module resources
00266 PR_STATIC_CALLBACK(void)
00267 Shutdown(nsIModule* aSelf)
00268 {
00269     NS_PRECONDITION(gInitialized, "module not initialized");
00270     if (!gInitialized)
00271         return;
00272 
00273     gInitialized = PR_FALSE;
00274     if (gXPathExceptionProvider) {
00275         nsCOMPtr<nsIExceptionService> xs =
00276             do_GetService(NS_EXCEPTIONSERVICE_CONTRACTID);
00277         if (xs)
00278             xs->UnregisterExceptionProvider(gXPathExceptionProvider,
00279                                             NS_ERROR_MODULE_DOM_XPATH);
00280         NS_RELEASE(gXPathExceptionProvider);
00281     }
00282 
00283     NS_IF_RELEASE(NS_CLASSINFO_NAME(XSLTProcessor));
00284     NS_IF_RELEASE(NS_CLASSINFO_NAME(XPathEvaluator));
00285     NS_IF_RELEASE(NS_CLASSINFO_NAME(XPathException));
00286     NS_IF_RELEASE(NS_CLASSINFO_NAME(XPathExpression));
00287     NS_IF_RELEASE(NS_CLASSINFO_NAME(XPathNSResolver));
00288     NS_IF_RELEASE(NS_CLASSINFO_NAME(XPathResult));
00289 
00290     txXSLTProcessor::shutdown();
00291 
00292     NS_IF_RELEASE(gTxSecurityManager);
00293     NS_IF_RELEASE(gTxNameSpaceManager);
00294     NS_IF_RELEASE(gTxParserService);
00295 }
00296 
00297 // Component Table
00298 static const nsModuleComponentInfo gComponents[] = {
00299     { "XSLTProcessor",
00300       TRANSFORMIIX_XSLT_PROCESSOR_CID,
00301       TRANSFORMIIX_XSLT_PROCESSOR_CONTRACTID,
00302       txMozillaXSLTProcessorConstructor,
00303       RegisterTransformiix },
00304     { "XPathEvaluator",
00305       TRANSFORMIIX_XPATH_EVALUATOR_CID,
00306       NS_XPATH_EVALUATOR_CONTRACTID,
00307       nsXPathEvaluatorConstructor },
00308 #ifndef DISABLE_XFORMS_HOOKS
00309     { "XFormsXPathEvaluator",
00310       TRANSFORMIIX_XFORMS_XPATH_EVALUATOR_CID,
00311       NS_XFORMS_XPATH_EVALUATOR_CONTRACTID,
00312       nsXFormsXPathEvaluatorConstructor },
00313 #endif
00314     { "Transformiix DOMCI Extender",
00315       TRANSFORMIIX_DOMCI_EXTENSION_CID,
00316       TRANSFORMIIX_DOMCI_EXTENSION_CONTRACTID,
00317       NS_DOMCI_EXTENSION_CONSTRUCTOR(Transformiix) },
00318     { "XPath1 XPointer Scheme Processor",
00319       TRANSFORMIIX_XPATH1_SCHEME_CID,
00320       NS_XPOINTER_SCHEME_PROCESSOR_BASE "xpath1",
00321       nsXPath1SchemeProcessorConstructor }
00322 };
00323 
00324 NS_IMPL_NSGETMODULE_WITH_CTOR_DTOR(TransformiixModule, gComponents,
00325                                    Initialize, Shutdown)