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.xml.internal.ws.server;
027:
028: import com.sun.xml.internal.ws.server.DocInfo.DOC_TYPE;
029: import com.sun.xml.internal.ws.util.ByteArrayBuffer;
030: import com.sun.xml.internal.ws.wsdl.parser.Service;
031: import com.sun.xml.internal.ws.wsdl.writer.WSDLOutputResolver;
032:
033: import javax.xml.transform.Result;
034: import javax.xml.transform.stream.StreamResult;
035: import javax.xml.ws.Holder;
036: import java.io.InputStream;
037: import java.net.URL;
038: import java.util.ArrayList;
039: import java.util.HashMap;
040: import java.util.List;
041: import java.util.Map;
042: import java.util.Map.Entry;
043: import java.util.Set;
044:
045: /**
046: * @author WS Development Team
047: */
048:
049: public class WSDLGenResolver implements WSDLOutputResolver {
050:
051: private Map<String, DocInfo> docs;
052: private DocInfo abstractWsdl;
053: private DocInfo concreteWsdl;
054: private Map<String, List<String>> nsMapping; // targetNS -> system id list
055:
056: public WSDLGenResolver(Map<String, DocInfo> docs) {
057: this .docs = docs;
058: nsMapping = new HashMap<String, List<String>>();
059: Set<Entry<String, DocInfo>> docEntries = docs.entrySet();
060: for (Entry<String, DocInfo> entry : docEntries) {
061: DocInfo docInfo = entry.getValue();
062: if (docInfo.isHavingPortType()) {
063: abstractWsdl = docInfo;
064: }
065: if (docInfo.getDocType() == DOC_TYPE.SCHEMA) {
066: List<String> sysIds = nsMapping.get(docInfo
067: .getTargetNamespace());
068: if (sysIds == null) {
069: sysIds = new ArrayList<String>();
070: nsMapping.put(docInfo.getTargetNamespace(), sysIds);
071: }
072: sysIds.add(docInfo.getUrl().toString());
073: }
074: }
075: }
076:
077: public String getWSDLFile() {
078: return concreteWsdl.getUrl().toString();
079: }
080:
081: public Map<String, DocInfo> getDocs() {
082: return docs;
083: }
084:
085: /*
086: public Result getWSDLOutput(String suggestedFileName) {
087: ByteArrayOutputStream bout = new ByteArrayOutputStream();
088:
089: StreamDocInfo docInfo = new StreamDocInfo(suggestedFileName, bout);
090:
091: if (wsdlFile == null) {
092: docInfo.setQueryString("wsdl");
093: wsdlFile = suggestedFileName;
094: } else {
095: docInfo.setQueryString("wsdl="+suggestedFileName);
096: }
097: docs.put(docInfo.getPath(), docInfo);
098:
099: StreamResult result = new StreamResult();
100: result.setOutputStream(bout);
101: result.setSystemId(suggestedFileName);
102: return result;
103: }
104: */
105:
106: public Result getSchemaOutput(String namespaceUri,
107: String suggestedFileName) {
108: ByteArrayBuffer bout = new ByteArrayBuffer();
109:
110: StreamDocInfo docInfo = new StreamDocInfo(suggestedFileName,
111: bout);
112: docInfo.setQueryString("xsd=" + suggestedFileName);
113: docInfo.setDocType(DOC_TYPE.SCHEMA);
114: docs.put(docInfo.getUrl().toString(), docInfo);
115:
116: StreamResult result = new StreamResult();
117: result.setOutputStream(bout);
118: result.setSystemId(docInfo.getUrl().toString());
119: return result;
120: }
121:
122: /*
123: * return null if concrete WSDL need not be generated
124: */
125: public Result getWSDLOutput(String filename) {
126: ByteArrayBuffer bout = new ByteArrayBuffer();
127: StreamDocInfo docInfo = new StreamDocInfo(filename, bout);
128: docInfo.setDocType(DOC_TYPE.WSDL);
129: docInfo.setQueryString("wsdl");
130: concreteWsdl = docInfo;
131: docs.put(docInfo.getUrl().toString(), docInfo);
132: StreamResult result = new StreamResult();
133: result.setOutputStream(bout);
134: result.setSystemId(docInfo.getUrl().toString());
135: return result;
136: }
137:
138: /*
139: * Updates filename if the suggested filename need to be changed in
140: * wsdl:import
141: *
142: * return null if abstract WSDL need not be generated
143: */
144: public Result getAbstractWSDLOutput(Holder<String> filename) {
145: if (abstractWsdl != null) {
146: filename.value = abstractWsdl.getUrl().toString();
147: return null; // Don't generate abstract WSDL
148: }
149: ByteArrayBuffer bout = new ByteArrayBuffer();
150: StreamDocInfo abstractWsdl = new StreamDocInfo(filename.value,
151: bout);
152: abstractWsdl.setDocType(DOC_TYPE.WSDL);
153: //abstractWsdl.setQueryString("wsdl="+filename.value);
154: docs.put(abstractWsdl.getUrl().toString(), abstractWsdl);
155: StreamResult result = new StreamResult();
156: result.setOutputStream(bout);
157: result.setSystemId(abstractWsdl.getUrl().toString());
158: return result;
159: }
160:
161: /*
162: * Updates filename if the suggested filename need to be changed in
163: * xsd:import
164: *
165: * return null if schema need not be generated
166: */
167: public Result getSchemaOutput(String namespace,
168: Holder<String> filename) {
169: List<String> schemas = nsMapping.get(namespace);
170: if (schemas != null) {
171: if (schemas.size() > 1) {
172: throw new ServerRtException("server.rt.err",
173: "More than one schema for the target namespace "
174: + namespace);
175: }
176: filename.value = schemas.get(0);
177: return null; // Don't generate schema
178: }
179: ByteArrayBuffer bout = new ByteArrayBuffer();
180: StreamDocInfo docInfo = new StreamDocInfo(filename.value, bout);
181: docInfo.setDocType(DOC_TYPE.SCHEMA);
182: //docInfo.setQueryString("xsd="+filename.value);
183: docs.put(docInfo.getUrl().toString(), docInfo);
184: StreamResult result = new StreamResult();
185: result.setOutputStream(bout);
186: result.setSystemId(docInfo.getUrl().toString());
187: return result;
188: }
189:
190: private static class StreamDocInfo implements DocInfo {
191: private ByteArrayBuffer bout;
192: private String resource;
193: private String queryString;
194: private DOC_TYPE docType;
195:
196: public StreamDocInfo(String resource, ByteArrayBuffer bout) {
197: this .resource = resource;
198: this .bout = bout;
199: }
200:
201: public InputStream getDoc() {
202: bout.close();
203: return bout.newInputStream();
204: }
205:
206: public String getPath() {
207: return resource;
208: }
209:
210: public URL getUrl() {
211: try {
212: return new URL("file:///" + resource);
213: } catch (Exception e) {
214:
215: }
216: return null;
217: }
218:
219: public String getQueryString() {
220: return queryString;
221: }
222:
223: public void setQueryString(String queryString) {
224: this .queryString = queryString;
225: }
226:
227: public void setDocType(DOC_TYPE docType) {
228: this .docType = docType;
229: }
230:
231: public DOC_TYPE getDocType() {
232: return docType;
233: }
234:
235: public void setTargetNamespace(String ns) {
236:
237: }
238:
239: public String getTargetNamespace() {
240: return null;
241: }
242:
243: public void setService(Service service) {
244:
245: }
246:
247: public Service getService() {
248: return null;
249: }
250:
251: public void setHavingPortType(boolean portType) {
252:
253: }
254:
255: public boolean isHavingPortType() {
256: return false;
257: }
258: }
259:
260: }
|