如何使用Eclipse API 提供 org.eclipse.wst.wsdl 要解决阅读WSDL档?

时间:2023-03-08 15:38:22

相对而言。Eclipse API中国的数据是比较小的。但Eclipse的API提供了许多的。非常强大。

实例,eclipse的Eclipse API 提供 org.eclipse.wst.wsdl包裹,它提供了大量的类来解决WSDL档。

。其提供的API简单易懂。并且其API是和专业术语相应起来的,比方,

一个WSDL文档通常包括7个重要的元素。即types、import、message、portType、operation、binding、 service元素。

这些元素嵌套在definitions元素中,

(1) Definitions是WSDL文档的根元素。相应于这个类: org.eclipse.wst.wsdl.Definition 其它的对象都能够通过这个对象获得

(2) Types - 数据类型定义的容器,它使用某种类型系统(一般地使用XML Schema中的类型系统)。

(3) Message - 通信消息的数据结构的抽象类型化定义。使用Types所定义的类型来定义整个消息的数据结构。 

(4) PortType - 对于某个訪问入口点类型所支持的操作的抽象集合。这些操作能够由一个或多个服务訪问点来支持。

 (子节点) Operation - 对服务中所支持的操作的抽象描写叙述。一般单个Operation描写叙述了一个訪问入口的请求/响应消息对。

(5) Binding - 特定port类型的详细协议和数据格式规范的绑定。

(6) Service- 相关服务訪问点的集合。

(子节点) Port - 定义为协议/数据格式绑定与详细Web訪问地址组合的单个服务訪问点。

以下是代码实例:

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set; import javax.wsdl.Message;
import javax.wsdl.Part;
import javax.wsdl.PortType;
import javax.xml.namespace.QName; import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.wst.wsdl.Definition;
import org.eclipse.wst.wsdl.Types;
import org.eclipse.wst.wsdl.internal.impl.PartImpl;
import org.eclipse.wst.wsdl.internal.util.WSDLResourceFactoryImpl;
import org.eclipse.wst.wsdl.util.WSDLResourceImpl;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.util.XSDResourceImpl;
import org.junit.Test;
import org.junit.Before; public class WSDLParserWithEclipse {
Definition definition=null;
String wsdlPathString="E:/HelloEclipse-EMF-WSDL-XSD/test.wsdl"; @Before
public void setup(){
ResourceSet resourceSet = new ResourceSetImpl();
Resource.Factory.Registry registry = resourceSet.getResourceFactoryRegistry();
Map extensionToFactoryMap = registry.getExtensionToFactoryMap();
extensionToFactoryMap.put("wsdl", new WSDLResourceFactoryImpl());
File wsdlFile =new File(wsdlPathString);
URI uri = URI.createFileURI(wsdlFile.getAbsolutePath());
// You can avoid this cast, but will have to cast anyway later to get the Definition out the resource contents
WSDLResourceImpl wsdlResource = (WSDLResourceImpl) resourceSet.createResource(uri);
try {
wsdlResource.load(null);
definition = wsdlResource.getDefinition();
}catch(Exception e){
e.printStackTrace();
}
} @Test
public void testTypes(){
Types types = definition.getETypes();
List schemas = types.getSchemas("http://www.xxxxx.com/problem");
XSDSchema schema = (XSDSchema) schemas.get(0);
org.eclipse.xsd.util.XSDResourceImpl.serialize(System.out, schema.getElement());
} @Test
public void testMessage(){
Map messages=definition.getMessages();
System.out.println("The message size is:"+messages.size());
Set setMessages=messages.keySet();
Iterator iteratorMessages=setMessages.iterator();
while(iteratorMessages.hasNext()){
QName key=(QName)iteratorMessages.next();
Message message=(Message)messages.get(key);
//{http://www.xxxxx.com/problem}getKeysSoapIn
//System.out.println("Message Name:"+message.getQName());
if(message.getQName().toString().indexOf("getKeysSoapIn")>0){
System.out.println("Message Name:"+message.getQName());
Map partsMap=message.getParts();
//org.eclipse.xsd.impl.XSDElementDeclarationImpl
System.out.println("Message Part size for getKeysSoapIn message is:"+partsMap.size());
PartImpl part= (PartImpl)partsMap.get("problem");
XSDElementDeclaration xsdElementDeclaration=part.getElementDeclaration();
XSDResourceImpl.serialize(System.out, xsdElementDeclaration.getElement());
}
}
}
@Test
public void testPortType(){
Map portTypes=definition.getPortTypes();
System.out.println("Port Type size:"+portTypes.size());
if(portTypes!=null&&portTypes.size()>0){
Set set=portTypes.keySet();
Iterator iterator=set.iterator();
while(iterator.hasNext()){
QName object=(QName)iterator.next();
PortType portType=(PortType)portTypes.get(object);
System.out.println("Port Type name:"+portType.getQName());
org.eclipse.xsd.util.XSDResourceImpl.serialize(System.out, portType.getDocumentationElement());
}
}
} }

版权声明:本文博主原创文章,博客,未经同意不得转载。