文章标题

来源:互联网 发布:口琴校音器软件 编辑:程序博客网 时间:2024/06/08 00:24
  1. 主函数
public static void main(String[] args) {        String wsdlURI="D:/eclips/testSoftware/test9/Test/testExample.wsdl";        try {            WSDLParser wsdlParse = new WSDLParser();            //解析wsdl            wsdlParse.parseWSDL(wsdlURI);        } catch (WSDLException e1) {            e1.printStackTrace();        }     }

2.解析wsdl

public void parseWSDL(String wsdlURI) throws WSDLException {        WSDLFactory wsdlFactory = WSDLFactory.newInstance();        WSDLReader reader = wsdlFactory.newWSDLReader();        Definition defintion = reader.readWSDL(wsdlURI);        System.out.println("targetNamespace = "+defintion.getTargetNamespace());        processTypes(defintion);        Map servicesMap = defintion.getAllServices();        Set serviceKeys = servicesMap.keySet();        for (Iterator iterator = serviceKeys.iterator(); iterator.hasNext();) {            Object key = (Object) iterator.next();            ServiceImpl service = (ServiceImpl)servicesMap.get(key);            //解析服务            processService(service);        }    }

3.processService(service)解析服务

public void processService(ServiceImpl service) {        System.out.println("Service信息如下:");        String serviceName = service.getQName().getLocalPart();        ServiceInfo serviceInfo = new ServiceInfo();        serviceInfo.setName(serviceName);        services.add(serviceInfo);        System.out.println("serviceName = "+serviceName);        Collection ports = service.getPorts().values();        for (Iterator iterator = ports.iterator(); iterator.hasNext();) {            Port port = (Port) iterator.next();            Binding binding = port.getBinding();            System.out.println("portName = "+port.getName());            System.out.println("bindingName = "+binding.getQName().getLocalPart());            System.out.println("nService location:");              List l=port.getExtensibilityElements();              ExtensibilityElement element=(ExtensibilityElement) l.get(0);              String s=element.toString();              System.out.println(s.substring(s.indexOf("location")));              List operations = binding.getBindingOperations();            for (Iterator iterator2 = operations.iterator(); iterator2.hasNext();) {                BindingOperation bindingOperation = (BindingOperation) iterator2.next();                Operation operation = bindingOperation.getOperation();                //处理操作                processOperation(serviceInfo,operation);            }        }    }

4.processOperation(serviceInfo,operation)处理操作信息

public void processOperation(ServiceInfo serviceInfo, Operation operation) {        String operationName = operation.getName();        OperationInfo operationInfo = new OperationInfo();        operationInfo.setOperationName(operationName);        System.out.println("operationName = "+operationName);        if (!containOperation(serviceInfo, operationInfo)) {            serviceInfo.addOperation(operationInfo);        }        Input input = operation.getInput();        System.out.println("输入参数信息:");        processInputOrOutputParam(operationInfo,input,null);        Output output = operation.getOutput();        System.out.println("输出参数信息:");        processInputOrOutputParam(operationInfo,null,output);    }

5.processInputOrOutputParam(operationInfo,input,null);输入输出参数处理

public void processInputOrOutputParam(OperationInfo operationInfo,  Input input,Output output) {        List inputParams = operationInfo.getInputParams();        Message message;        if(input != null ){                  message = input.getMessage();        }else{             message = output.getMessage();        }        String messageName = message.getQName().getLocalPart();        System.out.println("messageName = "+ messageName);        Map partMap = message.getParts();        Collection parts = partMap.values();        for (Iterator iterator = parts.iterator(); iterator.hasNext();) {            Part part = (Part) iterator.next();            String partName = part.getName();            System.out.println("partName = "+ partName);            ParameterInfo parameterInfo = new ParameterInfo();            parameterInfo.setName(partName);            inputParams.add(parameterInfo);            String typeName = null;            QName qName = part.getTypeName();            if (qName != null) {                typeName = qName.getLocalPart();                System.out.println("typeName = "+typeName);                //TODO complex type;            } else {                typeName = part.getElementName().getLocalPart();                System.out.println("ElementName = "+typeName);                //TODO process element.            }                   parameterInfo.setType(typeName);        }    }

6.processTypes(defintion);处理types信息

public void processTypes(Definition defintion) {        XmlSchemaCollection xmlSchemaCollection = new XmlSchemaCollection();        Map namespaces = defintion.getNamespaces();        Types types = defintion.getTypes();        List list = types.getExtensibilityElements();        types.getDocumentationElement();        for (Iterator iterator = list.iterator(); iterator.hasNext();) {            SchemaImpl schemaImpl = (SchemaImpl) iterator.next();            Element element = (Element)schemaImpl.getElement();            XmlSchema xmlSchema = xmlSchemaCollection.read(element);            String targetNamespace = xmlSchema.getTargetNamespace();            XmlSchemaObjectTable smlSchemaObjectTable = xmlSchema.getSchemaTypes();            XmlSchemaObjectTable elements = xmlSchema.getElements();            System.out.println();            System.out.println(smlSchemaObjectTable);            System.out.println();            Iterator typesItr = smlSchemaObjectTable.getValues();            //Iterator typesItr = elements.getValues();            while (typesItr.hasNext()) {                System.out.println("执行到while了!");                //XmlSchemaType type = (XmlSchemaType) typesItr.next();                XmlSchemaType type = (XmlSchemaType) typesItr;                //EntityClass entityClass = new EntityClass();                //entityClasses.add(entityClass);                String typeName = type.getName();                if (type instanceof XmlSchemaComplexType) {                    XmlSchemaComplexType xmlSchemaComplexType = (XmlSchemaComplexType) type;                    XmlSchemaParticle xmlSchemaParticle = xmlSchemaComplexType.getParticle();                    if (xmlSchemaParticle instanceof XmlSchemaSequence) {                        XmlSchemaSequence xmlSchemaSequence = (XmlSchemaSequence) xmlSchemaParticle;                        XmlSchemaObjectCollection xmlSchemaObjectCollection = xmlSchemaSequence.getItems();                        int count = xmlSchemaObjectCollection.getCount();                        for (int i = 0; i < count; i++) {                            XmlSchemaObject xmlSchemaObject = xmlSchemaObjectCollection.getItem(i);                            if (xmlSchemaObject instanceof XmlSchemaElement) {                                XmlSchemaElement xmlSchemaElement = (XmlSchemaElement)xmlSchemaObject;                                String elementName = xmlSchemaElement.getName();                                XmlSchemaType xmlSchemaType = xmlSchemaElement.getSchemaType();                                String elementTypeName = xmlSchemaType.getName();                                if (xmlSchemaType instanceof XmlSchemaComplexType) {                                    // TODO                                }                            }                        }                    }                }                 System.out.println("type:" + type);            }        }    }

注:处理types信息,这里有个问题是,smlSchemaObjectTable好像并没有获取到数据,这是问题所在,正在调试。请见下篇博客,分解。

0 0
原创粉丝点击