001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.tool.codegen;
020:
021: import org.apache.axis2.description.AxisService;
022: import org.apache.axis2.description.WSDL11ToAxisServiceBuilder;
023: import org.apache.axis2.util.CommandLineOption;
024: import org.apache.axis2.util.CommandLineOptionConstants;
025:
026: import javax.wsdl.WSDLException;
027:
028: import java.io.File;
029: import java.io.IOException;
030: import java.net.MalformedURLException;
031: import java.net.URL;
032: import java.util.HashMap;
033: import java.util.Map;
034:
035: public class WSDL2JavaGenerator {
036:
037: /**
038: * Maps a string containing the name of a language to a constant defined in CommandLineOptionConstants.LanguageNames
039: *
040: * @param UILangValue a string containg a language, e.g. "java", "cs", "cpp" or "vb"
041: * @return a normalized string constant
042: */
043: private String mapLanguagesWithCombo(String UILangValue) {
044: return UILangValue;
045: }
046:
047: /**
048: * Creates a list of parameters for the code generator based on the decisions made by the user on the OptionsPage
049: * (page2). For each setting, there is a Command-Line option for the Axis2 code generator.
050: *
051: * @return a Map with keys from CommandLineOptionConstants with the values entered by the user on the Options Page.
052: */
053: public Map fillOptionMap(boolean isAyncOnly, boolean isSyncOnly,
054: boolean isServerSide, boolean isServerXML,
055: boolean isTestCase, boolean isGenerateAll,
056: String serviceName, String portName,
057: String databindingName, String WSDLURI, String packageName,
058: String selectedLanguage, String outputLocation,
059: String namespace2packageList, boolean isServerSideInterface) {
060: Map optionMap = new HashMap();
061: //WSDL file name
062: optionMap
063: .put(
064: CommandLineOptionConstants.WSDL2JavaConstants.WSDL_LOCATION_URI_OPTION,
065: new CommandLineOption(
066: CommandLineOptionConstants.WSDL2JavaConstants.WSDL_LOCATION_URI_OPTION,
067: getStringArray(WSDLURI)));
068:
069: //Async only
070: if (isAyncOnly) {
071: optionMap
072: .put(
073: CommandLineOptionConstants.WSDL2JavaConstants.CODEGEN_ASYNC_ONLY_OPTION,
074: new CommandLineOption(
075: CommandLineOptionConstants.WSDL2JavaConstants.CODEGEN_ASYNC_ONLY_OPTION,
076: new String[0]));
077: }
078: //sync only
079: if (isSyncOnly) {
080: optionMap
081: .put(
082: CommandLineOptionConstants.WSDL2JavaConstants.CODEGEN_SYNC_ONLY_OPTION,
083: new CommandLineOption(
084: CommandLineOptionConstants.WSDL2JavaConstants.CODEGEN_SYNC_ONLY_OPTION,
085: new String[0]));
086: }
087: //serverside
088: if (isServerSide) {
089: optionMap
090: .put(
091: CommandLineOptionConstants.WSDL2JavaConstants.SERVER_SIDE_CODE_OPTION,
092: new CommandLineOption(
093: CommandLineOptionConstants.WSDL2JavaConstants.SERVER_SIDE_CODE_OPTION,
094: new String[0]));
095: //server xml
096: if (isServerXML) {
097: optionMap
098: .put(
099: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_SERVICE_DESCRIPTION_OPTION,
100: new CommandLineOption(
101: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_SERVICE_DESCRIPTION_OPTION,
102: new String[0]));
103: }
104: if (isGenerateAll) {
105: optionMap
106: .put(
107: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_ALL_OPTION,
108: new CommandLineOption(
109: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_ALL_OPTION,
110: new String[0]));
111: }
112: }
113: //test case
114: if (isTestCase) {
115: optionMap
116: .put(
117: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_TEST_CASE_OPTION,
118: new CommandLineOption(
119: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_TEST_CASE_OPTION,
120: new String[0]));
121: }
122: //package name
123: optionMap
124: .put(
125: CommandLineOptionConstants.WSDL2JavaConstants.PACKAGE_OPTION,
126: new CommandLineOption(
127: CommandLineOptionConstants.WSDL2JavaConstants.PACKAGE_OPTION,
128: getStringArray(packageName)));
129: //selected language
130: optionMap
131: .put(
132: CommandLineOptionConstants.WSDL2JavaConstants.STUB_LANGUAGE_OPTION,
133: new CommandLineOption(
134: CommandLineOptionConstants.WSDL2JavaConstants.STUB_LANGUAGE_OPTION,
135: getStringArray(mapLanguagesWithCombo(selectedLanguage))));
136: //output location
137: optionMap
138: .put(
139: CommandLineOptionConstants.WSDL2JavaConstants.OUTPUT_LOCATION_OPTION,
140: new CommandLineOption(
141: CommandLineOptionConstants.WSDL2JavaConstants.OUTPUT_LOCATION_OPTION,
142: getStringArray(outputLocation)));
143:
144: //databinding
145: optionMap
146: .put(
147: CommandLineOptionConstants.WSDL2JavaConstants.DATA_BINDING_TYPE_OPTION,
148: new CommandLineOption(
149: CommandLineOptionConstants.WSDL2JavaConstants.DATA_BINDING_TYPE_OPTION,
150: getStringArray(databindingName)));
151:
152: //port name
153: if (portName != null) {
154: optionMap
155: .put(
156: CommandLineOptionConstants.WSDL2JavaConstants.PORT_NAME_OPTION,
157: new CommandLineOption(
158: CommandLineOptionConstants.WSDL2JavaConstants.PORT_NAME_OPTION,
159: getStringArray(portName)));
160: }
161: //service name
162: if (serviceName != null) {
163: optionMap
164: .put(
165: CommandLineOptionConstants.WSDL2JavaConstants.SERVICE_NAME_OPTION,
166: new CommandLineOption(
167: CommandLineOptionConstants.WSDL2JavaConstants.SERVICE_NAME_OPTION,
168: getStringArray(serviceName)));
169: }
170: //ns2pkg mapping
171: if (namespace2packageList != null) {
172: optionMap
173: .put(
174: CommandLineOptionConstants.WSDL2JavaConstants.NAME_SPACE_TO_PACKAGE_OPTION,
175: new CommandLineOption(
176: CommandLineOptionConstants.WSDL2JavaConstants.NAME_SPACE_TO_PACKAGE_OPTION,
177: getStringArray(namespace2packageList)));
178: }
179:
180: //server side interface mapping
181: if (isServerSideInterface) {
182: optionMap
183: .put(
184: CommandLineOptionConstants.WSDL2JavaConstants.SERVER_SIDE_INTERFACE_OPTION,
185: new CommandLineOption(
186: CommandLineOptionConstants.WSDL2JavaConstants.SERVER_SIDE_INTERFACE_OPTION,
187: new String[0]));
188: }
189: return optionMap;
190:
191: }
192:
193: public String getBaseUri(String wsdlURI) {
194:
195: try {
196: URL url;
197: if (wsdlURI.indexOf("://") == -1) {
198: url = new URL("file", "", wsdlURI);
199: } else {
200: url = new URL(wsdlURI);
201: }
202:
203: String baseUri;
204: if ("file".equals(url.getProtocol())) {
205: baseUri = new File(url.getFile()).getParentFile()
206: .toURL().toExternalForm();
207: } else {
208: baseUri = url.toExternalForm().substring(0,
209: url.toExternalForm().lastIndexOf("/"));
210: }
211:
212: return baseUri;
213: } catch (MalformedURLException e) {
214: throw new RuntimeException(e);
215: }
216: }
217:
218: /**
219: * Reads the WSDL Object Model from the given location.
220: *
221: * @param wsdlURI the filesystem location (full path) of the WSDL file to read in.
222: * @return the WSDLDescription object containing the WSDL Object Model of the given WSDL file
223: * @throws WSDLException when WSDL File is invalid
224: * @throws IOException on errors reading the WSDL file
225: */
226: public AxisService getAxisService(String wsdlURI) throws Exception {
227:
228: URL url;
229: if (wsdlURI.indexOf("://") == -1) {
230: url = new URL("file", "", wsdlURI);
231: } else {
232: url = new URL(wsdlURI);
233: }
234:
235: //FIXME @author soi -
236: // This quick fix assume that the wsdlURI points to a wsdl 1.1 version.
237: // A better fix should be to determine which builder to use based on the wsdl version.
238: // The current implementation of the wsdl builder classes did not allow for this. I will suggest
239: // that the determination of which builder to use should be done in the builder classes, preferably
240: // in the parent builder class.
241: // Accessable through a static reference to a method like getBuilderInstance(String wsdlURI) in
242: // the parent builder class or through a builder Abstract Factor or Abstract factory methods.
243:
244: WSDL11ToAxisServiceBuilder builder = new WSDL11ToAxisServiceBuilder(
245: url.openConnection().getInputStream());
246:
247: builder.setBaseUri(getBaseUri(wsdlURI));
248: builder.setCodegen(true);
249: return builder.populateService();
250: }
251:
252: /**
253: * Converts a single String into a String Array
254: *
255: * @param value a single string
256: * @return an array containing only one element
257: */
258: private String[] getStringArray(String value) {
259: String[] values = new String[1];
260: values[0] = value;
261: return values;
262: }
263: }
|