java使用soapui解析wsdl

以前从网上找了一个解析wsdl的程序,非常老用的jar包也是02年或者以前的导致一些服务不能解析为此想了很多办法,更新wsdl4j的jar包什么的都试过,结果都失败了。

今天就试着使用soapui的jar来解析,因为发现soapui是啥都能解析的。

开始写代码:

import com.eviware.soapui.impl.wsdl.WsdlInterface;
import com.eviware.soapui.impl.wsdl.WsdlOperation;
import com.eviware.soapui.impl.wsdl.WsdlProject;
import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlImporter;
import com.eviware.soapui.model.iface.Operation;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class ImportWsdl {
	public static Map<String, HashMap<String, String>> result = new HashMap();

	public List<WsMethodInfo> getProBySoap(String address) throws Exception {
		URL url = new URL(address);
		InputStream openStream = url.openStream();
		openStream.close();

		Map<String,Object> result = new HashMap<String,Object>();
		WsdlProject project = new WsdlProject();
		WsdlInterface[] wsdls = WsdlImporter.importWsdl(project, address);

		WsdlInterface wsdl = wsdls[0];
		List<Operation> operationList = wsdl.getOperationList();
		for (int i = 0; i < operationList.size(); i++) {
			Operation operation = operationList.get(i);
			WsdlOperation op = (WsdlOperation) operation;
			Map<String,Object> tmp = new HashMap<String,Object>();
			tmp.put(op.getName(), op.createRequest(true));
			result.put(i+"", tmp);
		}

		if ((result != null) && (result.size() != 0)) {
			List importWsdl = new ArrayList();
			Set keySet = result.keySet();
			Iterator iterator = keySet.iterator();
			while (iterator.hasNext()) {
				WsMethodInfo info = new WsMethodInfo();
				List inputType = new ArrayList();
				List inputNames = new ArrayList();
				List isparent = new ArrayList();
				String next = ""+ iterator.next();
				HashMap hashMap = (HashMap) result.get(next);
				Set keySet2 = hashMap.keySet();
				Iterator iterator2 = keySet2.iterator();
				if (iterator2.hasNext()) {
					String next2 = (String) iterator2.next();
					String string = (String) hashMap.get(next2);
					//处理targetNameSpace
					String qname=string.substring(string.lastIndexOf("\"http://")+1,string.lastIndexOf("\">"));
					info.setTargetNameSpace(qname);

					String soap11 = "http://schemas.xmlsoap.org/soap/envelope";
					String soap12 = "http://www.w3.org/2003/05/soap-envelope";
					InputStreamReader is = new InputStreamReader(new ByteArrayInputStream(string.getBytes("utf-8")));
					BufferedReader ibr = new BufferedReader(is);
					String readLine = ibr.readLine();
					if (readLine != null) {
						if (readLine.indexOf(soap11) >= 0)
							info.setTargetXsd("11");
						else if (readLine.indexOf(soap12) >= 0) {
							info.setTargetXsd("12");
						}
					}
					ibr.close();
					is.close();

					info.setMethodSoapAction(string);

					Document read = DocumentHelper.parseText(string);
					Element rootElement = read.getRootElement();
					List<Element> elements = rootElement.elements();
					for (Element element : elements) {
						if ("Body".equals(element.getName())) {
							List<Element> elements2 = element.elements();
							info.setMethodName(next2);
							for (Element element2 : elements2) {
								getParameter(element2, 1, 1, inputType, inputNames, isparent);
								info.setInputNames(inputNames);
								info.setInputType(inputType);
								info.setOutputType(isparent);
							}
						}
					}

					importWsdl.add(info);
				}
			}
			return importWsdl;
		}
		return null;
	}

	public void getParameter(Element element2, int gen, int genParent, List<String> inputType, List<String> inputNames,
			List<String> isparent) {
		if (element2 != null) {
			List<Element> elements3 = element2.elements();
			if ((elements3 != null) && (elements3.size() != 0))
				for (Element element : elements3) {
					inputType.add(gen + "," + genParent);
					inputNames.add(element.getQualifiedName());
					if (element != null) {
						List e = element.elements();
						if ((e != null) && (e.size() != 0)) {
							isparent.add("1");
							int gen1 = gen + gen;
							getParameter(element, gen1, gen, inputType, inputNames, isparent);
						} else {
							isparent.add("0");
						}
					}
				}
		}
	}
}
WsMethodInfo.java

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;

public class WsMethodInfo
{
  private String methodName;
  private String methodDesc;
  private Map<String, Map> methodName2InputParam = new HashMap();
  private String targetNameSpace;
  private String methodSoapAction;
  private String endPoint;
  private String targetXsd;
  private List<String> inputNames;
  private List<String> inputType;
  private List<String> inputDesc;
  private List<String> outputNames;
  private List<String> outputType;
  private String sep = "#";

  public String getMethodName()
  {
    return this.methodName;
  }
  public void setMethodName(String methodName) {
    this.methodName = methodName;
  }
  public List<String> getInputNames() {
    return this.inputNames;
  }
  public void setInputNames(List<String> inputNames) {
    this.inputNames = inputNames;
  }

  public Map<String, Map> getMethodName2InputParam() {
    return this.methodName2InputParam;
  }
  public void setMethodName2InputParam(Map<String, Map> methodName2InputParam) {
    this.methodName2InputParam = methodName2InputParam;
  }
  public String getTargetNameSpace() {
    return this.targetNameSpace;
  }
  public void setTargetNameSpace(String targetNameSpace) {
    this.targetNameSpace = targetNameSpace;
  }
  public String getMethodSoapAction() {
    return this.methodSoapAction;
  }
  public void setMethodSoapAction(String methodSoapAction) {
    this.methodSoapAction = methodSoapAction;
  }
  public String getEndPoint() {
    return this.endPoint;
  }
  public void setEndPoint(String endPoint) {
    this.endPoint = endPoint;
  }
  public String getTargetXsd() {
    return this.targetXsd;
  }
  public void setTargetXsd(String targetXsd) {
    this.targetXsd = targetXsd;
  }

  public List<String> getOutputNames() {
    return this.outputNames;
  }
  public void setOutputNames(List<String> outputNames) {
    this.outputNames = outputNames;
  }
  public List<String> getInputType() {
    return this.inputType;
  }
  public void setInputType(List<String> inputType) {
    this.inputType = inputType;
  }
  public List<String> getOutputType() {
    return this.outputType;
  }
  public void setOutputType(List<String> outputType) {
    this.outputType = outputType;
  }

  public List<String> getInputDesc() {
    return this.inputDesc;
  }
  public void setInputDesc(List<String> inputDesc) {
    this.inputDesc = inputDesc;
  }

  public String madeNewString()
  {
    StringBuffer su = new StringBuffer();
    su.append(this.methodName);
    su.append(this.sep);
    su.append(this.inputType == null ? "" : StringUtils.join(this.inputType.toArray(), "@"));
    su.append(this.sep);
    su.append(this.inputNames == null ? "" : StringUtils.join(this.inputNames.toArray(), "@"));
    su.append(this.sep);
    su.append(this.methodDesc == null ? "" : this.methodDesc);
    su.append(this.sep);
    su.append(this.sep);
    su.append(this.methodSoapAction == null ? "" : this.methodSoapAction);
    su.append(this.sep);
    su.append(this.outputType == null ? "" : StringUtils.join(this.outputType.toArray(), "@"));
    su.append(this.sep);
    su.append(this.targetXsd == null ? "" : this.targetXsd);
    return su.toString();
  }

  public String getMethodDesc() {
    return this.methodDesc;
  }
  public void setMethodDesc(String methodDesc) {
    this.methodDesc = methodDesc;
  }
}


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 28
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值