1 19 20 package org.netbeans.modules.websvc.jaxws.api; 21 22 import java.io.BufferedInputStream ; 23 import java.io.BufferedOutputStream ; 24 import java.io.File ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.net.URL ; 30 import javax.xml.parsers.ParserConfigurationException ; 31 import javax.xml.parsers.SAXParser ; 32 import javax.xml.parsers.SAXParserFactory ; 33 import javax.xml.transform.Source ; 34 import javax.xml.transform.Templates ; 35 import javax.xml.transform.Transformer ; 36 import javax.xml.transform.TransformerConfigurationException ; 37 import javax.xml.transform.TransformerException ; 38 import javax.xml.transform.TransformerFactory ; 39 import javax.xml.transform.URIResolver ; 40 import javax.xml.transform.stream.StreamResult ; 41 import javax.xml.transform.stream.StreamSource ; 42 import org.xml.sax.SAXException ; 43 44 49 public class WsdlWrapperGenerator { 50 51 public static WsdlWrapperHandler parse(String wsdlUrl) throws ParserConfigurationException , SAXException , IOException { 52 WsdlWrapperHandler handler = new WsdlWrapperHandler(); 53 SAXParserFactory factory = SAXParserFactory.newInstance(); 54 factory.setNamespaceAware(true); 55 SAXParser saxParser = factory.newSAXParser(); 56 saxParser.parse(wsdlUrl, handler); 57 return handler; 58 } 59 60 public static WsdlWrapperHandler parse(File file) throws ParserConfigurationException , SAXException , IOException { 61 WsdlWrapperHandler handler = new WsdlWrapperHandler(); 62 SAXParserFactory factory = SAXParserFactory.newInstance(); 63 factory.setNamespaceAware(true); 64 SAXParser saxParser = factory.newSAXParser(); 65 saxParser.parse(file, handler); 66 return handler; 67 } 68 69 private static final String TEMPLATE_BASE="/org/netbeans/modules/websvc/jaxws/resources/"; 71 private static Transformer getTransformer() throws TransformerConfigurationException { 72 InputStream is = new BufferedInputStream (WsdlWrapperGenerator.class.getResourceAsStream(TEMPLATE_BASE+"WsdlServiceGenerator.xsl")); TransformerFactory transFactory = TransformerFactory.newInstance(); 74 transFactory.setURIResolver(new URIResolver () { 75 public Source resolve(String href, String base) 76 throws TransformerException { 77 InputStream is = getClass().getResourceAsStream( 78 TEMPLATE_BASE + href.substring(href.lastIndexOf('/')+1)); 79 if (is == null) { 80 return null; 81 } 82 83 return new StreamSource (is); 84 } 85 }); 86 Templates t = transFactory.newTemplates(new StreamSource (is)); 87 return t.newTransformer(); 88 } 89 90 public static void generateWrapperWSDLContent(File wrapperWsdlFile, StreamSource wsdlSource, String tnsPrefix, String wsdlLocation) throws IOException 91 { 92 107 OutputStream os = null; 108 try { 109 os = new BufferedOutputStream (new FileOutputStream (wrapperWsdlFile)); 110 Transformer transformer = getTransformer(); 111 transformer.setParameter("tns_prefix",tnsPrefix); 112 transformer.setParameter("wsdl_location",wsdlLocation); 113 transformer.transform(wsdlSource, new StreamResult (os)); 114 os.close(); 115 } 116 catch(TransformerConfigurationException tce) { 117 IOException ioe = new IOException (); 118 ioe.initCause(tce); 119 throw ioe; 120 } 121 catch(TransformerException te) { 122 IOException ioe = new IOException (); 123 ioe.initCause(te); 124 throw ioe; 125 } 126 finally { 127 if(os != null) { 128 os.close(); 129 } 130 } 131 } 132 133 public static String getWrapperName(URL wsdlURL) { 134 String urlString = wsdlURL.toExternalForm(); 135 int start = urlString.lastIndexOf("/"); int end = urlString.lastIndexOf("."); if (start>=0) { 138 if (start<end) return urlString.substring(start+1,end)+"Wrapper.wsdl"; else if (start+1<urlString.length()) return urlString.substring(start+1)+"Wrapper.wsdl"; } else if (end>0) return urlString.substring(0,end)+"Wrapper.wsdl"; return "WsdlWrapper.wsdl"; } 143 } 144 | Popular Tags |