KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > jaxws > api > WsdlWrapperGenerator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.websvc.jaxws.api;
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.BufferedOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.net.URL JavaDoc;
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31 import javax.xml.parsers.SAXParser JavaDoc;
32 import javax.xml.parsers.SAXParserFactory JavaDoc;
33 import javax.xml.transform.Source JavaDoc;
34 import javax.xml.transform.Templates JavaDoc;
35 import javax.xml.transform.Transformer JavaDoc;
36 import javax.xml.transform.TransformerConfigurationException JavaDoc;
37 import javax.xml.transform.TransformerException JavaDoc;
38 import javax.xml.transform.TransformerFactory JavaDoc;
39 import javax.xml.transform.URIResolver JavaDoc;
40 import javax.xml.transform.stream.StreamResult JavaDoc;
41 import javax.xml.transform.stream.StreamSource JavaDoc;
42 import org.xml.sax.SAXException JavaDoc;
43
44 /** Utility
45  * This is the case when service element is missing
46  *
47  * @author mkuchtiak
48  */

49 public class WsdlWrapperGenerator {
50     
51     public static WsdlWrapperHandler parse(String JavaDoc wsdlUrl) throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
52         WsdlWrapperHandler handler = new WsdlWrapperHandler();
53         SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
54         factory.setNamespaceAware(true);
55         SAXParser JavaDoc saxParser = factory.newSAXParser();
56         saxParser.parse(wsdlUrl, handler);
57         return handler;
58     }
59     
60     public static WsdlWrapperHandler parse(File JavaDoc file) throws ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
61         WsdlWrapperHandler handler = new WsdlWrapperHandler();
62         SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
63         factory.setNamespaceAware(true);
64         SAXParser JavaDoc saxParser = factory.newSAXParser();
65         saxParser.parse(file, handler);
66         return handler;
67     }
68     
69     private static final String JavaDoc TEMPLATE_BASE="/org/netbeans/modules/websvc/jaxws/resources/"; //NOI8N
70

71     private static Transformer JavaDoc getTransformer() throws TransformerConfigurationException JavaDoc {
72         InputStream JavaDoc is = new BufferedInputStream JavaDoc(WsdlWrapperGenerator.class.getResourceAsStream(TEMPLATE_BASE+"WsdlServiceGenerator.xsl")); //NOI18N
73
TransformerFactory JavaDoc transFactory = TransformerFactory.newInstance();
74         transFactory.setURIResolver(new URIResolver JavaDoc() {
75             public Source JavaDoc resolve(String JavaDoc href, String JavaDoc base)
76             throws TransformerException JavaDoc {
77                 InputStream JavaDoc is = getClass().getResourceAsStream(
78                 TEMPLATE_BASE + href.substring(href.lastIndexOf('/')+1));
79                 if (is == null) {
80                     return null;
81                 }
82                 
83                 return new StreamSource JavaDoc(is);
84             }
85         });
86         Templates JavaDoc t = transFactory.newTemplates(new StreamSource JavaDoc(is));
87         return t.newTransformer();
88     }
89    
90     public static void generateWrapperWSDLContent(File JavaDoc wrapperWsdlFile, StreamSource JavaDoc wsdlSource, String JavaDoc tnsPrefix, String JavaDoc wsdlLocation) throws IOException JavaDoc
91     {
92         //File file = new File("/space/home/mkuchtiak/nb_projects/JavaApplication1/src/javaapplication1/resources/"+wsdlName);
93
/*
94         File wsdlFile = new File(System.getProperty("java.io.tmpdir"), wsdlName); //NOI18N
95         
96         if(!wsdlFile.exists()) {
97             try {
98                 wsdlFile.createNewFile();
99             } catch(IOException ex) {
100                 String mes = NbBundle.getMessage(WebServiceFromWSDLPanel.class, "ERR_UnableToCreateTempFile", wsdlFile.getPath()); // NOI18N
101                 NotifyDescriptor desc = new NotifyDescriptor.Message(mes, NotifyDescriptor.Message.ERROR_MESSAGE);
102                 DialogDisplayer.getDefault().notify(desc);
103                 return null;
104             }
105         }
106         */

107         OutputStream JavaDoc os = null;
108         try {
109             os = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(wrapperWsdlFile));
110             Transformer JavaDoc transformer = getTransformer();
111             transformer.setParameter("tns_prefix",tnsPrefix);
112             transformer.setParameter("wsdl_location",wsdlLocation);
113             transformer.transform(wsdlSource, new StreamResult JavaDoc(os));
114             os.close();
115         }
116         catch(TransformerConfigurationException JavaDoc tce) {
117             IOException JavaDoc ioe = new IOException JavaDoc();
118             ioe.initCause(tce);
119             throw ioe;
120         }
121         catch(TransformerException JavaDoc te) {
122             IOException JavaDoc ioe = new IOException JavaDoc();
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 JavaDoc getWrapperName(URL JavaDoc wsdlURL) {
134         String JavaDoc urlString = wsdlURL.toExternalForm();
135         int start = urlString.lastIndexOf("/"); //NOI18N
136
int end = urlString.lastIndexOf("."); //NOI18N
137
if (start>=0) {
138             if (start<end) return urlString.substring(start+1,end)+"Wrapper.wsdl"; //NOI18N
139
else if (start+1<urlString.length()) return urlString.substring(start+1)+"Wrapper.wsdl"; //NOI18N
140
} else if (end>0) return urlString.substring(0,end)+"Wrapper.wsdl"; //NOI18N
141
return "WsdlWrapper.wsdl"; //NOI18N
142
}
143 }
144
Popular Tags