Generate Java objects for FpML using JAXB and Maven: The Easy Way!

http://www.stephennimmo.com/generate-java-objects-for-fpml-using-jaxb-and-maven-the-easy-way/

http://www.fpml.org/dev/modules/newbbex/viewtopic.php?topic_id=135&forum=4

http://www.whatis.com.cn/word_1665.htm

金融产品标记语言(Financial Products Markup Language,FpML)是一种基于可扩展标记语言(XML)的商业信息交换标准,它使用互联网进行商业对商业柜台市场(场外市场,over-the-counter,OTC)的金融衍生交易。金融产品标记语言可用来在参与公司之间交流柜台市场交易详细资料,它也可以在公司内部分享柜台市场交易信息,也可用于在参与公司和外部公司之间提供关于柜台买卖交易的服务。金融产品标记语言(FpML)是免费使用的,因为它独立于软件或硬件被参与公司用来确保他们协同工作能力。FpML强调利率互换和远期利率协议(FRA),但是最终它将用于柜台市场(OTC)交易的各个方面。

  两个公司之间共有的柜台市场(OTC)合同在满足双方需求的基础下被高度用户化。由于这一原因,在使用互联网和可扩展标记语言(XML)之前,有效地在线传输OTC合同是不可能的。现在,公司能够通过网络构建和谈判OTC合同条款、完成和确认这个合同,沟通结算细节,并用金融产品标记语言(FpML)分析风险。

  美国大通曼哈顿银行审查并在它们柜台市场利率衍生工具应用中使用金融产品标记语言(FpML)。富士资本市场公司使用FpML的定义来设计它们基于XML的远期利率协议认证模型。摩根公司开发了一种FpML利率交换模型应用程序。参与FpML的开发和应用的机构包括美国银行、花旗集团、德意志银行、IBM公司、普华永道公司、摩根公司、路透社和瑞银华宝。

 

First, go get the schemas from http://www.fpml.org/spec/latest.php. You will have to register. When you log in, make sure your have Specifications radio selected.

We are going to work with the latest version. Download the zip file for whichever group of FpML you want.

Once that file is downloaded, fire up eclipse and create a generic maven project.

For generating the JAXB objects, we are going to use a maven plugin from Codehaus. Documentation for the plugin can be found at http://mojo.codehaus.org/jaxb2-maven-plugin/index.html. Notice that I put the url for the plugin into a comment at the top of the plugin.

To use the plugins out of the box (mostly), let’s start by creating a directory for the xsds. Because there are four sets of xsds for FpML, I organize them by creating the normal src/main/xsd folder and then unzip the contents of the FpML download into the folder. It should unzip to a directory for whatever download you selected (xml_confirmation).

I like making my objects serializable for JAXB, so I add an xjb binding. Create the folder src/main/xjb and add a jaxb-bindings.xml file in there with:

?
<? xml version = "1.0" encoding = "UTF-8" ?>
< bindings xmlns = "http://java.sun.com/xml/ns/jaxb" 
     xmlns:xjc = "http://java.sun.com/xml/ns/jaxb/xjc"
     < globalBindings >
         < serializable uid = "54" />
     </ globalBindings >
</ bindings >

Next we are going to add the plugin. Again, because FpML could be four different downloads and if you want to generate objects for all 4 flavors, we will want to use the “Multiple schemas with different configuration” which has documentation at http://mojo.codehaus.org/jaxb2-maven-plugin/usage.html. For our purposes, we will implement like this.

?
< plugin >
     < groupId >org.codehaus.mojo</ groupId >
     < artifactId >jaxb2-maven-plugin</ artifactId >
     < version >1.5</ version >
     < executions >
         < execution >
             < id >xml_confirmation-xjc</ id >
             < goals >
                 < goal >xjc</ goal >
             </ goals >
             < configuration >
                 < schemaDirectory >${project.basedir}/src/main/xsd/xml_confirmation</ schemaDirectory >
                 < packageName >org.fpml.confirmation</ packageName >
                 < staleFile >${project.build.directory}/jaxb2/.confirmationXjcStaleFlag</ staleFile >
             </ configuration >
         </ execution >
         < execution >
             < id >xml_recordkeeping-xjc</ id >
             < goals >
                 < goal >xjc</ goal >
             </ goals >
             < configuration >
                 < schemaDirectory >${project.basedir}/src/main/xsd/xml_recordkeeping</ schemaDirectory >
                 < packageName >org.fpml.recordkeeping</ packageName >
                 < staleFile >${project.build.directory}/jaxb2/.recordkeepingXjcStaleFlag</ staleFile >
             </ configuration >
         </ execution >
         < execution >
             < id >xml_reporting-xjc</ id >
             < goals >
                 < goal >xjc</ goal >
             </ goals >
             < configuration >
                 < schemaDirectory >${project.basedir}/src/main/xsd/xml_reporting</ schemaDirectory >
                 < packageName >org.fpml.reporting</ packageName >
                 < staleFile >${project.build.directory}/jaxb2/.reportingXjcStaleFlag</ staleFile >
             </ configuration >
         </ execution >
         < execution >
             < id >xml_transparency-xjc</ id >
             < goals >
                 < goal >xjc</ goal >
             </ goals >
             < configuration >
                 < schemaDirectory >${project.basedir}/src/main/xsd/xml_transparency</ schemaDirectory >
                 < packageName >org.fpml.transparency</ packageName >
                 < staleFile >${project.build.directory}/jaxb2/.transparencyXjcStaleFlag</ staleFile >
             </ configuration >
         </ execution >
     </ executions >
     < configuration >
         < outputDirectory >${project.basedir}/src/main/java</ outputDirectory >
     </ configuration >
</ plugin >

Once everything is there, simply run the generate-sources target with Maven (I use the IDE because I am lazy) and it will generate your entire object model. Start coding….

But wait! There’s more. One last trick. The JAXB generation does not create and @XmlRootElement annotations on ANY of the objects. Why? Travel back to 2006 and have the answer. http://weblogs.java.net/blog/2006/03/03/why-does-jaxb-put-xmlrootelement-sometimes-not-always

So how do we marshall? Use the ObjectFactory. Here’s some example code which took me way too long to figure out.

?
JAXBContext jc = JAXBContext.newInstance( "org.fpml.recordkeeping" );
ObjectFactory objectFactory = new ObjectFactory();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  
NonpublicExecutionReport report = objectFactory.createNonpublicExecutionReport();
RequestMessageHeader header = new RequestMessageHeader();
MessageId messageId = objectFactory.createMessageId();
messageId.setValue(UUID.randomUUID().toString());
header.setMessageId(messageId);
report.getHeader().getMessageId().setValue(UUID.randomUUID().toString());
  
//Use the ObjectFactory to create the JAXB<Object> wrapped!!! This is the key!
marshaller.marshal(objectFactory.createNonpublicExecutionReport(report), System.out);

Alright, get started.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值