Back to index

lightning-sunbird  0.9+nobinonly
nsXFormsContextInfo.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) 2007
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Merle Sterling <msterlin@us.ibm.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 "nsXFormsContextInfo.h"
00040 #include "nsIDOMDocument.h"
00041 #include "nsIDOMDOMImplementation.h"
00042 #include "nsIDOMText.h"
00043 #include "nsXFormsUtils.h"
00044 
00050 NS_IMPL_ISUPPORTS1(nsXFormsContextInfo, nsIXFormsContextInfo)
00051 
00052 nsXFormsContextInfo::nsXFormsContextInfo(nsIDOMElement *aElement)
00053  : mElement(aElement), mType(0)
00054 {
00055   mNode = nsnull;
00056 }
00057 
00058 nsXFormsContextInfo::~nsXFormsContextInfo()
00059 {
00060   if (mType == nsIXFormsContextInfo::NODESET_TYPE) {
00061     NS_IF_RELEASE(mNodeset);
00062   } else {
00063     // String, Number, and Node are all nodes.
00064     NS_IF_RELEASE(mNode);
00065   }
00066 }
00067 
00068 nsresult
00069 nsXFormsContextInfo::SetName(nsAString &aName)
00070 {
00071   mName = aName;
00072   return NS_OK;
00073 }
00074 
00075 NS_IMETHODIMP
00076 nsXFormsContextInfo::GetName(nsAString &aResult)
00077 {
00078   aResult = mName;
00079   return NS_OK;
00080 }
00081 
00082 NS_IMETHODIMP
00083 nsXFormsContextInfo::GetType(PRInt32 *aType)
00084 {
00085   *aType = mType;
00086   return NS_OK;
00087 }
00088 
00089 nsresult
00090 nsXFormsContextInfo::SetStringValue(const char *aName, const nsAString &aString)
00091 {
00092   // Store the string value of the context property as a node.
00093   SetNodeValueInternal(aName, aString);
00094   mType = nsIXFormsContextInfo::STRING_TYPE;
00095   return NS_OK;
00096 }
00097 
00098 NS_IMETHODIMP
00099 nsXFormsContextInfo::GetStringValue(nsAString &aResult)
00100 {
00101   aResult.Truncate();
00102 
00103   if (mType == nsIXFormsContextInfo::STRING_TYPE) {
00104     nsXFormsUtils::GetNodeValue(mNode, aResult);
00105   } else {
00106     NS_WARNING("GetStringValue: context type is not a string!");
00107   }
00108   return NS_OK;
00109 }
00110 
00111 nsresult
00112 nsXFormsContextInfo::SetNumberValue(const char *aName, PRInt32 aNumber)
00113 {
00114   // Convert the number to a string and store the (string)number
00115   // value of the context property as a node.
00116   nsAutoString numberStr;
00117   numberStr.AppendInt(aNumber);
00118 
00119   SetNodeValueInternal(aName, numberStr);
00120   mType = nsIXFormsContextInfo::NUMBER_TYPE;
00121   return NS_OK;
00122 }
00123 
00124 NS_IMETHODIMP
00125 nsXFormsContextInfo::GetNumberValue(PRInt32 *aResult)
00126 {
00127   *aResult = 0;
00128 
00129   if (mType == nsIXFormsContextInfo::NUMBER_TYPE) {
00130     nsAutoString numberStr;
00131     nsXFormsUtils::GetNodeValue(mNode, numberStr);
00132 
00133     PRInt32 errCode;
00134     *aResult = numberStr.ToInteger(&errCode);
00135     NS_ENSURE_TRUE(errCode == 0, NS_ERROR_FAILURE);
00136   } else {
00137     NS_WARNING("GetNumberValue: context type is not a number!");
00138   }
00139 
00140   return NS_OK;
00141 }
00142 
00143 nsresult
00144 nsXFormsContextInfo::SetNodeValue(const char *aName, nsIDOMNode *aNode)
00145 {
00146   // Set the name of the context property.
00147   nsAutoString name;
00148   name.AppendASCII(aName);
00149   SetName(name);
00150 
00151   // Set the node value of the context property.
00152   NS_IF_ADDREF(mNode = aNode);
00153   mType = nsIXFormsContextInfo::NODE_TYPE;
00154   return NS_OK;
00155 }
00156 
00157 NS_IMETHODIMP
00158 nsXFormsContextInfo::GetNodeValue(nsIDOMNode **aResult)
00159 {
00160   *aResult = nsnull;
00161 
00162   // String and number context types are stored as nodes, so
00163   // GetNodeValue can be used to get a string or number type
00164   // as a node.
00165   if (mType == nsIXFormsContextInfo::NODE_TYPE   ||
00166       mType == nsIXFormsContextInfo::STRING_TYPE ||
00167       mType == nsIXFormsContextInfo::NUMBER_TYPE) {
00168     NS_IF_ADDREF(*aResult = mNode);
00169   } else {
00170     NS_WARNING("GetNodeValue: context type is not a node, string, or number!");
00171   }
00172   return NS_OK;
00173 }
00174 
00175 nsresult
00176 nsXFormsContextInfo::SetNodesetValue(const char *aName,
00177                                      nsIDOMXPathResult *aNodeset)
00178 {
00179   // Set the name of the context property.
00180   nsAutoString name;
00181   name.AppendASCII(aName);
00182   SetName(name);
00183 
00184   // Set the nodeset value of the context property.
00185   NS_IF_ADDREF(mNodeset = aNodeset);
00186   mType = nsIXFormsContextInfo::NODESET_TYPE;
00187   return NS_OK;
00188 }
00189 
00190 NS_IMETHODIMP
00191 nsXFormsContextInfo::GetNodesetValue(nsIDOMXPathResult **aResult)
00192 {
00193   *aResult = nsnull;
00194 
00195   if (mType == nsIXFormsContextInfo::NODESET_TYPE) {
00196     NS_IF_ADDREF(*aResult = mNodeset);
00197   } else {
00198     NS_WARNING("GetNodesetValue: context type is not a nodeset!");
00199   }
00200   return NS_OK;
00201 }
00202 
00203 nsresult
00204 nsXFormsContextInfo::SetNodeValueInternal(const char *aName,
00205                                           const nsAString &aValue)
00206 {
00207   if (!mElement)
00208     return NS_ERROR_FAILURE;
00209 
00210   nsresult rv;
00211 
00212   nsCOMPtr<nsIDOMDocument> doc;
00213   rv = mElement->GetOwnerDocument(getter_AddRefs(doc));
00214   NS_ENSURE_SUCCESS(rv, rv);
00215 
00216   nsCOMPtr<nsIDOMDOMImplementation> domImpl;
00217   rv = doc->GetImplementation(getter_AddRefs(domImpl));
00218   NS_ENSURE_SUCCESS(rv, rv);
00219 
00220   nsCOMPtr<nsIDOMText> textNode;
00221   rv = doc->CreateTextNode(aValue, getter_AddRefs(textNode));
00222   NS_ENSURE_SUCCESS(rv, rv);
00223 
00224   SetNodeValue(aName, textNode);
00225 
00226   return NS_OK;
00227 }