📄 componentbuilder.java
字号:
package wsclient.util;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.wsdl.Binding;
import javax.wsdl.BindingInput;
import javax.wsdl.BindingOperation;
import javax.wsdl.BindingOutput;
import javax.wsdl.Definition;
import javax.wsdl.Input;
import javax.wsdl.Message;
import javax.wsdl.Operation;
import javax.wsdl.Output;
import javax.wsdl.Part;
import javax.wsdl.Port;
import javax.wsdl.Service;
import javax.wsdl.extensions.ExtensibilityElement;
import javax.wsdl.extensions.UnknownExtensibilityElement;
import javax.wsdl.extensions.soap.SOAPAddress;
import javax.wsdl.extensions.soap.SOAPBinding;
import javax.wsdl.extensions.soap.SOAPBody;
import javax.wsdl.extensions.soap.SOAPOperation;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;
import javax.xml.namespace.QName;
import org.exolab.castor.xml.schema.ComplexType;
import org.exolab.castor.xml.schema.ElementDecl;
import org.exolab.castor.xml.schema.Group;
import org.exolab.castor.xml.schema.Particle;
import org.exolab.castor.xml.schema.Schema;
import org.exolab.castor.xml.schema.SimpleTypesFactory;
import org.exolab.castor.xml.schema.Structure;
import org.exolab.castor.xml.schema.XMLType;
import org.jdom.input.DOMBuilder;
import wsclient.domain.OperationInfo;
import wsclient.domain.ParameterInfo;
import wsclient.domain.ServiceInfo;
public class ComponentBuilder {
WSDLFactory wsdlFactory = null;
SimpleTypesFactory simpleTypesFactory = null;
private Vector wsdlTypes = new Vector();
public final static String DEFAULT_SOAP_ENCODING_STYLE = "http://schemas.xmlsoap.org/soap/encoding/";
public ComponentBuilder() {
try {
wsdlFactory = WSDLFactory.newInstance();
simpleTypesFactory = new SimpleTypesFactory();
} catch (Throwable t) {
System.err.println(t.getMessage());
}
}
public ServiceInfo buildserviceinformation(ServiceInfo serviceinfo)
throws Exception{
WSDLReader reader=wsdlFactory.newWSDLReader();
Definition def=reader.readWSDL(null, serviceinfo.getWsdllocation());
wsdlTypes = createSchemaFromTypes(def);
System.out.println("WSDL的Schema已经成功构建完,总共有多少个Schema定义:"+wsdlTypes.size());
Map services=def.getServices();
if (services!= null) {
Iterator svcIter = services.values().iterator();
populateComponent(serviceinfo, (Service) svcIter.next());
System.out.println("***恭喜您!系统的Web服务对象:ServiceInfo已经成功构建***");
System.out.println("");
}
return serviceinfo;
}
private Schema createschemafromtype(org.w3c.dom.Element schemaElement,
Definition wsdlDefinition) {
System.out.println("现在的Schema还是一个Dom型的<xsd:schema>元素,属性还不够完全,必须构建命名空间等属性");
System.out.println("使用JDom,先把Dom型的<xsd:schema>元素转换成JDom型...");
System.out.println("开始构建...");
if (schemaElement == null) {
System.err.println("Unable to find schema extensibility element in WSDL");
return null;
}
DOMBuilder domBuilder = new DOMBuilder();
org.jdom.Element jdomSchemaElement = domBuilder.build(schemaElement);
if (jdomSchemaElement == null) {
System.err.println("Unable to read schema defined in WSDL");
return null;
}
Map namespaces = wsdlDefinition.getNamespaces();
if (namespaces != null && !namespaces.isEmpty()) {
System.out.println("WSDL文档Definition的所有命名空间为:");
Iterator nsIter = namespaces.keySet().iterator();
while (nsIter.hasNext()) {
String nsPrefix = (String) nsIter.next();
String nsURI = (String) namespaces.get(nsPrefix);
System.out.println("命名空间:"+nsPrefix+" "+nsURI);
if (nsPrefix!=null&&nsPrefix.length() > 0) {
org.jdom.Namespace nsDecl = org.jdom.Namespace
.getNamespace(nsPrefix, nsURI);
jdomSchemaElement.addNamespaceDeclaration(nsDecl);
}
}
}
jdomSchemaElement.detach();
Schema schema = null;
try {
schema = XMLSupport.convertElementToSchema(jdomSchemaElement);
}
catch (Exception e) {
System.out.println("a");
System.err.println(e.getMessage());
System.out.println("a");
}
return schema;
}
protected Vector createSchemaFromTypes(Definition wsdlDefinition) {
System.out.println("*****************");
System.out.println("进入createSchemaFromTypes()方法");
System.out.println("开始从Types中产生Schema,Definition下types元素的Schema元素个数可以多个,传进来的参数是一个Definition对象:");
Vector schemas=new Vector();
org.w3c.dom.Element schemaElementt=null;
if (wsdlDefinition.getTypes()!= null) {
Vector schemaExtElem=findExtensibilityElement(wsdlDefinition
.getTypes().getExtensibilityElements(), "schema");
for (int i = 0; i < schemaExtElem.size(); i++) {
ExtensibilityElement schemaElement = (ExtensibilityElement) schemaExtElem
.elementAt(i);
if (schemaElement != null
&& schemaElement instanceof UnknownExtensibilityElement) {
schemaElementt = ((UnknownExtensibilityElement) schemaElement)
.getElement();
Schema schema = createschemafromtype(schemaElementt,
wsdlDefinition);
schemas.add(schema);
}
}
}
System.out.println("*****************");
return schemas;
}
private ServiceInfo populateComponent(ServiceInfo component, Service service) {
System.out.println("***************************");
System.out.println("");
System.out.println("***开始构建系统的Web服务对象:ServiceInfo***");
QName qName = service.getQName();
String namespace = qName.getNamespaceURI();
System.out.println("namespace为:"+namespace);
String name = qName.getLocalPart();
System.out.println("name为:"+name);
component.setName(name);
Map ports=service.getPorts();
Iterator portIter = ports.values().iterator();
while (portIter.hasNext()) {
Port port = (Port) portIter.next();
Binding binding = port.getBinding();
List operations=buildOperations(binding);
Iterator operIter = operations.iterator();
while (operIter.hasNext()) {
OperationInfo operation = (OperationInfo) operIter.next();
Vector addrElems = findExtensibilityElement(port
.getExtensibilityElements(), "address");
ExtensibilityElement element = (ExtensibilityElement) addrElems
.elementAt(0);
if (element != null && element instanceof SOAPAddress) {
SOAPAddress soapAddr = (SOAPAddress) element;
operation.setTargetURL(soapAddr.getLocationURI());
}
component.addOperation(operation);
}
}
return component;
}
private List buildOperations(Binding binding) {
System.out.println("进入buildOperations(Binding binding)方法,构建服务所有的操作对象List buildOperations");
List operationInfos = new ArrayList();
List operations=binding.getBindingOperations();
if (operations!= null && !operations.isEmpty()) {
Vector soapBindingElems = findExtensibilityElement(binding
.getExtensibilityElements(), "binding");
String style = "document"; // default
ExtensibilityElement soapBindingElem = (ExtensibilityElement) soapBindingElems
.elementAt(0);
if (soapBindingElem != null
&& soapBindingElem instanceof SOAPBinding) {
//SOAPBinding类代表的就是<wsdl:binding>下的子元素:<wsdlsoap:binding元素>
SOAPBinding soapBinding = (SOAPBinding) soapBindingElem;
style = soapBinding.getStyle();
}
Iterator opIter = operations.iterator();
while (opIter.hasNext()) {
//BindingOperation类代表的就是<wsdl:binding>下的子元素:<wsdlsoap:operation元素>
BindingOperation oper = (BindingOperation) opIter.next();
Vector operElems = findExtensibilityElement(oper
.getExtensibilityElements(), "operation");
ExtensibilityElement operElem = (ExtensibilityElement) operElems
.elementAt(0);
//SOAPOperation类代表的就是<wsdlsoap:operation>下的子元素:<wsdlsoap:operation/>
if (operElem != null && operElem instanceof SOAPOperation) {
OperationInfo operationInfo = new OperationInfo(style);
buildOperation(operationInfo, oper);
operationInfos.add(operationInfo);
}
}
}
return operationInfos;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -