extra: import at 3.0.1 beta 1
[mozilla-extra.git] / extensions / webservices / soap / src / nsSOAPBlock.cpp
blob59132bf32beca2ce0425f9751dacdec13ea9f1ce
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2001
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsString.h"
39 #include "nsReadableUtils.h"
40 #include "nsSOAPBlock.h"
41 #include "nsSOAPUtils.h"
42 #include "nsIServiceManager.h"
43 #include "nsISOAPAttachments.h"
44 #include "nsISOAPMessage.h"
45 #include "nsSOAPException.h"
47 nsSOAPBlock::nsSOAPBlock()
51 nsSOAPBlock::~nsSOAPBlock()
55 NS_IMPL_ISUPPORTS2(nsSOAPBlock, nsISOAPBlock, nsIJSNativeInitializer)
56 NS_IMETHODIMP nsSOAPBlock::Init(nsISOAPAttachments * aAttachments,
57 PRUint16 aVersion)
59 if (aVersion == nsISOAPMessage::VERSION_1_1
60 || aVersion == nsISOAPMessage::VERSION_1_2) {
61 mAttachments = aAttachments;
62 mVersion = aVersion;
63 return NS_OK;
65 return SOAP_EXCEPTION(NS_ERROR_ILLEGAL_VALUE,"SOAP_BAD_VERSION", "Bad version used to initialize block.");
68 /* attribute AString namespaceURI; */
69 NS_IMETHODIMP nsSOAPBlock::GetNamespaceURI(nsAString & aNamespaceURI)
71 if (mElement) {
72 if (mEncoding) {
73 nsAutoString temp;
74 nsresult rc = mElement->GetNamespaceURI(temp);
75 if (NS_FAILED(rc))
76 return rc;
77 return mEncoding->GetInternalSchemaURI(temp, aNamespaceURI);
79 return mElement->GetNamespaceURI(aNamespaceURI);
80 } else {
81 aNamespaceURI.Assign(mNamespaceURI);
83 return NS_OK;
86 NS_IMETHODIMP nsSOAPBlock::SetNamespaceURI(const nsAString & aNamespaceURI)
88 nsresult rc = SetElement(nsnull);
89 if (NS_FAILED(rc))
90 return rc;
91 mNamespaceURI.Assign(aNamespaceURI);
92 return NS_OK;
95 /* attribute AString name; */
96 NS_IMETHODIMP nsSOAPBlock::GetName(nsAString & aName)
98 if (mElement) {
99 return mElement->GetLocalName(aName);
100 } else {
101 aName.Assign(mName);
103 return NS_OK;
106 NS_IMETHODIMP nsSOAPBlock::SetName(const nsAString & aName)
108 nsresult rc = SetElement(nsnull);
109 if (NS_FAILED(rc))
110 return rc;
111 mName.Assign(aName);
112 return NS_OK;
115 /* attribute nsISOAPEncoding encoding; */
116 NS_IMETHODIMP nsSOAPBlock::GetEncoding(nsISOAPEncoding * *aEncoding)
118 NS_ENSURE_ARG_POINTER(aEncoding);
119 *aEncoding = mEncoding;
120 NS_IF_ADDREF(*aEncoding);
121 return NS_OK;
124 NS_IMETHODIMP nsSOAPBlock::SetEncoding(nsISOAPEncoding * aEncoding)
126 mEncoding = aEncoding;
127 mComputeValue = PR_TRUE;
128 return NS_OK;
131 /* attribute nsISchemaType schemaType; */
132 NS_IMETHODIMP nsSOAPBlock::GetSchemaType(nsISchemaType * *aSchemaType)
134 NS_ENSURE_ARG_POINTER(aSchemaType);
135 *aSchemaType = mSchemaType;
136 NS_IF_ADDREF(*aSchemaType);
137 return NS_OK;
140 NS_IMETHODIMP nsSOAPBlock::SetSchemaType(nsISchemaType * aSchemaType)
142 mSchemaType = aSchemaType;
143 mComputeValue = PR_TRUE;
144 return NS_OK;
147 /* attribute nsIDOMElement element; */
148 NS_IMETHODIMP nsSOAPBlock::GetElement(nsIDOMElement * *aElement)
150 NS_ENSURE_ARG_POINTER(aElement);
151 *aElement = mElement;
152 NS_IF_ADDREF(*aElement);
153 return NS_OK;
156 NS_IMETHODIMP nsSOAPBlock::SetElement(nsIDOMElement * aElement)
158 mElement = aElement;
159 mComputeValue = PR_TRUE;
160 return NS_OK;
163 /* attribute nsIVariant value; */
164 NS_IMETHODIMP nsSOAPBlock::GetValue(nsIVariant * *aValue)
166 NS_ENSURE_ARG_POINTER(aValue);
167 if (mElement) { // Check for auto-computation
168 if (mComputeValue) {
169 mComputeValue = PR_FALSE;
170 if (mEncoding) {
171 mStatus =
172 mEncoding->Decode(mElement, mSchemaType, mAttachments,
173 getter_AddRefs(mValue));
174 } else {
175 mStatus = SOAP_EXCEPTION(NS_ERROR_NOT_INITIALIZED,"SOAP_NO_ENCODING","No encoding found to decode block.");
179 *aValue = mValue;
180 NS_IF_ADDREF(*aValue);
181 return mElement ? mStatus : NS_OK;
184 NS_IMETHODIMP nsSOAPBlock::SetValue(nsIVariant * aValue)
186 nsresult rc = SetElement(nsnull);
187 if (NS_FAILED(rc))
188 return rc;
189 mValue = aValue;
190 return NS_OK;
193 NS_IMETHODIMP
194 nsSOAPBlock::Initialize(nsISupports* aOwner, JSContext* cx,
195 JSObject* obj, PRUint32 argc, jsval* argv)
198 // Get the arguments.
201 nsAutoString name;
202 nsAutoString namespaceURI;
203 nsIVariant* s1 = nsnull;
204 nsISupports* s2 = nsnull;
205 nsISupports* s3 = nsnull;
207 if (!JS_ConvertArguments(cx, argc, argv, "/%iv %is %is %ip %ip",
208 &s1,
209 static_cast<nsAString *>(&name),
210 static_cast<nsAString *>(&namespaceURI),
211 &s2,
212 &s3))
213 return SOAP_EXCEPTION(NS_ERROR_ILLEGAL_VALUE,"SOAP_BLOCK_INIT", "Could not interpret block initialization arguments.");
215 nsCOMPtr<nsIVariant> value = dont_AddRef(s1);
216 nsCOMPtr<nsISupports> schemaType = dont_AddRef(s2);
217 nsCOMPtr<nsISupports> encoding = dont_AddRef(s3);
219 nsresult rc = SetValue(value);
220 if (NS_FAILED(rc))
221 return rc;
222 rc = SetName(name);
223 if (NS_FAILED(rc))
224 return rc;
225 rc = SetNamespaceURI(namespaceURI);
226 if (NS_FAILED(rc))
227 return rc;
228 if (schemaType) {
229 nsCOMPtr<nsISchemaType> v = do_QueryInterface(schemaType, &rc);
230 if (NS_FAILED(rc))
231 return rc;
232 rc = SetSchemaType(v);
233 if (NS_FAILED(rc))
234 return rc;
236 if (encoding) {
237 nsCOMPtr<nsISOAPEncoding> v = do_QueryInterface(encoding, &rc);
238 if (NS_FAILED(rc))
239 return rc;
240 rc = SetEncoding(v);
241 if (NS_FAILED(rc))
242 return rc;
245 return NS_OK;