相对来说,Eclipse API的中文资料比较少,但是Eclipse的API提供了非常多的,非常强大的功能。
比如,eclipse的Eclipse API 提供的 org.eclipse.wst.wsdl包,里面提供了很多的类去解析WSDL文件。
总体来说,其提供的API简单易懂,而且其API是和专业术语对应起来的,比如,
一个WSDL文档通常包含7个重要的元素,即types、import、message、portType、operation、binding、 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()); } } } }