001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.ws.wsdl.document;
027:
028: import java.util.ArrayList;
029: import java.util.Iterator;
030: import java.util.Set;
031:
032: import javax.xml.namespace.QName;
033:
034: import com.sun.tools.internal.ws.wsdl.framework.AbstractDocument;
035: import com.sun.tools.internal.ws.wsdl.framework.Entity;
036: import com.sun.tools.internal.ws.wsdl.framework.EntityAction;
037: import com.sun.tools.internal.ws.wsdl.framework.EntityReferenceAction;
038: import com.sun.tools.internal.ws.wsdl.framework.EntityReferenceValidator;
039: import com.sun.tools.internal.ws.wsdl.framework.GloballyKnown;
040: import com.sun.tools.internal.ws.wsdl.framework.Kind;
041: import com.sun.tools.internal.ws.wsdl.framework.NoSuchEntityException;
042: import com.sun.tools.internal.ws.wsdl.framework.ValidationException;
043:
044: /**
045: * A WSDL document.
046: *
047: * @author WS Development Team
048: */
049: public class WSDLDocument extends AbstractDocument {
050:
051: public WSDLDocument() {
052: }
053:
054: public Definitions getDefinitions() {
055: return _definitions;
056: }
057:
058: public void setDefinitions(Definitions d) {
059: _definitions = d;
060: }
061:
062: public Set collectAllNamespaces() {
063: Set result = super .collectAllNamespaces();
064: if (_definitions.getTargetNamespaceURI() != null) {
065: result.add(_definitions.getTargetNamespaceURI());
066: }
067: return result;
068: }
069:
070: public QName[] getAllServiceQNames() {
071:
072: ArrayList serviceQNames = new ArrayList();
073:
074: for (Iterator iter = getDefinitions().services(); iter
075: .hasNext();) {
076: Service next = (Service) iter.next();
077: String targetNamespace = next.getDefining()
078: .getTargetNamespaceURI();
079: String localName = next.getName();
080: QName serviceQName = new QName(targetNamespace, localName);
081: serviceQNames.add(serviceQName);
082: }
083: return (QName[]) serviceQNames.toArray(new QName[serviceQNames
084: .size()]);
085: }
086:
087: public QName[] getAllPortQNames() {
088: ArrayList portQNames = new ArrayList();
089:
090: for (Iterator iter = getDefinitions().services(); iter
091: .hasNext();) {
092: Service next = (Service) iter.next();
093: //Iterator ports = next.ports();
094: for (Iterator piter = next.ports(); piter.hasNext();) {
095: // If it's a relative import
096: Port pnext = (Port) piter.next();
097: String targetNamespace = pnext.getDefining()
098: .getTargetNamespaceURI();
099: String localName = pnext.getName();
100: QName portQName = new QName(targetNamespace, localName);
101: portQNames.add(portQName);
102: }
103: }
104: return (QName[]) portQNames
105: .toArray(new QName[portQNames.size()]);
106: }
107:
108: public QName[] getPortQNames(String serviceNameLocalPart) {
109:
110: ArrayList portQNames = new ArrayList();
111:
112: for (Iterator iter = getDefinitions().services(); iter
113: .hasNext();) {
114: Service next = (Service) iter.next();
115: if (next.getName().equals(serviceNameLocalPart)) {
116: for (Iterator piter = next.ports(); piter.hasNext();) {
117: Port pnext = (Port) piter.next();
118: String targetNamespace = pnext.getDefining()
119: .getTargetNamespaceURI();
120: String localName = pnext.getName();
121: QName portQName = new QName(targetNamespace,
122: localName);
123: portQNames.add(portQName);
124: }
125: }
126: }
127: return (QName[]) portQNames
128: .toArray(new QName[portQNames.size()]);
129: }
130:
131: public void accept(WSDLDocumentVisitor visitor) throws Exception {
132: _definitions.accept(visitor);
133: }
134:
135: public void validate(EntityReferenceValidator validator) {
136: GloballyValidatingAction action = new GloballyValidatingAction(
137: this , validator);
138: withAllSubEntitiesDo(action);
139: if (action.getException() != null) {
140: throw action.getException();
141: }
142: }
143:
144: protected Entity getRoot() {
145: return _definitions;
146: }
147:
148: private Definitions _definitions;
149:
150: private class GloballyValidatingAction implements EntityAction,
151: EntityReferenceAction {
152: public GloballyValidatingAction(AbstractDocument document,
153: EntityReferenceValidator validator) {
154: _document = document;
155: _validator = validator;
156: }
157:
158: public void perform(Entity entity) {
159: try {
160: entity.validateThis();
161: entity.withAllEntityReferencesDo(this );
162: entity.withAllSubEntitiesDo(this );
163: } catch (ValidationException e) {
164: if (_exception == null) {
165: _exception = e;
166: }
167: }
168: }
169:
170: public void perform(Kind kind, QName name) {
171: try {
172: GloballyKnown entity = _document.find(kind, name);
173: } catch (NoSuchEntityException e) {
174: // failed to resolve, check with the validator
175: if (_exception == null) {
176: if (_validator == null
177: || !_validator.isValid(kind, name)) {
178: _exception = e;
179: }
180: }
181: }
182: }
183:
184: public ValidationException getException() {
185: return _exception;
186: }
187:
188: private ValidationException _exception;
189: private AbstractDocument _document;
190: private EntityReferenceValidator _validator;
191: }
192: }
|