1 /*
   2  * Copyright (c) 1997, 2013, 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.wsdl.document;
  27 
  28 import com.sun.tools.internal.ws.api.wsdl.TWSDLExtensible;
  29 import com.sun.tools.internal.ws.api.wsdl.TWSDLExtension;
  30 import com.sun.tools.internal.ws.wsdl.framework.*;
  31 import com.sun.tools.internal.ws.resources.WsdlMessages;
  32 import com.sun.tools.internal.ws.wscompile.AbortException;
  33 import com.sun.tools.internal.ws.wscompile.ErrorReceiver;
  34 import org.xml.sax.Locator;
  35 
  36 import javax.xml.namespace.QName;
  37 import java.util.ArrayList;
  38 import java.util.Iterator;
  39 import java.util.List;
  40 
  41 /**
  42  * Entity corresponding to the "binding" WSDL element.
  43  *
  44  * @author WS Development Team
  45  */
  46 public class Binding extends GlobalEntity implements TWSDLExtensible {
  47 
  48     public Binding(Defining defining, Locator locator, ErrorReceiver receiver) {
  49         super(defining, locator, receiver);
  50         _operations = new ArrayList();
  51         _helper = new ExtensibilityHelper();
  52     }
  53 
  54     public void add(BindingOperation operation) {
  55         _operations.add(operation);
  56     }
  57 
  58     public Iterator operations() {
  59         return _operations.iterator();
  60     }
  61 
  62     public QName getPortType() {
  63         return _portType;
  64     }
  65 
  66     public void setPortType(QName n) {
  67         _portType = n;
  68     }
  69 
  70     public PortType resolvePortType(AbstractDocument document) {
  71         try {
  72             return (PortType) document.find(Kinds.PORT_TYPE, _portType);
  73         } catch (NoSuchEntityException e) {
  74             errorReceiver.error(getLocator(), WsdlMessages.ENTITY_NOT_FOUND_PORT_TYPE(_portType, new QName(getNamespaceURI(), getName())));
  75             throw new AbortException();
  76         }
  77     }
  78 
  79     public Kind getKind() {
  80         return Kinds.BINDING;
  81     }
  82 
  83     public QName getElementName() {
  84         return WSDLConstants.QNAME_BINDING;
  85     }
  86 
  87     public Documentation getDocumentation() {
  88         return _documentation;
  89     }
  90 
  91     public void setDocumentation(Documentation d) {
  92         _documentation = d;
  93     }
  94 
  95     public void withAllSubEntitiesDo(EntityAction action) {
  96         for (Iterator iter = _operations.iterator(); iter.hasNext();) {
  97             action.perform((Entity) iter.next());
  98         }
  99         _helper.withAllSubEntitiesDo(action);
 100     }
 101 
 102     public void withAllQNamesDo(QNameAction action) {
 103         super.withAllQNamesDo(action);
 104 
 105         if (_portType != null) {
 106             action.perform(_portType);
 107         }
 108     }
 109 
 110     public void withAllEntityReferencesDo(EntityReferenceAction action) {
 111         super.withAllEntityReferencesDo(action);
 112         if (_portType != null) {
 113             action.perform(Kinds.PORT_TYPE, _portType);
 114         }
 115     }
 116 
 117     public void accept(WSDLDocumentVisitor visitor) throws Exception {
 118         visitor.preVisit(this);
 119         //bug fix: 4947340, extensions should be the first element
 120         _helper.accept(visitor);
 121         for (Iterator iter = _operations.iterator(); iter.hasNext();) {
 122             ((BindingOperation) iter.next()).accept(visitor);
 123         }
 124         visitor.postVisit(this);
 125     }
 126 
 127     public void validateThis() {
 128         if (getName() == null) {
 129             failValidation("validation.missingRequiredAttribute", "name");
 130         }
 131         if (_portType == null) {
 132             failValidation("validation.missingRequiredAttribute", "type");
 133         }
 134     }
 135 
 136     public String getNameValue() {
 137         return getName();
 138     }
 139 
 140     public String getNamespaceURI() {
 141         return getDefining().getTargetNamespaceURI();
 142     }
 143 
 144     public QName getWSDLElementName() {
 145         return getElementName();
 146     }
 147 
 148     public void addExtension(TWSDLExtension e) {
 149         _helper.addExtension(e);
 150     }
 151 
 152     public Iterable<TWSDLExtension> extensions() {
 153         return _helper.extensions();
 154     }
 155 
 156     public TWSDLExtensible getParent() {
 157         return parent;
 158     }
 159 
 160     private ExtensibilityHelper _helper;
 161     private Documentation _documentation;
 162     private QName _portType;
 163     private List _operations;
 164 
 165     public void setParent(TWSDLExtensible parent) {
 166         this.parent = parent;
 167     }
 168 
 169     private TWSDLExtensible parent;
 170 }