KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > model > impl > ChildComponentUpdateVisitor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.wsdl.model.impl;
21
22 import org.netbeans.modules.xml.wsdl.model.*;
23 import org.netbeans.modules.xml.wsdl.model.spi.GenericExtensibilityElement;
24 import org.netbeans.modules.xml.wsdl.model.visitor.WSDLVisitor;
25 import org.netbeans.modules.xml.xam.AbstractComponent;
26 import org.netbeans.modules.xml.xam.Component;
27 import org.netbeans.modules.xml.xam.ComponentUpdater;
28 import org.netbeans.modules.xml.xam.dom.DocumentComponent;
29
30 /**
31  * Visitor to add or remove a child of a WSDL component.
32  * @author Nam Nguyen
33  */

34 public class ChildComponentUpdateVisitor<T extends WSDLComponent> implements WSDLVisitor, ComponentUpdater<T> {
35     
36     private Operation operation;
37     private WSDLComponent parent;
38     private int index;
39     private boolean canAdd = false;
40     
41     /**
42      * Creates a new instance of ChildComponentUpdateVisitor
43      */

44     public ChildComponentUpdateVisitor() {
45     }
46     
47     public boolean canAdd(WSDLComponent target, Component child) {
48         if (!(child instanceof WSDLComponent)) return false;
49         update(target, (WSDLComponent) child, null);
50         return canAdd;
51     }
52     
53     public void update(WSDLComponent target, WSDLComponent child, Operation operation) {
54         update(target, child, -1, operation);
55     }
56     
57     public void update(WSDLComponent target, WSDLComponent child, int index, Operation operation) {
58         assert target != null;
59         assert child != null;
60
61         this.parent = target;
62         this.operation = operation;
63         this.index = index;
64         child.accept(this);
65     }
66     
67     private void addChild(String JavaDoc eventName, DocumentComponent child) {
68         ((AbstractComponent) parent).insertAtIndex(eventName, child, index);
69     }
70     
71     private void removeChild(String JavaDoc eventName, DocumentComponent child) {
72         ((AbstractComponent) parent).removeChild(eventName, child);
73     }
74     
75     public void visit(Definitions child) {
76         checkOperationOnUnmatchedParent();
77     }
78
79     public void visit(Types child) {
80         if (parent instanceof Definitions) {
81             if (operation == Operation.ADD) {
82                 addChild(Definitions.TYPES_PROPERTY, child);
83             } else if (operation == Operation.REMOVE) {
84                 removeChild(Definitions.TYPES_PROPERTY, child);
85             } else if (operation == null) {
86                 canAdd = true;
87             }
88         } else {
89             checkOperationOnUnmatchedParent();
90         }
91     }
92     
93     public void visit(Binding child) {
94         if (parent instanceof Definitions) {
95             Definitions target = (Definitions)parent;
96             if (operation == Operation.ADD) {
97                 target.addBinding(child);
98             } else if (operation == Operation.REMOVE) {
99                 target.removeBinding(child);
100             } else if (operation == null) {
101                 canAdd = true;
102             }
103         } else {
104             checkOperationOnUnmatchedParent();
105         }
106     }
107     
108     public void visit(Message child) {
109         if (parent instanceof Definitions) {
110             Definitions target = (Definitions)parent;
111             if (operation == Operation.ADD) {
112                 target.addMessage(child);
113             } else if (operation == Operation.REMOVE) {
114                 target.removeMessage(child);
115             } else if (operation == null) {
116                 canAdd = true;
117             }
118         } else {
119             checkOperationOnUnmatchedParent();
120         }
121     }
122     
123     public void visit(Service child) {
124         if (parent instanceof Definitions) {
125             Definitions target = (Definitions)parent;
126             if (operation == Operation.ADD) {
127                 target.addService(child);
128             } else if (operation == Operation.REMOVE) {
129                 target.removeService(child);
130             } else if (operation == null) {
131                 canAdd = true;
132             }
133         } else {
134             checkOperationOnUnmatchedParent();
135         }
136     }
137     
138     public void visit(PortType child) {
139         if (parent instanceof Definitions) {
140             Definitions target = (Definitions)parent;
141             if (operation == Operation.ADD) {
142                 target.addPortType(child);
143             } else if (operation == Operation.REMOVE) {
144                 target.removePortType(child);
145             } else if (operation == null) {
146                 canAdd = true;
147             }
148         } else {
149             checkOperationOnUnmatchedParent();
150         }
151     }
152     
153     public void visit(Import child) {
154         if (parent instanceof Definitions) {
155             Definitions target = (Definitions)parent;
156             if (operation == Operation.ADD) {
157                 target.addImport(child);
158             } else if (operation == Operation.REMOVE) {
159                 target.removeImport(child);
160             } else if (operation == null) {
161                 canAdd = true;
162             }
163         } else {
164             checkOperationOnUnmatchedParent();
165         }
166     }
167     
168     public void visit(Port child) {
169         if (parent instanceof Service) {
170             Service target = (Service)parent;
171             if (operation == Operation.ADD) {
172                 target.addPort(child);
173             } else if (operation == Operation.REMOVE) {
174                 target.removePort(child);
175             } else if (operation == null) {
176                 canAdd = true;
177             }
178         } else {
179             checkOperationOnUnmatchedParent();
180         }
181     }
182     
183     public void visit(BindingOperation child) {
184         if (parent instanceof Binding) {
185             Binding target = (Binding)parent;
186             if (operation == Operation.ADD) {
187                 target.addBindingOperation(child);
188             } else if (operation == Operation.REMOVE) {
189                 target.removeBindingOperation(child);
190             } else if (operation == null) {
191                 canAdd = true;
192             }
193         } else {
194             checkOperationOnUnmatchedParent();
195         }
196     }
197     
198     public void visit(BindingInput child) {
199         if (parent instanceof BindingOperation) {
200             if (operation == Operation.ADD) {
201                 addChild(BindingOperation.BINDING_INPUT_PROPERTY, child);
202             } else if (operation == Operation.REMOVE) {
203                 removeChild(BindingOperation.BINDING_INPUT_PROPERTY, child);
204             } else if (operation == null) {
205                 canAdd = true;
206             }
207         } else {
208             checkOperationOnUnmatchedParent();
209         }
210     }
211     
212     public void visit(BindingOutput child) {
213         if (parent instanceof BindingOperation) {
214             if (operation == Operation.ADD) {
215                 addChild(BindingOperation.BINDING_OUTPUT_PROPERTY, child);
216             } else if (operation == Operation.REMOVE) {
217                 removeChild(BindingOperation.BINDING_OUTPUT_PROPERTY, child);
218             } else if (operation == null) {
219                 canAdd = true;
220             }
221         } else {
222             checkOperationOnUnmatchedParent();
223         }
224     }
225     
226     public void visit(BindingFault child) {
227         if (parent instanceof BindingOperation) {
228             BindingOperation target = (BindingOperation)parent;
229             if (operation == Operation.ADD) {
230                 target.addBindingFault(child);
231             } else if (operation == Operation.REMOVE) {
232                 target.removeBindingFault(child);
233             } else if (operation == null) {
234                 canAdd = true;
235             }
236         } else {
237             checkOperationOnUnmatchedParent();
238         }
239     }
240     
241     public void visit(Part child) {
242         if (parent instanceof Message) {
243             Message target = (Message)parent;
244             if (operation == Operation.ADD) {
245                 target.addPart(child);
246             } else if (operation == Operation.REMOVE) {
247                 target.removePart(child);
248             } else if (operation == null) {
249                 canAdd = true;
250             }
251         } else {
252             checkOperationOnUnmatchedParent();
253         }
254     }
255     
256     public void visit(Documentation doc) {
257         if (operation == Operation.ADD) {
258             addChild(WSDLComponent.DOCUMENTATION_PROPERTY, doc);
259         } else if (operation == Operation.REMOVE) {
260             removeChild(WSDLComponent.DOCUMENTATION_PROPERTY, doc);
261         } else if (operation == null) {
262             canAdd = true;
263         }
264     }
265     
266     public void visit(Output child) {
267         if (parent instanceof RequestResponseOperation ||
268             parent instanceof SolicitResponseOperation ||
269             parent instanceof NotificationOperation)
270         {
271             if (operation == Operation.ADD) {
272                 addChild(org.netbeans.modules.xml.wsdl.model.Operation.OUTPUT_PROPERTY, child);
273             } else if (operation == Operation.REMOVE) {
274                 removeChild(org.netbeans.modules.xml.wsdl.model.Operation.OUTPUT_PROPERTY, child);
275             } else if (operation == null) {
276                 canAdd = true;
277             }
278         } else {
279             checkOperationOnUnmatchedParent();
280         }
281     }
282     
283     public void visit(Input child) {
284         if (parent instanceof OneWayOperation ||
285             parent instanceof RequestResponseOperation ||
286             parent instanceof SolicitResponseOperation)
287         {
288             if (operation == Operation.ADD) {
289                 addChild(org.netbeans.modules.xml.wsdl.model.Operation.INPUT_PROPERTY, child);
290             } else if (operation == Operation.REMOVE) {
291                 removeChild(org.netbeans.modules.xml.wsdl.model.Operation.INPUT_PROPERTY, child);
292             } else if (operation == null) {
293                 canAdd = true;
294             }
295         } else {
296             checkOperationOnUnmatchedParent();
297         }
298     }
299     
300     public void visit(Fault child) {
301         if (parent instanceof org.netbeans.modules.xml.wsdl.model.Operation) {
302             org.netbeans.modules.xml.wsdl.model.Operation target =
303                     (org.netbeans.modules.xml.wsdl.model.Operation)parent;
304             if (operation == Operation.ADD) {
305                 target.addFault(child);
306             } else if (operation == Operation.REMOVE) {
307                 target.removeFault(child);
308             } else if (operation == null) {
309                 canAdd = true;
310             }
311         } else {
312             checkOperationOnUnmatchedParent();
313         }
314     }
315     
316     private void update(org.netbeans.modules.xml.wsdl.model.Operation child) {
317         if (parent instanceof PortType) {
318             PortType target = (PortType)parent;
319             if (operation == Operation.ADD) {
320                 target.addOperation(child);
321             } else if (operation == Operation.REMOVE) {
322                 target.removeOperation(child);
323             } else if (operation == null) {
324                 canAdd = true;
325             }
326         } else {
327             checkOperationOnUnmatchedParent();
328         }
329     }
330     
331     public void visit(NotificationOperation child) {
332         update(child);
333     }
334     
335     public void visit(SolicitResponseOperation child) {
336         update(child);
337     }
338
339     public void visit(RequestResponseOperation child) {
340         update(child);
341     }
342     
343     public void visit(OneWayOperation child) {
344         update(child);
345     }
346     
347     public void visit(ExtensibilityElement child) {
348         if (parent instanceof ExtensibilityElement.UpdaterProvider) {
349             ExtensibilityElement.UpdaterProvider target = (ExtensibilityElement.UpdaterProvider) parent;
350             ComponentUpdater<ExtensibilityElement> updater = target.getComponentUpdater();
351             if (operation != null) {
352                 updater.update(target, child, index, operation);
353             } else {
354                 canAdd = false;
355                 if (updater instanceof ComponentUpdater.Query) {
356                     canAdd = ((ComponentUpdater.Query) updater).canAdd(target, child);
357                 }
358             }
359         } else {
360             if (operation == Operation.ADD) {
361                 parent.addExtensibilityElement(child);
362             } else if (operation == Operation.REMOVE) {
363                 parent.removeExtensibilityElement(child);
364             } else if (operation == null) {
365                 canAdd = true;
366                 if (child instanceof ExtensibilityElement.ParentSelector) {
367                     canAdd = ((ExtensibilityElement.ParentSelector)child).canBeAddedTo(parent);
368                 }
369             }
370         }
371     }
372
373     private void checkOperationOnUnmatchedParent() {
374         if (operation != null) {
375             // note this unmatch should be caught by validation,
376
// we don't want the UI view to go blank on invalid but still well-formed document
377
//throw new IllegalArgumentException("Unmatched parent-child components"); //NO18N
378
} else {
379             canAdd = false;
380         }
381     }
382 }
383
Popular Tags