~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to xml/wsdl/api/src/org/netbeans/modules/xml/wsdl/model/impl/ChildComponentUpdateVisitor.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.xml.wsdl.model.impl;
 
43
 
 
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;
 
51
 
 
52
/**
 
53
 * Visitor to add or remove a child of a WSDL component.
 
54
 * @author Nam Nguyen
 
55
 */
 
56
public class ChildComponentUpdateVisitor<T extends WSDLComponent> implements WSDLVisitor, ComponentUpdater<T> {
 
57
    
 
58
    private Operation operation;
 
59
    private WSDLComponent parent;
 
60
    private int index;
 
61
    private boolean canAdd = false;
 
62
    
 
63
    /**
 
64
     * Creates a new instance of ChildComponentUpdateVisitor
 
65
     */
 
66
    public ChildComponentUpdateVisitor() {
 
67
    }
 
68
    
 
69
    public boolean canAdd(WSDLComponent target, Component child) {
 
70
        if (!(child instanceof WSDLComponent)) return false;
 
71
        update(target, (WSDLComponent) child, null);
 
72
        return canAdd;
 
73
    }
 
74
    
 
75
    public void update(WSDLComponent target, WSDLComponent child, Operation operation) {
 
76
        update(target, child, -1, operation);
 
77
    }
 
78
    
 
79
    public void update(WSDLComponent target, WSDLComponent child, int index, Operation operation) {
 
80
        assert target != null;
 
81
        assert child != null;
 
82
 
 
83
        this.parent = target;
 
84
        this.operation = operation;
 
85
        this.index = index;
 
86
        child.accept(this);
 
87
    }
 
88
    
 
89
    private void addChild(String eventName, DocumentComponent child) {
 
90
        ((AbstractComponent) parent).insertAtIndex(eventName, child, index);
 
91
    }
 
92
    
 
93
    private void removeChild(String eventName, DocumentComponent child) {
 
94
        ((AbstractComponent) parent).removeChild(eventName, child);
 
95
    }
 
96
    
 
97
    public void visit(Definitions child) {
 
98
        checkOperationOnUnmatchedParent();
 
99
    }
 
100
 
 
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) {
 
108
                canAdd = true;
 
109
            }
 
110
        } else {
 
111
            checkOperationOnUnmatchedParent();
 
112
        }
 
113
    }
 
114
    
 
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) {
 
123
                canAdd = true;
 
124
            }
 
125
        } else {
 
126
            checkOperationOnUnmatchedParent();
 
127
        }
 
128
    }
 
129
    
 
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) {
 
138
                canAdd = true;
 
139
            }
 
140
        } else {
 
141
            checkOperationOnUnmatchedParent();
 
142
        }
 
143
    }
 
144
    
 
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) {
 
153
                canAdd = true;
 
154
            }
 
155
        } else {
 
156
            checkOperationOnUnmatchedParent();
 
157
        }
 
158
    }
 
159
    
 
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) {
 
168
                canAdd = true;
 
169
            }
 
170
        } else {
 
171
            checkOperationOnUnmatchedParent();
 
172
        }
 
173
    }
 
174
    
 
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) {
 
183
                canAdd = true;
 
184
            }
 
185
        } else {
 
186
            checkOperationOnUnmatchedParent();
 
187
        }
 
188
    }
 
189
    
 
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) {
 
198
                canAdd = true;
 
199
            }
 
200
        } else {
 
201
            checkOperationOnUnmatchedParent();
 
202
        }
 
203
    }
 
204
    
 
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) {
 
213
                canAdd = true;
 
214
            }
 
215
        } else {
 
216
            checkOperationOnUnmatchedParent();
 
217
        }
 
218
    }
 
219
    
 
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) {
 
227
                canAdd = true;
 
228
            }
 
229
        } else {
 
230
            checkOperationOnUnmatchedParent();
 
231
        }
 
232
    }
 
233
    
 
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) {
 
241
                canAdd = true;
 
242
            }
 
243
        } else {
 
244
            checkOperationOnUnmatchedParent();
 
245
        }
 
246
    }
 
247
    
 
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) {
 
256
                canAdd = true;
 
257
            }
 
258
        } else {
 
259
            checkOperationOnUnmatchedParent();
 
260
        }
 
261
    }
 
262
    
 
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) {
 
271
                canAdd = true;
 
272
            }
 
273
        } else {
 
274
            checkOperationOnUnmatchedParent();
 
275
        }
 
276
    }
 
277
    
 
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) {
 
284
            canAdd = true;
 
285
        }
 
286
    }
 
287
    
 
288
    public void visit(Output child) {
 
289
        if (parent instanceof RequestResponseOperation || 
 
290
            parent instanceof SolicitResponseOperation ||
 
291
            parent instanceof NotificationOperation) 
 
292
        {
 
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) {
 
298
                canAdd = true;
 
299
            }
 
300
        } else {
 
301
            checkOperationOnUnmatchedParent();
 
302
        }
 
303
    }
 
304
    
 
305
    public void visit(Input child) {
 
306
        if (parent instanceof OneWayOperation ||
 
307
            parent instanceof RequestResponseOperation ||
 
308
            parent instanceof SolicitResponseOperation) 
 
309
        {
 
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) {
 
315
                canAdd = true;
 
316
            }
 
317
        } else {
 
318
            checkOperationOnUnmatchedParent();
 
319
        }
 
320
    }
 
321
    
 
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;
 
329
 
 
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;
 
336
            }
 
337
        } else {
 
338
            checkOperationOnUnmatchedParent();
 
339
        }
 
340
    }
 
341
    
 
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) {
 
350
                canAdd = true;
 
351
            }
 
352
        } else {
 
353
            checkOperationOnUnmatchedParent();
 
354
        }
 
355
    }
 
356
    
 
357
    public void visit(NotificationOperation child) {
 
358
        update(child);
 
359
    }
 
360
    
 
361
    public void visit(SolicitResponseOperation child) {
 
362
        update(child);
 
363
    }
 
364
 
 
365
    public void visit(RequestResponseOperation child) {
 
366
        update(child);
 
367
    }
 
368
    
 
369
    public void visit(OneWayOperation child) {
 
370
        update(child);
 
371
    }
 
372
    
 
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);
 
379
            } else {
 
380
                canAdd = false;
 
381
                if (updater instanceof ComponentUpdater.Query) {
 
382
                    canAdd = ((ComponentUpdater.Query) updater).canAdd(target, child);
 
383
                } 
 
384
            }
 
385
        } else {
 
386
            if (operation == Operation.ADD) {
 
387
                parent.addExtensibilityElement(child);
 
388
            } else if (operation == Operation.REMOVE) {
 
389
                parent.removeExtensibilityElement(child);
 
390
            } else if (operation == null) {
 
391
                canAdd = true;
 
392
                if (child instanceof ExtensibilityElement.ParentSelector) {
 
393
                    canAdd = ((ExtensibilityElement.ParentSelector)child).canBeAddedTo(parent);
 
394
                }
 
395
            }
 
396
        }
 
397
    }
 
398
 
 
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
 
404
        } else {
 
405
            canAdd = false;
 
406
        }
 
407
    }
 
408
}