日历

2008 5.17 Sat
    123
45678910
11121314151617
18192021222324
25262728293031
«» 2008 - 5 «»

文章搜索

日志文章

2007年11月23日 17:22:54

Web服务搜索与执行引擎——WSDL解析精髓(二)

4.根据当前Schema所对应的对象(org.w3c.dom.Element)schemaElementt,返回一个标准的Java对象schema.

    Schema schema=createschemafromtype(schemaElementt,wsdlDefinition);

  而第4步其实也是一个复杂过程,如清单3。而最终要实现的就是:

这几个变换:<definitions>——< schema>——WSDL4J包的ExtensibilityElement ——org.w3c.dom.Element下的Element——org.exolab.castor.xml.schema.SchemaSchema



  清单3 Transformation.java

清单3的一个任务就是实现这几个变换:<definitions>——< schema>——WSDL4J包的

private Schema createschemafromtype(org.w3c.dom.Element schemaElement,Definition wsdlDefinition)...{

      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");
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]           return null;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]       }
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]       Map namespaces = wsdlDefinition.getNamespaces();
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif[/img][img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif[/img]       if(namespaces != null && !namespaces.isEmpty())...{
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]           Iterator nsIter = namespaces.keySet().iterator();
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif[/img][img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif[/img]           while(nsIter.hasNext())...{
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]               String nsPrefix = (String)nsIter.next();
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]               String nsURI = (String)namespaces.get(nsPrefix);
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif[/img][img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif[/img]               if(nsPrefix != null && nsPrefix.length() > 0) ...{
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]                     org.jdom.Namespace nsDecl = org.jdom.Namespace.getNamespace(nsPrefix, nsURI);
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]                     jdomSchemaElement.addNamespaceDeclaration(nsDecl);
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]               }
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]           }
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]       }
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]     jdomSchemaElement.detach();
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]     Schema schema = null;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]     try
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif[/img][img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif[/img]     ...{
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]       schema = XMLSupport.convertElementToSchema(jdomSchemaElement);
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]     }
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]     catch(Exception e)
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif[/img][img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif[/img]     ...{
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]         System.err.println(e.getMessage());
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]     }
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]     return schema;
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif[/img]   }
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif[/img]
[img]http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif[/img]
ExtensibilityElement ——org.w3c.dom.Element类型的Element——org.jdom.Element类型

Element——org.exolab.castor.xml.schema.Schema类型的Schema的最后一步:org.exolab.castor.xml.schema.SchemaSchema

使用JDOMimport org.jdom.input.DOMBuilderorg.w3c.dom.Element类型的Element转化为org.jdom.Element类型的Element

    // DOMJDOM的转换

    DOMBuilder domBuilder = new DOMBuilder();

    org.jdom.Element jdomSchemaElement = domBuilder.build(schemaElement);

这时候离目标org.exolab.castor.xml.schema.SchemaSchema。只有一步了:

schema = XMLSupport.convertElementToSchema(jdomSchemaElement);即,

通过XMLSupport类把一个JDOM类型的SchemaElemen转化为org.exolab.castor.xml.schema.SchemaSchema具体过程就不再细说了。

还有一个任务就是:在传进来的实参Definition wsdlDefinition对象里获得definition元素
所有相关的命名空间:
  Map namespaces = wsdlDefinition.getNamespaces();

      if(namespaces != null && !namespaces.isEmpty()){

          Iterator nsIter = namespaces.keySet().iterator();

          while(nsIter.hasNext()){

              String nsPrefix = (String)nsIter.next();

              String nsURI = (String)namespaces.get(nsPrefix);

              if(nsPrefix != null && nsPrefix.length() > 0) {

                    org.jdom.Namespace nsDecl = org.jdom.Namespace.getNamespace(nsPrefix, nsURI);

                    jdomSchemaElement.addNamespaceDeclaration(nsDecl);

              }

          }

      }
  其实以上说的清单2跟清单3用一句话来说就是:帮助清单1实现第二步:使用castor工具根据上一步生成的def对象,从Definition 的子元素types生成java对象wsdlTypes,它是一个向量vectorwsdlTypes = createSchemaFromTypes(def);

  收集这些wsdlTypes信息是为后面构造操作operation作铺垫的。

  对于清单1中的

第四步:为找到的当前Service对象构建一个com.swc.se.domain.Service类,实现过程如下面清单4

清单4 Transformation.java

private com.swc.se.domain.Service populateComponent(com.swc.se.domain.Service component, Service service)

  {

  QName qName = service.getQName();

    String namespace = qName.getNamespaceURI();

    String name = qName.getLocalPart();

    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())

      {

        com.swc.se.domain.Operation operation=(com.swc.se.domain.Operation)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;

  }

清单4代码是构建一个我们的对象:com.swc.se.domain.Service。代码很容易读懂,因为都是一些WSDLforJ的特殊API,即对应WSDL文档大部分元素的相对应的Java类。这个过程就不像上面那样分析了,因为道理是一样的。

提示:

按这样的思路:

1. WSDLforJ API下对应WSDL文档的某个类A

2. 调用类A的方法findExtensibilityElement(A.getExtensibilityElements());返回向量Vector

3. 获取其中一个ExtensibilityElement B;它对应的就是wsdl文档的某个标签<B>对应的类为BB

4. if(B!= null && B instanceof BB)

      {      

BB bb = (BB)B;

        ….

      }

……

我想暂时就先总结这么多吧,掌握了规律后我想就不难了,如果你坚持看到这里了,应该对WSDL4J搭配castor+jdom来解析WSDL文档有初步的认识了吧。肯定还有很多细节没有说到,以后再慢慢补吧.

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1574212

类别: 技术专栏 |  评论(0) |  浏览(1293) |  收藏
发表评论