随笔 - 181, 文章 - 2, 评论 - 85, 引用 - 0
数据加载中……

IBM WebSphere 开发者技术期刊: 使用 Rational Application Developer 和 WebSphere Application Server 来加速基于 XML 的 SOA 的 JSF 开发——第 3 部分

本文是提出了加快基于 XML 的面向服务的体系结构(Service Oriented Architecture,SOA)应用程序表示开发的解决方案系列文章的第 3 部分。这种解决方案包括 Eclipse 功能,可以生成静态类型的用于 XSD Schema 的服务对象数据(Service Data Object,SDO),并提供了在表示元素数据与 XML 数据相互转换中使用 SDO 的运行框架。第三部分荟萃了操作 SDO 实例的通常使用的技术。

前言

这个由五部分组成的系列文章提出了能够加快基于 XML 的 SOA 应用程序表示开发的解决方案,这种解决方案包括 Eclipse 功能,可以生成静态类型的用于 XSD Schema 的服务对象数据(Service Data Object,SDO),并提供了在表示元素数据与 XML 数据相互转换中使用 SDO 的运行框架。在该系列文献的

  • 第 1 部分 中,我们经历了通过使用提供的插件集来向每个指定的 XML schema 中的服务传递信息的简单 JSF 应用程序的整个开发过程,我们使用的应用程序场景进行了一次服务调用——发出一个 XML 请求及收到一个 XML 响应。这个应用程序说明了主从复合结构的数据对象视图,连同分页及分类的能力。在
  • 第 2 部分 中,我们增强了在 第 1 部分 中开发的解决方案,使之包括创建、更新和删除功能,包括用本地附加的变量及基本的转换来自定义生成 SDO。我们举例说明多 schema 模型、对于单一页面中多个请求及响应的场景,并且说明了绑定 XML 数据的 JSF 下拉式控制。
  • 在第三部分中,我们将提供一些在 IBM® Rational® Application Developer V6 中使用 XML SDO 以及合计方法使用的一些技巧,比如总和、平均数和转化特写提高的一些其他方法。

IBM Rational Application Developer V6(后面称为 Application Developer)和 IBM Websphere® Application Server V6 是利用这些转化特性的必备条件。







安装 XSD SDO Transform 特性

如果想安装 XSD SDO Transform 特性,请参考第一部分中提供的详细步骤。







XSD SDO Transform 特性内容

XSD SDO Transform 特性包含从 XSD schema 中生成 SDO 包的工件,以及一些用来将 SDP 包转化为 XML 实例文档(或相反)的框架组件。

  1. 创建 SDO Package Action
    XSD 资源中的这个动作可以从 XSD schema 生成 SDP 包。
    图 1. 创建 SDO 包
    创建 SDO 包
  2. dw.ibm.etools.xsd.sdo.xmltransformservice.XMLTransformServiceFactory
    这个类提供了将 SDO 包实例转化为 XML 实例文档(或相反)的 API。
  3. dw.ibm.etools.xsd.sdo.xmltransforms.util.ObjectUtil
    这个类提供了操作 SDO 的合计和辅助方法。







导入项目交换

将项目交换 xyzinsurancetestclient_pi.zip下载文件导入到 Application Developer 工作区中。结果,项目 XYZInsuranceTestClient 被导入了进来(图 2)。这个 Java™ 项目包含一个测试 Java 类,里面有所有下面描述到的方法。为了简化的目的,SDO 包被包含在了 Java 项目中,并且 SDO 运行时体现在其类路径中。你可以选择参考包含 SDO 包的 Web 项目,或者在包含 SDO 包的项目中创建这个 Java 类。提供的 Eclipse Modeling Framework(EMF) JAR 文件, emf.jar,或者 EMF Web Site 中一样的 JAR 文件(ecore.sdo.jarcommonj.sdo.jarecore.xmi.jarecore.jarcommon.jar),应该添加到项目的类路径和构建路径中。文件 xsdsdotransform.jar 也被添加到项目中。

稍后,我们将描述使用 Application Developer 提供的 SDO 生成器特性来从一个独立 Java 项目中的 XSD 来创建 SDO 包。


图 2. 导入项目交换
导入项目交换






分析 SDO

在这一部分中,我们将分析 Java 客户端,这个客户端说明了用来操作生成的 SDO 包的 API。我们也会看一下 XMLTransformServiceFactory 中使用的其他的 API,以及工具类 ObjectUtil 中提供的合计类。

正如上一篇文章图示的那样,XSD 的 SDO 包名是由 schema 定义中的 targetNamespace 声明来决定的(图 3)。生成的 SDO 包包含一个 DocumentRoot 类,该类作为 schema中定义的所有类型的根容器(图 4)。Schema 中定义的每个类型都生成了其相应的 Java 类。这个生成的 Java 类包含了所有为 XSD 类型定义的元素和属性(作为 Java 类型)、一个所有多值元素的清单以及任何单个值元素的引用。


图 3. TargetNamespace 定义
TargetNamespace 定义

图 4. 生成的 SDO java 包
生成的 SDO java 包

为 XSD schema 类型生成的类在包注册表中被初始化,这样来自于或者到 Java 类型的 XSD 类型在转化发生的时候可以被识别。在生成大量类的大型 schema 的情况下,该初始化代码通过加载 EMF 资源来执行。在较小的 schema 情况下(取决于类、属性等的总数的估计),该初始化代码在包类本身中生成。

同样还为 Java 类中的 XSD 引用类型生成了 create<Element Name> 方法。这些 API 可以用来创建任何生成的 SDO Java 类型的实例。如清单 1 所示,一个 brokerType 的实例被创建并且添加到包含在 BrokerServiceType SDO 实例中的清单中。

清单 1

												
														public void TestCreateReference(){
	DocumentRoot root = DocumentRoot)XMLTransformServiceFactory.INSTANCE.create
		(BrokerservicePackage.eINSTANCE.getNsURI());
	root.getXMLNSPrefixMap().put("", BrokerservicePackage.eINSTANCE.getNsURI());
	xyz.brokerservice.BrokerServiceType bs = root.createBrokerService();
	BrokerType bType  = bs.createBroker();
	bType.setBrokerId("111");
	bType.setFirstName("Joe");
	System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)root));
}
												
										

该生成的包包含用来创建或者访问用于所有类型和属性的 Java 类型的方法(清单 2)。

清单 2

												
														public void TestTypeCreation(){
	EClass brokerType = BrokerservicePackage.eINSTANCE.getBrokerType();
	System.out.println("Type Name: " + brokerType.getName());
}
												
										

生成的工厂类提供了一些 API,用来创建任何生成的 SDO Java 类型的实例。正如在清单 3 中所见的那样,为了创建一个 brokerType 的实例,你可以使用 createBrokerType API。这个新创建的实例没有被添加到任何清单或者引用中,其如清单 1 所示的那些相关 API 那样。setBroker API 用来设置 brokerServiceType 中创建的新实例。基于需求,你可以使用任何一个这里描述的实例创建 API。

Listing 3

												
														public void TestInstanceCreation(){
	DocumentRoot root = DocumentRoot)XMLTransformServiceFactory.INSTANCE.create
		(BrokerservicePackage.eINSTANCE.getNsURI());
	root.getXMLNSPrefixMap().put("", BrokerservicePackage.eINSTANCE.getNsURI());
	xyz.brokerservice.BrokerServiceType bs = root.createBrokerService();
	BrokerType bType  = BrokerserviceFactory.eINSTANCE.createBrokerType();	bs.setBroker(bType);
	bType.setBrokerId("111");
	bType.setFirstName("Joe");
	System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)root));
}
												
										

想要了解 EMF 框架更多的细节,请参阅 Eclipse Modeling Framework Overview







创建 XML 请求

再上一篇文章中,我们看到了 XMLTransformFactory 加载和转化 API 如何被用来从 XML 流中序列化一系列 SDO 对象(或相反)。也有一些情况,你需要创建一个 SDO 对象(或一系列 SDO 对象)新的实例,并且将其序列化到一个 XML 中。XMLTransformFactory 中的创建 API 可以用来做这些,如清单 4a 所示。该方法的输出显示在清单 4b 中。

清单 4a. 方法

												
														public void TestNewDocumentCreation(){

														
																
																		
																					DocumentRoot root = DocumentRoot)XMLTransformServiceFactory.INSTANCE.create
		(BrokerservicePackage.eINSTANCE.getNsURI());
																		
	xyz.brokerservice.BrokerServiceType bs = root.createBrokerService();
	BrokerType bType  = bs.createBroker();
	bType.setBrokerId("111");
	bType.setFirstName("Joe");
	System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)root));
}

														
												
										

清单 4b. 输出

												
														<?xml version="1.0" encoding="ASCII"?>
<brokerservice:brokerService
xmlns:brokerservice="http:///xyz.brokerservice.ecore">
<brokerservice:broker brokerId="111">
<brokerservice:firstName>Joe</brokerservice:firstName>
</brokerservice:broker>
</brokerservice:brokerService>
												
										

改变序列化的 XML 的命名空间

上面例子中序列化的 XML 包含所有用于新的命名空间声明的合格的元素。如果消费这些生成的 XML 的后台系统不不能够处理这样的元素,可以配置序列化器生成一个默认的命名空间声明。虽然不推荐,但是在一些特定的情况下这是可行的。下面显示了方法(清单 5a)和输出(清单 5b)。

清单 5a. 方法

												
														public void TestNamespaceRemove(){
	DocumentRoot root = DocumentRoot)XMLTransformServiceFactory.INSTANCE.create
		(BrokerservicePackage.eINSTANCE.getNsURI());
	 root.getXMLNSPrefixMap().put("", BrokerservicePackage.eINSTANCE.getNsURI());
	xyz.brokerservice.BrokerServiceType bs = root.createBrokerService();
	BrokerType bType  = bs.createBroker();
	bType.setBrokerId("111");
	bType.setFirstName("Joe");
	System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)root));
}
												
										

清单 5b. 输出

												
														<?xml version="1.0" encoding="ASCII"?>
<brokerService xmlns="http:///xyz.brokerservice.ecore">
  <broker brokerId="111">
    <firstName>Joe</firstName>
  </broker>
</brokerService>
												
										

处理 schema 变更

让我们思考一个场景,生成 XML 响应的服务被更一遍来适应新的 schema 定义(BrokerServiceV2.xsd,包含在 xsd_sdo_soa_part3_listings.zip下载文件),并且这个 schema 定义比用来生成 SDO 包的包含更多的元素或者属性。XMLTransformServiceFactory 使用的序列化器被预配置来记录任何新的不属于初始 schema 的元素和属性。这个“Version 2” XML 响应(BrokerDetailResponseV2.xml,包含在 xsd_sdo_soa_part3_listings.zip下载文件)可以被加载到使用“Version 1” schema 生成的 SDO 包中。如果这个包被序列化为 XML,它将包含在加载时记录的可选的元素,如图 5 所示。


图 5. BrokerServiceV1.xsd
BrokerServiceV1.xsd

清单 6. BrokerDetailResponseV1.xml

												
														<?xml version="1.0" encoding="UTF-8"?>
<brokerService xmlns="http:///xyz.brokerservice.ecore" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<broker brokerId="000-00-9999">
	<firstName>Narinder</firstName>
	<lastName>Makin</lastName>
	<client clientId="001-00-9999">
		<firstName>Dan</firstName>
		<lastName>Moore</lastName>
		<dateOfBirth>1967-08-13</dateOfBirth>
		<currentAddress>
			<street>113 Oak Pine St.</street>
			<city>Santa Clara</city>
			<state>LA</state>
			<zip>91929</zip>
			<country>US</country>
		</currentAddress>
		<permanentAddress>
			<street>123 Demi Lane</street>
			<city>Cary</city>
			<state>NC</state>
			<zip>22999</zip>
			<country>US</country>
		</permanentAddress>
		<policy policyId="L000000000">
			<policyName>Life</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>100000.00</policyAmount>
			<policyDescription>Life Insurance policy includes any accidental damages.</policyDescription>
		</policy>
		<policy policyId="H000000000">
			<policyName>House</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>50000.00</policyAmount>
			<policyDescription>Home Insurance</policyDescription>
		</policy>
		<policy policyId="C000000001">
			<policyName>Car 1</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>15000.00</policyAmount>
			<policyDescription>Car Insurance - Ferrari 2004 - Primary Car </policyDescription>
		</policy>
		<policy policyId="C000000002">
			<policyName>Car 2</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>5000.00</policyAmount>
			<policyDescription>Car Insurance - Lexus 2003 - Secondary Car </policyDescription>
		</policy>
		<policy policyId="B000000002">
			<policyName>Restaurant</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>25000.00</policyAmount>
			<policyDescription>Business Insurance - Restaurant</policyDescription>
		</policy>
	</client>
	<client clientId="002-00-9999">
		<firstName>Tom</firstName>
		<lastName>Cross</lastName>
		<dateOfBirth>1970-11-11</dateOfBirth>
		<currentAddress>
			<street>113 Duke St.</street>
			<city>Shelton</city>
			<state>CT</state>
			<zip>08989</zip>
			<country>US</country>
		</currentAddress>
		<permanentAddress>
			<street>123 Lex Lane</street>
			<city>Fairfield</city>
			<state>NY</state>
			<zip>09833</zip>
			<country>US</country>
		</permanentAddress>
		<policy policyId="H100000000">
			<policyName>House</policyName>	
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>2000.00</policyAmount>
			<policyDescription>Home Insurance</policyDescription>
		</policy>
		<policy policyId="L100000000">
			<policyName>Life</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>100000.00</policyAmount>
			<policyDescription>Life Insurance</policyDescription>
		</policy>
		<policy policyId="C100000001">
			<policyName>Car 1</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>2000.00</policyAmount>
			<policyDescription>Car Insurance - Camry 2004 - Primary Car </policyDescription>
		</policy>
	</client>
</broker>
</brokerService>

												
										

继续我们的场景,为了为来自另一个客户端的安全服务请求服务,一个新的可选属性,securityToken,以及一个新的可选的元素,ssn,被添加到“Version 2” brokerservice 的 clientType 定义中。一个需求指示在已有的、使用旧的 XSD 为其生成 SDO 包的客户端应用程序中不需要做任何的修改。XMLTransformFactory 具有一定的格式,这样 XML 流中呈现的任何未知的元素将被记录到 SDO 实例中。当这些实例被序列化时,这些被记录的未知元素(ssn 和 securityToken)也会被序列化。


图 6. BrokerServiceV2.xsd 修改
BrokerServiceV2.xsd Modifications

清单 7. BrokerDetailResponseV2.xml

												
														<?xml version="1.0" encoding="UTF-8"?>
<brokerService xmlns="http:///xyz.brokerservice.ecore" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<broker brokerId="000-00-9999">
	<firstName>Narinder</firstName>
	<lastName>Makin</lastName>
	<client clientId="001-00-9999" securityToken="abcded">
		<firstName>Dan</firstName>
		<lastName>Moore</lastName>
		<dateOfBirth>1967-08-13</dateOfBirth>
		<currentAddress>
			<street>113 Oak Pine St.</street>
			<city>Santa Clara</city>
			<state>LA</state>
			<zip>91929</zip>
			<country>US</country>
		</currentAddress>
		<permanentAddress>
			<street>123 Demi Lane</street>
			<city>Cary</city>
			<state>NC</state>
			<zip>22999</zip>
			<country>US</country>
		</permanentAddress>
		<policy policyId="L000000000">
			<policyName>Life</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>100000.00</policyAmount>
			<policyDescription>Life Insurance policy includes any accidental damages.</policyDescription>
		</policy>
		<policy policyId="H000000000">
			<policyName>House</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>50000.00</policyAmount>
			<policyDescription>Home Insurance</policyDescription>
		</policy>
		<policy policyId="C000000001">
			<policyName>Car 1</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>15000.00</policyAmount>
			<policyDescription>Car Insurance - Ferrari 2004 - Primary Car </policyDescription>
		</policy>
		<policy policyId="C000000002">
			<policyName>Car 2</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>5000.00</policyAmount>
			<policyDescription>Car Insurance - Lexus 2003 - Secondary Car </policyDescription>
		</policy>
		<policy policyId="B000000002">
			<policyName>Restaurant</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>25000.00</policyAmount>
			<policyDescription>Business Insurance - Restaurant</policyDescription>
		</policy>
		<ssn>1234567</ssn>
	</client>
	<client clientId="002-00-9999"  securityToken="xyz">
		<firstName>Tom</firstName>
		<lastName>Cross</lastName>
		<dateOfBirth>1970-11-11</dateOfBirth>
		<currentAddress>
			<street>113 Duke St.</street>
			<city>Shelton</city>
			<state>CT</state>
			<zip>08989</zip>
			<country>US</country>
		</currentAddress>
		<permanentAddress>
			<street>123 Lex Lane</street>
			<city>Fairfield</city>
			<state>NY</state>
			<zip>09833</zip>
			<country>US</country>
		</permanentAddress>
		<policy policyId="H100000000">
			<policyName>House</policyName>	
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>2000.00</policyAmount>
			<policyDescription>Home Insurance</policyDescription>
		</policy>
		<policy policyId="L100000000">
			<policyName>Life</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>100000.00</policyAmount>
			<policyDescription>Life Insurance</policyDescription>
		</policy>
		<policy policyId="C100000001">
			<policyName>Car 1</policyName>
			<policyStartDate>2004-01-01</policyStartDate>
			<policyEndDate>2005-01-01</policyEndDate>
			<policyAmount>2000.00</policyAmount>
			<policyDescription>Car Insurance - Camry 2004 - Primary Car </policyDescription>
		</policy>
		<ssn>12345678</ssn>
	</client>
</broker>
</brokerService>

												
										

在清单 8 的代码里面,加载了 BrokerDetailResponseV2.xml 并修改了 firstName。这时该数据对象被序列化回 XML。序列化的 XML(清单)包含了修改,以及在加载过程中记录的可选的 ssn 和 securityToken 元素。

清单 8

												
														public void TestSchemaChange()
{
xyz.brokerservice.DocumentRoot loaded = ( xyz.brokerservice.DocumentRoot)
	XMLTransformServiceFactory.INSTANCE.loadFile("./input/brokerDetailResponseV2.xml");
xyz.brokerservice.BrokerServiceType bs = loaded.getBrokerService();
bs.getBroker().setFirstName("NEW NAME");
System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)loaded))     
}
												
										

清单 9

												
														<?xml version="1.0" encoding="UTF-8"?>
<brokerService xmlns="http:///xyz.brokerservice.ecore">
  <broker brokerId="000-00-9999">
    <firstName>NEW NAME</firstName>
    <lastName>Makin</lastName>
    <client clientId="001-00-9999" securityToken="abcded">
      <firstName>Dan</firstName>
      <lastName>Moore</lastName>
      <dateOfBirth>1967-08-13</dateOfBirth>
      <currentAddress>
        <street>113 Oak Pine St.</street>
        <city>Santa Clara</city>
        <state>LA</state>
        <zip>91929</zip>
        <country>US</country>
      </currentAddress>
      <permanentAddress>
        <street>123 Demi Lane</street>
        <city>Cary</city>
        <state>NC</state>
        <zip>22999</zip>
        <country>US</country>
      </permanentAddress>
      <policy policyId="L000000000">
        <policyName>Life</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>100000.00</policyAmount>
        <policyDescription>Life Insurance policy includes any accidental damages.</policyDescription>
      </policy>
      <policy policyId="H000000000">
        <policyName>House</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>50000.00</policyAmount>
        <policyDescription>Home Insurance</policyDescription>
      </policy>
      <policy policyId="C000000001">
        <policyName>Car 1</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>15000.00</policyAmount>
        <policyDescription>Car Insurance - Ferrari 2004 - Primary Car </policyDescription>
      </policy>
      <policy policyId="C000000002">
        <policyName>Car 2</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>5000.00</policyAmount>
        <policyDescription>Car Insurance - Lexus 2003 - Secondary Car </policyDescription>
      </policy>
      <policy policyId="B000000002">
        <policyName>Restaurant</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>25000.00</policyAmount>
        <policyDescription>Business Insurance - Restaurant</policyDescription>
      </policy>
      <ssn>1234567</ssn>
    </client>
    <client clientId="002-00-9999" securityToken="xyz">
      <firstName>Tom</firstName>
      <lastName>Cross</lastName>
      <dateOfBirth>1970-11-11</dateOfBirth>
      <currentAddress>
        <street>113 Duke St.</street>
        <city>Shelton</city>
        <state>CT</state>
        <zip>08989</zip>
        <country>US</country>
      </currentAddress>
      <permanentAddress>
        <street>123 Lex Lane</street>
        <city>Fairfield</city>
        <state>NY</state>
        <zip>09833</zip>
        <country>US</country>
      </permanentAddress>
      <policy policyId="H100000000">
        <policyName>House</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Home Insurance</policyDescription>
      </policy>
      <policy policyId="L100000000">
        <policyName>Life</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>100000.00</policyAmount>
        <policyDescription>Life Insurance</policyDescription>
      </policy>
      <policy policyId="C100000001">
        <policyName>Car 1</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Car Insurance - Camry 2004 - Primary Car </policyDescription>
      </policy>
      <ssn>12345678</ssn>
    </client>
  </broker>
</brokerService>

												
										

上面的行为可能并不需要,并且也可以通过修改清单 10a 中的序列化器选项来关闭,其使用 getOptions API。该序列化器为正在加载的 XML 中的元素抛出一个异常,但是该 XML 并不是用来生成包的原始 schema 的一部分。XML 输出入清单 10b 所示。

清单 10a. 方法

												
														public void TestRestrictSchemaChange()
{
	try
	{
		XMLTransformServiceFactory.INSTANCE.getOptions().put
			(org.eclipse.emf.ecore.xmi.XMLResource.OPTION_RECORD_UNKNOWN_FEATURE, Boolean.FALSE);
	        xyz.brokerservice.DocumentRoot loaded = ( xyz.brokerservice.DocumentRoot)
			XMLTransformServiceFactory.INSTANCE.loadFile("./DataFile/brokerDetailResponseV2.xml");
	        xyz.brokerservice.BrokerServiceType bs = loaded.getBrokerService();
		bs.getBroker().setFirstName("NEW NAME");
		System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)loaded));
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
}

												
										

清单 10b. 输出

												
														org.eclipse.emf.ecore.xmi.FeatureNotFoundException: Feature 'securityToken' not found. (root.xml, 6, 56)
	at org.eclipse.emf.ecore.xmi.impl.XMLHandler.reportUnknownFeature(XMLHandler.java:1168)
	at org.eclipse.emf.ecore.xmi.impl.XMLHandler.handleUnknownFeature(XMLHandler.java:1138)
	at org.eclipse.emf.ecore.xmi.impl.XMLHandler.setAttribValue(XMLHandler.java:1780)
	at org.eclipse.emf.ecore.xmi.impl.SAXXMLHandler.handleObjectAttribs(SAXXMLHandler.java:145)
	at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectFromFactory(XMLHandler.java:1346)
	at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectFromFeatureType(XMLHandler.java:1318)
	at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObject(XMLHandler.java:1213)
	at org.eclipse.emf.ecore.xmi.impl.XMLHandler.handleFeature(XMLHandler.java:1022)
	at org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XMLHandler.java:448)
	at org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHandler.java:431)
	at org.eclipse.emf.ecore.xmi.impl.SAXWrapper.startElement(SAXWrapper.java:75)
	at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl
		$FragmentContentDispatcher.dispatch(Unknown Source)
	at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
	at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
	at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
	at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
	at javax.xml.parsers.SAXParser.parse(Unknown Source)
	at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:129)
	at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:155)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:884)
	at dw.ibm.etools.xsd.sdo.xmltransformservice.impl.XMLTransformServiceImpl.load
		(XMLTransformServiceImpl.java:85)
	at dw.ibm.etools.xsd.sdo.xmltransformservice.impl.XMLTransformServiceImpl.load
		(XMLTransformServiceImpl.java:102)
	at dw.ibm.etools.xsd.sdo.xmltransformservice.impl.XMLTransformServiceFactoryImpl.loadStream
		(XMLTransformServiceFactoryImpl.java:50)
	at dw.ibm.etools.xsd.sdo.xmltransformservice.impl.XMLTransformServiceFactoryImpl.loadFile
		(XMLTransformServiceFactoryImpl.java:65)
	at sdotestclient.TestClient.TestRestrictSchemaChange(TestClient.java:101)
	at sdotestclient.TestClient.main(TestClient.java:33)
java.lang.NullPointerException
	at sdotestclient.TestClient.TestRestrictSchemaChange(TestClient.java:102)
	at sdotestclient.TestClient.main(TestClient.java:33)

												
										







SDO API 的使用

生成的 SDO 包使用了一个潜在的 Eclipse Modeling Framework 实现。EMF 提供了用于图表或相关(或不相关)对象的通用导航模型,并且包含了一个内置的变更通知机制。因此,生成的 SDO 包含使其可以穿过相关(或不相关)的 SDO 的 API。对这些对象实例或它们清单的任何修改都导致了管理变更的通知问题。下一部分主要强调了在操作从 XML 流中加载的 SDO 实例时的一些基本考虑。功能包含:

(参阅资源来查看关于 EMF 和 SDO 的更多信息。)

添加

正如我们在上一部分中看到的那样,SDO 对象的一个新的实例既可以通过使用工具包中生成的工厂类来创建,以可以使用相关 SDO 的 create<SDO Name> 方法。让我们考虑一个例子,我们有一个从 XML 请求中加载的 SDO 图表实例。现在我们需要创建一个新的包含来自于加载的 SDO 图表实例的 SDO 图表。

在清单 11a 中,来自于 moveFrom 的 Broker 实例在 moveTo SDO 中被摄置。如果我们检查 moveFrom 的内容,我们将看到它不再包含 broker 实例,然而 moveTo SDO 却包含。默认情况下,这个案例中一个简单的,添加操作导致了从原始容器到新的容器的一个移动。因此一个 SDO 实例在同一时刻仅能被一个容器所包含。

清单 11a. 方法

public void TestMove()
   {
	xyz.brokerservice.DocumentRoot moveFrom = ( xyz.brokerservice.DocumentRoot)
		XMLTransformServiceFactory.INSTANCE.loadFile("./DataFile/brokerDetailResponseV1.xml");        
	xyz.brokerservice.DocumentRoot moveTo = xyz.brokerservice.DocumentRoot)
		XMLTransformServiceFactory.INSTANCE.create(BrokerservicePackage.eINSTANCE.getNsURI());
	moveTo.getXMLNSPrefixMap().put("", BrokerservicePackage.eINSTANCE.getNsURI());
	moveTo.createBrokerService().setBroker(moveFrom.getBrokerService().getBroker());
	System.out.println("**********Changes in Original XML********");	    
	System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)moveFrom));
	System.out.println("**********************************************************");
	System.out.println("**********Target XML**************************");
	System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)moveTo));
	System.out.println("**********************************************************");
   }

清单 11b. 输出

**********Changes in Original XML********
<?xml version="1.0" encoding="UTF-8"?>
<brokerService xmlns="http:///xyz.brokerservice.ecore"/>
**********************************************************
**********Target XML**************************
<?xml version="1.0" encoding="ASCII"?>
<brokerService xmlns="http:///xyz.brokerservice.ecore">
  <broker brokerId="000-00-9999">
    <firstName>Narinder</firstName>
    <lastName>Makin</lastName>
    <client clientId="001-00-9999">
      <firstName>Dan</firstName>
      <lastName>Moore</lastName>
      <dateOfBirth>1967-08-13</dateOfBirth>
      <currentAddress>
        <street>113 Oak Pine St.</street>
        <city>Santa Clara</city>
        <state>LA</state>
        <zip>91929</zip>
        <country>US</country>
      </currentAddress>
      <permanentAddress>
        <street>123 Demi Lane</street>
        <city>Cary</city>
        <state>NC</state>
        <zip>22999</zip>
        <country>US</country>
      </permanentAddress>
      <policy policyId="L000000000">
        <policyName>Life</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>100000.00</policyAmount>
        <policyDescription>Life Insurance policy includes any accidental damages.</policyDescription>
      </policy>
      <policy policyId="H000000000">
        <policyName>House</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>50000.00</policyAmount>
        <policyDescription>Home Insurance</policyDescription>
      </policy>
      <policy policyId="C000000001">
        <policyName>Car 1</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>15000.00</policyAmount>
        <policyDescription>Car Insurance - Ferrari 2004 - Primary Car </policyDescription>
      </policy>
      <policy policyId="C000000002">
        <policyName>Car 2</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>5000.00</policyAmount>
        <policyDescription>Car Insurance - Lexus 2003 - Secondary Car </policyDescription>
      </policy>
      <policy policyId="B000000002">
        <policyName>Restaurant</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>25000.00</policyAmount>
        <policyDescription>Business Insurance - Restaurant</policyDescription>
      </policy>
    </client>
    <client clientId="002-00-9999">
      <firstName>Tom</firstName>
      <lastName>Cross</lastName>
      <dateOfBirth>1970-11-11</dateOfBirth>
      <currentAddress>
        <street>113 Duke St.</street>
        <city>Shelton</city>
        <state>CT</state>
        <zip>08989</zip>
        <country>US</country>
      </currentAddress>
      <permanentAddress>
        <street>123 Lex Lane</street>
        <city>Fairfield</city>
        <state>NY</state>
        <zip>09833</zip>
        <country>US</country>
      </permanentAddress>
      <policy policyId="H100000000">
        <policyName>House</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Home Insurance</policyDescription>
      </policy>
      <policy policyId="L100000000">
        <policyName>Life</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>100000.00</policyAmount>
        <policyDescription>Life Insurance</policyDescription>
      </policy>
      <policy policyId="C100000001">
        <policyName>Car 1</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Car Insurance - Camry 2004 - Primary Car </policyDescription>
      </policy>
    </client>
  </broker>
</brokerService>
**********************************************************

复制

如果需要一个准确的复制操作,这样两个 SDO 图表都将包含 SDO,应该改用 ObjectUtil.copyFrom 操作,因为在内部使用的是 EMF API。这个 API 执行了深度拷贝,包含所有嵌套的子对象都被复制到新的图表里面。

在清单 12a 中,copyFrom 的内容和以前相比并没有改变,但是 copyTo 包含该 broker 的新实例,并且同为其从 copyFrom SDO 获得的数据有相同的结构。

清单 12a. 方法

public void TestCopy()
   {
	xyz.brokerservice.DocumentRoot copyFrom = (xyz.brokerservice.DocumentRoot)
		XMLTransformServiceFactory.INSTANCE.loadFile("./DataFile/brokerDetailResponseV1.xml");
	xyz.brokerservice.DocumentRoot copyTo = (xyz.brokerservice.DocumentRoot)
		XMLTransformServiceFactory.INSTANCE.create(BrokerservicePackage.eINSTANCE.getNsURI());
	copyTo.getXMLNSPrefixMap().put("", BrokerservicePackage.eINSTANCE.getNsURI());
	copyTo.createBrokerService().setBroker((BrokerType)ObjectUtil.INSTANCE.copyFrom((DataObject)
		copyFrom.getBrokerService().getBroker()));
	System.out.println("**********No Changes in Original XML********");
	System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)copyFrom));
	System.out.println("**********************************************************");
	System.out.println("**********Target XML**************************");
	System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)copyTo));
	System.out.println("**********************************************************");
   }

清单 12b. 输出

**********No Changes in Original XML********
<?xml version="1.0" encoding="UTF-8"?>
<brokerService xmlns="http:///xyz.brokerservice.ecore">
  <broker brokerId="000-00-9999">
    <firstName>Narinder</firstName>
    <lastName>Makin</lastName>
    <client clientId="001-00-9999">
      <firstName>Dan</firstName>
      <lastName>Moore</lastName>
      <dateOfBirth>1967-08-13</dateOfBirth>
      <currentAddress>
        <street>113 Oak Pine St.</street>
        <city>Santa Clara</city>
        <state>LA</state>
        <zip>91929</zip>
        <country>US</country>
      </currentAddress>
      <permanentAddress>
        <street>123 Demi Lane</street>
        <city>Cary</city>
        <state>NC</state>
        <zip>22999</zip>
        <country>US</country>
      </permanentAddress>
      <policy policyId="L000000000">
        <policyName>Life</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>100000.00</policyAmount>
        <policyDescription>Life Insurance policy includes any accidental damages.</policyDescription>
      </policy>
      <policy policyId="H000000000">
        <policyName>House</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>50000.00</policyAmount>
        <policyDescription>Home Insurance</policyDescription>
      </policy>
      <policy policyId="C000000001">
        <policyName>Car 1</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>15000.00</policyAmount>
        <policyDescription>Car Insurance - Ferrari 2004 - Primary Car </policyDescription>
      </policy>
      <policy policyId="C000000002">
        <policyName>Car 2</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>5000.00</policyAmount>
        <policyDescription>Car Insurance - Lexus 2003 - Secondary Car </policyDescription>
      </policy>
      <policy policyId="B000000002">
        <policyName>Restaurant</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>25000.00</policyAmount>
        <policyDescription>Business Insurance - Restaurant</policyDescription>
      </policy>
    </client>
    <client clientId="002-00-9999">
      <firstName>Tom</firstName>
      <lastName>Cross</lastName>
      <dateOfBirth>1970-11-11</dateOfBirth>
      <currentAddress>
        <street>113 Duke St.</street>
        <city>Shelton</city>
        <state>CT</state>
        <zip>08989</zip>
        <country>US</country>
      </currentAddress>
      <permanentAddress>
        <street>123 Lex Lane</street>
        <city>Fairfield</city>
        <state>NY</state>
        <zip>09833</zip>
        <country>US</country>
      </permanentAddress>
      <policy policyId="H100000000">
        <policyName>House</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Home Insurance</policyDescription>
      </policy>
      <policy policyId="L100000000">
        <policyName>Life</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>100000.00</policyAmount>
        <policyDescription>Life Insurance</policyDescription>
      </policy>
      <policy policyId="C100000001">
        <policyName>Car 1</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Car Insurance - Camry 2004 - Primary Car </policyDescription>
      </policy>
    </client>
  </broker>
</brokerService>
**********************************************************
**********Target XML**************************
<?xml version="1.0" encoding="ASCII"?>
<brokerService xmlns="http:///xyz.brokerservice.ecore">
  <broker brokerId="000-00-9999">
    <firstName>Narinder</firstName>
    <lastName>Makin</lastName>
    <client clientId="001-00-9999">
      <firstName>Dan</firstName>
      <lastName>Moore</lastName>
      <dateOfBirth>1967-08-13</dateOfBirth>
      <currentAddress>
        <street>113 Oak Pine St.</street>
        <city>Santa Clara</city>
        <state>LA</state>
        <zip>91929</zip>
        <country>US</country>
      </currentAddress>
      <permanentAddress>
        <street>123 Demi Lane</street>
        <city>Cary</city>
        <state>NC</state>
        <zip>22999</zip>
        <country>US</country>
      </permanentAddress>
      <policy policyId="L000000000">
        <policyName>Life</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>100000.00</policyAmount>
        <policyDescription>Life Insurance policy includes any accidental damages.</policyDescription>
      </policy>
      <policy policyId="H000000000">
        <policyName>House</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>50000.00</policyAmount>
        <policyDescription>Home Insurance</policyDescription>
      </policy>
      <policy policyId="C000000001">
        <policyName>Car 1</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>15000.00</policyAmount>
        <policyDescription>Car Insurance - Ferrari 2004 - Primary Car </policyDescription>
      </policy>
      <policy policyId="C000000002">
        <policyName>Car 2</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>5000.00</policyAmount>
        <policyDescription>Car Insurance - Lexus 2003 - Secondary Car </policyDescription>
      </policy>
      <policy policyId="B000000002">
        <policyName>Restaurant</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>25000.00</policyAmount>
        <policyDescription>Business Insurance - Restaurant</policyDescription>
      </policy>
    </client>
    <client clientId="002-00-9999">
      <firstName>Tom</firstName>
      <lastName>Cross</lastName>
      <dateOfBirth>1970-11-11</dateOfBirth>
      <currentAddress>
        <street>113 Duke St.</street>
        <city>Shelton</city>
        <state>CT</state>
        <zip>08989</zip>
        <country>US</country>
      </currentAddress>
      <permanentAddress>
        <street>123 Lex Lane</street>
        <city>Fairfield</city>
        <state>NY</state>
        <zip>09833</zip>
        <country>US</country>
      </permanentAddress>
      <policy policyId="H100000000">
        <policyName>House</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Home Insurance</policyDescription>
      </policy>
      <policy policyId="L100000000">
        <policyName>Life</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>100000.00</policyAmount>
        <policyDescription>Life Insurance</policyDescription>
      </policy>
      <policy policyId="C100000001">
        <policyName>Car 1</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Car Insurance - Camry 2004 - Primary Car </policyDescription>
      </policy>
    </client>
  </broker>
</brokerService>
**********************************************************

删除

通过一个简单的列表"remove"操作,SDO 可以从容器中删除。如清单 13a 所示,客户端的实例从 broker 实例中被移除。

清单 13a. 方法

public void TestDelete()
   {
	xyz.brokerservice.DocumentRoot deleteFrom = ( xyz.brokerservice.DocumentRoot)
		XMLTransformServiceFactory.INSTANCE.loadFile("./DataFile/brokerDetailResponseV1.xml");
	BrokerType brokerType = deleteFrom.getBrokerService().getBroker();
	List clientList = brokerType.getClient();
	Iterator clientListItr = clientList.iterator();
	ClientType clientMoore = null;
	while(clientListItr.hasNext()){
		ClientType clientType = (ClientType)clientListItr.next();
		if(clientType.getLastName().equals("Moore")){
			clientMoore = clientType;
			break;
		}
        }
	clientList.remove(clientMoore);        
		System.out.println("**********Client with last name 'Moore'  removed from Original XML********");
		System.out.println(XMLTransformServiceFactory.INSTANCE.convert((DataObject)deleteFrom));
		System.out.println("**********************************************************");
   }

清单 13b. 输出

**********Client with last name 'Moore'  removed from Original XML********
<?xml version="1.0" encoding="UTF-8"?>
<brokerService xmlns="http:///xyz.brokerservice.ecore">
  <broker brokerId="000-00-9999">
    <firstName>Narinder</firstName>
    <lastName>Makin</lastName>
    <client clientId="002-00-9999">
      <firstName>Tom</firstName>
      <lastName>Cross</lastName>
      <dateOfBirth>1970-11-11</dateOfBirth>
      <currentAddress>
        <street>113 Duke St.</street>
        <city>Shelton</city>
        <state>CT</state>
        <zip>08989</zip>
        <country>US</country>
      </currentAddress>
      <permanentAddress>
        <street>123 Lex Lane</street>
        <city>Fairfield</city>
        <state>NY</state>
        <zip>09833</zip>
        <country>US</country>
      </permanentAddress>
      <policy policyId="H100000000">
        <policyName>House</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Home Insurance</policyDescription>
      </policy>
      <policy policyId="L100000000">
        <policyName>Life</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>100000.00</policyAmount>
        <policyDescription>Life Insurance</policyDescription>
      </policy>
      <policy policyId="C100000001">
        <policyName>Car 1</policyName>
        <policyStartDate>2004-01-01</policyStartDate>
        <policyEndDate>2005-01-01</policyEndDate>
        <policyAmount>2000.00</policyAmount>
        <policyDescription>Car Insurance - Camry 2004 - Primary Car </policyDescription>
      </policy>
    </client>
  </broker>
</brokerService>
**********************************************************

旋转

因为 SDO 实现是基于 EMF 的,所以可以使用 EMF 旋转 API 来旋转一个 SDO 图表。你即可以访问相关的子对象,也可以使用 ObjectUtil 的 getParent API 来旋转父对象树。这个 API 将该引用旋转到容器 SDO,其包含 SDO 并作为一个类型引用或在一个列表中。在清单 14a 和 14b 中,broker 实例从客户端 SDO 的实例旋转过来。

清单 14a. 代码

public void TestGraphTravesal()
   {
	xyz.brokerservice.DocumentRoot traverseFrom = ( xyz.brokerservice.DocumentRoot)
		XMLTransformServiceFactory.INSTANCE.loadFile("./DataFile/brokerDetailResponseV1.xml");
	BrokerType brokerType = traverseFrom.getBrokerService().getBroker();
	System.out.println("BrokerType is: " + brokerType.getFirstName());
	List clientList = brokerType.getClient();
	Iterator clientListItr = clientList.iterator();
	ClientType clientMoore = null;
	while(clientListItr.hasNext()){
		ClientType clientType = (ClientType)clientListItr.next();
		BrokerType parent = (BrokerType)ObjectUtil.INSTANCE.getParent(((DataObject)clientType));
		System.out.println("Parent brokerType is: " + parent.getFirstName());
	}
   }

清单 14b. 结果

BrokerType is: Narinder
Parent brokerType is: Narinder
Parent brokerType is: Narinder







合计 API 的使用

工具 API,比如 sort,sum,mean 等,其使用一般贯穿于整个应用程序。为一个基于 XML 的 SOA 创建的应用程序一般都会需要这种 API 的编码。如果我们使用 SDO 实现,这些方法在贯穿该 SDO 实例都会提供。在 XSD SDO Transform 特性中提供的 ObjectUtil 辅助类,包含合计和其他工具 API,来操作 SDO 对象。这一部分图示了这些 API 的使用。

Sum

Sum API 可以用来提供一个列表中 EAttribute 值的总和。这个 API 通过对列表一个简单的调用来使用,该列表包含属性的实例和 EAttribute。EPackage 提供了到所有 EMF 类型的访问,这些类型用于呈现在 SDO 包中的 EObject 和 EAttribute。类型 Integer,Float,Long,Short,Double,BigDecimal 和 String 的属性对于 Sum 是可用的。对于类型为 String 的属性,已经在暗地里将其转化为 Long,Double 或者 Float。

清单 15. Sum API

/**
 * Get the sum for the values of the attribute in an EList
 * @exception Exception Throws java.lang.Exception
 * @param list a EList containing the attribute values
 * @param attr The EAttribute for the attribute whose sum is to be determined
 * @return Object  The sum of the value of the EAttributes in the list.If the length is less than 0
 * then return null. If the attribute is not valid for doing sum and mean then return null
 */
public Object sum(List list, EAttribute attr) throws Exception

Mean

Mean API 可以用来提供列表中呈现的 EAttribute 的平均值。这个 API 通过对列表的一个简单调用来实现,该列表包含该属性的实例和 EAttribute。其合法的属性类型同 Sun 是相同的。

Listing 16. Mean API

/**
 * Get the mean for the values of the attribute in an EList
 * @exception Exception Throws java.lang.Exception
 * @param list a EList containing the attribute values
 * @param attr The EAttribute for the attribute whose mean is to be determined
 * @return Object  The mean of the value of the EAttributes in the list.If the length is less than 0
 * then return null. If the attribute is not valid for doing sum and mean then return null
 */
public Object mean(List list, EAttribute attr) throws Exception

Min

Min API 可以用来提供呈现在列表中 EAttribute 的最小值。这个 API 可以通过一个对列表的简单调用来使用,该列表包含用于该属性的实例和 EAttribute。类型为 Integer,Float,Long,Short,Double,BigDecimal,Date,Time,Timestamp 和 String 的属性对于 Min 都是可用的。

清单 17. Min API

/**
 * Get the minimum value from the attributes in the EList
 * @param list a EList containing the attribute values
 * @param attr an EAttribute defining the EType of the attributes in the list
 * @return Object  The minimum value in the list 
 * If the length is less than 0 then return null.
 * If the attribute is not valid then return null
 */
public Object min(List list, EAttribute attr) throws Exception

Max

Max API 可以用来提供列表中呈现的 EAttribute 的最大值。这个 API 通过对列表的一个简单调用来实现,该列表包含该属性的实例和 EAttribute。其合法的属性类型同 Min 是相同的。

清单 18. Max API

/**
 * Get the maximum value from the attributes in the EList
 * @param list a EList containing the attribute values
 * @param attr an EAttribute defining the EType of the attributes in the list
 * @return Object  The maximum value in the list 
 * If the length is less than 0 then return null.
 * If the attribute is not valid then return null
 */
public Object max(List list, EAttribute attr) throws Exception

这个合计 API 的编码和结果如图 19a 和 19b 所示。

清单 19a. 代码

public void TestAggregateAPI(){
	xyz.brokerservice.DocumentRoot root = ( xyz.brokerservice.DocumentRoot)
		XMLTransformServiceFactory.INSTANCE.loadFile("./DataFile/brokerDetailResponseV1.xml");
	BrokerServiceType bs = root.getBrokerService();
	try {
		//sum test
		System.out.println("Sum is : " + ObjectUtil.INSTANCE.sum(bs.getBroker()
			.getClientAsArray()[0].getPolicy(), 
			BrokerservicePackage.eINSTANCE.getPolicyType_PolicyAmount()));
			
		//mean test
		System.out.println("Mean is : " + ObjectUtil.INSTANCE.mean(bs.getBroker()
			.getClientAsArray()[0].getPolicy(), 
			BrokerservicePackage.eINSTANCE.getPolicyType_PolicyAmount()));
			
		//max test
		System.out.println("Max is : " + ObjectUtil.INSTANCE.max(bs.getBroker()
			.getClientAsArray()[0].getPolicy(), 
			BrokerservicePackage.eINSTANCE.getPolicyType_PolicyName()));
			
		//min test
		System.out.println("Min is : " + ObjectUtil.INSTANCE.min(bs.getBroker()
			.getClientAsArray()[0].getPolicy(), 
			BrokerservicePackage.eINSTANCE.getPolicyType_PolicyName()));
	} 
	catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

清单 19b. 结果

Sum is : 195000.0
Mean is : 39000.0
Max is : Restaurant
Min is : Car 1







使用 XSD-to-SDO 生成器

Application Developer 还提供了从 XSD 文档生成 SDO 包的特性。你可以使用这个特性来生成 SDO Java 包,而不是通过e Create SDO Package 动作。Create SDO Package 动作生成的代码包含数组方法和用于相关 SDO 对象的列表方法。这就简化了数据库表到 collection 的绑定,而不用提示定义 collection 中定义的类型。除了这些,这个生成器还可以添加需要的框架 JAR 到项目中,这样就不需要人工的设置。当你不仅仅需要创建 SDO 包的时候,使用 Create SDO Package 动作是可行的,比如当开发一个 Web 项目的时候。当使用一个简单的 Java 项目的时候,你可能希望使用 XSD SDO 生成器并且手工的添加框架 JAR 文件,xsdsdotransform.jar,到类路径中,并且如果需要转化能力的话就构建项目路径。EMF 框架 JAR 被生成器自动添加到项目中。(这些 JAR 在站点 EMF Web site 上可以下载,并且同 Application Developer 或者 WebSphere Application Server 运行时提供的 emf.jar 时相同的。)

为了使用 Application Developer 中的 SDO 生成器:

  1. 在参数选项对话框中打开 XML 开发能力(图片 7)。
    图 7. 打开 XML 能力
    打开 XML 能力
  2. 这使一个 XSD 文档被选中的时候,一个相应的菜单可以用来生成 Java。(图 8)
    图 8. 菜单动作
    菜单动作
  3. 在 Generate Java 对话框中,选择 SDO Generator 来使 XSD SDO Java 包的生成可用(图 9)。
    图 9. XSD SDO 生成器向导
    XSD SDO 生成器向导
  4. 最为向操作的结果,SDO Java 包被创建(图 10)并且所需的框架 JAR 被添加到 Java 项目的构建和类路径中。
    图 10. SDO Java Package
    SDO Java Package

已知的局限性:XSD 继承在 SDO 包的生成中是不支持的。在创建 SDO 包以前,你将需要手动修复生成的代码或者将 schema 放入一个单独的文件。







结束语

这一系列文章的第三部分着重讲了在构建一个基于 XML 的 SOA 应用程序的时候,通常使用的用于操作 SDO 实例的一些技术。怎么使用 SDO 生成器,以及在代码生成的时候使用 Create SDO Package 动作的不同也都进行了讨论。

第四部分将集中在使用 XSD SDO 转化特性的基于 XML 的 SOA 的 portlet 开发,选择 JSR 168 portlet 创建,在 portlet 之间的参数传递,以及在一个 portlet 动作上动态的设置其他任何 portlet 的目标 portlet 的视图。第五部分将讨论为使用 XSD SDO 转化特性的基于 XML 的 SOA 本地化 JSF 和 portlet 应用程序,并且将定位基于优选的区域显示本地化的静态和动态内容。








下载

描述名字大小下载方法
Download file 1xyzinsurancetestclient_pi.zip2.0 MB FTP|HTTP
Download file 2xsdsdotransform-feature.zip52 KB FTP|HTTP
Download file 3xsd_sdo_soa_part3_listings.zip4 KB FTP|HTTP

posted on 2006-04-17 03:22 wsdfsdf 阅读(459) 评论(0)  编辑 收藏 引用 所属分类: 技术文章


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理