2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.xml.wsdl.model.impl;
44
import org.netbeans.modules.xml.wsdl.model.*;
45
import org.netbeans.modules.xml.wsdl.model.spi.GenericExtensibilityElement;
46
import org.netbeans.modules.xml.wsdl.model.visitor.WSDLVisitor;
47
import org.netbeans.modules.xml.xam.AbstractComponent;
48
import org.netbeans.modules.xml.xam.Component;
49
import org.netbeans.modules.xml.xam.ComponentUpdater;
50
import org.netbeans.modules.xml.xam.dom.DocumentComponent;
53
* Visitor to add or remove a child of a WSDL component.
56
public class ChildComponentUpdateVisitor<T extends WSDLComponent> implements WSDLVisitor, ComponentUpdater<T> {
58
private Operation operation;
59
private WSDLComponent parent;
61
private boolean canAdd = false;
64
* Creates a new instance of ChildComponentUpdateVisitor
66
public ChildComponentUpdateVisitor() {
69
public boolean canAdd(WSDLComponent target, Component child) {
70
if (!(child instanceof WSDLComponent)) return false;
71
update(target, (WSDLComponent) child, null);
75
public void update(WSDLComponent target, WSDLComponent child, Operation operation) {
76
update(target, child, -1, operation);
79
public void update(WSDLComponent target, WSDLComponent child, int index, Operation operation) {
80
assert target != null;
84
this.operation = operation;
89
private void addChild(String eventName, DocumentComponent child) {
90
((AbstractComponent) parent).insertAtIndex(eventName, child, index);
93
private void removeChild(String eventName, DocumentComponent child) {
94
((AbstractComponent) parent).removeChild(eventName, child);
97
public void visit(Definitions child) {
98
checkOperationOnUnmatchedParent();
101
public void visit(Types child) {
102
if (parent instanceof Definitions) {
103
if (operation == Operation.ADD) {
104
addChild(Definitions.TYPES_PROPERTY, child);
105
} else if (operation == Operation.REMOVE) {
106
removeChild(Definitions.TYPES_PROPERTY, child);
107
} else if (operation == null) {
111
checkOperationOnUnmatchedParent();
115
public void visit(Binding child) {
116
if (parent instanceof Definitions) {
117
Definitions target = (Definitions)parent;
118
if (operation == Operation.ADD) {
119
addChild(target.BINDING_PROPERTY, child);
120
} else if (operation == Operation.REMOVE) {
121
target.removeBinding(child);
122
} else if (operation == null) {
126
checkOperationOnUnmatchedParent();
130
public void visit(Message child) {
131
if (parent instanceof Definitions) {
132
Definitions target = (Definitions)parent;
133
if (operation == Operation.ADD) {
134
addChild(target.MESSAGE_PROPERTY, child);
135
} else if (operation == Operation.REMOVE) {
136
target.removeMessage(child);
137
} else if (operation == null) {
141
checkOperationOnUnmatchedParent();
145
public void visit(Service child) {
146
if (parent instanceof Definitions) {
147
Definitions target = (Definitions)parent;
148
if (operation == Operation.ADD) {
149
addChild(target.SERVICE_PROPERTY, child);
150
} else if (operation == Operation.REMOVE) {
151
target.removeService(child);
152
} else if (operation == null) {
156
checkOperationOnUnmatchedParent();
160
public void visit(PortType child) {
161
if (parent instanceof Definitions) {
162
Definitions target = (Definitions)parent;
163
if (operation == Operation.ADD) {
164
addChild(target.PORT_TYPE_PROPERTY, child);
165
} else if (operation == Operation.REMOVE) {
166
target.removePortType(child);
167
} else if (operation == null) {
171
checkOperationOnUnmatchedParent();
175
public void visit(Import child) {
176
if (parent instanceof Definitions) {
177
Definitions target = (Definitions)parent;
178
if (operation == Operation.ADD) {
179
addChild(target.IMPORT_PROPERTY, child);
180
} else if (operation == Operation.REMOVE) {
181
target.removeImport(child);
182
} else if (operation == null) {
186
checkOperationOnUnmatchedParent();
190
public void visit(Port child) {
191
if (parent instanceof Service) {
192
Service target = (Service)parent;
193
if (operation == Operation.ADD) {
194
addChild(target.PORT_PROPERTY, child);
195
} else if (operation == Operation.REMOVE) {
196
target.removePort(child);
197
} else if (operation == null) {
201
checkOperationOnUnmatchedParent();
205
public void visit(BindingOperation child) {
206
if (parent instanceof Binding) {
207
Binding target = (Binding)parent;
208
if (operation == Operation.ADD) {
209
addChild(target.BINDING_OPERATION_PROPERTY, child);
210
} else if (operation == Operation.REMOVE) {
211
target.removeBindingOperation(child);
212
} else if (operation == null) {
216
checkOperationOnUnmatchedParent();
220
public void visit(BindingInput child) {
221
if (parent instanceof BindingOperation) {
222
if (operation == Operation.ADD) {
223
addChild(BindingOperation.BINDING_INPUT_PROPERTY, child);
224
} else if (operation == Operation.REMOVE) {
225
removeChild(BindingOperation.BINDING_INPUT_PROPERTY, child);
226
} else if (operation == null) {
230
checkOperationOnUnmatchedParent();
234
public void visit(BindingOutput child) {
235
if (parent instanceof BindingOperation) {
236
if (operation == Operation.ADD) {
237
addChild(BindingOperation.BINDING_OUTPUT_PROPERTY, child);
238
} else if (operation == Operation.REMOVE) {
239
removeChild(BindingOperation.BINDING_OUTPUT_PROPERTY, child);
240
} else if (operation == null) {
244
checkOperationOnUnmatchedParent();
248
public void visit(BindingFault child) {
249
if (parent instanceof BindingOperation) {
250
BindingOperation target = (BindingOperation)parent;
251
if (operation == Operation.ADD) {
252
addChild(BindingOperation.BINDING_FAULT_PROPERTY, child);
253
} else if (operation == Operation.REMOVE) {
254
target.removeBindingFault(child);
255
} else if (operation == null) {
259
checkOperationOnUnmatchedParent();
263
public void visit(Part child) {
264
if (parent instanceof Message) {
265
Message target = (Message)parent;
266
if (operation == Operation.ADD) {
267
addChild(Message.PART_PROPERTY, child);
268
} else if (operation == Operation.REMOVE) {
269
target.removePart(child);
270
} else if (operation == null) {
274
checkOperationOnUnmatchedParent();
278
public void visit(Documentation doc) {
279
if (operation == Operation.ADD) {
280
addChild(WSDLComponent.DOCUMENTATION_PROPERTY, doc);
281
} else if (operation == Operation.REMOVE) {
282
removeChild(WSDLComponent.DOCUMENTATION_PROPERTY, doc);
283
} else if (operation == null) {
288
public void visit(Output child) {
289
if (parent instanceof RequestResponseOperation ||
290
parent instanceof SolicitResponseOperation ||
291
parent instanceof NotificationOperation)
293
if (operation == Operation.ADD) {
294
addChild(org.netbeans.modules.xml.wsdl.model.Operation.OUTPUT_PROPERTY, child);
295
} else if (operation == Operation.REMOVE) {
296
removeChild(org.netbeans.modules.xml.wsdl.model.Operation.OUTPUT_PROPERTY, child);
297
} else if (operation == null) {
301
checkOperationOnUnmatchedParent();
305
public void visit(Input child) {
306
if (parent instanceof OneWayOperation ||
307
parent instanceof RequestResponseOperation ||
308
parent instanceof SolicitResponseOperation)
310
if (operation == Operation.ADD) {
311
addChild(org.netbeans.modules.xml.wsdl.model.Operation.INPUT_PROPERTY, child);
312
} else if (operation == Operation.REMOVE) {
313
removeChild(org.netbeans.modules.xml.wsdl.model.Operation.INPUT_PROPERTY, child);
314
} else if (operation == null) {
318
checkOperationOnUnmatchedParent();
322
public void visit(Fault child) {
323
if (parent instanceof org.netbeans.modules.xml.wsdl.model.Operation) {
324
org.netbeans.modules.xml.wsdl.model.Operation target =
325
(org.netbeans.modules.xml.wsdl.model.Operation)parent;
326
boolean operationWithFaults =
327
parent instanceof RequestResponseOperation ||
328
parent instanceof SolicitResponseOperation;
330
if (operationWithFaults && operation == Operation.ADD) {
331
addChild(target.FAULT_PROPERTY, child);
332
} else if (operation == Operation.REMOVE) {
333
target.removeFault(child);
334
} else if (operation == null) {
335
canAdd = operationWithFaults;
338
checkOperationOnUnmatchedParent();
342
private void update(org.netbeans.modules.xml.wsdl.model.Operation child) {
343
if (parent instanceof PortType) {
344
PortType target = (PortType)parent;
345
if (operation == Operation.ADD) {
346
addChild(target.OPERATION_PROPERTY, child);
347
} else if (operation == Operation.REMOVE) {
348
target.removeOperation(child);
349
} else if (operation == null) {
353
checkOperationOnUnmatchedParent();
357
public void visit(NotificationOperation child) {
361
public void visit(SolicitResponseOperation child) {
365
public void visit(RequestResponseOperation child) {
369
public void visit(OneWayOperation child) {
373
public void visit(ExtensibilityElement child) {
374
if (parent instanceof ExtensibilityElement.UpdaterProvider) {
375
ExtensibilityElement.UpdaterProvider target = (ExtensibilityElement.UpdaterProvider) parent;
376
ComponentUpdater<ExtensibilityElement> updater = target.getComponentUpdater();
377
if (operation != null) {
378
updater.update(target, child, index, operation);
381
if (updater instanceof ComponentUpdater.Query) {
382
canAdd = ((ComponentUpdater.Query) updater).canAdd(target, child);
386
if (operation == Operation.ADD) {
387
parent.addExtensibilityElement(child);
388
} else if (operation == Operation.REMOVE) {
389
parent.removeExtensibilityElement(child);
390
} else if (operation == null) {
392
if (child instanceof ExtensibilityElement.ParentSelector) {
393
canAdd = ((ExtensibilityElement.ParentSelector)child).canBeAddedTo(parent);
399
private void checkOperationOnUnmatchedParent() {
400
if (operation != null) {
401
// note this unmatch should be caught by validation,
402
// we don't want the UI view to go blank on invalid but still well-formed document
403
//throw new IllegalArgumentException("Unmatched parent-child components"); //NO18N