001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.ws.wsdl.document;
038:
039: import com.sun.tools.ws.wsdl.framework.*;
040: import com.sun.tools.ws.wsdl.parser.MetadataFinder;
041: import com.sun.tools.ws.wscompile.ErrorReceiver;
042:
043: import javax.xml.namespace.QName;
044: import java.util.ArrayList;
045: import java.util.Iterator;
046: import java.util.Set;
047:
048: /**
049: * A WSDL document.
050: *
051: * @author WS Development Team
052: */
053: public class WSDLDocument extends AbstractDocument {
054:
055: public WSDLDocument(MetadataFinder forest, ErrorReceiver errReceiver) {
056: super (forest, errReceiver);
057: }
058:
059: public Definitions getDefinitions() {
060: return _definitions;
061: }
062:
063: public void setDefinitions(Definitions d) {
064: _definitions = d;
065: }
066:
067: public QName[] getAllServiceQNames() {
068:
069: ArrayList serviceQNames = new ArrayList();
070:
071: for (Iterator iter = getDefinitions().services(); iter
072: .hasNext();) {
073: Service next = (Service) iter.next();
074: String targetNamespace = next.getDefining()
075: .getTargetNamespaceURI();
076: String localName = next.getName();
077: QName serviceQName = new QName(targetNamespace, localName);
078: serviceQNames.add(serviceQName);
079: }
080: return (QName[]) serviceQNames.toArray(new QName[serviceQNames
081: .size()]);
082: }
083:
084: public QName[] getAllPortQNames() {
085: ArrayList portQNames = new ArrayList();
086:
087: for (Iterator iter = getDefinitions().services(); iter
088: .hasNext();) {
089: Service next = (Service) iter.next();
090: //Iterator ports = next.ports();
091: for (Iterator piter = next.ports(); piter.hasNext();) {
092: // If it's a relative import
093: Port pnext = (Port) piter.next();
094: String targetNamespace = pnext.getDefining()
095: .getTargetNamespaceURI();
096: String localName = pnext.getName();
097: QName portQName = new QName(targetNamespace, localName);
098: portQNames.add(portQName);
099: }
100: }
101: return (QName[]) portQNames
102: .toArray(new QName[portQNames.size()]);
103: }
104:
105: public QName[] getPortQNames(String serviceNameLocalPart) {
106:
107: ArrayList portQNames = new ArrayList();
108:
109: for (Iterator iter = getDefinitions().services(); iter
110: .hasNext();) {
111: Service next = (Service) iter.next();
112: if (next.getName().equals(serviceNameLocalPart)) {
113: for (Iterator piter = next.ports(); piter.hasNext();) {
114: Port pnext = (Port) piter.next();
115: String targetNamespace = pnext.getDefining()
116: .getTargetNamespaceURI();
117: String localName = pnext.getName();
118: QName portQName = new QName(targetNamespace,
119: localName);
120: portQNames.add(portQName);
121: }
122: }
123: }
124: return (QName[]) portQNames
125: .toArray(new QName[portQNames.size()]);
126: }
127:
128: public void accept(WSDLDocumentVisitor visitor) throws Exception {
129: _definitions.accept(visitor);
130: }
131:
132: public void validate(EntityReferenceValidator validator) {
133: GloballyValidatingAction action = new GloballyValidatingAction(
134: this , validator);
135: withAllSubEntitiesDo(action);
136: if (action.getException() != null) {
137: throw action.getException();
138: }
139: }
140:
141: protected Entity getRoot() {
142: return _definitions;
143: }
144:
145: private Definitions _definitions;
146:
147: private class GloballyValidatingAction implements EntityAction,
148: EntityReferenceAction {
149: public GloballyValidatingAction(AbstractDocument document,
150: EntityReferenceValidator validator) {
151: _document = document;
152: _validator = validator;
153: }
154:
155: public void perform(Entity entity) {
156: try {
157: entity.validateThis();
158: entity.withAllEntityReferencesDo(this );
159: entity.withAllSubEntitiesDo(this );
160: } catch (ValidationException e) {
161: if (_exception == null) {
162: _exception = e;
163: }
164: }
165: }
166:
167: public void perform(Kind kind, QName name) {
168: try {
169: GloballyKnown entity = _document.find(kind, name);
170: } catch (NoSuchEntityException e) {
171: // failed to resolve, check with the validator
172: if (_exception == null) {
173: if (_validator == null
174: || !_validator.isValid(kind, name)) {
175: _exception = e;
176: }
177: }
178: }
179: }
180:
181: public ValidationException getException() {
182: return _exception;
183: }
184:
185: private ValidationException _exception;
186: private AbstractDocument _document;
187: private EntityReferenceValidator _validator;
188: }
189: }
|