001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.webservices;
018    
019    import org.apache.commons.logging.Log;
020    import org.apache.commons.logging.LogFactory;
021    
022    import java.util.Collection;
023    import java.util.Iterator;
024    import java.util.List;
025    import java.util.Map;
026    import javax.wsdl.*;
027    import javax.wsdl.extensions.soap.SOAPBody;
028    import javax.wsdl.extensions.soap.SOAPBinding;
029    
030    public class WSDLVisitor {
031    
032        private static final Log log = LogFactory.getLog(WSDLVisitor.class);
033    
034        protected final Definition definition;
035    
036        public WSDLVisitor(Definition definition) {
037            this.definition = definition;
038        }
039    
040        public void walkTree() {
041            begin();
042            try {
043                visit(definition);
044                for (Iterator iterator = definition.getImports().entrySet().iterator(); iterator.hasNext();) {
045                    Map.Entry entry = (Map.Entry) iterator.next();
046                    String namespaceURI = (String) entry.getKey();
047                    List importsForNamespace = (List) entry.getValue();
048                    for (Iterator iterator1 = importsForNamespace.iterator(); iterator1.hasNext();) {
049                        Import anImport = (Import) iterator1.next();
050                        visit(anImport);
051                    }
052                }
053                visit(definition.getTypes());
054                Collection messages = definition.getMessages().values();
055                for (Iterator iterator = messages.iterator(); iterator.hasNext();) {
056                    Message message = (Message) iterator.next();
057                    visit(message);
058                    Collection parts = message.getParts().values();
059                    for (Iterator iterator2 = parts.iterator(); iterator2.hasNext();) {
060                        Part part = (Part) iterator2.next();
061                        visit(part);
062                    }
063                }
064                Collection services = definition.getServices().values();
065                for (Iterator iterator = services.iterator(); iterator.hasNext();) {
066                    Service service = (Service) iterator.next();
067                    visit(service);
068                    Collection ports = service.getPorts().values();
069                    for (Iterator iterator1 = ports.iterator(); iterator1.hasNext();) {
070                        Port port = (Port) iterator1.next();
071                        visit(port);
072                        Binding binding = port.getBinding();
073                        visit(binding);
074                        List bindingOperations = binding.getBindingOperations();
075                        for (int i = 0; i < bindingOperations.size(); i++) {
076                            BindingOperation bindingOperation = (BindingOperation) bindingOperations.get(i);
077                            visit(bindingOperation);
078                            visit(bindingOperation.getBindingInput());
079                            visit(bindingOperation.getBindingOutput());
080                            Collection bindingFaults = bindingOperation.getBindingFaults().values();
081                            for (Iterator iterator2 = bindingFaults.iterator(); iterator2.hasNext();) {
082                                BindingFault bindingFault = (BindingFault) iterator2.next();
083                                visit(bindingFault);
084                            }
085    
086                        }
087                        PortType portType = binding.getPortType();
088                        visit(portType);
089                        List operations = portType.getOperations();
090                        for (int i = 0; i < operations.size(); i++) {
091                            Operation operation = (Operation) operations.get(i);
092                            visit(operation);
093                            {
094                                Input input = operation.getInput();
095                                visit(input);
096                            }
097                            {
098                                Output output = operation.getOutput();
099                                visit(output);
100                            }
101                            Collection faults = operation.getFaults().values();
102                            for (Iterator iterator2 = faults.iterator(); iterator2.hasNext();) {
103                                Fault fault = (Fault) iterator2.next();
104                                visit(fault);
105                            }
106    
107                        }
108                    }
109                }
110            } catch(Exception e){
111                log.error(e.getMessage(), e);
112            } finally {
113                end();
114            }
115        }
116    
117        protected void begin() {
118        }
119    
120        protected void end() {
121        }
122    
123        protected void visit(Fault fault) {
124        }
125    
126        protected void visit(Definition definition) {
127        }
128    
129        protected void visit(Import wsdlImport) {
130        }
131    
132        protected void visit(Types types) {
133        }
134    
135        protected void visit(BindingFault bindingFault) {
136        }
137    
138        protected void visit(BindingOutput bindingOutput) {
139        }
140    
141        protected void visit(BindingInput bindingInput) {
142        }
143    
144        protected void visit(Output output) {
145        }
146    
147        protected void visit(Part part) {
148        }
149    
150        protected void visit(Message message) {
151        }
152    
153        protected void visit(Input input) {
154        }
155    
156        protected void visit(Operation operation) {
157        }
158    
159        protected void visit(PortType portType) {
160        }
161    
162        protected void visit(BindingOperation bindingOperation) {
163        }
164    
165        protected void visit(Binding binding) {
166        }
167    
168        protected void visit(Port port) {
169        }
170    
171        protected void visit(Service service) {
172        }
173    
174        protected SOAPBody getSOAPBody(List extensibilityElements) {
175            SOAPBody body = null;
176            for (int j = 0; j < extensibilityElements.size(); j++) {
177                Object element = extensibilityElements.get(j);
178                if (element instanceof SOAPBody) {
179                    body = (SOAPBody) element;
180                    break;
181                }
182            }
183            return body;
184        }
185    
186        protected SOAPBinding getSOAPBinding(Binding binding) {
187            SOAPBinding soapBinding = null;
188            List extensibilityElements = binding.getExtensibilityElements();
189            for (int i = 0; i < extensibilityElements.size(); i++) {
190                Object element = extensibilityElements.get(i);
191                if (element instanceof SOAPBinding) {
192                    soapBinding = (SOAPBinding) element;
193                }
194            }
195            return soapBinding;
196        }
197    }