Back to index

lightning-sunbird  0.9+nobinonly
nsXFormsElementFactory.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 Mozilla XForms support.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * IBM Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Brian Ryner <bryner@brianryner.com>
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 "nsXFormsElementFactory.h"
00040 #include "nsIDOMDocument.h"
00041 #include "nsXFormsAtoms.h"
00042 #include "nsIDocument.h"
00043 #include "nsIDOMNode.h"
00044 #include "nsString.h"
00045 
00046 // Form controls
00047 NS_HIDDEN_(nsresult) NS_NewXFormsInputElement(nsIXTFElement **aElement);
00048 NS_HIDDEN_(nsresult) NS_NewXFormsSecretElement(nsIXTFElement **aElement);
00049 NS_HIDDEN_(nsresult) NS_NewXFormsTextAreaElement(nsIXTFElement **aElement);
00050 NS_HIDDEN_(nsresult) NS_NewXFormsGroupElement(nsIXTFElement **aElement);
00051 NS_HIDDEN_(nsresult) NS_NewXFormsOutputElement(nsIXTFElement **aElement);
00052 NS_HIDDEN_(nsresult) NS_NewXFormsRepeatElement(nsIXTFElement **aElement);
00053 NS_HIDDEN_(nsresult) NS_NewXFormsInsertElement(nsIXTFElement **aElement);
00054 NS_HIDDEN_(nsresult) NS_NewXFormsDeleteElement(nsIXTFElement **aElement);
00055 NS_HIDDEN_(nsresult) NS_NewXFormsUploadElement(nsIXTFElement **aElement);
00056 
00061 NS_HIDDEN_(nsresult) NS_NewXFormsContextContainer(nsIXTFElement **aElement);
00062 NS_HIDDEN_(nsresult) NS_NewXFormsTriggerElement(nsIXTFElement **aElement);
00063 NS_HIDDEN_(nsresult) NS_NewXFormsSubmitElement(nsIXTFElement **aElement);
00064 NS_HIDDEN_(nsresult) NS_NewXFormsLabelElement(nsIXTFElement **aElement);
00065 NS_HIDDEN_(nsresult) NS_NewXFormsSelectElement(nsIXTFElement **aElement);
00066 NS_HIDDEN_(nsresult) NS_NewXFormsSelect1Element(nsIXTFElement **aElement);
00067 NS_HIDDEN_(nsresult) NS_NewXFormsItemElement(nsIXTFElement **aElement);
00068 NS_HIDDEN_(nsresult) NS_NewXFormsValueElement(nsIXTFElement **aElement);
00069 NS_HIDDEN_(nsresult) NS_NewXFormsChoicesElement(nsIXTFElement **aElement);
00070 NS_HIDDEN_(nsresult) NS_NewXFormsItemSetElement(nsIXTFElement **aElement);
00071 NS_HIDDEN_(nsresult) NS_NewXFormsRangeElement(nsIXTFElement **aElement);
00072 NS_HIDDEN_(nsresult) NS_NewXFormsCopyElement(nsIXTFElement **aElement);
00073 
00074 //Action Module Elements
00075 NS_HIDDEN_(nsresult) NS_NewXFormsDispatchElement(nsIXTFElement **aResult);
00076 NS_HIDDEN_(nsresult) NS_NewXFormsSendElement(nsIXTFElement **aResult);
00077 NS_HIDDEN_(nsresult) NS_NewXFormsSetFocusElement(nsIXTFElement **aResult);
00078 NS_HIDDEN_(nsresult) NS_NewXFormsResetElement(nsIXTFElement **aResult);
00079 NS_HIDDEN_(nsresult) NS_NewXFormsRebuildElement(nsIXTFElement **aResult);
00080 NS_HIDDEN_(nsresult) NS_NewXFormsRecalculateElement(nsIXTFElement **aResult);
00081 NS_HIDDEN_(nsresult) NS_NewXFormsRevalidateElement(nsIXTFElement **aResult);
00082 NS_HIDDEN_(nsresult) NS_NewXFormsRefreshElement(nsIXTFElement **aResult);
00083 NS_HIDDEN_(nsresult) NS_NewXFormsActionElement(nsIXTFElement **aResult);
00084 NS_HIDDEN_(nsresult) NS_NewXFormsLoadElement(nsIXTFElement **aResult);
00085 NS_HIDDEN_(nsresult) NS_NewXFormsSetValueElement(nsIXTFElement **aResult);
00086 NS_HIDDEN_(nsresult) NS_NewXFormsSetIndexElement(nsIXTFElement **aResult);
00087 NS_HIDDEN_(nsresult) NS_NewXFormsMessageElement(nsIXTFElement **aResult);
00088 
00089 // XForms Controls. Additional Elements.
00090 NS_HIDDEN_(nsresult) NS_NewXFormsMediatypeElement(nsIXTFElement **aResult);
00091 NS_HIDDEN_(nsresult) NS_NewXFormsHintElement(nsIXTFElement **aResult);
00092 NS_HIDDEN_(nsresult) NS_NewXFormsHelpElement(nsIXTFElement **aResult);
00093 NS_HIDDEN_(nsresult) NS_NewXFormsAlertElement(nsIXTFElement **aResult);
00094 
00095 // XForms User Interface. The XForms Switch Module.
00096 NS_HIDDEN_(nsresult) NS_NewXFormsToggleElement(nsIXTFElement **aResult);
00097 NS_HIDDEN_(nsresult) NS_NewXFormsCaseElement(nsIXTFElement **aResult);
00098 NS_HIDDEN_(nsresult) NS_NewXFormsSwitchElement(nsIXTFElement **aResult);
00099 
00100 NS_HIDDEN_(nsresult) NS_NewXFormsModelElement(nsIXTFElement **aElement);
00101 NS_HIDDEN_(nsresult) NS_NewXFormsSubmissionElement(nsIXTFElement **aElement);
00102 NS_HIDDEN_(nsresult) NS_NewXFormsInstanceElement(nsIXTFElement **aElement);
00103 NS_HIDDEN_(nsresult) NS_NewXFormsStubElement(nsIXTFElement **aElement);
00104 
00105 NS_IMPL_ISUPPORTS2(nsXFormsElementFactory,
00106                    nsIXTFElementFactory,
00107                    nsIDOMNSFeatureFactory)
00108 
00109 NS_IMETHODIMP
00110 nsXFormsElementFactory::CreateElement(const nsAString& aTagName,
00111                                       nsIXTFElement **aElement)
00112 {
00113   if (aTagName.EqualsLiteral("model"))
00114     return NS_NewXFormsModelElement(aElement);
00115   if (aTagName.EqualsLiteral("instance"))
00116     return NS_NewXFormsInstanceElement(aElement);
00117   if (aTagName.EqualsLiteral("bind"))
00118     return NS_NewXFormsStubElement(aElement);
00119   if (aTagName.EqualsLiteral("input"))
00120     return NS_NewXFormsInputElement(aElement);
00121   if (aTagName.EqualsLiteral("secret"))
00122     return NS_NewXFormsSecretElement(aElement);
00123   if (aTagName.EqualsLiteral("textarea"))
00124     return NS_NewXFormsTextAreaElement(aElement);
00125   if (aTagName.EqualsLiteral("group"))
00126     return NS_NewXFormsGroupElement(aElement);
00127   if (aTagName.EqualsLiteral("output"))
00128     return NS_NewXFormsOutputElement(aElement);
00129   if (aTagName.EqualsLiteral("repeat"))
00130     return NS_NewXFormsRepeatElement(aElement);
00131   if (aTagName.EqualsLiteral("insert"))
00132     return NS_NewXFormsInsertElement(aElement);
00133   if (aTagName.EqualsLiteral("delete"))
00134     return NS_NewXFormsDeleteElement(aElement);
00135   if (aTagName.EqualsLiteral("contextcontainer") ||
00136       aTagName.EqualsLiteral("contextcontainer-inline"))
00137     return NS_NewXFormsContextContainer(aElement);
00138   if (aTagName.EqualsLiteral("label"))
00139     return NS_NewXFormsLabelElement(aElement);
00140   if (aTagName.EqualsLiteral("select"))
00141     return NS_NewXFormsSelectElement(aElement);
00142   if (aTagName.EqualsLiteral("select1"))
00143     return NS_NewXFormsSelect1Element(aElement);
00144   if (aTagName.EqualsLiteral("item"))
00145     return NS_NewXFormsItemElement(aElement);
00146   if (aTagName.EqualsLiteral("itemset"))
00147     return NS_NewXFormsItemSetElement(aElement);
00148   if (aTagName.EqualsLiteral("value"))
00149     return NS_NewXFormsValueElement(aElement);
00150   if (aTagName.EqualsLiteral("choices"))
00151     return NS_NewXFormsChoicesElement(aElement);
00152   if (aTagName.EqualsLiteral("dispatch"))
00153     return NS_NewXFormsDispatchElement(aElement);
00154   if (aTagName.EqualsLiteral("send"))
00155     return NS_NewXFormsSendElement(aElement);
00156   if (aTagName.EqualsLiteral("setfocus"))
00157     return NS_NewXFormsSetFocusElement(aElement);
00158   if (aTagName.EqualsLiteral("reset"))
00159     return NS_NewXFormsResetElement(aElement);
00160   if (aTagName.EqualsLiteral("rebuild"))
00161     return NS_NewXFormsRebuildElement(aElement);
00162   if (aTagName.EqualsLiteral("recalculate"))
00163     return NS_NewXFormsRecalculateElement(aElement);
00164   if (aTagName.EqualsLiteral("revalidate"))
00165     return NS_NewXFormsRevalidateElement(aElement);
00166   if (aTagName.EqualsLiteral("refresh"))
00167     return NS_NewXFormsRefreshElement(aElement);
00168   if (aTagName.EqualsLiteral("action"))
00169     return NS_NewXFormsActionElement(aElement);
00170   if (aTagName.EqualsLiteral("load"))
00171     return NS_NewXFormsLoadElement(aElement);
00172   if (aTagName.EqualsLiteral("setvalue"))
00173     return NS_NewXFormsSetValueElement(aElement);
00174   if (aTagName.EqualsLiteral("setindex"))
00175     return NS_NewXFormsSetIndexElement(aElement);
00176   if (aTagName.EqualsLiteral("message"))
00177     return NS_NewXFormsMessageElement(aElement);
00178   if (aTagName.EqualsLiteral("hint"))
00179     return NS_NewXFormsHintElement(aElement);
00180   if (aTagName.EqualsLiteral("help"))
00181     return NS_NewXFormsHelpElement(aElement);
00182   if (aTagName.EqualsLiteral("alert"))
00183     return NS_NewXFormsAlertElement(aElement);
00184   if (aTagName.EqualsLiteral("mediatype"))
00185     return NS_NewXFormsMediatypeElement(aElement);
00186   if (aTagName.EqualsLiteral("submission"))
00187     return NS_NewXFormsSubmissionElement(aElement);
00188   if (aTagName.EqualsLiteral("trigger"))
00189     return NS_NewXFormsTriggerElement(aElement);
00190   if (aTagName.EqualsLiteral("submit"))
00191     return NS_NewXFormsSubmitElement(aElement);
00192   if (aTagName.EqualsLiteral("toggle"))
00193     return NS_NewXFormsToggleElement(aElement);
00194   if (aTagName.EqualsLiteral("case"))
00195     return NS_NewXFormsCaseElement(aElement);
00196   if (aTagName.EqualsLiteral("switch"))
00197     return NS_NewXFormsSwitchElement(aElement);
00198   if (aTagName.EqualsLiteral("upload"))
00199     return NS_NewXFormsUploadElement(aElement);
00200   if (aTagName.EqualsLiteral("range"))
00201     return NS_NewXFormsRangeElement(aElement);
00202   if (aTagName.EqualsLiteral("copy"))
00203     return NS_NewXFormsCopyElement(aElement);
00204 
00205   *aElement = nsnull;
00206   return NS_ERROR_FAILURE;
00207 }
00208 
00209 NS_IMETHODIMP
00210 nsXFormsElementFactory::HasFeature(nsISupports *aObject,
00211                                    const nsAString& aFeature,
00212                                    const nsAString& aVersion,
00213                                    PRBool *aReturn)
00214 {
00215   *aReturn = (aFeature.EqualsLiteral("org.w3c.xforms.dom") &&
00216               aVersion.EqualsLiteral("1.0")) ||
00217              (aFeature.EqualsLiteral(NS_XFORMS_INSTANCE_OWNER) &&
00218               aVersion.EqualsLiteral("1.0"));
00219   return NS_OK;
00220 }
00221 
00222 NS_IMETHODIMP
00223 nsXFormsElementFactory::GetFeature(nsISupports *aObject,
00224                                    const nsAString & aFeature,
00225                                    const nsAString & aVersion,
00226                                    nsISupports **aReturn)
00227 {
00228   *aReturn = nsnull;
00229   if (aFeature.EqualsLiteral(NS_XFORMS_INSTANCE_OWNER) &&
00230       aVersion.EqualsLiteral("1.0")) {
00231     nsCOMPtr<nsIDOMNode> node(do_QueryInterface(aObject));
00232     NS_ENSURE_STATE(node);
00233 
00234     nsCOMPtr<nsIDOMDocument> domDoc;
00235     node->GetOwnerDocument(getter_AddRefs(domDoc));
00236     if (!domDoc) {
00237       // if node is nsIDOMDocument the ownerDocument is null.
00238       domDoc = do_QueryInterface(node);
00239     }
00240     NS_ENSURE_STATE(domDoc);
00241 
00242     nsCOMPtr<nsIDocument> doc(do_QueryInterface(domDoc));
00243     NS_ENSURE_STATE(doc);
00244 
00245     nsISupports* owner =
00246       NS_STATIC_CAST(nsISupports*,
00247                      doc->GetProperty(nsXFormsAtoms::instanceDocumentOwner));
00248     NS_IF_ADDREF(*aReturn = owner);
00249     return NS_OK;
00250   }
00251 
00252   return NS_ERROR_NOT_AVAILABLE;
00253 }