1 /*
   2  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.internal.ws.processor.util;
  27 
  28 import com.sun.tools.internal.ws.processor.model.*;
  29 import com.sun.tools.internal.ws.processor.model.java.JavaInterface;
  30 import com.sun.tools.internal.ws.processor.model.jaxb.JAXBType;
  31 import com.sun.tools.internal.ws.processor.model.jaxb.JAXBTypeVisitor;
  32 import com.sun.tools.internal.ws.processor.model.jaxb.RpcLitStructure;
  33 
  34 import javax.xml.namespace.QName;
  35 import java.util.HashSet;
  36 import java.util.Iterator;
  37 import java.util.Set;
  38 
  39 /**
  40  * This class writes out a Model as an XML document.
  41  *
  42  * @author WS Development Team
  43  */
  44 public class ClassNameCollector extends ExtendedModelVisitor
  45     implements JAXBTypeVisitor {
  46 
  47     public ClassNameCollector() {
  48     }
  49 
  50     public void process(Model model) {
  51         try {
  52             _allClassNames = new HashSet();
  53             _exceptions = new HashSet();
  54             _wsdlBindingNames = new HashSet();
  55             _conflictingClassNames = new HashSet();
  56             _seiClassNames = new HashSet<String>();
  57             _jaxbGeneratedClassNames = new HashSet<String>();
  58             _exceptionClassNames = new HashSet<String>();
  59             _portTypeNames = new HashSet<QName>();
  60             visit(model);
  61         } catch (Exception e) {
  62             e.printStackTrace();
  63             // fail silently
  64         } finally {
  65             _allClassNames = null;
  66             _exceptions = null;
  67         }
  68     }
  69 
  70     public Set getConflictingClassNames() {
  71         return _conflictingClassNames;
  72     }
  73 
  74     protected void postVisit(Model model) throws Exception {
  75         for (Iterator iter = model.getExtraTypes(); iter.hasNext();) {
  76             visitType((AbstractType)iter.next());
  77         }
  78     }
  79 
  80     protected void preVisit(Service service) throws Exception {
  81         registerClassName(
  82             ((JavaInterface)service.getJavaInterface()).getName());
  83         // We don't generate Impl classes, commenting it out.
  84         // Otherwise, it would cause naming conflicts
  85         //registerClassName(
  86         //    ((JavaInterface)service.getJavaInterface()).getImpl());
  87     }
  88 
  89     protected void processPort11x(Port port){
  90         QName wsdlBindingName = (QName) port.getProperty(
  91             ModelProperties.PROPERTY_WSDL_BINDING_NAME);
  92         if (!_wsdlBindingNames.contains(wsdlBindingName)) {
  93 
  94             // multiple ports can share a binding without causing a conflict
  95             registerClassName(port.getJavaInterface().getName());
  96         }
  97         registerClassName((String) port.getProperty(
  98             ModelProperties.PROPERTY_STUB_CLASS_NAME));
  99         registerClassName((String) port.getProperty(
 100             ModelProperties.PROPERTY_TIE_CLASS_NAME));
 101     }
 102 
 103     protected void preVisit(Port port) throws Exception {
 104         QName portTypeName = (QName)port.getProperty(ModelProperties.PROPERTY_WSDL_PORT_TYPE_NAME);
 105         if(_portTypeNames.contains(portTypeName))
 106             return;
 107 
 108         //in 2.0, stub/tie class are binding agnostic so they should be per port, that is multiple
 109         // bindings can share the same port
 110 
 111         addSEIClassName(port.getJavaInterface().getName());
 112     }
 113 
 114     private void addSEIClassName(String s) {
 115         _seiClassNames.add(s);
 116         registerClassName(s);
 117     }
 118 
 119     protected void postVisit(Port port) throws Exception {
 120         QName wsdlBindingName = (QName) port.getProperty(
 121             ModelProperties.PROPERTY_WSDL_BINDING_NAME);
 122         if (!_wsdlBindingNames.contains(wsdlBindingName)) {
 123             _wsdlBindingNames.add(wsdlBindingName);
 124         }
 125 
 126         QName portTypeName = (QName)port.getProperty(ModelProperties.PROPERTY_WSDL_PORT_TYPE_NAME);
 127         if(!_portTypeNames.contains(portTypeName)){
 128             _portTypeNames.add(portTypeName);
 129         }
 130     }
 131 
 132     protected boolean shouldVisit(Port port) {
 133         QName wsdlBindingName = (QName) port.getProperty(
 134             ModelProperties.PROPERTY_WSDL_BINDING_NAME);
 135         return !_wsdlBindingNames.contains(wsdlBindingName);
 136     }
 137 
 138     protected void preVisit(Fault fault) throws Exception {
 139         if (!_exceptions.contains(fault.getJavaException())) {
 140 
 141             /* the same exception can be used in several faults, but that
 142              * doesn't mean that there is a conflict
 143              */
 144             _exceptions.add(fault.getJavaException());
 145             addExceptionClassName(fault.getJavaException().getName());
 146 
 147             if (fault.getParentFault() != null) {
 148                 preVisit(fault.getParentFault());
 149             }
 150             for (Iterator iter = fault.getSubfaults();
 151                 iter != null && iter.hasNext();) {
 152 
 153                 Fault subfault = (Fault) iter.next();
 154                 preVisit(subfault);
 155             }
 156         }
 157     }
 158 
 159     private void addExceptionClassName(String name) {
 160         if(_allClassNames.contains(name))
 161             _exceptionClassNames.add(name);
 162         registerClassName(name);
 163         //To change body of created methods use File | Settings | File Templates.
 164     }
 165 
 166     protected void visitBodyBlock(Block block) throws Exception {
 167         visitBlock(block);
 168     }
 169 
 170     protected void visitHeaderBlock(Block block) throws Exception {
 171         visitBlock(block);
 172     }
 173 
 174     protected void visitFaultBlock(Block block) throws Exception {
 175     }
 176 
 177     protected void visitBlock(Block block) throws Exception {
 178         visitType(block.getType());
 179     }
 180 
 181     protected void visit(Parameter parameter) throws Exception {
 182         visitType(parameter.getType());
 183     }
 184 
 185     private void visitType(AbstractType type) throws Exception {
 186         if (type != null) {
 187             if (type instanceof JAXBType)
 188                 visitType((JAXBType)type);
 189             else if (type instanceof RpcLitStructure)
 190                 visitType((RpcLitStructure)type);
 191         }
 192     }
 193 
 194 
 195     private void visitType(JAXBType type) throws Exception {
 196         type.accept(this);
 197     }
 198 
 199     private void visitType(RpcLitStructure type) throws Exception {
 200         type.accept(this);
 201     }
 202     private void registerClassName(String name) {
 203         if (name == null || name.equals("")) {
 204             return;
 205         }
 206         if (_allClassNames.contains(name)) {
 207             _conflictingClassNames.add(name);
 208         } else {
 209             _allClassNames.add(name);
 210         }
 211     }
 212 
 213     public Set<String> getSeiClassNames() {
 214         return _seiClassNames;
 215     }
 216 
 217     private Set<String> _seiClassNames;
 218 
 219     public Set<String> getJaxbGeneratedClassNames() {
 220         return _jaxbGeneratedClassNames;
 221     }
 222 
 223     private Set<String> _jaxbGeneratedClassNames;
 224 
 225 
 226     public Set<String> getExceptionClassNames() {
 227         return _exceptionClassNames;
 228     }
 229 
 230     private Set<String> _exceptionClassNames;
 231     boolean doneVisitingJAXBModel = false;
 232     public void visit(JAXBType type) throws Exception {
 233         if(!doneVisitingJAXBModel && type.getJaxbModel() != null){
 234             Set<String> classNames = type.getJaxbModel().getGeneratedClassNames();
 235             for(String className : classNames){
 236                 addJAXBGeneratedClassName(className);
 237             }
 238             doneVisitingJAXBModel = true;
 239         }
 240     }
 241 
 242     public void visit(RpcLitStructure type) throws Exception {
 243         if(!doneVisitingJAXBModel){
 244             Set<String> classNames = type.getJaxbModel().getGeneratedClassNames();
 245             for(String className : classNames){
 246                 addJAXBGeneratedClassName(className);
 247             }
 248             doneVisitingJAXBModel = true;
 249         }
 250     }
 251 
 252 
 253     private void addJAXBGeneratedClassName(String name) {
 254         _jaxbGeneratedClassNames.add(name);
 255         registerClassName(name);
 256     }
 257 
 258     private Set _allClassNames;
 259     private Set _exceptions;
 260     private Set _wsdlBindingNames;
 261     private Set _conflictingClassNames;
 262     private Set<QName> _portTypeNames;
 263 }