001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JOnASWSDL2Java.java 5401 2004-09-09 16:12:37Z sauthieg $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ws.wsgen.generator.axis.wsdl2java;
025:
026: import java.io.File;
027: import java.io.IOException;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.Map;
031:
032: import javax.wsdl.WSDLException;
033: import javax.xml.parsers.DocumentBuilder;
034: import javax.xml.parsers.DocumentBuilderFactory;
035: import javax.xml.parsers.ParserConfigurationException;
036:
037: import org.w3c.dom.Document;
038: import org.xml.sax.SAXException;
039:
040: import org.apache.axis.wsdl.WSDL2Java;
041: import org.apache.axis.wsdl.gen.Parser;
042:
043: import org.objectweb.jonas_ws.wsgen.WsGenException;
044: import org.objectweb.jonas_ws.wsgen.generator.axis.AxisWsClientGenerator;
045:
046: /**
047: * Programmatic interface to the WSDL2Java Axis tool.
048: * @author Guillaume Sauthier
049: */
050: public class JOnASWSDL2Java extends WSDL2Java {
051:
052: /**
053: * Field emitter
054: */
055: private JOnASEmitter jEmitter;
056:
057: /**
058: * WSDL Localtion URL
059: */
060: private String wsdlURL = null;
061:
062: /**
063: * Parsed WSDL Document
064: */
065: private Document wsdlDoc = null;
066:
067: /**
068: * JOnASWSDL2Java Constructor.
069: */
070: public JOnASWSDL2Java() {
071: super ();
072: // just cast it once
073: jEmitter = (JOnASEmitter) getParser();
074: }
075:
076: /**
077: * @return Returns an extension of the Parser
078: */
079: protected Parser createParser() {
080: return new JOnASEmitter();
081: } // createParser
082:
083: /**
084: * Setup the JOnASWSDL2Java generator.
085: * @param wsc WsClientGenerator from wich the configuration will be read.
086: * @throws WsGenException Cannot parse WSDL Document
087: */
088: private void setup(AxisWsClientGenerator wsc) throws WsGenException {
089: setupEmitter(wsc);
090: setupWSDL(wsc);
091: }
092:
093: /**
094: * Setup the WSDL Document before giving it to the Emitter.
095: * @param wsc configuration
096: * @throws WsGenException Cannot load WSDL
097: */
098: private void setupWSDL(AxisWsClientGenerator wsc)
099: throws WsGenException {
100: try {
101: if (wsc.getArchive().isPacked()) {
102: // URL Loading
103: String jarpath = wsc.getArchive().getRootFile()
104: .getCanonicalFile().toURL().toExternalForm();
105: wsdlURL = "jar:" + jarpath + "!/"
106: + wsc.getRef().getWsdlFileName();
107: } else {
108: // File Loading
109: wsdlURL = new File(wsc.getArchive().getRootFile(), wsc
110: .getRef().getWsdlFileName()).toURL()
111: .toExternalForm();
112: }
113:
114: DocumentBuilderFactory factory = DocumentBuilderFactory
115: .newInstance();
116: factory.setNamespaceAware(true);
117: DocumentBuilder builder = factory.newDocumentBuilder();
118: wsdlDoc = builder.parse(wsdlURL);
119: } catch (ParserConfigurationException pce) {
120: throw new WsGenException("", pce);
121: } catch (IOException ioe) {
122: throw new WsGenException("", ioe);
123: } catch (SAXException se) {
124: throw new WsGenException("", se);
125: }
126: }
127:
128: /**
129: * Setup the JOnASEmitter instance.
130: * @param wsc configuration
131: */
132: private void setupEmitter(AxisWsClientGenerator wsc) {
133: jEmitter.setDebug(wsc.getConfig().isDebug());
134: jEmitter.setVerbose(wsc.getConfig().isVerbose());
135: jEmitter.setNamespaceMap(convert2HashMap(wsc.getRef()
136: .getMappingFile().getMappings()));
137: jEmitter.setOutputDir(wsc.getSources().getPath());
138: }
139:
140: /**
141: * Converts Hashtable to HashMap.
142: * @param m Hashtable to be converted
143: * @return HashMap converted
144: */
145: private HashMap convert2HashMap(Map m) {
146: HashMap ns2pkg = new HashMap();
147: for (Iterator i = m.keySet().iterator(); i.hasNext();) {
148: String ns = (String) i.next();
149: ns2pkg.put(ns, m.get(ns));
150: }
151: return ns2pkg;
152: }
153:
154: /**
155: * Setup and runs the JOnASEmitter.
156: *
157: * @param wsc Configuration.
158: *
159: * @throws WsGenException When WSDL has not been properly parsed
160: * @throws SAXException Cannot parse WSDL document
161: * @throws WSDLException When WSDL is incorrect
162: * @throws ParserConfigurationException Cannot Configure Parser
163: * @throws IOException Import URL cannot be loaded
164: */
165: public void run(AxisWsClientGenerator wsc) throws WsGenException,
166: IOException, SAXException, WSDLException,
167: ParserConfigurationException {
168: setup(wsc);
169: parser.run(wsdlURL, wsdlDoc);
170: } // run
171:
172: }
|