KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ws > wsgen > generator > axis > wsdl2java > JOnASWSDL2Java


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JOnASWSDL2Java.java,v 1.1 2004/09/09 16:12:37 sauthieg Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas_ws.wsgen.generator.axis.wsdl2java;
26
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.wsdl.WSDLException;
34 import javax.xml.parsers.DocumentBuilder JavaDoc;
35 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
36 import javax.xml.parsers.ParserConfigurationException JavaDoc;
37
38 import org.w3c.dom.Document JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40
41 import org.apache.axis.wsdl.WSDL2Java;
42 import org.apache.axis.wsdl.gen.Parser;
43
44 import org.objectweb.jonas_ws.wsgen.WsGenException;
45 import org.objectweb.jonas_ws.wsgen.generator.axis.AxisWsClientGenerator;
46
47 /**
48  * Programmatic interface to the WSDL2Java Axis tool.
49  * @author Guillaume Sauthier
50  */

51 public class JOnASWSDL2Java extends WSDL2Java {
52
53     /**
54      * Field emitter
55      */

56     private JOnASEmitter jEmitter;
57
58     /**
59      * WSDL Localtion URL
60      */

61     private String JavaDoc wsdlURL = null;
62
63     /**
64      * Parsed WSDL Document
65      */

66     private Document JavaDoc wsdlDoc = null;
67
68     /**
69      * JOnASWSDL2Java Constructor.
70      */

71     public JOnASWSDL2Java() {
72         super();
73         // just cast it once
74
jEmitter = (JOnASEmitter) getParser();
75     }
76
77     /**
78      * @return Returns an extension of the Parser
79      */

80     protected Parser createParser() {
81         return new JOnASEmitter();
82     } // createParser
83

84     /**
85      * Setup the JOnASWSDL2Java generator.
86      * @param wsc WsClientGenerator from wich the configuration will be read.
87      * @throws WsGenException Cannot parse WSDL Document
88      */

89     private void setup(AxisWsClientGenerator wsc) throws WsGenException {
90         setupEmitter(wsc);
91         setupWSDL(wsc);
92     }
93
94     /**
95      * Setup the WSDL Document before giving it to the Emitter.
96      * @param wsc configuration
97      * @throws WsGenException Cannot load WSDL
98      */

99     private void setupWSDL(AxisWsClientGenerator wsc) throws WsGenException {
100         try {
101             if (wsc.getArchive().isPacked()) {
102                 // URL Loading
103
String JavaDoc jarpath = wsc.getArchive().getRootFile().getCanonicalFile().toURL().toExternalForm();
104                 wsdlURL = "jar:" + jarpath + "!/" + wsc.getRef().getWsdlFileName();
105             } else {
106                 // File Loading
107
wsdlURL = new File JavaDoc(wsc.getArchive().getRootFile(), wsc.getRef().getWsdlFileName()).toURL()
108                         .toExternalForm();
109             }
110
111             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
112             factory.setNamespaceAware(true);
113             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
114             wsdlDoc = builder.parse(wsdlURL);
115         } catch (ParserConfigurationException JavaDoc pce) {
116             throw new WsGenException("", pce);
117         } catch (IOException JavaDoc ioe) {
118             throw new WsGenException("", ioe);
119         } catch (SAXException JavaDoc se) {
120             throw new WsGenException("", se);
121         }
122     }
123
124     /**
125      * Setup the JOnASEmitter instance.
126      * @param wsc configuration
127      */

128     private void setupEmitter(AxisWsClientGenerator wsc) {
129         jEmitter.setDebug(wsc.getConfig().isDebug());
130         jEmitter.setVerbose(wsc.getConfig().isVerbose());
131         jEmitter.setNamespaceMap(convert2HashMap(wsc.getRef().getMappingFile().getMappings()));
132         jEmitter.setOutputDir(wsc.getSources().getPath());
133     }
134
135     /**
136      * Converts Hashtable to HashMap.
137      * @param m Hashtable to be converted
138      * @return HashMap converted
139      */

140     private HashMap JavaDoc convert2HashMap(Map JavaDoc m) {
141         HashMap JavaDoc ns2pkg = new HashMap JavaDoc();
142         for (Iterator JavaDoc i = m.keySet().iterator(); i.hasNext();) {
143             String JavaDoc ns = (String JavaDoc) i.next();
144             ns2pkg.put(ns, m.get(ns));
145         }
146         return ns2pkg;
147     }
148
149     /**
150      * Setup and runs the JOnASEmitter.
151      *
152      * @param wsc Configuration.
153      *
154      * @throws WsGenException When WSDL has not been properly parsed
155      * @throws SAXException Cannot parse WSDL document
156      * @throws WSDLException When WSDL is incorrect
157      * @throws ParserConfigurationException Cannot Configure Parser
158      * @throws IOException Import URL cannot be loaded
159      */

160     public void run(AxisWsClientGenerator wsc) throws WsGenException, IOException JavaDoc, SAXException JavaDoc, WSDLException, ParserConfigurationException JavaDoc {
161         setup(wsc);
162         parser.run(wsdlURL, wsdlDoc);
163     } // run
164

165 }
Popular Tags