[CXF] WSDLs2Java: 从一堆WSDL中一次性生成Java文件

目前CXF的 WSDL2JAVA 一次只能搞一个WSDL, 但一个项目中可能需要搞多个WSDL并打成一个JAR包。 下面的这个类就是帮助你从多个WSDL中生成一团JAVA文件。与ANT结合起来,可以得到更好的效果。


import java.util.ArrayList;
import java.util.Arrays;

import org.apache.cxf.tools.wsdlto.WSDLToJava;

/**
 * Generate Java Codes from a number of WSDL files/urls <br/>
 * 
 * @author chenjianjx
 * 
 */
public class WSDLsToJava {

	/**
	 * 
	 * @param args
	 *            The last args should be like "-wsdlList %wsdl1% %wsdl2% ..." <br/>
	 *            The args between these are just those of CXF
	 */
	public static void main(String[] args) {

		ArrayList<String> argsList = new ArrayList<String>(Arrays.asList(args));

		int argIndexForWsdl = getArgIndexForWsdl(argsList);
		ArrayList<String> preWsdlArgs = getPreWsdlArgs(argsList, argIndexForWsdl);

		for (int i = argIndexForWsdl; i < argsList.size(); i++) {
			String wsdl = argsList.get(i);
			ArrayList<String> cxfArgsList = new ArrayList<String>();
			cxfArgsList.addAll(preWsdlArgs);
			cxfArgsList.add(wsdl);
			WSDLToJava.main(listToArray(cxfArgsList));
		}

	}

	private static String[] listToArray(ArrayList<String> list) {
		String[] array = new String[list.size()];
		list.toArray(array);
		return array;
	}

	private static ArrayList<String> getPreWsdlArgs(ArrayList<String> argsList, int argIndexForWsdl) {
		ArrayList<String> preWsdlArgs = new ArrayList<String>();
		for (int i = 0; i < argIndexForWsdl - 1; i++) {
			String arg = argsList.get(i);
			preWsdlArgs.add(arg);
		}
		return preWsdlArgs;
	}

	private static int getArgIndexForWsdl(ArrayList<String> argsList) {

		for (int i = 0; i < argsList.size(); i++) {
			String arg = argsList.get(i);
			if (arg.trim().equals("-wsdlList")) {
				return i + 1;
			}
		}

		throw new IllegalStateException("No \"-wsdlList\" arg found");
	}

}


Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.