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.wsdl.codegen;
020:
021: import org.apache.axis2.util.CommandLineOption;
022: import org.apache.axis2.util.CommandLineOptionConstants;
023: import org.apache.axis2.wsdl.i18n.CodegenMessages;
024: import org.apache.axis2.wsdl.codegen.extension.XMLBeansExtension;
025:
026: import java.io.File;
027: import java.io.FileInputStream;
028: import java.io.IOException;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.Map;
032: import java.util.Properties;
033:
034: class CodegenConfigLoader implements CommandLineOptionConstants {
035:
036: public static void loadConfig(CodeGenConfiguration config,
037: Map optionMap) {
038: String outputLocation = "."; //default output directory is the current working directory
039: CommandLineOption commandLineOption = loadOption(
040: WSDL2JavaConstants.OUTPUT_LOCATION_OPTION,
041: WSDL2JavaConstants.OUTPUT_LOCATION_OPTION_LONG,
042: optionMap);
043:
044: if (commandLineOption != null) {
045: outputLocation = commandLineOption.getOptionValue();
046: }
047: File outputLocationFile = new File(outputLocation);
048: config.setOutputLocation(outputLocationFile);
049:
050: //check and create the directories
051: if (outputLocationFile.exists()) {
052: if (outputLocationFile.isFile()) {
053: throw new RuntimeException(CodegenMessages
054: .getMessage("options.notADirectoryException"));
055: }
056: } else {
057: outputLocationFile.mkdirs();
058: }
059:
060: config.setServerSide(loadOption(
061: WSDL2JavaConstants.SERVER_SIDE_CODE_OPTION,
062: WSDL2JavaConstants.SERVER_SIDE_CODE_OPTION_LONG,
063: optionMap) != null);
064: config
065: .setGenerateDeployementDescriptor(loadOption(
066: WSDL2JavaConstants.GENERATE_SERVICE_DESCRIPTION_OPTION,
067: WSDL2JavaConstants.GENERATE_SERVICE_DESCRIPTION_OPTION_LONG,
068: optionMap) != null);
069: config.setWriteTestCase(loadOption(
070: WSDL2JavaConstants.GENERATE_TEST_CASE_OPTION,
071: WSDL2JavaConstants.GENERATE_TEST_CASE_OPTION_LONG,
072: optionMap) != null);
073: config
074: .setSkipWriteWSDLs(loadOption(null,
075: WSDL2JavaConstants.NO_WSDLS_OPTION_LONG,
076: optionMap) != null);
077: config.setSkipMessageReceiver(loadOption(null,
078: WSDL2JavaConstants.NO_MESSAGE_RECEIVER_OPTION_LONG,
079: optionMap) != null);
080: config
081: .setSkipBuildXML(loadOption(null,
082: WSDL2JavaConstants.NO_BUILD_XML_OPTION_LONG,
083: optionMap) != null);
084:
085: boolean asyncFlagPresent = (loadOption(
086: WSDL2JavaConstants.CODEGEN_ASYNC_ONLY_OPTION,
087: WSDL2JavaConstants.CODEGEN_ASYNC_ONLY_OPTION_LONG,
088: optionMap) != null);
089: boolean syncFlagPresent = (loadOption(
090: WSDL2JavaConstants.CODEGEN_SYNC_ONLY_OPTION,
091: WSDL2JavaConstants.CODEGEN_SYNC_ONLY_OPTION_LONG,
092: optionMap) != null);
093: if (asyncFlagPresent && !syncFlagPresent) {
094: config.setAsyncOn(true);
095: config.setSyncOn(false);
096: }
097: if (syncFlagPresent && !asyncFlagPresent) {
098: config.setAsyncOn(false);
099: config.setSyncOn(true);
100: }
101:
102: commandLineOption = loadOption(
103: WSDL2JavaConstants.PACKAGE_OPTION,
104: WSDL2JavaConstants.PACKAGE_OPTION_LONG, optionMap);
105: if (commandLineOption != null) {
106: config.setPackageName(commandLineOption.getOptionValue());
107: }
108:
109: commandLineOption = loadOption(
110: WSDL2JavaConstants.STUB_LANGUAGE_OPTION,
111: WSDL2JavaConstants.STUB_LANGUAGE_OPTION_LONG, optionMap);
112: if (commandLineOption != null) {
113: config
114: .setOutputLanguage(commandLineOption
115: .getOptionValue());
116: }
117:
118: commandLineOption = loadOption(
119: WSDL2JavaConstants.DATA_BINDING_TYPE_OPTION,
120: WSDL2JavaConstants.DATA_BINDING_TYPE_OPTION_LONG,
121: optionMap);
122: if (commandLineOption != null) {
123: config.setDatabindingType(commandLineOption
124: .getOptionValue());
125: }
126:
127: commandLineOption = loadOption(
128: WSDL2JavaConstants.UNPACK_CLASSES_OPTION,
129: WSDL2JavaConstants.UNPACK_CLASSES_OPTION_LONG,
130: optionMap);
131: if (commandLineOption != null) {
132: config.setPackClasses(false);
133: }
134:
135: // source folder
136: commandLineOption = loadOption(
137: WSDL2JavaConstants.SOURCE_FOLDER_NAME_OPTION,
138: WSDL2JavaConstants.SOURCE_FOLDER_NAME_OPTION_LONG,
139: optionMap);
140: if (commandLineOption != null) {
141: config
142: .setSourceLocation(commandLineOption
143: .getOptionValue());
144: }
145:
146: // resource folder
147: commandLineOption = loadOption(
148: WSDL2JavaConstants.RESOURCE_FOLDER_OPTION,
149: WSDL2JavaConstants.RESOURCE_FOLDER_OPTION_LONG,
150: optionMap);
151: if (commandLineOption != null) {
152: config.setResourceLocation(commandLineOption
153: .getOptionValue());
154: }
155:
156: commandLineOption = loadOption(
157: WSDL2JavaConstants.PORT_NAME_OPTION,
158: WSDL2JavaConstants.PORT_NAME_OPTION_LONG, optionMap);
159: config
160: .setPortName(commandLineOption != null ? commandLineOption
161: .getOptionValue()
162: : null);
163:
164: commandLineOption = loadOption(
165: WSDL2JavaConstants.SERVICE_NAME_OPTION,
166: WSDL2JavaConstants.SERVICE_NAME_OPTION_LONG, optionMap);
167: config
168: .setServiceName(commandLineOption != null ? commandLineOption
169: .getOptionValue()
170: : null);
171:
172: commandLineOption = loadOption(
173: WSDL2JavaConstants.REPOSITORY_PATH_OPTION,
174: WSDL2JavaConstants.REPOSITORY_PATH_OPTION_LONG,
175: optionMap);
176: config
177: .setRepositoryPath(commandLineOption != null ? commandLineOption
178: .getOptionValue()
179: : null);
180:
181: config.setServerSideInterface(loadOption(
182: WSDL2JavaConstants.SERVER_SIDE_INTERFACE_OPTION,
183: WSDL2JavaConstants.SERVER_SIDE_INTERFACE_OPTION_LONG,
184: optionMap) != null);
185:
186: config
187: .setGenerateAll(loadOption(
188: WSDL2JavaConstants.GENERATE_ALL_OPTION,
189: WSDL2JavaConstants.GENERATE_ALL_OPTION_LONG,
190: optionMap) != null);
191:
192: //populate the external mapping
193: commandLineOption = loadOption(
194: WSDL2JavaConstants.EXTERNAL_MAPPING_OPTION,
195: WSDL2JavaConstants.EXTERNAL_MAPPING_OPTION_LONG,
196: optionMap);
197: if (commandLineOption != null) {
198: try {
199: config.setTypeMappingFile(new File(commandLineOption
200: .getOptionValue()));
201: } catch (Exception e) {
202: throw new RuntimeException(CodegenMessages
203: .getMessage("options.nomappingFile"), e);
204: }
205: }
206:
207: // load the namespace to package list
208: commandLineOption = loadOption(
209: WSDL2JavaConstants.NAME_SPACE_TO_PACKAGE_OPTION,
210: WSDL2JavaConstants.NAME_SPACE_TO_PACKAGE_OPTION_LONG,
211: optionMap);
212: if (commandLineOption != null) {
213: //the syntax for the value of the namespaces and packages is
214: //to be a comma seperated list with uri=packagename,uri=packagename...
215: String value = commandLineOption.getOptionValue();
216: if (value != null) {
217: // Try treating the values as a name=value pair separated by comma's
218: if (value.indexOf('=') != -1) {
219: String valuepairs[] = value.split(",");
220: if (valuepairs.length > 0) {
221: //put them in the hash map
222: HashMap map = new HashMap(valuepairs.length);
223: for (int i = 0; i < valuepairs.length; i++) {
224: String values[] = valuepairs[i].split("=");
225: if (values.length == 2) {
226: map.put(values[0], values[1]);
227: }
228: }
229: config.setUri2PackageNameMap(map);
230: }
231: } else {
232: // Try loading the properties from the file specified
233: try {
234: Properties p = new Properties();
235: p.load(new FileInputStream(value));
236: config.setUri2PackageNameMap(p);
237: } catch (IOException e) {
238: throw new RuntimeException(CodegenMessages
239: .getMessage("options.noFile", value), e);
240: }
241: }
242: }
243: }
244:
245: commandLineOption = loadOption(
246: WSDL2JavaConstants.UNWRAP_PARAMETERS,
247: WSDL2JavaConstants.UNWRAP_PARAMETERS_LONG, optionMap);
248: if (commandLineOption != null) {
249: config.setParametersWrapped(false);
250: }
251:
252: commandLineOption = loadOption(
253: WSDL2JavaConstants.WSDL_VERSION_OPTION,
254: WSDL2JavaConstants.WSDL_VERSION_OPTION_LONG, optionMap);
255: if (commandLineOption != null) {
256: String optionValue = commandLineOption.getOptionValue();
257:
258: if (WSDL2JavaConstants.WSDL_VERSION_2.equals(optionValue)
259: || WSDL2JavaConstants.WSDL_VERSION_2_OPTIONAL
260: .equals(optionValue)) {
261: //users can say either 2.0 or 2 - we just set it to the constant
262: config
263: .setWSDLVersion(WSDL2JavaConstants.WSDL_VERSION_2);
264: } //ignore the other cases - they'll be taken as 1.1
265:
266: }
267:
268: config
269: .setFlattenFiles(loadOption(
270: WSDL2JavaConstants.FLATTEN_FILES_OPTION,
271: WSDL2JavaConstants.FLATTEN_FILES_OPTION_LONG,
272: optionMap) != null);
273:
274: commandLineOption = loadOption(
275: WSDL2JavaConstants.BACKWORD_COMPATIBILITY_OPTION,
276: WSDL2JavaConstants.BACKWORD_COMPATIBILITY_OPTION_LONG,
277: optionMap);
278: if (commandLineOption != null) {
279: config.setBackwordCompatibilityMode(true);
280: }
281:
282: commandLineOption = loadOption(
283: WSDL2JavaConstants.SUPPRESS_PREFIXES_OPTION,
284: WSDL2JavaConstants.SUPPRESS_PREFIXES_OPTION_LONG,
285: optionMap);
286: if (commandLineOption != null) {
287: config.setSuppressPrefixesMode(true);
288: }
289:
290: commandLineOption = loadOption(
291: XMLBeansExtension.XSDCONFIG_OPTION,
292: XMLBeansExtension.XSDCONFIG_OPTION_LONG, optionMap);
293: if (commandLineOption != null) {
294: config.getProperties().put(
295: XMLBeansExtension.XSDCONFIG_OPTION,
296: commandLineOption.getOptionValue());
297: }
298:
299: // setting the overrid and all ports options
300: config
301: .setAllPorts(loadOption(
302: WSDL2JavaConstants.All_PORTS_OPTION,
303: WSDL2JavaConstants.All_PORTS_OPTION_LONG,
304: optionMap) != null);
305:
306: config
307: .setOverride(loadOption(
308: WSDL2JavaConstants.OVERRIDE_OPTION,
309: WSDL2JavaConstants.OVERRIDE_OPTION_LONG,
310: optionMap) != null);
311:
312: // loop through the map and find parameters having the extra prefix.
313: //put them in the property map
314: Iterator keyIterator = optionMap.keySet().iterator();
315: while (keyIterator.hasNext()) {
316: Object key = keyIterator.next();
317: CommandLineOption option = (CommandLineOption) optionMap
318: .get(key);
319: if (key.toString().startsWith(
320: WSDL2JavaConstants.EXTRA_OPTIONTYPE_PREFIX)) {
321: //add this to the property map
322: config
323: .getProperties()
324: .put(
325: key
326: .toString()
327: .replaceFirst(
328: WSDL2JavaConstants.EXTRA_OPTIONTYPE_PREFIX,
329: ""),
330: option.getOptionValue());
331: }
332: }
333:
334: }
335:
336: private static CommandLineOption loadOption(String shortOption,
337: String longOption, Map options) {
338: //short option gets precedence
339: CommandLineOption option = null;
340: if (longOption != null) {
341: option = (CommandLineOption) options.get(longOption);
342: if (option != null) {
343: return option;
344: }
345: }
346: if (shortOption != null) {
347: option = (CommandLineOption) options.get(shortOption);
348: }
349:
350: return option;
351: }
352:
353: }
|