KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > refactoring > FindWSDLUsageVisitor


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.wsdl.refactoring;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23 import org.netbeans.modules.xml.refactoring.UsageGroup;
24 import org.netbeans.modules.xml.refactoring.spi.RefactoringEngine;
25 import org.netbeans.modules.xml.wsdl.model.Binding;
26 import org.netbeans.modules.xml.wsdl.model.BindingFault;
27 import org.netbeans.modules.xml.wsdl.model.BindingInput;
28 import org.netbeans.modules.xml.wsdl.model.BindingOperation;
29 import org.netbeans.modules.xml.wsdl.model.BindingOutput;
30 import org.netbeans.modules.xml.wsdl.model.Definitions;
31 import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement;
32 import org.netbeans.modules.xml.wsdl.model.Fault;
33 import org.netbeans.modules.xml.wsdl.model.Input;
34 import org.netbeans.modules.xml.wsdl.model.Message;
35 import org.netbeans.modules.xml.wsdl.model.OperationParameter;
36 import org.netbeans.modules.xml.wsdl.model.Output;
37 import org.netbeans.modules.xml.wsdl.model.Part;
38 import org.netbeans.modules.xml.wsdl.model.Port;
39 import org.netbeans.modules.xml.wsdl.model.ReferenceableWSDLComponent;
40 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
41 import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPAddress;
42 import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPBinding;
43 import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPBody;
44 import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPComponent;
45 import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPFault;
46 import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPHeader;
47 import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPHeaderFault;
48 import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPOperation;
49 import org.netbeans.modules.xml.wsdl.model.visitor.ChildVisitor;
50 import org.netbeans.modules.xml.wsdl.model.visitor.WSDLVisitor;
51 import org.netbeans.modules.xml.xam.Component;
52 import org.netbeans.modules.xml.xam.Reference;
53 import org.netbeans.modules.xml.xam.Referenceable;
54 import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
55 import org.openide.ErrorManager;
56
57 /**
58  *
59  * @author Nam Nguyen
60  */

61 public class FindWSDLUsageVisitor extends ChildVisitor implements WSDLVisitor {
62     
63     private ReferenceableWSDLComponent referenced;
64     private UsageGroup usage;
65     private List JavaDoc<UsageGroup> usages;
66     
67     /** Creates a new instance of FindWSDLUsageVisitor */
68     public FindWSDLUsageVisitor() {
69     }
70     
71     public List JavaDoc<UsageGroup> findUsages(ReferenceableWSDLComponent referenced,
72             Definitions wsdl, RefactoringEngine engine) {
73         this.referenced = referenced;
74         usage = new UsageGroup(engine, wsdl.getModel(), referenced);
75         usages = new ArrayList JavaDoc<UsageGroup>();
76         wsdl.accept(this);
77         if (!usage.isEmpty()) {
78             usages.add(usage);
79         }
80         return usages;
81     }
82     
83     private <T extends ReferenceableWSDLComponent> void check(NamedComponentReference<T> ref, Component referencing) {
84         if (ref == null || ! ref.getType().isAssignableFrom(referenced.getClass())) {
85             return;
86         }
87         
88         try {
89             if (ref.references(ref.getType().cast(referenced))) {
90                 usage.addItem(referencing);
91             }
92         } catch(Exception JavaDoc e) {
93             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
94             usage.addError(referencing, e.getMessage());
95         }
96     }
97     
98     private <T extends ReferenceableWSDLComponent> void check(Reference<T> ref, Component referencing) {
99         if (ref == null || ! ref.getType().isAssignableFrom(referenced.getClass())) {
100             return;
101         }
102         try {
103             if (ref.references(ref.getType().cast(referenced))) {
104                 usage.addItem(referencing);
105             }
106         } catch(Exception JavaDoc e) {
107             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
108             usage.addError(referencing, e.getMessage());
109         }
110     }
111     
112     private void check(OperationParameter referencing) {
113         check(referencing.getMessage(), referencing);
114     }
115     
116     public void visit(BindingOperation component) {
117         check(component.getOperation(), component);
118         super.visit(component);
119     }
120     
121     public void visit(Input oparam) {
122         check(oparam);
123         super.visit(oparam);
124     }
125     
126     public void visit(Output oparam) {
127         check(oparam);
128         super.visit(oparam);
129     }
130     
131     public void visit(Fault oparam) {
132         check(oparam);
133         super.visit(oparam);
134     }
135     
136     public void visit(Port port) {
137         check(port.getBinding(), port);
138         super.visit(port);
139     }
140     
141     public void visit(BindingInput component) {
142         check(component.getInput(), component);
143         super.visit(component);
144     }
145     
146     public void visit(BindingOutput component) {
147         check(component.getOutput(), component);
148         super.visit(component);
149     }
150     
151     public void visit(BindingFault component) {
152         check(component.getFault(), component);
153         super.visit(component);
154     }
155     
156     public void visit(Binding component) {
157         check(component.getType(), component);
158         super.visit(component);
159     }
160     
161     public void visit(ExtensibilityElement ee) {
162         if (ee instanceof SOAPComponent) {
163             ((SOAPComponent) ee).accept(new SOAPVisitor());
164         }
165         super.visit(ee);
166     }
167     
168     // SOAPComponent.Visitor
169
class SOAPVisitor implements SOAPComponent.Visitor {
170         public void visit(SOAPFault component) {
171             check(component.getFault(), component);
172             visitChildren(component);
173         }
174
175         public void visit(SOAPHeader component) {
176             if (referenced instanceof Message) {
177                 check(component.getMessage(), component);
178             } else if (referenced instanceof Part) {
179                 check(component.getPartRef(), component);
180             }
181             visitChildren(component);
182         }
183         
184         public void visit(SOAPHeaderFault component) {
185             if (referenced instanceof Message) {
186                 check(component.getMessage(), component);
187             } else if (referenced instanceof Part) {
188                 check(component.getPartRef(), component);
189             }
190             visitChildren(component);
191         }
192         
193         public void visit(SOAPOperation component) {
194             //no references
195
visitChildren(component);
196         }
197         
198         public void visit(SOAPBinding component) {
199             //no references
200
visitChildren(component);
201         }
202         
203         public void visit(SOAPBody component) {
204             if (component.getParts() != null) {
205                 for (Reference<Part> ref : component.getPartRefs()) {
206                     check(ref, component);
207                 }
208             }
209             visitChildren(component);
210         }
211         
212         public void visit(SOAPAddress component) {
213             //no references
214
visitChildren(component);
215         }
216         
217         protected void visitChildren(SOAPComponent component) {
218             for (SOAPComponent c : component.getChildren(SOAPComponent.class)) {
219                 c.accept(this);
220             }
221         }
222     }
223 }
224
225
226
Popular Tags