-
Notifications
You must be signed in to change notification settings - Fork 3
/
GWSSOAPCoder.m
executable file
·1293 lines (1173 loc) · 32.9 KB
/
GWSSOAPCoder.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
Copyright (C) 2008 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <[email protected]>
Date: January 2008
This file is part of the WebServices Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
$Date: 2007-09-24 14:19:12 +0100 (Mon, 24 Sep 2007) $ $Revision: 25500 $
*/
#import <Foundation/Foundation.h>
#import "GWSPrivate.h"
NSString * const GWSSOAPBodyEncodingStyleKey
= @"GWSSOAPBodyEncodingStyleKey";
NSString * const GWSSOAPBodyEncodingStyleDocument
= @"GWSSOAPBodyEncodingStyleDocument";
NSString * const GWSSOAPBodyEncodingStyleRPC
= @"GWSSOAPBodyEncodingStyleRPC";
NSString * const GWSSOAPBodyEncodingStyleWrapped
= @"GWSSOAPBodyEncodingStyleWrapped";
NSString * const GWSSOAPNamespaceURIKey
= @"GWSSOAPNamespaceURIKey";
NSString * const GWSSOAPNamespaceNameKey
= @"GWSSOAPNamespaceNameKey";
NSString * const GWSSOAPMessageHeadersKey
= @"GWSSOAPMessageHeadersKey";
NSString * const GWSSOAPArrayKey
= @"GWSSOAPArrayKey";
NSString * const GWSSOAPTypeKey
= @"GWSSOAPTypeKey";
NSString * const GWSSOAPUseEncoded
= @"encoded";
NSString * const GWSSOAPUseKey
= @"GWSSOAPUseKey";
NSString * const GWSSOAPUseLiteral
= @"literal";
NSString * const GWSSOAPValueKey
= @"GWSSOAPValueKey";
#if !defined(GNUSTEP)
/* Older versions of MacOS-X don't have -boolValue as an NSString method,
* so we add it here if we might be building with one of those versions.
*/
@implementation NSString(GWSCoder)
- (BOOL) boolValue
{
unsigned length = [self length];
if (length > 0)
{
unsigned index;
SEL sel = @selector(characterAtIndex:);
unichar (*imp)() = (unichar (*)())[self methodForSelector: sel];
for (index = 0; index < length; index++)
{
unichar c = (*imp)(self, sel, index);
if (c > 'y')
{
break;
}
if (strchr("123456789yYtT", c) != 0)
{
return YES;
}
if (!isspace(c) && c != '0' && c != '-' && c != '+')
{
break;
}
}
}
return NO;
}
@end
#endif
@interface GWSSOAPCoder (Private)
- (void) _createElementFor: (id)o named: (NSString*)name in: (GWSElement*)ctct;
- (id) _simplify: (GWSElement*)elem;
@end
@implementation GWSSOAPCoder
static NSCharacterSet *illegal = nil;
static id boolN;
static id boolY;
+ (void) initialize
{
if (illegal == nil)
{
NSMutableCharacterSet *tmp = [NSMutableCharacterSet new];
[tmp addCharactersInRange: NSMakeRange('0', 10)];
[tmp addCharactersInRange: NSMakeRange('a', 26)];
[tmp addCharactersInRange: NSMakeRange('A', 26)];
[tmp addCharactersInString: @"_.:/"];
[tmp invert];
illegal = [tmp copy];
[tmp release];
boolN = [[NSNumber numberWithBool: NO] retain];
boolY = [[NSNumber numberWithBool: YES] retain];
}
}
/* Build a new header using the specified namespace prefix or the prefix
* and namespace specified in o (if it is a dictionary specifying them).
*/
static GWSElement*
newHeader(NSString *prefix, id o)
{
NSString *hdrNamespace = nil;
NSString *hdrPrefix = prefix;
NSString *qualified;
GWSElement *header;
/* Get namespace and prefix from dictionary if possible.
*/
if ([o isKindOfClass: [NSDictionary class]])
{
hdrNamespace = [o objectForKey: GWSSOAPNamespaceURIKey];
if ([o objectForKey: GWSSOAPNamespaceNameKey] != nil)
{
hdrPrefix = [o objectForKey: GWSSOAPNamespaceNameKey];
}
}
qualified = @"Header";
if (hdrPrefix != nil)
{
qualified = [NSString stringWithFormat: @"%@:%@",
hdrPrefix, qualified];
}
header = [[GWSElement alloc] initWithName: @"Header"
namespace: hdrPrefix
qualified: qualified
attributes: nil];
if (hdrNamespace != nil && hdrPrefix == nil)
{
/* We have a namespace but no name ... make it the default
* namespace for the body.
*/
[header setNamespace: hdrNamespace forPrefix: @""];
}
return header;
}
- (NSData*) buildRequest: (NSString*)method
parameters: (NSDictionary*)parameters
order: (NSArray*)order
{
NSAutoreleasePool *pool;
NSString *nsName;
NSString *nsURI;
GWSElement *envelope;
GWSElement *header;
GWSElement *body;
GWSElement *container;
NSString *prefix;
NSString *qualified;
NSString *use;
NSMutableString *ms;
id o;
unsigned c;
unsigned i;
[self reset];
pool = [NSAutoreleasePool new];
/* Determine the order of the parameters in the message body.
*/
o = [parameters objectForKey: GWSOrderKey];
if (o != nil)
{
if (order != nil && [order isEqual: o] == NO)
{
NSLog(@"Parameter order specified both in the 'order' argument and using GWSOrderKey. Using the value from GWSOrderKey.");
}
order = o;
}
o = [parameters objectForKey: GWSParametersKey];
if (nil != o)
{
parameters = o;
}
if ([order count] == 0)
{
NSEnumerator *kEnum = [parameters keyEnumerator];
NSString *k;
NSMutableArray *a = [NSMutableArray array];
while ((k = [kEnum nextObject]) != nil)
{
if (NO == [k hasPrefix: @"GWSCoder"]
&& NO == [k hasPrefix: @"GWSSOAP"])
{
[a addObject: k];
}
}
order = a;
}
/* Set the operation style if it's specified (otherwise we keep it
* unchanged).
*/
if ((o = [parameters objectForKey: GWSSOAPBodyEncodingStyleKey]) != nil)
{
[self setOperationStyle: o];
}
/* The method name is required for RPC operations ...
* for document style operations the method is implicit in the URL
* that the document is sent to.
* We therefore check the method name only if we are doing an RPC.
*/
if (_style == GWSSOAPBodyEncodingStyleRPC && [self fault] == NO)
{
if ([method length] == 0)
{
return nil;
}
else
{
NSRange r;
r = [method rangeOfCharacterFromSet: illegal];
if (r.length > 0)
{
if ([self debug])
{
NSLog(@"Illegal character in method name '%@'", method);
}
return nil; // Bad method name.
}
}
}
envelope = [[[GWSElement alloc] initWithName: @"Envelope"
namespace: nil
qualified: @"soapenv:Envelope"
attributes: nil] autorelease];
[envelope setNamespace: @"http://schemas.xmlsoap.org/soap/envelope/"
forPrefix: @"soapenv"];
[envelope setNamespace: @"http://www.w3.org/2001/XMLSchema"
forPrefix: @"xsd"];
[envelope setNamespace: @"http://www.w3.org/2001/XMLSchema-instance"
forPrefix: @"xsi"];
/* Check the method namespace ... if we have a URI and a name then we
* want to specify the namespace in the envelope.
*/
nsName = [parameters objectForKey: GWSSOAPNamespaceNameKey];
nsURI = [parameters objectForKey: GWSSOAPNamespaceURIKey];
if (_style == GWSSOAPBodyEncodingStyleRPC && nsName != nil && nsURI != nil)
{
[envelope setNamespace: nsURI forPrefix: nsName];
}
if ([self delegate] != nil)
{
envelope = [[self delegate] coder: self willEncode: envelope];
}
if ([[envelope qualified] isEqualToString: @"Envelope"])
{
prefix = nil;
}
else
{
prefix = [envelope qualified];
prefix = [prefix substringToIndex: [prefix rangeOfString: @":"].location];
}
/* Now look for a value listing the headers to be encoded.
* If there is no value, we see if there are header values provided in
* the parameters dictionary which are not part of the message body,
* and build a headers dictionary from them.
*/
o = [parameters objectForKey: GWSSOAPMessageHeadersKey];
if (o == nil)
{
NSEnumerator *kEnum;
NSString *k;
kEnum = [parameters keyEnumerator];
while ((k = [kEnum nextObject]) != nil)
{
if (NO == [k hasPrefix: @"GWSCoder"]
&& NO == [k hasPrefix: @"GWSSOAP"]
&& NO == [order containsObject: k])
{
if (o == nil)
{
o = [NSMutableDictionary new];
}
[o setObject: [parameters objectForKey: k] forKey: k];
}
}
if (o != nil)
{
NSMutableDictionary *m = [parameters mutableCopy];
kEnum = [m keyEnumerator];
while ((k = [kEnum nextObject]) != nil)
{
[m removeObjectForKey: k];
}
[m setObject: o forKey: GWSSOAPMessageHeadersKey];
[o release];
parameters = [m autorelease];
}
}
if (o == nil)
{
/* If we still have no headers, just don't create a Header element.
*/
header = nil;
}
else
{
header = newHeader(prefix, o);
[envelope addChild: header];
[header release];
if ([o isKindOfClass: [NSDictionary class]] && [o count] > 0)
{
NSDictionary *d = (NSDictionary*)o;
NSArray *order = [o objectForKey: GWSOrderKey];
/* See if we have a key in the parameters to specify how the header
* should be encoded.
*/
use = [d objectForKey: GWSSOAPUseKey];
if ([use isEqualToString: GWSSOAPUseLiteral] == YES)
{
[self setUseLiteral: YES];
}
else if ([use isEqualToString: GWSSOAPUseEncoded] == YES)
{
[self setUseLiteral: NO];
}
if ([order count] == 0)
{
NSEnumerator *kEnum = [d keyEnumerator];
NSString *k;
NSMutableArray *a = [NSMutableArray array];
while ((k = [kEnum nextObject]) != nil)
{
if (NO == [k hasPrefix: @"GWSCoder"]
&& NO == [k hasPrefix: @"GWSSOAP"])
{
[a addObject: k];
}
}
order = a;
}
c = [order count];
/* The dictionary contains header elements by name.
*/
for (i = 0; i < c; i++)
{
NSString *k = [order objectAtIndex: i];
id v = [d objectForKey: k];
if (v == nil)
{
[NSException raise: NSInvalidArgumentException
format: @"Header '%@' missing", k];
}
[self _createElementFor: v named: k in: header];
}
}
}
/* Now we give the delegate a chance to entirely replace the header
* (which may be nil) with an element of its own.
*/
if ([[self delegate] respondsToSelector: @selector(coder:willEncode:)])
{
GWSElement *elem;
elem = [[self delegate] coder: self willEncode: header];
if (elem != header)
{
[header remove];
if (elem != nil)
{
/* If we have the content of a header rather than a
* complete header, we put it inside a standard header.
*/
if ([[elem name] isEqualToString: @"Header"] == NO)
{
header = [newHeader(prefix, o) autorelease];
[header addChild: elem];
}
else
{
header = elem;
}
[envelope addChild: header];
}
}
}
/* See if we have a key in the parameters to specify how the body
* should be encoded.
*/
use = [parameters objectForKey: GWSSOAPUseKey];
if ([use isEqualToString: GWSSOAPUseLiteral] == YES)
{
[self setUseLiteral: YES];
}
else if ([use isEqualToString: GWSSOAPUseEncoded] == YES)
{
[self setUseLiteral: NO];
}
qualified = @"Body";
if (prefix != nil)
{
qualified = [NSString stringWithFormat: @"%@:%@", prefix, qualified];
}
body = [[GWSElement alloc] initWithName: @"Body"
namespace: nil
qualified: qualified
attributes: nil];
[envelope addChild: body];
[body release];
if ([self delegate] != nil)
{
GWSElement *elem;
elem = [[self delegate] coder: self willEncode: body];
if (elem != body)
{
[body remove];
body = elem;
[envelope addChild: body];
}
}
if ([self fault] == YES)
{
GWSElement *fault;
qualified = @"Fault";
if (prefix != nil)
{
qualified = [NSString stringWithFormat: @"%@:%@", prefix, qualified];
}
fault = [[GWSElement alloc] initWithName: @"Fault"
namespace: nil
qualified: qualified
attributes: nil];
[body addChild: fault];
[fault release];
if ([self delegate] != nil)
{
GWSElement *elem;
elem = [[self delegate] coder: self willEncode: fault];
if (elem != fault)
{
[fault remove];
fault = elem;
[body addChild: fault];
}
}
c = [order count];
for (i = 0; i < c; i++)
{
NSString *k = [order objectAtIndex: i];
id v = [parameters objectForKey: k];
[self _createElementFor: v named: k in: fault];
}
}
else
{
if (_style == GWSSOAPBodyEncodingStyleRPC)
{
if (nil == nsName)
{
qualified = method;
if (YES == [qualified isEqualToString: method]
&& [qualified rangeOfString: @":"].length > 0)
{
method = [qualified substringFromIndex:
NSMaxRange([qualified rangeOfString: @":"])];
}
}
else
{
qualified = [NSString stringWithFormat: @"%@:%@", nsName, method];
}
container = [[GWSElement alloc] initWithName: method
namespace: nsName
qualified: qualified
attributes: nil];
[body addChild: container];
[container release];
qualified = @"encodingStyle";
if (prefix != nil)
{
qualified = [NSString stringWithFormat: @"%@:%@",
prefix, qualified];
}
[container setAttribute: @"http://schemas.xmlsoap.org/soap/encoding/"
forKey: qualified];
if (nsURI != nil && nsName == nil)
{
/* We have a namespace but no name ... make it the default
* namespace for the body.
*/
[container setNamespace: nsURI forPrefix: @""];
}
if ([self delegate] != nil)
{
GWSElement *elem;
elem = [[self delegate] coder: self willEncode: container];
if (elem != container)
{
[container remove];
container = elem;
[body addChild: container];
}
}
}
else if (_style == GWSSOAPBodyEncodingStyleWrapped)
{
NSLog(@"FIXME GWSSOAPBodyEncodingStyleWrapped not implemented");
container = body;
}
else
{
container = body; // Direct encoding inside the body.
}
c = [order count];
for (i = 0; i < c; i++)
{
NSString *k = [order objectAtIndex: i];
id v = [parameters objectForKey: k];
if (v == nil)
{
[NSException raise: NSInvalidArgumentException
format: @"Value '%@' (order %u) missing", k, i];
}
[self _createElementFor: v named: k in: container];
}
}
if ([[self delegate] respondsToSelector: @selector(coder:didEncode:)])
{
envelope = [[self delegate] coder: self didEncode: envelope];
}
ms = [self mutableString];
[ms setString: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"];
[envelope encodeWith: self];
[pool release];
return [ms dataUsingEncoding: NSUTF8StringEncoding];
}
- (NSData*) buildResponse: (NSString*)method
parameters: (NSDictionary*)parameters
order: (NSArray*)order;
{
/* For SOAP a request and a response look the same ... both are just
* messages.
*/
return [self buildRequest: method parameters: parameters order: order];
}
- (NSString*) encodeDateTimeFrom: (NSDate*)source
{
NSTimeZone *tz;
int t;
if ([source isKindOfClass: [NSCalendarDate class]] == YES)
{
tz = [(NSCalendarDate*)source timeZone];
}
else
{
tz = [self timeZone];
}
source = [NSCalendarDate dateWithTimeIntervalSinceReferenceDate:
[source timeIntervalSinceReferenceDate]];
[(NSCalendarDate*)source setTimeZone: tz];
t = [tz secondsFromGMTForDate: source];
if (t != 0)
{
char sign;
NSString *fmt;
if (t < 0)
{
sign = '-';
t = -t;
}
else
{
sign = '+';
}
t /= 60;
fmt = [NSString stringWithFormat: @"%%Y-%%m-%%dT%%H:%%M:%%S%c%02d:%02d",
sign, t / 60, t % 60];
[(NSCalendarDate*)source setCalendarFormat: fmt];
}
else
{
[(NSCalendarDate*)source setCalendarFormat: @"%Y-%m-%dT%H:%M:%SZ"];
}
return [source description];
}
- (id) init
{
if ((self = [super init]) != nil)
{
_style = GWSSOAPBodyEncodingStyleDocument;
}
return self;
}
- (NSString*) operationStyle
{
return _style;
}
- (NSMutableDictionary*) parseMessage: (NSData*)data
{
NSAutoreleasePool *pool;
NSMutableDictionary *result;
result = [NSMutableDictionary dictionaryWithCapacity: 3];
pool = [NSAutoreleasePool new];
NS_DURING
{
GWSCoder *parser;
NSEnumerator *enumerator;
GWSElement *elem;
GWSElement *envelope;
GWSElement *header;
GWSElement *body;
NSMutableDictionary *p;
NSMutableArray *o;
NSArray *children;
unsigned c;
unsigned i;
parser = [[GWSCoder new] autorelease];
envelope = [parser parseXML: data];
if (envelope == nil)
{
[NSException raise: NSInvalidArgumentException
format: @"Document is NOT parsable as XML"];
}
if ([self delegate] != nil)
{
envelope = [[self delegate] coder: self willDecode: envelope];
}
if ([[envelope name] isEqualToString: @"Envelope"] == NO)
{
[NSException raise: NSInvalidArgumentException
format: @"Document is not Envelope but '%@'",
[envelope name]];
}
enumerator = [[envelope children] objectEnumerator];
elem = [enumerator nextObject];
if (elem == nil)
{
[NSException raise: NSInvalidArgumentException
format: @"Envelope is empty"];
}
if ([[elem name] isEqualToString: @"Header"] == YES)
{
header = elem;
elem = [enumerator nextObject];
if ([self delegate] != nil)
{
[[self delegate] coder: self willDecode: header];
}
}
if (elem == nil)
{
[NSException raise: NSInvalidArgumentException
format: @"Envelope contains no Body"];
}
else if ([[elem name] isEqualToString: @"Body"] == NO)
{
[NSException raise: NSInvalidArgumentException
format: @"Envelope contains '%@' where Body was expected",
[elem name]];
}
body = elem;
if ([self delegate] != nil)
{
body = [[self delegate] coder: self willDecode: body];
}
children = [body children];
c = [children count];
elem = [children lastObject];
if (c == 1 && [[elem name] isEqualToString: @"Fault"] == YES)
{
NSMutableDictionary *f;
GWSElement *fault = elem;
f = [[NSMutableDictionary alloc] initWithCapacity: 4];
[result setObject: f forKey: GWSFaultKey];
[f release];
if ([self delegate] != nil)
{
fault = [[self delegate] coder: self willDecode: fault];
}
children = [fault children];
c = [children count];
i = 0;
while (i < c)
{
NSString *n;
NSString *v;
elem = [children objectAtIndex: i++];
n = [elem name];
v = [elem content];
if ([n isEqualToString: @"faultcode"] == YES
&& [v length] > 0)
{
[f setObject: v forKey: @"faultcode"];
}
else if ([n isEqualToString: @"faultstring"] == YES)
{
/* faultstring must be present but may be empty. */
[f setObject: v forKey: @"faultstring"];
}
else if ([n isEqualToString: @"faultactor"] == YES
&& [v length] > 0)
{
[f setObject: v forKey: @"faultactor"];
}
else if ([n isEqualToString: @"detail"] == YES)
{
if ([v length] > 0)
{
[f setObject: v forKey: @"detail"];
}
else
{
id arg;
arg = [[self delegate] decodeWithCoder: self
item: elem
named: n];
if (arg == nil)
{
/*
* FIXME ... convert subelements to the sort of type
* we should really have.
*/
arg = [self _simplify: elem];
}
[f setObject: arg forKey: @"detail"];
}
}
}
}
else
{
NSCountedSet *cs;
/* If the body contains a single element with no content,
* we assume it is a method and its children are the
* parameters. Otherwise we assume that the parameters
* are found directly inside the body.
*/
if (c == 1 && [[elem content] length] == 0)
{
if ([self delegate] != nil)
{
elem = [[self delegate] coder: self willDecode: elem];
}
[result setObject: [elem name] forKey: GWSMethodKey];
children = [elem children];
}
cs = [[NSCountedSet alloc] initWithCapacity: c];
c = [children count];
for (i = 0; i < c; i++)
{
[cs addObject: [[children objectAtIndex: i] name]];
}
p = [[NSMutableDictionary alloc] initWithCapacity: [cs count]];
[result setObject: p forKey: GWSParametersKey];
[p release];
o = [[NSMutableArray alloc] initWithCapacity: [cs count]];
[result setObject: o forKey: GWSOrderKey];
[o release];
c = [children count];
for (i = 0; i < c; i++)
{
id arg;
NSString *n;
unsigned rCount;
elem = [children objectAtIndex: i];
n = [elem name];
if ((rCount = [cs countForObject: n]) == 1)
{
[o addObject: n];
arg = [[self delegate] decodeWithCoder: self
item: elem
named: n];
if (arg == nil)
{
arg = [self _simplify: elem];
}
[p setObject: arg forKey: n];
}
else
{
NSMutableArray *ma;
ma = [p objectForKey: n];
if (ma == nil)
{
ma = [[NSMutableArray alloc] initWithCapacity: rCount];
[p setObject: ma forKey: n];
[ma release];
[o addObject: n];
}
arg = [[self delegate] decodeWithCoder: self
item: elem
named: n];
if (arg == nil)
{
arg = [self _simplify: elem];
}
[ma addObject: arg];
}
}
[cs release];
}
}
NS_HANDLER
{
[result setObject: [localException description] forKey: GWSErrorKey];
}
NS_ENDHANDLER
[pool release];
return result;
}
- (void) setOperationStyle: (NSString*)style
{
if (style == nil)
{
return;
}
if ([GWSSOAPBodyEncodingStyleDocument isEqualToString: style])
{
_style = GWSSOAPBodyEncodingStyleDocument;
}
else if ([GWSSOAPBodyEncodingStyleWrapped isEqualToString: style])
{
_style = GWSSOAPBodyEncodingStyleWrapped;
}
else if ([GWSSOAPBodyEncodingStyleRPC isEqualToString: style])
{
_style = GWSSOAPBodyEncodingStyleRPC;
}
}
- (void) setUseLiteral: (BOOL)use
{
_useLiteral = use;
}
- (BOOL) useLiteral
{
return _useLiteral;
}
@end
@implementation GWSSOAPCoder (Private)
- (void) _createElementFor: (id)o
named: (NSString*)name
in: (GWSElement*)ctxt
{
id v;
GWSElement *e;
NSString *q; // Qualified name
NSString *x; // xsi:type if any
NSString *c; // Content if any
NSString *a; // Array item name
NSString *nsURI = nil;
NSString *nsName = nil;
BOOL array = NO;
BOOL dictionary = NO;
if (o == nil)
{
[NSException raise: NSInvalidArgumentException
format: @"Value for '%@' (in %@) is nil", name, ctxt];
}
if ([[self delegate] encodeWithCoder: self item: o named: name in: ctxt])
{
return;
}
x = nil;
c = nil;
a = nil;
q = name;
/* If this is a dictionary describing a single value, rather than a
* complex value, we can get the information we need now.
*/
if (YES == [o isKindOfClass: [NSDictionary class]]
&& (v = [o objectForKey: GWSSOAPValueKey]) != nil)
{
/* If our value is a sequence of items, we can handle that now.
*/
a = [o objectForKey: GWSSOAPArrayKey];
if (a == nil && [v isKindOfClass: [NSArray class]] == YES)
{
NSMutableDictionary *m = [[o mutableCopy] autorelease];
v = [v objectEnumerator];
while ((o = [v nextObject]) != nil)
{
[m setObject: o forKey: GWSSOAPValueKey];
[self _createElementFor: m named: name in: ctxt];
}
return;
}
nsURI = [o objectForKey: GWSSOAPNamespaceURIKey];
nsName = [o objectForKey: GWSSOAPNamespaceNameKey];
x = [o objectForKey: GWSSOAPTypeKey];
o = v;
}
if (YES == [o isKindOfClass: [NSString class]])
{
if (NO == _useLiteral && x == nil)
{
x = @"xsd:string";
}
c = o;
}
else if (o == boolN)
{
if (NO == _useLiteral && x == nil)
{
x = @"xsd:boolean";
}
c = @"false";
}
else if (o == boolY)
{
if (NO == _useLiteral && x == nil)
{
x = @"xsd:boolean";