src/share/jaxws_classes/com/sun/tools/internal/ws/util/WSDLFetcher.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
equal deleted inserted replaced
284:88b85470e72c 286:f50545b5e2f1
1 /*
2 * Copyright (c) 1997, 2010, 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.util;
27
28 import com.sun.istack.internal.NotNull;
29 import com.sun.tools.internal.ws.resources.WscompileMessages;
30 import com.sun.tools.internal.ws.wscompile.WsimportListener;
31 import com.sun.tools.internal.ws.wscompile.WsimportOptions;
32 import com.sun.tools.internal.ws.wsdl.parser.DOMForest;
33 import com.sun.tools.internal.ws.wsdl.parser.MetadataFinder;
34 import com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter;
35 import com.sun.xml.internal.ws.api.server.PortAddressResolver;
36 import com.sun.xml.internal.ws.streaming.SourceReaderFactory;
37 import com.sun.xml.internal.ws.wsdl.parser.WSDLConstants;
38 import com.sun.xml.internal.ws.wsdl.writer.DocumentLocationResolver;
39 import com.sun.xml.internal.ws.wsdl.writer.WSDLPatcher;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Element;
42 import org.w3c.dom.Node;
43 import org.w3c.dom.NodeList;
44
45 import javax.xml.namespace.QName;
46 import javax.xml.stream.XMLOutputFactory;
47 import javax.xml.stream.XMLStreamException;
48 import javax.xml.stream.XMLStreamReader;
49 import javax.xml.stream.XMLStreamWriter;
50 import javax.xml.transform.dom.DOMSource;
51 import java.io.*;
52 import java.net.MalformedURLException;
53 import java.net.URL;
54 import java.util.HashMap;
55 import java.util.Map;
56 import java.util.Set;
57
58 /**
59 * @author Rama Pulavarthi
60 */
61 public class WSDLFetcher {
62 private WsimportOptions options;
63 private WsimportListener listener;
64 public WSDLFetcher(WsimportOptions options, WsimportListener listener) {
65 this.options = options;
66 this.listener = listener;
67 }
68
69
70 /**
71 * Fetches the wsdls in the DOMForest to the options.destDir
72 * @param forest
73 * @return
74 * @throws IOException
75 * @throws XMLStreamException
76 * @throws FileNotFoundException
77 */
78 public String fetchWsdls(MetadataFinder forest) throws IOException, XMLStreamException {
79 String rootWsdl = null;
80 for(String root: forest.getRootDocuments()) {
81 rootWsdl = root;
82 }
83
84 Set<String> externalRefs = forest.getExternalReferences();
85 Map<String,String> documentMap = createDocumentMap(forest, getWSDLDownloadDir(), rootWsdl, externalRefs);
86 String rootWsdlName = fetchFile(rootWsdl,forest, documentMap,getWSDLDownloadDir());
87 for(String reference: forest.getExternalReferences()) {
88 fetchFile(reference,forest,documentMap,getWSDLDownloadDir());
89 }
90 return WSDL_PATH +"/" + rootWsdlName;
91 }
92
93 private String fetchFile(final String doc, DOMForest forest, final Map<String, String> documentMap, File destDir) throws IOException, XMLStreamException {
94
95 DocumentLocationResolver docLocator = createDocResolver(doc, forest, documentMap);
96 WSDLPatcher wsdlPatcher = new WSDLPatcher(new PortAddressResolver() {
97 @Override
98 public String getAddressFor(@NotNull QName serviceName, @NotNull String portName) {
99 return null;
100 }
101 }, docLocator);
102
103 //XMLInputFactory readerFactory = XMLInputFactory.newInstance();
104 //XMLStreamReader xsr = readerFactory.createXMLStreamReader(new DOMSource(forest.get(rootWsdl)));
105
106 XMLStreamReader xsr = SourceReaderFactory.createSourceReader(new DOMSource(forest.get(doc)), false);
107 XMLOutputFactory writerfactory = XMLOutputFactory.newInstance();
108 String resolvedRootWsdl = docLocator.getLocationFor(null, doc);
109 File outFile = new File(destDir, resolvedRootWsdl);
110 OutputStream os = new FileOutputStream(outFile);
111 if(options.verbose) {
112 listener.message(WscompileMessages.WSIMPORT_DOCUMENT_DOWNLOAD(doc,outFile));
113 }
114 XMLStreamWriter xsw = writerfactory.createXMLStreamWriter(os);
115 //DOMForest eats away the whitespace loosing all the indentation, so write it through
116 // indenting writer for better readability of fetched documents
117 IndentingXMLStreamWriter indentingWriter = new IndentingXMLStreamWriter(xsw);
118 wsdlPatcher.bridge(xsr, indentingWriter);
119 xsr.close();
120 xsw.close();
121 os.close();
122 options.addGeneratedFile(outFile);
123 return resolvedRootWsdl;
124
125
126 }
127 private Map<String,String> createDocumentMap(MetadataFinder forest, File baseDir, final String rootWsdl, Set<String> externalReferences) {
128 Map<String,String> map = new HashMap<String,String>();
129 String rootWsdlFileName = rootWsdl;
130 String rootWsdlName;
131
132 int slashIndex = rootWsdl.lastIndexOf("/");
133 if( slashIndex >= 0) {
134 rootWsdlFileName = rootWsdl.substring(slashIndex+1);
135 }
136 if(!rootWsdlFileName.endsWith(WSDL_FILE_EXTENSION)) {
137 Document rootWsdlDoc = forest.get(rootWsdl);
138 NodeList serviceNodes = rootWsdlDoc.getElementsByTagNameNS(WSDLConstants.QNAME_SERVICE.getNamespaceURI(),WSDLConstants.QNAME_SERVICE.getLocalPart());
139 if(serviceNodes.getLength() == 0)
140 rootWsdlName = "Service";
141 else {
142 Node serviceNode = serviceNodes.item(0);
143 String serviceName = ((Element)serviceNode).getAttribute( WSDLConstants.ATTR_NAME);
144 rootWsdlName = serviceName;
145 }
146 rootWsdlFileName = rootWsdlName+ WSDL_FILE_EXTENSION;
147 } else {
148 rootWsdlName = rootWsdlFileName.substring(0,rootWsdlFileName.length()-5);
149 }
150
151 map.put(rootWsdl,sanitize(rootWsdlFileName));
152
153 int i =1;
154 for(String ref: externalReferences) {
155 Document refDoc = forest.get(ref);
156 Element rootEl = refDoc.getDocumentElement();
157 String fileExtn;
158 String fileName = null;
159 int index = ref.lastIndexOf("/");
160 if (index >= 0) {
161 fileName = ref.substring(index + 1);
162 }
163 if(rootEl.getLocalName().equals(WSDLConstants.QNAME_DEFINITIONS.getLocalPart()) && rootEl.getNamespaceURI().equals(WSDLConstants.NS_WSDL)) {
164 fileExtn = WSDL_FILE_EXTENSION;
165 } else if(rootEl.getLocalName().equals(WSDLConstants.QNAME_SCHEMA.getLocalPart()) && rootEl.getNamespaceURI().equals(WSDLConstants.NS_XMLNS)) {
166 fileExtn = SCHEMA_FILE_EXTENSION;
167 } else {
168 fileExtn = ".xml";
169 }
170 if(fileName != null && (fileName.endsWith(WSDL_FILE_EXTENSION) || fileName.endsWith(SCHEMA_FILE_EXTENSION))) {
171 map.put(ref, rootWsdlName+"_"+fileName);
172 } else {
173 map.put(ref, rootWsdlName+"_metadata"+ (i++) + fileExtn);
174 }
175 }
176 return map;
177 }
178
179 private DocumentLocationResolver createDocResolver(final String baseWsdl, final DOMForest forest, final Map<String,String> documentMap) {
180
181 return new DocumentLocationResolver() {
182 public String getLocationFor(String namespaceURI, String systemId) {
183 try {
184 URL reference = new URL(new URL(baseWsdl),systemId);
185 systemId = reference.toExternalForm();
186 } catch (MalformedURLException e) {
187 throw new RuntimeException(e);
188 }
189 if(documentMap.get(systemId) != null) {
190 return documentMap.get(systemId);
191 } else {
192 String parsedEntity = forest.getReferencedEntityMap().get(systemId);
193 return documentMap.get(parsedEntity);
194 }
195 }
196 };
197 }
198
199 private String sanitize(String fileName) {
200 fileName = fileName.replace('?', '.');
201 StringBuffer sb = new StringBuffer(fileName);
202 for (int i = 0; i < sb.length(); i++) {
203 char c = sb.charAt(i);
204 if (Character.isLetterOrDigit(c) ||
205 (c == '/') ||
206 (c == '.') ||
207 (c == '_') ||
208 (c == ' ') ||
209 (c == '-')) {
210 continue;
211 } else {
212 sb.setCharAt(i, '_');
213 }
214 }
215 return sb.toString();
216 }
217
218 private File getWSDLDownloadDir() {
219 File wsdlDir = new File(options.destDir,WSDL_PATH);
220 wsdlDir.mkdirs();
221 return wsdlDir;
222 }
223
224 private static String WSDL_PATH="META-INF/wsdl";
225 private static String WSDL_FILE_EXTENSION=".wsdl";
226 private static String SCHEMA_FILE_EXTENSION=".xsd";
227 }

mercurial