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

Tue, 06 Mar 2012 16:09:35 -0800

author
ohair
date
Tue, 06 Mar 2012 16:09:35 -0800
changeset 286
f50545b5e2f1
child 368
0989ad8c0860
permissions
-rw-r--r--

7150322: Stop using drop source bundles in jaxws
Reviewed-by: darcy, ohrstrom

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.tools.internal.ws.util;
ohair@286 27
ohair@286 28 import com.sun.istack.internal.NotNull;
ohair@286 29 import com.sun.tools.internal.ws.resources.WscompileMessages;
ohair@286 30 import com.sun.tools.internal.ws.wscompile.WsimportListener;
ohair@286 31 import com.sun.tools.internal.ws.wscompile.WsimportOptions;
ohair@286 32 import com.sun.tools.internal.ws.wsdl.parser.DOMForest;
ohair@286 33 import com.sun.tools.internal.ws.wsdl.parser.MetadataFinder;
ohair@286 34 import com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter;
ohair@286 35 import com.sun.xml.internal.ws.api.server.PortAddressResolver;
ohair@286 36 import com.sun.xml.internal.ws.streaming.SourceReaderFactory;
ohair@286 37 import com.sun.xml.internal.ws.wsdl.parser.WSDLConstants;
ohair@286 38 import com.sun.xml.internal.ws.wsdl.writer.DocumentLocationResolver;
ohair@286 39 import com.sun.xml.internal.ws.wsdl.writer.WSDLPatcher;
ohair@286 40 import org.w3c.dom.Document;
ohair@286 41 import org.w3c.dom.Element;
ohair@286 42 import org.w3c.dom.Node;
ohair@286 43 import org.w3c.dom.NodeList;
ohair@286 44
ohair@286 45 import javax.xml.namespace.QName;
ohair@286 46 import javax.xml.stream.XMLOutputFactory;
ohair@286 47 import javax.xml.stream.XMLStreamException;
ohair@286 48 import javax.xml.stream.XMLStreamReader;
ohair@286 49 import javax.xml.stream.XMLStreamWriter;
ohair@286 50 import javax.xml.transform.dom.DOMSource;
ohair@286 51 import java.io.*;
ohair@286 52 import java.net.MalformedURLException;
ohair@286 53 import java.net.URL;
ohair@286 54 import java.util.HashMap;
ohair@286 55 import java.util.Map;
ohair@286 56 import java.util.Set;
ohair@286 57
ohair@286 58 /**
ohair@286 59 * @author Rama Pulavarthi
ohair@286 60 */
ohair@286 61 public class WSDLFetcher {
ohair@286 62 private WsimportOptions options;
ohair@286 63 private WsimportListener listener;
ohair@286 64 public WSDLFetcher(WsimportOptions options, WsimportListener listener) {
ohair@286 65 this.options = options;
ohair@286 66 this.listener = listener;
ohair@286 67 }
ohair@286 68
ohair@286 69
ohair@286 70 /**
ohair@286 71 * Fetches the wsdls in the DOMForest to the options.destDir
ohair@286 72 * @param forest
ohair@286 73 * @return
ohair@286 74 * @throws IOException
ohair@286 75 * @throws XMLStreamException
ohair@286 76 * @throws FileNotFoundException
ohair@286 77 */
ohair@286 78 public String fetchWsdls(MetadataFinder forest) throws IOException, XMLStreamException {
ohair@286 79 String rootWsdl = null;
ohair@286 80 for(String root: forest.getRootDocuments()) {
ohair@286 81 rootWsdl = root;
ohair@286 82 }
ohair@286 83
ohair@286 84 Set<String> externalRefs = forest.getExternalReferences();
ohair@286 85 Map<String,String> documentMap = createDocumentMap(forest, getWSDLDownloadDir(), rootWsdl, externalRefs);
ohair@286 86 String rootWsdlName = fetchFile(rootWsdl,forest, documentMap,getWSDLDownloadDir());
ohair@286 87 for(String reference: forest.getExternalReferences()) {
ohair@286 88 fetchFile(reference,forest,documentMap,getWSDLDownloadDir());
ohair@286 89 }
ohair@286 90 return WSDL_PATH +"/" + rootWsdlName;
ohair@286 91 }
ohair@286 92
ohair@286 93 private String fetchFile(final String doc, DOMForest forest, final Map<String, String> documentMap, File destDir) throws IOException, XMLStreamException {
ohair@286 94
ohair@286 95 DocumentLocationResolver docLocator = createDocResolver(doc, forest, documentMap);
ohair@286 96 WSDLPatcher wsdlPatcher = new WSDLPatcher(new PortAddressResolver() {
ohair@286 97 @Override
ohair@286 98 public String getAddressFor(@NotNull QName serviceName, @NotNull String portName) {
ohair@286 99 return null;
ohair@286 100 }
ohair@286 101 }, docLocator);
ohair@286 102
ohair@286 103 //XMLInputFactory readerFactory = XMLInputFactory.newInstance();
ohair@286 104 //XMLStreamReader xsr = readerFactory.createXMLStreamReader(new DOMSource(forest.get(rootWsdl)));
ohair@286 105
ohair@286 106 XMLStreamReader xsr = SourceReaderFactory.createSourceReader(new DOMSource(forest.get(doc)), false);
ohair@286 107 XMLOutputFactory writerfactory = XMLOutputFactory.newInstance();
ohair@286 108 String resolvedRootWsdl = docLocator.getLocationFor(null, doc);
ohair@286 109 File outFile = new File(destDir, resolvedRootWsdl);
ohair@286 110 OutputStream os = new FileOutputStream(outFile);
ohair@286 111 if(options.verbose) {
ohair@286 112 listener.message(WscompileMessages.WSIMPORT_DOCUMENT_DOWNLOAD(doc,outFile));
ohair@286 113 }
ohair@286 114 XMLStreamWriter xsw = writerfactory.createXMLStreamWriter(os);
ohair@286 115 //DOMForest eats away the whitespace loosing all the indentation, so write it through
ohair@286 116 // indenting writer for better readability of fetched documents
ohair@286 117 IndentingXMLStreamWriter indentingWriter = new IndentingXMLStreamWriter(xsw);
ohair@286 118 wsdlPatcher.bridge(xsr, indentingWriter);
ohair@286 119 xsr.close();
ohair@286 120 xsw.close();
ohair@286 121 os.close();
ohair@286 122 options.addGeneratedFile(outFile);
ohair@286 123 return resolvedRootWsdl;
ohair@286 124
ohair@286 125
ohair@286 126 }
ohair@286 127 private Map<String,String> createDocumentMap(MetadataFinder forest, File baseDir, final String rootWsdl, Set<String> externalReferences) {
ohair@286 128 Map<String,String> map = new HashMap<String,String>();
ohair@286 129 String rootWsdlFileName = rootWsdl;
ohair@286 130 String rootWsdlName;
ohair@286 131
ohair@286 132 int slashIndex = rootWsdl.lastIndexOf("/");
ohair@286 133 if( slashIndex >= 0) {
ohair@286 134 rootWsdlFileName = rootWsdl.substring(slashIndex+1);
ohair@286 135 }
ohair@286 136 if(!rootWsdlFileName.endsWith(WSDL_FILE_EXTENSION)) {
ohair@286 137 Document rootWsdlDoc = forest.get(rootWsdl);
ohair@286 138 NodeList serviceNodes = rootWsdlDoc.getElementsByTagNameNS(WSDLConstants.QNAME_SERVICE.getNamespaceURI(),WSDLConstants.QNAME_SERVICE.getLocalPart());
ohair@286 139 if(serviceNodes.getLength() == 0)
ohair@286 140 rootWsdlName = "Service";
ohair@286 141 else {
ohair@286 142 Node serviceNode = serviceNodes.item(0);
ohair@286 143 String serviceName = ((Element)serviceNode).getAttribute( WSDLConstants.ATTR_NAME);
ohair@286 144 rootWsdlName = serviceName;
ohair@286 145 }
ohair@286 146 rootWsdlFileName = rootWsdlName+ WSDL_FILE_EXTENSION;
ohair@286 147 } else {
ohair@286 148 rootWsdlName = rootWsdlFileName.substring(0,rootWsdlFileName.length()-5);
ohair@286 149 }
ohair@286 150
ohair@286 151 map.put(rootWsdl,sanitize(rootWsdlFileName));
ohair@286 152
ohair@286 153 int i =1;
ohair@286 154 for(String ref: externalReferences) {
ohair@286 155 Document refDoc = forest.get(ref);
ohair@286 156 Element rootEl = refDoc.getDocumentElement();
ohair@286 157 String fileExtn;
ohair@286 158 String fileName = null;
ohair@286 159 int index = ref.lastIndexOf("/");
ohair@286 160 if (index >= 0) {
ohair@286 161 fileName = ref.substring(index + 1);
ohair@286 162 }
ohair@286 163 if(rootEl.getLocalName().equals(WSDLConstants.QNAME_DEFINITIONS.getLocalPart()) && rootEl.getNamespaceURI().equals(WSDLConstants.NS_WSDL)) {
ohair@286 164 fileExtn = WSDL_FILE_EXTENSION;
ohair@286 165 } else if(rootEl.getLocalName().equals(WSDLConstants.QNAME_SCHEMA.getLocalPart()) && rootEl.getNamespaceURI().equals(WSDLConstants.NS_XMLNS)) {
ohair@286 166 fileExtn = SCHEMA_FILE_EXTENSION;
ohair@286 167 } else {
ohair@286 168 fileExtn = ".xml";
ohair@286 169 }
ohair@286 170 if(fileName != null && (fileName.endsWith(WSDL_FILE_EXTENSION) || fileName.endsWith(SCHEMA_FILE_EXTENSION))) {
ohair@286 171 map.put(ref, rootWsdlName+"_"+fileName);
ohair@286 172 } else {
ohair@286 173 map.put(ref, rootWsdlName+"_metadata"+ (i++) + fileExtn);
ohair@286 174 }
ohair@286 175 }
ohair@286 176 return map;
ohair@286 177 }
ohair@286 178
ohair@286 179 private DocumentLocationResolver createDocResolver(final String baseWsdl, final DOMForest forest, final Map<String,String> documentMap) {
ohair@286 180
ohair@286 181 return new DocumentLocationResolver() {
ohair@286 182 public String getLocationFor(String namespaceURI, String systemId) {
ohair@286 183 try {
ohair@286 184 URL reference = new URL(new URL(baseWsdl),systemId);
ohair@286 185 systemId = reference.toExternalForm();
ohair@286 186 } catch (MalformedURLException e) {
ohair@286 187 throw new RuntimeException(e);
ohair@286 188 }
ohair@286 189 if(documentMap.get(systemId) != null) {
ohair@286 190 return documentMap.get(systemId);
ohair@286 191 } else {
ohair@286 192 String parsedEntity = forest.getReferencedEntityMap().get(systemId);
ohair@286 193 return documentMap.get(parsedEntity);
ohair@286 194 }
ohair@286 195 }
ohair@286 196 };
ohair@286 197 }
ohair@286 198
ohair@286 199 private String sanitize(String fileName) {
ohair@286 200 fileName = fileName.replace('?', '.');
ohair@286 201 StringBuffer sb = new StringBuffer(fileName);
ohair@286 202 for (int i = 0; i < sb.length(); i++) {
ohair@286 203 char c = sb.charAt(i);
ohair@286 204 if (Character.isLetterOrDigit(c) ||
ohair@286 205 (c == '/') ||
ohair@286 206 (c == '.') ||
ohair@286 207 (c == '_') ||
ohair@286 208 (c == ' ') ||
ohair@286 209 (c == '-')) {
ohair@286 210 continue;
ohair@286 211 } else {
ohair@286 212 sb.setCharAt(i, '_');
ohair@286 213 }
ohair@286 214 }
ohair@286 215 return sb.toString();
ohair@286 216 }
ohair@286 217
ohair@286 218 private File getWSDLDownloadDir() {
ohair@286 219 File wsdlDir = new File(options.destDir,WSDL_PATH);
ohair@286 220 wsdlDir.mkdirs();
ohair@286 221 return wsdlDir;
ohair@286 222 }
ohair@286 223
ohair@286 224 private static String WSDL_PATH="META-INF/wsdl";
ohair@286 225 private static String WSDL_FILE_EXTENSION=".wsdl";
ohair@286 226 private static String SCHEMA_FILE_EXTENSION=".xsd";
ohair@286 227 }

mercurial