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.wsdl.framework.*;
  29 import com.sun.tools.internal.ws.wsdl.parser.MetadataFinder;
  30 import com.sun.tools.internal.ws.wscompile.ErrorReceiver;
  31 
  32 import javax.xml.namespace.QName;
  33 import java.util.ArrayList;
  34 import java.util.Iterator;
  35 
  36 /**
  37  * A WSDL document.
  38  *
  39  * @author WS Development Team
  40  */
  41 public class WSDLDocument extends AbstractDocument{
  42 
  43     public WSDLDocument(MetadataFinder forest, ErrorReceiver errReceiver) {
  44         super(forest, errReceiver);
  45     }
  46 
  47     public Definitions getDefinitions() {
  48         return _definitions;
  49     }
  50 
  51     public void setDefinitions(Definitions d) {
  52         _definitions = d;
  53     }
  54 
  55     public QName[] getAllServiceQNames() {
  56 
  57         ArrayList serviceQNames = new ArrayList();
  58 
  59         for (Iterator iter = getDefinitions().services(); iter.hasNext();) {
  60             Service next = (Service) iter.next();
  61             String targetNamespace = next.getDefining().getTargetNamespaceURI();
  62             String localName = next.getName();
  63             QName serviceQName = new QName(targetNamespace, localName);
  64             serviceQNames.add(serviceQName);
  65         }
  66         return (QName[]) serviceQNames.toArray(new QName[serviceQNames.size()]);
  67     }
  68 
  69     public QName[] getAllPortQNames() {
  70         ArrayList portQNames = new ArrayList();
  71 
  72         for (Iterator iter = getDefinitions().services(); iter.hasNext();) {
  73             Service next = (Service) iter.next();
  74             //Iterator ports = next.ports();
  75             for (Iterator piter = next.ports(); piter.hasNext();) {
  76                 // If it's a relative import
  77                 Port pnext = (Port) piter.next();
  78                 String targetNamespace =
  79                     pnext.getDefining().getTargetNamespaceURI();
  80                 String localName = pnext.getName();
  81                 QName portQName = new QName(targetNamespace, localName);
  82                 portQNames.add(portQName);
  83             }
  84         }
  85         return (QName[]) portQNames.toArray(new QName[portQNames.size()]);
  86     }
  87 
  88     public QName[] getPortQNames(String serviceNameLocalPart) {
  89 
  90         ArrayList portQNames = new ArrayList();
  91 
  92         for (Iterator iter = getDefinitions().services(); iter.hasNext();) {
  93             Service next = (Service) iter.next();
  94             if (next.getName().equals(serviceNameLocalPart)) {
  95                 for (Iterator piter = next.ports(); piter.hasNext();) {
  96                     Port pnext = (Port) piter.next();
  97                     String targetNamespace =
  98                         pnext.getDefining().getTargetNamespaceURI();
  99                     String localName = pnext.getName();
 100                     QName portQName = new QName(targetNamespace, localName);
 101                     portQNames.add(portQName);
 102                 }
 103             }
 104         }
 105         return (QName[]) portQNames.toArray(new QName[portQNames.size()]);
 106     }
 107 
 108     public void accept(WSDLDocumentVisitor visitor) throws Exception {
 109         _definitions.accept(visitor);
 110     }
 111 
 112     @Override
 113     public void validate(EntityReferenceValidator validator) {
 114         GloballyValidatingAction action =
 115             new GloballyValidatingAction(this, validator);
 116         withAllSubEntitiesDo(action);
 117         if (action.getException() != null) {
 118             throw action.getException();
 119         }
 120     }
 121 
 122     @Override
 123     protected Entity getRoot() {
 124         return _definitions;
 125     }
 126 
 127     private Definitions _definitions;
 128 
 129     private static class GloballyValidatingAction implements EntityAction, EntityReferenceAction {
 130         public GloballyValidatingAction(
 131             AbstractDocument document,
 132             EntityReferenceValidator validator) {
 133             _document = document;
 134             _validator = validator;
 135         }
 136 
 137         @Override
 138         public void perform(Entity entity) {
 139             try {
 140                 entity.validateThis();
 141                 entity.withAllEntityReferencesDo(this);
 142                 entity.withAllSubEntitiesDo(this);
 143             } catch (ValidationException e) {
 144                 if (_exception == null) {
 145                     _exception = e;
 146                 }
 147             }
 148         }
 149 
 150         @Override
 151         public void perform(Kind kind, QName name) {
 152             try {
 153                 _document.find(kind, name);
 154             } catch (NoSuchEntityException e) {
 155                 // failed to resolve, check with the validator
 156                 if (_exception == null) {
 157                     if (_validator == null
 158                         || !_validator.isValid(kind, name)) {
 159                         _exception = e;
 160                     }
 161                 }
 162             }
 163         }
 164 
 165         public ValidationException getException() {
 166             return _exception;
 167         }
 168 
 169         private ValidationException _exception;
 170         private AbstractDocument _document;
 171         private EntityReferenceValidator _validator;
 172     }
 173 }