Back to index

lightning-sunbird  0.9+nobinonly
nsSOAPBlock.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.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsString.h"
00039 #include "nsReadableUtils.h"
00040 #include "nsSOAPBlock.h"
00041 #include "nsSOAPUtils.h"
00042 #include "nsIServiceManager.h"
00043 #include "nsISOAPAttachments.h"
00044 #include "nsISOAPMessage.h"
00045 #include "nsSOAPException.h"
00046 
00047 nsSOAPBlock::nsSOAPBlock()
00048 {
00049 }
00050 
00051 nsSOAPBlock::~nsSOAPBlock()
00052 {
00053 }
00054 
00055 NS_IMPL_ISUPPORTS2(nsSOAPBlock, nsISOAPBlock, nsIJSNativeInitializer)
00056 NS_IMETHODIMP nsSOAPBlock::Init(nsISOAPAttachments * aAttachments,
00057                                 PRUint16 aVersion)
00058 {
00059   if (aVersion == nsISOAPMessage::VERSION_1_1
00060       || aVersion == nsISOAPMessage::VERSION_1_2) {
00061     mAttachments = aAttachments;
00062     mVersion = aVersion;
00063     return NS_OK;
00064   }
00065   return SOAP_EXCEPTION(NS_ERROR_ILLEGAL_VALUE,"SOAP_BAD_VERSION", "Bad version used to initialize block.");
00066 }
00067 
00068 /* attribute AString namespaceURI; */
00069 NS_IMETHODIMP nsSOAPBlock::GetNamespaceURI(nsAString & aNamespaceURI)
00070 {
00071   if (mElement) {
00072     if (mEncoding) {
00073       nsAutoString temp;
00074       nsresult rc = mElement->GetNamespaceURI(temp);
00075       if (NS_FAILED(rc))
00076         return rc;
00077       return mEncoding->GetInternalSchemaURI(temp, aNamespaceURI);
00078     }
00079     return mElement->GetNamespaceURI(aNamespaceURI);
00080   } else {
00081     aNamespaceURI.Assign(mNamespaceURI);
00082   }
00083   return NS_OK;
00084 }
00085 
00086 NS_IMETHODIMP nsSOAPBlock::SetNamespaceURI(const nsAString & aNamespaceURI)
00087 {
00088   nsresult rc = SetElement(nsnull);
00089   if (NS_FAILED(rc))
00090     return rc;
00091   mNamespaceURI.Assign(aNamespaceURI);
00092   return NS_OK;
00093 }
00094 
00095 /* attribute AString name; */
00096 NS_IMETHODIMP nsSOAPBlock::GetName(nsAString & aName)
00097 {
00098   if (mElement) {
00099     return mElement->GetLocalName(aName);
00100   } else {
00101     aName.Assign(mName);
00102   }
00103   return NS_OK;
00104 }
00105 
00106 NS_IMETHODIMP nsSOAPBlock::SetName(const nsAString & aName)
00107 {
00108   nsresult rc = SetElement(nsnull);
00109   if (NS_FAILED(rc))
00110     return rc;
00111   mName.Assign(aName);
00112   return NS_OK;
00113 }
00114 
00115 /* attribute nsISOAPEncoding encoding; */
00116 NS_IMETHODIMP nsSOAPBlock::GetEncoding(nsISOAPEncoding * *aEncoding)
00117 {
00118   NS_ENSURE_ARG_POINTER(aEncoding);
00119   *aEncoding = mEncoding;
00120   NS_IF_ADDREF(*aEncoding);
00121   return NS_OK;
00122 }
00123 
00124 NS_IMETHODIMP nsSOAPBlock::SetEncoding(nsISOAPEncoding * aEncoding)
00125 {
00126   mEncoding = aEncoding;
00127   mComputeValue = PR_TRUE;
00128   return NS_OK;
00129 }
00130 
00131 /* attribute nsISchemaType schemaType; */
00132 NS_IMETHODIMP nsSOAPBlock::GetSchemaType(nsISchemaType * *aSchemaType)
00133 {
00134   NS_ENSURE_ARG_POINTER(aSchemaType);
00135   *aSchemaType = mSchemaType;
00136   NS_IF_ADDREF(*aSchemaType);
00137   return NS_OK;
00138 }
00139 
00140 NS_IMETHODIMP nsSOAPBlock::SetSchemaType(nsISchemaType * aSchemaType)
00141 {
00142   mSchemaType = aSchemaType;
00143   mComputeValue = PR_TRUE;
00144   return NS_OK;
00145 }
00146 
00147 /* attribute nsIDOMElement element; */
00148 NS_IMETHODIMP nsSOAPBlock::GetElement(nsIDOMElement * *aElement)
00149 {
00150   NS_ENSURE_ARG_POINTER(aElement);
00151   *aElement = mElement;
00152   NS_IF_ADDREF(*aElement);
00153   return NS_OK;
00154 }
00155 
00156 NS_IMETHODIMP nsSOAPBlock::SetElement(nsIDOMElement * aElement)
00157 {
00158   mElement = aElement;
00159   mComputeValue = PR_TRUE;
00160   return NS_OK;
00161 }
00162 
00163 /* attribute nsIVariant value; */
00164 NS_IMETHODIMP nsSOAPBlock::GetValue(nsIVariant * *aValue)
00165 {
00166   NS_ENSURE_ARG_POINTER(aValue);
00167   if (mElement) {                //  Check for auto-computation
00168     if (mComputeValue) {
00169       mComputeValue = PR_FALSE;
00170       if (mEncoding) {
00171         mStatus =
00172             mEncoding->Decode(mElement, mSchemaType, mAttachments,
00173                               getter_AddRefs(mValue));
00174       } else {
00175         mStatus = SOAP_EXCEPTION(NS_ERROR_NOT_INITIALIZED,"SOAP_NO_ENCODING","No encoding found to decode block.");
00176       }
00177     }
00178   }
00179   *aValue = mValue;
00180   NS_IF_ADDREF(*aValue);
00181   return mElement ? mStatus : NS_OK;
00182 }
00183 
00184 NS_IMETHODIMP nsSOAPBlock::SetValue(nsIVariant * aValue)
00185 {
00186   nsresult rc = SetElement(nsnull);
00187   if (NS_FAILED(rc))
00188     return rc;
00189   mValue = aValue;
00190   return NS_OK;
00191 }
00192 
00193 NS_IMETHODIMP
00194     nsSOAPBlock::Initialize(JSContext * cx, JSObject * obj,
00195                             PRUint32 argc, jsval * argv)
00196 {
00197 
00198 //  Get the arguments.
00199 
00200 
00201   nsAutoString name;
00202   nsAutoString namespaceURI;
00203   nsIVariant* s1 = nsnull;
00204   nsISupports* s2 = nsnull;
00205   nsISupports* s3 = nsnull;
00206 
00207   if (!JS_ConvertArguments(cx, argc, argv, "/%iv %is %is %ip %ip",
00208                            &s1,
00209                            NS_STATIC_CAST(nsAString *, &name),
00210                            NS_STATIC_CAST(nsAString *, &namespaceURI),
00211                            &s2,
00212                            &s3))
00213     return SOAP_EXCEPTION(NS_ERROR_ILLEGAL_VALUE,"SOAP_BLOCK_INIT", "Could not interpret block initialization arguments.");
00214   
00215   nsCOMPtr<nsIVariant> value = dont_AddRef(s1);
00216   nsCOMPtr<nsISupports> schemaType = dont_AddRef(s2);
00217   nsCOMPtr<nsISupports> encoding = dont_AddRef(s3);
00218 
00219   nsresult rc = SetValue(value);
00220   if (NS_FAILED(rc))
00221     return rc;
00222   rc = SetName(name);
00223   if (NS_FAILED(rc))
00224     return rc;
00225   rc = SetNamespaceURI(namespaceURI);
00226   if (NS_FAILED(rc))
00227     return rc;
00228   if (schemaType) {
00229     nsCOMPtr<nsISchemaType> v = do_QueryInterface(schemaType, &rc);
00230     if (NS_FAILED(rc))
00231       return rc;
00232     rc = SetSchemaType(v);
00233     if (NS_FAILED(rc))
00234       return rc;
00235   }
00236   if (encoding) {
00237     nsCOMPtr<nsISOAPEncoding> v = do_QueryInterface(encoding, &rc);
00238     if (NS_FAILED(rc))
00239       return rc;
00240     rc = SetEncoding(v);
00241     if (NS_FAILED(rc))
00242       return rc;
00243   }
00244 
00245   return NS_OK;
00246 }