1 7 package org.jboss.webservice.metadata.wsdl; 8 9 11 import org.jboss.logging.Logger; 12 import org.xml.sax.InputSource ; 13 14 import javax.wsdl.Definition; 15 import javax.wsdl.WSDLException; 16 import javax.wsdl.factory.WSDLFactory; 17 import javax.wsdl.xml.WSDLLocator; 18 import javax.wsdl.xml.WSDLReader; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 24 30 public class WSDL11DefinitionFactory 31 { 32 private static final Logger log = Logger.getLogger(WSDL11DefinitionFactory.class); 34 35 public static final String FEATURE_IMPORT_DOCUMENTS = "javax.wsdl.importDocuments"; 37 public static final String FEATURE_VERBOSE = "javax.wsdl.verbose"; 39 40 private WSDLReader wsdlReader; 42 43 private WSDL11DefinitionFactory() throws WSDLException 45 { 46 WSDLFactory wsdlFactory = WSDLFactory.newInstance(); 47 wsdlReader = wsdlFactory.newWSDLReader(); 48 } 49 50 51 public static WSDL11DefinitionFactory newInstance() throws WSDLException 52 { 53 return new WSDL11DefinitionFactory(); 54 } 55 56 57 public void setFeature(String name, boolean value) throws IllegalArgumentException 58 { 59 wsdlReader.setFeature(name, value); 60 } 61 62 65 public Definition parse(URL wsdlLocation) throws WSDLException 66 { 67 if (wsdlLocation == null) 68 throw new IllegalArgumentException ("URL cannot be null"); 69 70 Definition wsdlDefinition = wsdlReader.readWSDL(new WSDLLocatorImpl(wsdlLocation)); 72 return wsdlDefinition; 73 } 74 75 77 public static class WSDLLocatorImpl implements WSDLLocator 78 { 79 private URL wsdlURL; 80 private String latestImportURI; 81 82 public WSDLLocatorImpl(URL wsdlFile) 83 { 84 if (wsdlFile == null) 85 throw new IllegalArgumentException ("WSDL file argument cannot be null"); 86 87 this.wsdlURL = wsdlFile; 88 } 89 90 public InputSource getBaseInputSource() 91 { 92 log.debug("getBaseInputSource [wsdlUrl=" + wsdlURL + "]"); 93 try 94 { 95 InputStream is = wsdlURL.openStream(); 96 if (is == null) 97 throw new IllegalArgumentException ("Cannot obtain wsdl from [" + wsdlURL + "]"); 98 99 return new InputSource (is); 100 } 101 catch (IOException e) 102 { 103 throw new RuntimeException ("Cannot access wsdl from [" + wsdlURL + "], " + e.getMessage()); 104 } 105 } 106 107 public String getBaseURI() 108 { 109 return wsdlURL.toExternalForm(); 110 } 111 112 public InputSource getImportInputSource(String parent, String resource) 113 { 114 log.debug("getImportInputSource [parent=" + parent + ",resource=" + resource + "]"); 115 116 URL parentURL = null; 117 try 118 { 119 parentURL = new URL (parent); 120 } 121 catch (MalformedURLException e) 122 { 123 log.error("Not a valid URL: " + parent); 124 return null; 125 } 126 127 String wsdlImport = null; 128 String external = parentURL.toExternalForm(); 129 130 if (resource.startsWith("http://") || resource.startsWith("https://")) 132 { 133 wsdlImport = resource; 134 } 135 136 else if (resource.startsWith("/")) 138 { 139 String beforePath = external.substring(0, external.indexOf(parentURL.getPath())); 140 wsdlImport = beforePath + resource; 141 } 142 143 else 145 { 146 String parentDir = external.substring(0, external.lastIndexOf("/")); 147 148 while (resource.startsWith("./")) 150 resource = resource.substring(2); 151 152 while (resource.startsWith("../")) 154 { 155 parentDir = parentDir.substring(0, parentDir.lastIndexOf("/")); 156 resource = resource.substring(3); 157 } 158 159 wsdlImport = parentDir + "/" + resource; 160 } 161 162 try 163 { 164 log.debug("Resolved to: " + wsdlImport); 165 InputStream is = new URL (wsdlImport).openStream(); 166 if (is == null) 167 throw new IllegalArgumentException ("Cannot import wsdl from [" + wsdlImport + "]"); 168 169 latestImportURI = wsdlImport; 170 return new InputSource (is); 171 } 172 catch (IOException e) 173 { 174 throw new RuntimeException ("Cannot access imported wsdl [" + wsdlImport + "], " + e.getMessage()); 175 } 176 } 177 178 public String getLatestImportURI() 179 { 180 return latestImportURI; 181 } 182 } 183 } 184 | Popular Tags |