Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2ba499ce3feabaa142248b497a0f0527ef0e9043 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.wsdl.internal.generator;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;

import org.eclipse.wst.wsdl.Binding;
import org.eclipse.wst.wsdl.BindingFault;
import org.eclipse.wst.wsdl.BindingInput;
import org.eclipse.wst.wsdl.BindingOperation;
import org.eclipse.wst.wsdl.BindingOutput;
import org.eclipse.wst.wsdl.Definition;
import org.eclipse.wst.wsdl.ExtensibilityElement;
import org.eclipse.wst.wsdl.ExtensibleElement;
import org.eclipse.wst.wsdl.Fault;
import org.eclipse.wst.wsdl.Input;
import org.eclipse.wst.wsdl.Operation;
import org.eclipse.wst.wsdl.Output;
import org.eclipse.wst.wsdl.Port;
import org.eclipse.wst.wsdl.PortType;
import org.eclipse.wst.wsdl.Service;
import org.eclipse.wst.wsdl.WSDLFactory;
import org.eclipse.wst.wsdl.internal.generator.extension.ContentGeneratorExtensionFactoryRegistry;

/*
 * Class used to generate the Binding and it's content.  We look in the registry for
 * appropriate ContentGenerator classes based on the existing Binding Content's namespace.
 * Alternatively, users can pass in a namespace through the constructor to specify which
 * namespace to use when searching the registry.
 * 
 * The ContentGenerator may also be set manually by calling
 * setContentGenerator(ContentGenerator).
 */
public class BindingGenerator extends BaseGenerator {
	private WSDLFactory factory = WSDLFactory.eINSTANCE;
	private Binding binding;

	/*
	 * When the constructor is used, automatically attempt to retrieve a proper
	 * ContentGenerator based on the Binding given.  The ContentGenerator may
	 * be replaced by calling setContentGenerator(ContentGenerator).
	 */
	public BindingGenerator(Definition definition, Binding binding) {	
		this.definition = definition;
		this.binding = binding;
		contentGenerator = getContentGenerator(binding);
	}

	/*
	 * When the constructor is used, automatically attempt to retrieve a proper
	 * ContentGenerator based on the namespace given.  The ContentGenerator may
	 * be replaced by calling setContentGenerator(ContentGenerator).
	 */
	public BindingGenerator(Definition definition, Binding binding, String namespace) {
		this.definition = definition;
		this.binding = binding;
		contentGenerator = getContentGenerator(namespace);
	}

	public static ContentGenerator getContentGenerator(Binding binding) {
		if (binding == null) {
			return null;
		}
		/******************************************************
		 * Find the regeistered Content Generator for the Binding
		 ******************************************************/
		// Get BindingContent namespace
		String namespace = null;
		List eeList = binding.getEExtensibilityElements();
		if (eeList.size() > 0) {
	        ExtensibilityElement ee = (ExtensibilityElement) eeList.get(0);			
			// TODO: QName qName = ee.getElementType(); go get the namespace instead?
	        namespace = ee.getElement().getNamespaceURI();
			return getContentGenerator(namespace);
		}
	
		return null;
	}
	
	public static ContentGenerator getContentGenerator(String namespace) {
		ContentGenerator contentGen = null;
		if (namespace != null) {
			ContentGeneratorExtensionFactoryRegistry factoryRegistry = ContentGeneratorExtensionFactoryRegistry.getInstance(); 
			contentGen = factoryRegistry.getGeneratorClassFromNamespace(namespace);
		}
		
		return contentGen;		
	}
	
	private Binding createEmptyBinding(String localName) {
	    Binding newBinding = WSDLFactory.eINSTANCE.createBinding();
		newBinding.setQName(new QName(definition.getTargetNamespace(),localName));
		newBinding.setEnclosingDefinition(definition);
	    definition.addBinding(newBinding);
		
		return newBinding;
	}

	/*
	 * TODO: Scenario:
	 * 1)overwrite == false
	 * 2)BindingOperation with 1 input
	 * 3)Corresponding Operation (with same name) with 1 input and 1 output
	 * If we generate with overwrite == false, nothing is done.  Thus,  an
	 * output is not generated on the BindingOperation.  This is because we
	 * search for existing Elements only at the level of BindingOperations.
	 * For example, if there is a corresponding BindingOperation with the same
	 * name as our Operation, leave it alone.... but since there is already
	 * a BindingOperation with the same name, we don't create a new BindingOperation.
	 * 
	 * The correct implementation is reduce this granularity to the MessageReference
	 * level.  The code is almost there except for how we generate the Binding element
	 * content.  Look at BindingGenrator.generateBindingOperation() and
	 * SOAPContentGenerator.java and it's content generation method
	 * for a good place to start.
	 * 
	 * For wtp RC1, We shall only look at the Operation level (as we do in the previous
	 * version).
	 */
	public Binding generateBinding() {
		try {
		// If Binding is null (No Binding was passed into the constructor), we create an empty Binding first.
		if (binding == null) {
			binding = createEmptyBinding(getName());
		}
		if (getName() != null && !binding.getQName().getLocalPart().equals(getName())) {
			binding.setQName(new QName(binding.getQName().getNamespaceURI(), getName()));
		}
		if (getRefName() != null) {
			PortType portType = getPortType();
			binding.setEPortType(portType);
			if (portType == null) {
				//The model doesn't reconile with it's Element properly when we're setting a null for it's PortType
				binding.getElement().setAttribute("type", "");
			}
		}

		List bindingOperations = binding.getEBindingOperations();		
		PortType portType = binding.getEPortType();
		
		if (!getOverwrite()) {
			// Don't Overwrite
			if (portType == null) {
				return binding;
			}
			else {
				addRequiredNamespaces(binding);				
				List operations = portType.getOperations();
				
				/*******************************************************************************
				 * Determine which Operations require new a new corresponding BindingOperations
				 *******************************************************************************/
				List newBindingOpsNeeded = new ArrayList();
				for (int index = 0; index < operations.size(); index++) {
					Operation operation = (Operation) operations.get(index);
					
					boolean foundMatch = false;
					Iterator bindingOperationsIt = bindingOperations.iterator();
					while (bindingOperationsIt.hasNext()) {
						BindingOperation bindingOperation = (BindingOperation) bindingOperationsIt.next();
						
						if (namesEqual(bindingOperation.getName(), operation.getName())) {
							foundMatch = true;
							break;
						}
					}
					
					if (!foundMatch){
						newBindingOpsNeeded.add(operation);
					}
				}
				// newBindingOpsNeeded is the List of Operations needing new corresponding
				// BindingOperation's
				List newBindingOps = createNewBindingOperations(newBindingOpsNeeded);
				
				// Generate the contents of the new BindingOperation's
				Iterator newBindingOpsIt = newBindingOps.iterator();
				while (newBindingOpsIt.hasNext()) {
					BindingOperation newBindingOp = (BindingOperation) newBindingOpsIt.next();
					generateBindingOperation(newBindingOp);
					generateBindingOperationContent(newBindingOp);
				}
			}
			
			generateBindingContent(binding);	
		}
		else {
			// Overwrite
			if (portType == null) {
				// We need to blow away everything under the Binding.  No PortType associated with this Binding
				bindingOperations.clear();
				return binding;
			}
			else {
				addRequiredNamespaces(binding);				
				List operations = portType.getOperations();
				
				/******************************************************
				 * Compare the Operations
				 ******************************************************/
				// Remove any BindingOperations which are no longer used
				for (int index = 0; index < bindingOperations.size(); index++) {
					BindingOperation bindingOperation = (BindingOperation) bindingOperations.get(index);
					
					boolean foundMatch = false;
					Iterator operationsIt = operations.iterator();
					while (operationsIt.hasNext()) {
						Operation operation = (Operation) operationsIt.next();
						
						if (namesEqual(bindingOperation.getName(), operation.getName())) {
							foundMatch = true;
							break;
						}
					}
					
					if (!foundMatch){
						// We need to remove this BindingFault from the bindingFaults List
						bindingOperations.remove(index);
						index--;
					}			
				}
				
				// Remove any Operations which already exists in bindingOperations.  What we
				// have left are the Operations which needs newly created BindingOperations
				List bindingOperationsNeeded = new ArrayList();
				for (int index = 0; index < operations.size(); index++) {
					Operation operation = (Operation) operations.get(index);
					
					boolean foundMatch = false;
					Iterator bindingOperationsIt = bindingOperations.iterator();
					while (bindingOperationsIt.hasNext()) {
						BindingOperation bindingOperation = (BindingOperation) bindingOperationsIt.next();
						
						if (namesEqual(bindingOperation.getName(), operation.getName())) {
							foundMatch = true;
							break;
						}
					}
					
					if (!foundMatch){
						// We need to remove this BindingFault from the bindingFaults List
						bindingOperationsNeeded.add(operation); // Store the actual Operation
					}			
				}		
				
				// Create required BindingOperations
				createNewBindingOperations(bindingOperationsNeeded);

				/******************************************************
				 * Process the contents of the Operations
				 ******************************************************/
				Iterator bindingOperationsIt = binding.getEBindingOperations().iterator();
				while (bindingOperationsIt.hasNext()) {
					generateBindingOperation((BindingOperation) bindingOperationsIt.next());
				}
				
				generateBindingContent(binding);
				
				return binding;
			}			
		}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private List createNewBindingOperations(List operations) {
		List newBindingOps = new ArrayList();
		
		Iterator neededBindingOperationsIt = operations.iterator();
		while (neededBindingOperationsIt.hasNext()) {
			Operation operation = (Operation) neededBindingOperationsIt.next();
			BindingOperation newBindingOperation = factory.createBindingOperation();
			newBindingOperation.setEOperation(operation);
			newBindingOperation.setName(operation.getName());
			binding.addBindingOperation(newBindingOperation);
			
			newBindingOps.add(newBindingOperation);
		}
		
		return newBindingOps;
	}
	
	private void generateBindingOperation(BindingOperation bindingOperation) {
		BindingInput bindingInput = bindingOperation.getEBindingInput();
		BindingOutput bindingOutput = bindingOperation.getEBindingOutput();
		List bindingFaults = bindingOperation.getEBindingFaults();
		
		Operation operation = bindingOperation.getEOperation();
		Input input = operation.getEInput();
		Output output = operation.getEOutput();
		List faults = operation.getEFaults();

		/******************************************************
		 * Compare the Operation names
		 ******************************************************/		
		if (!namesEqual(bindingOperation.getName(), operation.getName())) {
			bindingOperation.setName(operation.getName());
		}
		
		
		/******************************************************
		 * Compare the Output
		 ******************************************************/
		if (output == null) {
			bindingOperation.setBindingOutput(null);			
		}
		else {
			// Create BindingOutput if necessary
			if (bindingOutput == null) {
				BindingOutput newBindingOutput = factory.createBindingOutput();
				newBindingOutput.setEOutput(output);
				newBindingOutput.setName(output.getName());
				bindingOperation.setBindingOutput(newBindingOutput);				
			}
			else {
				// Compare the Output names
				if (!namesEqual(bindingOutput.getName(), output.getName())) {
					bindingOutput.setName(output.getName());
				}				
			}
		}
		generateBindingOutputContent(bindingOperation.getEBindingOutput());
		
		/******************************************************
		 * Compare the Input
		 ******************************************************/
		if (input == null) {
			bindingOperation.setBindingInput(null);			
		}
		else {
			// Create BindingInput if necessary
			if (bindingInput == null) {
				BindingInput newBindingInput = factory.createBindingInput();
				newBindingInput.setEInput(input);
				newBindingInput.setName(input.getName());
				bindingOperation.setBindingInput(newBindingInput);
			}
			else {
				// Compare the Input names
				if (!namesEqual(bindingInput.getName(), input.getName())) {
					bindingInput.setName(input.getName());
				}
			}
		}
		generateBindingInputContent(bindingOperation.getEBindingInput());
		
		
		/******************************************************
		 * Compare the Faults
		 ******************************************************/
		// Remove any BindingFaults which are no longer used
		for (int index = 0; index < bindingFaults.size(); index++) {
			BindingFault bindingFault = (BindingFault) bindingFaults.get(index);
			
			boolean foundMatch = false;
			Iterator faultsIt = faults.iterator();
			while (faultsIt.hasNext()) {
				Fault fault = (Fault) faultsIt.next();
				if (namesEqual(bindingFault.getName(), fault.getName())) {
					foundMatch = true;
					break;
				}
			}
			
			if (!foundMatch){
				// We need to remove this BindingFault from the bindingFaults List
				bindingFaults.remove(index);
				index--;
			}			
		}
		
		// Remove any Faults which already exists in bindingFaults.  What we
		// have left are the Faults which needs newly created BindingFaults
		List bindingFaultsNeeded = new ArrayList();
		for (int index = 0; index < faults.size(); index++) {
			Fault fault = (Fault) faults.get(index);
			
			boolean foundMatch = false;
			Iterator bindingFaultsIt = bindingFaults.iterator();
			while (bindingFaultsIt.hasNext()) {
				BindingFault bindingFault = (BindingFault) bindingFaultsIt.next();
				if (namesEqual(bindingFault.getName(), fault.getName())) {
					foundMatch = true;
					break;
				}
			}
			
			if (!foundMatch){
				// We need to remove this BindingFault from the bindingFaults List
				bindingFaultsNeeded.add(fault);
			}			
		}		
		
		// bindingFaultsNeeded contains the needed BindingFault's we need to create
		Iterator neededBindingFaultsIt = bindingFaultsNeeded.iterator();
		while (neededBindingFaultsIt.hasNext()) {
			Fault fault = (Fault) neededBindingFaultsIt.next();
			BindingFault newBindingFault = factory.createBindingFault();
			newBindingFault.setEFault(fault);
			newBindingFault.setName(fault.getName());
			bindingOperation.addBindingFault(newBindingFault);
		}
		
		// Create the contents for each BindingFault
		Iterator faultContentIt = bindingOperation.getEBindingFaults().iterator();
		while (faultContentIt.hasNext()) {
			BindingFault bindingFault = (BindingFault) faultContentIt.next();
			generateBindingFaultContent(bindingFault);
		}
		
		generateBindingOperationContent(bindingOperation);
	}
	
	private boolean namesEqual(String name1, String name2) {
		boolean match = false;
		
		if (name1 != null ^ name2 != null) {
			// one is null
			match = false;
		}
		else if (name1 != null && name2 != null) {
			// neither is null
			match = name1.equals(name2);
		}
		else {
			// both are null
			match = true;
		}		
		
		return match;
	}

	protected void generateBindingContent(Binding binding) {
		if (contentGenerator != null) {
			if (getOverwrite() || binding.getEExtensibilityElements().size() == 0) {
				contentGenerator.generateBindingContent(binding, (PortType) binding.getEPortType());
			}
		}
		else {
			removeExtensibilityElements(binding);
		}
	}

	protected void generateBindingOperationContent(BindingOperation bindingOperation) {
		if (bindingOperation != null && contentGenerator != null) {
			contentGenerator.generateBindingOperationContent(bindingOperation, bindingOperation.getEOperation());
		}
		else {
			removeExtensibilityElements(bindingOperation);
		}
	}
	
	protected void generateBindingInputContent(BindingInput bindingInput) {
		if (bindingInput != null && contentGenerator != null) {
			contentGenerator.generateBindingInputContent(bindingInput, bindingInput.getEInput());
		}
		else {
			removeExtensibilityElements(bindingInput);
		}
	}
	
	protected void generateBindingOutputContent(BindingOutput bindingOutput) {
		if (bindingOutput != null && contentGenerator != null) {
			contentGenerator.generateBindingOutputContent(bindingOutput, bindingOutput.getEOutput());
		}
		else {
			removeExtensibilityElements(bindingOutput);
		}
	}

	protected void generateBindingFaultContent(BindingFault bindingFault) {
		if (bindingFault != null && contentGenerator != null) {
			contentGenerator.generateBindingFaultContent(bindingFault, bindingFault.getEFault());
		}
		else {
			removeExtensibilityElements(bindingFault);
		}
	}
	
	private void removeExtensibilityElements(ExtensibleElement ee) {
		if (ee != null) {
			ee.getEExtensibilityElements().clear();
		}
	}
	
	/*
	 * Generate Port Content for all Ports with a reference to the Binding
	 * which was passed in through the constructor (or a reference to the
	 * newly created Binding).
	 */
	public void generatePortContent() {
		if (binding != null && contentGenerator != null) {
			List portsToUpdate = new ArrayList();
			
			Iterator servicesIt = binding.getEnclosingDefinition().getEServices().iterator();
			while (servicesIt.hasNext()) {
				Service service = (Service) servicesIt.next();
				Iterator portsIt = service.getEPorts().iterator();
				while (portsIt.hasNext()) {
					Port port = (Port) portsIt.next();
					if (binding.equals(port.getEBinding())) {
						// Found a match
						portsToUpdate.add(port);
					}
				}
			}
			
			Iterator portsToUpdateIt = portsToUpdate.iterator();
			while (portsToUpdateIt.hasNext()) {
				contentGenerator.generatePortContent((Port) portsToUpdateIt.next());
			}
		}	
	}
	
	/*
	 * methods addRequiredNamespaces() and computeUniquePrefix() are used to add necessary
	 * namespaces 
	 * 
	 * TODO:
	 * Does this belong here?  This is copied from wsdl.ui.  Can we sync up in some way?
	 */
	protected void addRequiredNamespaces(Binding binding) {
		if (contentGenerator != null) {
			String[] namespaceNames = contentGenerator.getRequiredNamespaces();
			String[] preferredPrefixes = new String[namespaceNames.length];
			for (int index = 0; index < namespaceNames.length; index++) {
				preferredPrefixes[index] = contentGenerator.getPreferredNamespacePrefix(namespaceNames[index]);				
			}
	
			Map map = binding.getEnclosingDefinition().getNamespaces();
			
		      for (int i = 0; i < namespaceNames.length; i++) {
		        String namespace = namespaceNames[i];
		        if (!map.containsValue(namespace)) {
		          String prefix = (i < preferredPrefixes.length) ? preferredPrefixes[i] : "p0";
		          if (map.containsKey(prefix)) {
		            prefix = computeUniquePrefix("p", map);
		          }
				  binding.getEnclosingDefinition().addNamespace(prefix, namespace);
		        }
		      }
		}
	}
	
	private String computeUniquePrefix(String base, Map table){
	    int i = 0;
	    String prefix = base;
	    while (true) {
	      if (!table.containsKey(prefix)) {
	        break;
	      }
	      else {
	        prefix = base + i;
	        i++;
	      }
	    }
	    return prefix;
	}
	
	private PortType getPortType() {
		if (getRefName().equals("")) {
			// Means we should set the PortType to Unspecified
			return null;
		}
		
		if (getRefName() != null) {
			Iterator portTypeIt = definition.getEPortTypes().iterator();			

			while (portTypeIt.hasNext()) {
				PortType pt = (PortType) portTypeIt.next();
				List prefixedNames = getPrefixedNames(pt);
				if (prefixedNames.contains(getRefName())) {
					return pt;
				}
			}
		}
		
		return binding.getEPortType();
	}
	
	private List getPrefixedNames(PortType portType) {
		List prefixedNames = new ArrayList();
		String currentPortTypeName = portType.getQName().getLocalPart();
		String currentNamespace = portType.getQName().getNamespaceURI();

		Map namespaceMap = definition.getNamespaces();
		Iterator keys = namespaceMap.keySet().iterator();
		while (keys.hasNext()) {
			Object key = keys.next();
			Object value = namespaceMap.get(key);

			if (currentNamespace.equals(value)) {
				// Found a match.  Add to our list
				prefixedNames.add(key + ":" + currentPortTypeName);
			}
		}
		
		return prefixedNames;
		
	}
}

Back to the top