-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGWSDocument.m
executable file
·885 lines (766 loc) · 21 KB
/
GWSDocument.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
/**
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"
static NSMutableDictionary *extDict = nil;
static NSLock *extLock = nil;
@implementation GWSDocument (Private)
/* Make sure that a name is the local version without the target prefix.
*/
- (NSString*) _local: (NSString*)name
{
NSRange r = [name rangeOfString: @":"];
if (r.length > 0)
{
return [name substringFromIndex: NSMaxRange(r)];
}
return name;
}
- (NSString*) _validate: (GWSElement*)element in: (id)section
{
NSString *n;
n = [element namespace];
if (n != nil)
{
GWSExtensibility *e = [_ext objectForKey: n];
if (e != nil)
{
return [e validate: element for: self in: section setup: nil];
}
}
return nil;
}
@end
@implementation GWSDocument
+ (void) initialize
{
if (extLock == nil)
{
GWSSOAPExtensibility *e;
extLock = [NSLock new];
extDict = [NSMutableDictionary new];
e = [GWSSOAPExtensibility new];
[self registerExtensibility: e forNamespace:
@"http://schemas.xmlsoap.org/wsdl/soap/"];
[e release];
}
}
+ (GWSExtensibility*) extensibilityForNamespace: (NSString*)namespaceURL
{
GWSExtensibility *e;
if (namespaceURL == nil)
{
e = nil;
}
else
{
[extLock lock];
e = [[extDict objectForKey: namespaceURL] retain];
[extLock unlock];
}
return [e autorelease];
}
+ (void) registerExtensibility: (GWSExtensibility*)extensibility
forNamespace: (NSString*)namespaceURL
{
if (namespaceURL != nil)
{
[extLock lock];
if (extensibility == nil)
{
[extDict removeObjectForKey: namespaceURL];
}
else
{
[extDict setObject: extensibility forKey: namespaceURL];
}
[extLock unlock];
}
}
- (NSArray*) bindingNames
{
NSArray *result;
[_lock lock];
result = [_bindings allKeys];
[_lock unlock];
return result;
}
- (GWSBinding*) bindingWithName: (NSString*)name
create: (BOOL)shouldCreate
{
GWSBinding *binding;
name = [self _local: name];
[_lock lock];
binding = [_bindings objectForKey: name];
if (binding == nil && shouldCreate == YES)
{
binding = [[GWSBinding alloc] _initWithName: name document: self];
[_bindings setObject: binding forKey: name];
}
else
{
[binding retain];
}
[_lock unlock];
return [binding autorelease];
}
- (NSData*) data
{
NSAutoreleasePool *pool;
GWSElement *tree;
GWSCoder *coder;
NSData *data;
pool = [NSAutoreleasePool new];
tree = [self tree];
coder = [[GWSCoder new] autorelease];
[[coder mutableString]
appendString: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"];
[tree encodeWith: coder];
data = [[coder mutableString] dataUsingEncoding: NSUTF8StringEncoding];
[data retain];
[pool release];
return [data autorelease];
}
- (void) dealloc
{
NSEnumerator *e;
id o;
[_ext release];
[_name release];
[_prefix release];
[_targetNamespace release];
[_documentation release];
[_extensibility release];
e = [_portTypes objectEnumerator];
while ((o = [e nextObject]) != nil) [o _remove];
[_portTypes release];
e = [_bindings objectEnumerator];
while ((o = [e nextObject]) != nil) [o _remove];
[_bindings release];
e = [_services objectEnumerator];
while ((o = [e nextObject]) != nil) [o _remove];
[_services release];
e = [_messages objectEnumerator];
while ((o = [e nextObject]) != nil) [o _remove];
[_messages release];
e = [_types objectEnumerator];
while ((o = [e nextObject]) != nil) [o _remove];
[_types release];
[_namespaces release];
[_lock release];
[super dealloc];
}
- (GWSElement*) documentation
{
return _documentation;
}
- (GWSExtensibility*) extensibilityForNamespace: (NSString*)namespaceURL
{
return [_ext objectForKey: namespaceURL];
}
- (NSArray*) extensibility
{
NSArray *a;
[_lock lock];
a = [_extensibility copy];
[_lock unlock];
return [a autorelease];
}
- (id) init
{
if ((self = [super init]) != nil)
{
_lock = [NSRecursiveLock new];
_portTypes = [NSMutableDictionary new];
_bindings = [NSMutableDictionary new];
_services = [NSMutableDictionary new];
_messages = [NSMutableDictionary new];
_namespaces = [NSMutableDictionary new];
_types = [NSMutableDictionary new];
_extensibility = [NSMutableArray new];
[extLock lock];
_ext = [extDict copy];
[extLock unlock];
}
return self;
}
- (GWSElement*) initializing
{
return _elem;
}
- (id) initWithContentsOfFile: (NSString*)file
{
NSData *data = [NSData dataWithContentsOfFile: file];
return [self initWithData: data];
}
- (id) initWithContentsOfURL: (NSURL*)url
{
NSData *data = [NSData dataWithContentsOfURL: url];
return [self initWithData: data];
}
- (id) initWithData: (NSData*)xml
{
if ([xml length] == 0)
{
NSLog(@"No data passed to -initWithData:");
[self release];
self = nil;
}
else
{
NS_DURING
{
GWSCoder *parser;
GWSElement *root = nil;
parser = [[GWSCoder new] autorelease];
[parser setDebug: YES];
root = [parser parseXML: xml];
if (root == nil)
{
NSLog(@"Data could not be parsed as XML in -initWithData:");
[self release];
self = nil;
}
else
{
self = [self initWithTree: root];
}
}
NS_HANDLER
{
NSLog(@"Problem parsing WSDL ... %@", localException);
[self release];
self = nil;
}
NS_ENDHANDLER
}
return self;
}
- (id) initWithTree: (GWSElement*)tree
{
if (tree == nil)
{
[self release];
self = nil;
}
else if ((self = [self init]) != nil)
{
NS_DURING
{
NSString *name;
if ([[tree name] isEqualToString: @"definitions"] == NO)
{
[NSException raise: NSInvalidArgumentException
format: @"root element is '%@', not 'definitions'",
[tree name]];
}
else
{
NSDictionary *d;
NSEnumerator *e;
NSString *k;
k = [tree qualified];
[_prefix release];
if ([k isEqualToString: @"definitions"])
{
_prefix = nil;
}
else
{
_prefix = [[k substringToIndex:
[k rangeOfString: @":"].location] retain];
}
d = [tree attributes];
e = [d keyEnumerator];
while ((k = [e nextObject]) != nil)
{
if ([k isEqualToString: @"name"] == YES)
{
[_name release];
_name = [[d objectForKey: k] copy];
}
else if ([k isEqualToString: @"targetNamespace"] == YES)
{
[_targetNamespace release];
_targetNamespace = [[d objectForKey: k] copy];
}
else if ([k hasPrefix: @"xmlns:"] == YES)
{
[_namespaces setObject: [d objectForKey: k]
forKey: [k substringFromIndex: 6]];
}
else if ([k isEqualToString: @"xmlns"] == YES)
{
[_namespaces setObject: [d objectForKey: k]
forKey: @""];
}
else
{
NSLog(@"Unexpected attribute: %@", k);
}
}
/* Now parse the namespaces and their prefixes so we have
* those mappings available.
*/
d = [tree namespaces];
e = [d keyEnumerator];
while ((k = [e nextObject]) != nil)
{
[_namespaces setObject: [d objectForKey: k] forKey: k];
}
/* Make sure that we have a default namespace ... that of
* the WSDL schema.
*/
if ([_namespaces objectForKey: @""] == nil)
{
[_namespaces setObject: @"http://schemas.xmlsoap.org/wsdl/"
forKey: @""];
}
}
_elem = [tree firstChild];
/* Handle imports.
*/
while ([(name = [_elem name]) isEqualToString: @"import"])
{
NSLog(@"Argh ... 'import' not handled");
_elem = [_elem sibling];
}
/* Extract documentation.
*/
if ([(name = [_elem name]) isEqualToString: @"documentation"])
{
_documentation = [_elem retain];
_elem = [_elem sibling];
[_documentation remove];
}
/* Handle types.
*/
if ([(name = [_elem name]) isEqualToString: @"types"])
{
GWSElement *next = [_elem sibling];
_elem = [_elem firstChild];
if ([(name = [_elem name]) isEqualToString: @"documentation"])
{
_elem = [_elem sibling];
}
/* FIXME ... need to parse schema info etc for types.
*/
_elem = next;
}
while ([(name = [_elem name]) isEqualToString: @"message"])
{
GWSMessage *message;
name = [[_elem attributes] objectForKey: @"name"];
message = [[GWSMessage alloc] _initWithName: name
document: self];
if (message != nil)
{
[_messages setObject: message
forKey: [message name]];
[message release];
}
_elem = [_elem sibling];
}
while ([(name = [_elem name]) isEqualToString: @"portType"])
{
GWSPortType *portType;
name = [[_elem attributes] objectForKey: @"name"];
portType = [[GWSPortType alloc] _initWithName: name
document: self];
if (portType != nil)
{
[_portTypes setObject: portType
forKey: [portType name]];
[portType release];
}
_elem = [_elem sibling];
}
while ([(name = [_elem name]) isEqualToString: @"binding"])
{
GWSBinding *binding;
name = [[_elem attributes] objectForKey: @"name"];
binding = [[GWSBinding alloc] _initWithName: name
document: self];
if (binding != nil)
{
[_bindings setObject: binding
forKey: [binding name]];
[binding release];
}
_elem = [_elem sibling];
}
while ([(name = [_elem name]) isEqualToString: @"service"])
{
GWSService *service;
name = [[_elem attributes] objectForKey: @"name"];
service = [[GWSService alloc] _initWithName: name
document: self];
if (service != nil)
{
[_services setObject: service
forKey: [service name]];
[service release];
}
_elem = [_elem sibling];
}
while (_elem != nil)
{
NSString *problem;
problem = [self _validate: _elem in: self];
if (problem != nil)
{
[NSException raise: NSInvalidArgumentException
format: @"%@", problem];
}
[_extensibility addObject: _elem];
_elem = [_elem sibling];
[[_extensibility lastObject] remove];
}
}
NS_HANDLER
{
_elem = nil;
NSLog(@"Problem in -initWithTree: ... %@", localException);
[self release];
self = nil;
}
NS_ENDHANDLER
}
return self;
}
- (NSArray*) messageNames
{
NSArray *result;
[_lock lock];
result = [_messages allKeys];
[_lock unlock];
return result;
}
- (GWSMessage*) messageWithName: (NSString*)name
create: (BOOL)shouldCreate
{
GWSMessage *message;
name = [self _local: name];
[_lock lock];
message = [_messages objectForKey: name];
if (message == nil && shouldCreate == YES)
{
message = [[GWSMessage alloc] _initWithName: name document: self];
[_messages setObject: message forKey: name];
}
else
{
[message retain];
}
[_lock unlock];
return [message autorelease];
}
- (NSString*) name
{
return _name;
}
- (NSString*) namespaceForPrefix: (NSString*)prefix
{
if (prefix == nil) prefix = @"";
return [_namespaces objectForKey: prefix];
}
- (NSString*) namespacePrefix
{
return _prefix;
}
- (NSArray*) portTypeNames
{
NSArray *result;
[_lock lock];
result = [_portTypes allKeys];
[_lock unlock];
return result;
}
- (GWSPortType*) portTypeWithName: (NSString*)name
create: (BOOL)shouldCreate
{
GWSPortType *portType;
name = [self _local: name];
[_lock lock];
portType = [_portTypes objectForKey: name];
if (portType == nil && shouldCreate == YES)
{
portType = [[GWSPortType alloc] _initWithName: name document: self];
[_portTypes setObject: portType forKey: name];
}
else
{
[portType retain];
}
[_lock unlock];
return [portType autorelease];
}
- (NSString*) prefixForNamespace: (NSString*)url
{
NSEnumerator *e = [_namespaces keyEnumerator];
NSString *k;
while ((k = [e nextObject]) != nil)
{
if ([[_namespaces objectForKey: k] isEqual: url])
{
break;
}
}
return k;
}
- (void) removeBindingNamed: (NSString*)name
{
[_lock lock];
[[_bindings objectForKey: name] _remove];
[_bindings removeObjectForKey: name];
[_lock unlock];
}
- (void) removeMessageNamed: (NSString*)name
{
[_lock lock];
[[_bindings objectForKey: name] _remove];
[_messages removeObjectForKey: name];
[_lock unlock];
}
- (void) removePortTypeNamed: (NSString*)name
{
[_lock lock];
[[_bindings objectForKey: name] _remove];
[_portTypes removeObjectForKey: name];
[_lock unlock];
}
- (void) removeServiceNamed: (NSString*)name
{
[_lock lock];
[[_bindings objectForKey: name] _remove];
[_services removeObjectForKey: name];
[_lock unlock];
}
- (void) removeTypeNamed: (NSString*)name
{
[_lock lock];
[[_bindings objectForKey: name] _remove];
[_types removeObjectForKey: name];
[_lock unlock];
}
- (NSString*) qualify: (NSString*)name
{
if (_prefix != nil)
{
return [NSString stringWithFormat: @"%@:%@", _prefix, name];
}
return name;
}
- (NSArray*) serviceNames
{
NSArray *result;
[_lock lock];
result = [_services allKeys];
[_lock unlock];
return result;
}
- (GWSService*) serviceWithName: (NSString*)name
create: (BOOL)shouldCreate
{
GWSService *service;
name = [self _local: name];
[_lock lock];
service = [_services objectForKey: name];
if (service == nil && shouldCreate == YES)
{
service = [[GWSService alloc] _initWithName: name document: self];
[_services setObject: service forKey: name];
}
else
{
[service retain];
}
[_lock unlock];
return [service autorelease];
}
- (void) setDocumentation: (GWSElement*)documentation
{
if (_documentation != documentation)
{
id o = _documentation;
_documentation = [documentation retain];
[o release];
}
}
- (void) setExtensibility: (NSArray*)extensibility
{
NSMutableArray *m;
unsigned c;
c = [extensibility count];
while (c-- > 0)
{
NSString *problem;
GWSElement *element;
element = [extensibility objectAtIndex: c];
problem = [self _validate: element in: self];
if (problem != nil)
{
[NSException raise: NSInvalidArgumentException
format: @"%@", problem];
}
}
m = [extensibility mutableCopy];
[_lock lock];
[_extensibility release];
_extensibility = m;
[_lock unlock];
}
- (void) setName: (NSString*)name
{
if (_name != name)
{
id o = _name;
_name = [name copy];
[o release];
}
}
- (void) setTargetNamespace: (NSString*)uri
{
if (_targetNamespace != uri)
{
id o = _targetNamespace;
_targetNamespace = [uri copy];
[o release];
}
}
- (NSString*) targetNamespace
{
return _targetNamespace;
}
- (GWSElement*) tree
{
GWSElement *tree;
GWSElement *elem;
NSEnumerator *enumerator;
NSString *key;
NSString *uri;
key = @"";
uri = [_namespaces objectForKey: key];
if (uri == nil)
{
uri = @"http://schemas.xmlsoap.org/wsdl/";
}
tree = [[GWSElement alloc] initWithName: @"definitions"
namespace: uri
qualified: [self qualify: @"definitions"]
attributes: nil];
[tree autorelease];
if (_name != nil)
{
[tree setAttribute: _name forKey: @"name"];
}
if (_targetNamespace != nil)
{
[tree setAttribute: _targetNamespace forKey: @"targetNamespace"];
}
enumerator = [_namespaces keyEnumerator];
while ((key = [enumerator nextObject]) != nil)
{
if ([key length] > 0)
{
[tree setNamespace: [_namespaces objectForKey: key] forPrefix: key];
}
}
// FIXME ... imports
if (_documentation != nil)
{
[tree addChild: _documentation];
}
if ([_types count] > 0)
{
elem = [[GWSElement alloc] initWithName: @"types"
namespace: nil
qualified: @"types"
attributes: nil];
[tree addChild: elem];
[elem release];
enumerator = [_types keyEnumerator];
while ((key = [enumerator nextObject]) != nil)
{
[elem addChild: [[_types objectForKey: key] tree]];
}
}
enumerator = [_messages keyEnumerator];
while ((key = [enumerator nextObject]) != nil)
{
[tree addChild: [[_messages objectForKey: key] tree]];
}
enumerator = [_portTypes keyEnumerator];
while ((key = [enumerator nextObject]) != nil)
{
[tree addChild: [[_portTypes objectForKey: key] tree]];
}
enumerator = [_bindings keyEnumerator];
while ((key = [enumerator nextObject]) != nil)
{
[tree addChild: [[_bindings objectForKey: key] tree]];
}
enumerator = [_services keyEnumerator];
while ((key = [enumerator nextObject]) != nil)
{
[tree addChild: [[_services objectForKey: key] tree]];
}
enumerator = [_extensibility objectEnumerator];
while ((elem = [enumerator nextObject]) != nil)
{
[tree addChild: elem];
}
return tree;
}
- (NSArray*) typeNames
{
NSArray *result;
[_lock lock];
result = [_types allKeys];
[_lock unlock];
return result;
}
- (GWSType*) typeWithName: (NSString*)name
create: (BOOL)shouldCreate
{
GWSType *type;
name = [self _local: name];
[_lock lock];
type = [_types objectForKey: name];
if (type == nil && shouldCreate == YES)
{
type = [[GWSType alloc] _initWithName: name document: self];
[_types setObject: type forKey: name];
}
else
{
[type retain];
}
[_lock unlock];
return [type autorelease];
}
- (BOOL) writeToFile: (NSString*)file atomically: (BOOL)atomically
{
return [[self data] writeToFile: file atomically: atomically];
}
- (BOOL) writeToURL: (NSURL*)anURL atomically: (BOOL)atomically
{
return [[self data] writeToURL: anURL atomically: atomically];
}
@end