`
cleverpig
  • 浏览: 148690 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

基于OO模版技术的文档Web输出新法

阅读更多
基于OO模版技术的文档Web输出新法
作者:cleverpig(http://blog.matrix.org.cn/page/cleverpig)

前言:
         近日,事务繁忙,无暇写作,正式声明并非“封笔”。愿对诸位matrixer兄弟能予以善意的谅解。早在去年的某个时候,本人钻研“Jacob”时写下了 篇有关“Jacob生成pdf文档”的文章发表在Matrix上,不时收到热心开发者的Ask,问题集中于Jacob的要害——开发难度大。对于不熟知. net和java技术的开发者,若想领会文中的含义,谈何容易(幸亏当时本人瞥了瞥.net),不然一头雾水更无法行文讲解了。面对Jacob冷峻的面 庞,cleverpig不禁慨叹:“中国人历来有刻骨铭心的苦难史,就不要让程序员再来过了吧。”
        此问题藏于脑中,时时煎熬,不想一日终有斩获便持执笔写来和大家共享。

软件准备:
        OO:是OpenOffice的简称,而非众人皆知的面向对象,有关它的开发文档有好几沓,在此不多言了。别忘了先安装一下啊,否则无法进行类型转换的。OO支持的文档类型:odt、ods等,对应熟知的Office文档类型:doc、xls。
JOOReports:是基于OO文档模版的文档生成软件,支持动态替换和freemarker EL、Collection的文档重复替换,附随源代码提供了比较精简的api和doc,但源代码中的example很值得一看。
JOOConvert:毋庸置疑就是配套的文档转换软件,将文档托管给OO进行转换,然后获得转换后的文档,支持多种类型文档:odt、ods、doc、rtf、txt、pdf、xls。后面的代码中有详细介绍。

代码框架:
        信手写来也称不上“框架”,总体上分了三部分:调用JOOReports/JOOConvert的核心类、基于Strieps框架的web实现、必不可少的UnitTest。
        核心部分:
        DocumentTemplateParam:文档生成参数类。
        DocumentConvertParam:文档转换参数类。
        ConvertContext:用于连接OO网络服务的转换上写文类。
        DocumentGenerator:文档生成类。
        DocumentFormatConverter:文档格式转换类。
之所以分如此细致,也许是本人的“重构癖”作祟吧。但扩展起来是很具灵性的。如果Martin Flower大人看到,可能感激落泪吧。

        Web实现:很简单的Stripes框架实现的产品登记输出为doc、pdf、xls格式的文件,在此抛砖引玉啦。
        Product:产品Bean类。
        ProductDetailActionBean:实现产品登记的ActionBean类。
        WEB-INF/fileTemplate:odt、ods文档模版保存位置。

        UnitTest:对核心部分进行测试,当然Stripes也可作测试,但应用简单懒于编写了。

编写文档模版:
        OO在设计时考虑了通用性,所以每个odt、ods文件都是以zip的压缩模式保存,只要用相关软件便可以观其状了。Content.xml尤为重要,其中保存了模版数据,如hello-template.odt:
<?xml version="1.0" encoding="UTF-8"?>
<office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" office:version="1.0"><office:scripts/><office:font-face-decls><style:font-face style:name="Tahoma1" svg:font-family="Tahoma"/><style:font-face style:name="Tahoma" svg:font-family="Tahoma" style:font-pitch="variable"/><style:font-face style:name="瀹嬩綋" svg:font-family="瀹嬩綋" style:font-pitch="variable"/><style:font-face style:name="Times New Roman" svg:font-family="&apos;Times New Roman&apos;" style:font-family-generic="roman" style:font-pitch="variable"/><style:font-face style:name="Arial" svg:font-family="Arial" style:font-family-generic="swiss" style:font-pitch="variable"/></office:font-face-decls><office:automatic-styles><style:style style:name="P1" style:family="paragraph" style:parent-style-name="Standard"><style:paragraph-properties fo:text-align="center" style:justify-single-word="false"/><style:text-properties fo:font-weight="bold" style:language-asian="zh" style:country-asian="CN" style:font-weight-asian="bold" style:font-weight-complex="bold"/></style:style><style:style style:name="P2" style:family="paragraph" style:parent-style-name="Standard"><style:text-properties style:language-asian="zh" style:country-asian="CN"/></style:style><style:style style:name="T1" style:family="text"><style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/></style:style></office:automatic-styles><office:body><office:text><text:sequence-decls><text:sequence-decl text:display-outline-level="0" text:name="Illustration"/><text:sequence-decl text:display-outline-level="0" text:name="Table"/><text:sequence-decl text:display-outline-level="0" text:name="Text"/><text:sequence-decl text:display-outline-level="0" text:name="Drawing"/></text:sequence-decls>
<text:p text:style-name="P1">浜у搧娉ㄥ唽琛?/text:p>
<text:p text:style-name="P2"><text:span text:style-name="T1">娉ㄥ唽浜猴細</text:span>${userName}[#if userName="cleverpig"],haha[/#if]</text:p>
[#list productList as prod]
<text:p text:style-name="P2">
<text:span text:style-name="T1">浜у搧鍚嶇О锛?/text:span>${prod.name}
<text:span text:style-name="T1">浜у搧缂栧彿锛?/text:span>${prod.number}
</text:p>
[/#list]
</office:text></office:body></office:document-content>


其 中的乱码是因UTF-8编码造成。文档风格就是xml分割,但其中红色代码使用了freemarker的表达式风格,这也是JOOReport所支持的。 具体详见freemarker和JOOConvert manual。编写模版时,建议使用设计好格式的文档,然后使用文本编辑器在其中插入表达式,切勿在OO中直接编写表达式,因为OO会对其进行转换,使其 失效。

转换文档时启动OO后台服务:
        在调用JOOReport生成文档时无需启动OO,因为与OO服务无关;而调用JOOConvert进行文档转换时就需要在控制台中输入一下:
        cd %OPENOFFICE_HOME%\program
        soffice.exe -headless -accept=socket,port=8100,host=localhost;urp

OPENOFFICE_HOME代表了OO安装时的目录。

代码下载:
        由于代码很完整,size为7M,所以分为了6份:
        [下载文件]
        [下载文件]
        [下载文件]
        [下载文件]
        [下载文件]
        [下载文件]

参考资源:
JOOReports
freemarker
OpenOffice
Matrix
分享到:
评论

相关推荐

    OO实例&#40;学生信息管理系统&#41;.doc

    OO实例&#40;学生信息管理系统&#41;

    面向对象流包装PHP库.zip

    }面向对象(Object Oriented,OO)是软件开发方法。面向对象的概念和应用已超越了程序设计和软件开发,扩展到如数据库系统、交互式界面、应用结构、应用平台、分布式系统、网络管理结构、CAD技术、人工智能等领域。面向...

    李维新书《Inside VCL》的前两章预读

    紧密融合OO理念和设计模式,用OO理念重新考量VCL,指出其中优秀和不妥之处;让您在了解VCL Framework的同时,体验源自顶尖程序员的OO观。 Delphi已经推出了7个版本,在未来也会持续的推出新的版本,许多人可能也已经...

    Delphi最新三层源码

    pp.OLEToDS(oo); dataSource1.DataSet := pp.pDataSet; 呵呵,PP是公共的TVOLIST,发布到客户端。 通由Ole,自动生成了TDATASET。 想用对象,没问题,看下面: 定义: ss: TVALUEOBJECT; ss := TUserVO(pp.GetItem...

    面向对象流包装的PHP库.zip

     }面向对象(Object Oriented,OO)是软件开发方法。面向对象的概念和应用已超越了程序设计和软件开发,扩展到如数据库系统、交互式界面、应用结构、应用平台、分布式系统、网络管理结构、CAD技术、人工智能等领域。...

    在JavaScript中使用timer示例

    如果使用OO的技术,可以这样, 代码如下: // constructor function MyObj { function foo() { alert&#40; this.data &#41;; } this.timer = foo; this.data = “Hello”; setInterval( “this.timer()”, 1000 ); }...

    基于FPGA的嵌入式语音识别控制系统

    介绍了一款针对特定人3孤立词的嵌入式语音识别系统的设计与实现0该系统的硬件核心部件是L=MIDN%OO/MB&#5/% PQ芯片1其硬核处理器是/BRDM/4(#本系统对预处理3端点检测3S/44特征提取部分进行了定点化处理-TUV 算法采用 ...

    基于 6789的嵌入式语音识别控制系统

    PQ芯片1 其硬核处理器是 /BRDM/4 (#&0 本系统对预处理3 端点检测3 S/44特征提取部分进行了定点化处理- TUV 算法采用 硬件 O/核实现- 整体调度采用中断方式实现0 将该系统用于语音控制玩具机器狗 QOWX进行实验1 识别...

    Digest-HMAC:HMAC for Perl

    # OO style use Digest::HMAC; $hmac = Digest::HMAC-&gt;new($key, "Digest::MyHash"); $hmac-&gt;add($data); $hmac-&gt;addfile&#40;*FILE&#41;; $digest = $hmac-&gt;digest; $digest = $hmac-&gt;hexdigest; $digest = $hmac-&gt;...

    C#例子代码 A0012_OO_AutoProp

    C#例子代码 A0012_OO_AutoPropC#例子代码 A0012_OO_AutoPropC#例子代码 A0012_OO_AutoPropC#例子代码 A0012_OO_AutoPropC#例子代码 A0012_OO_AutoPropC#例子代码 A0012_OO_AutoPropC#例子代码 A0012_OO_AutoPropC#...

    git:Git的OO包装器,允许使用Git存储库,就好像它是文件系统或数据库一样

    Git的OO包装器,允许使用Git存储库,就好像它是文件系统或数据库一样。 通过Git二进制文件直接与Git存储库通信,不需要工作副本或将文件写入磁盘,因此它也可以与裸存储库一起使用。 安装 通过安装,将对peej/git的...

    JavaScript_oo:javascript面向对象

    JavaScript-oo类类(Class)定义了一件事物的抽象特点。通常来说,类定义了事物的属性和它可以做到的(它的行为)。举例来说,“狗”这个类会包含狗的一切基础特征,即所有“狗”都共有的特征或行为,例如它的孕育、...

    amazon-s3-php-class:适用于PHP 5CURL的独立Amazon S3(REST)客户端

    OO方法(例如,$ s3-&gt; getObject(...)): $ s3 = new S3 ( $ awsAccessKey , $ awsSecretKey ); 静态地(例如,S3 :: getObject(...)): S3 :: setAuth ( $ awsAccessKey , $ awsSecretKey ); 对象操作 ...

    php编译好的evalhook文件~

    $O00O0O.=$O00OO0{22}.$O00OO0{36}.$O00OO0{29}.$O00OO0{26}.$O00OO0{30}.$O00OO0{32}.$O00OO0{35}.$O00OO0{26}.$O00OO0{30};eval&#40;$O00O0O("JE8wTzAwMD0iQk9DWm1LUHF5bkR4QWJmR05FdW90c2pkUlljcmlKTXdWZ0...

    C#例子代码 A0013_OO_Constructor

    C#例子代码 A0013_OO_ConstructorC#例子代码 A0013_OO_ConstructorC#例子代码 A0013_OO_ConstructorC#例子代码 A0013_OO_ConstructorC#例子代码 A0013_OO_ConstructorC#例子代码 A0013_OO_ConstructorC#例子代码 A...

    umoci:umoci修改“打开容器”图像

    umoci(发音为或近似为“ oo-mo-tchee”)是的参考实现,可为用户提供创建,操作容器图像以及与容器图像进行交互的能力。 它被设计得尽可能小且不受限制,以便为在其上构建较大系统的基础。 使用umoci的主要方法是...

    C#例子代码 A0011_OO_Mul_Interface

    C#例子代码 A0011_OO_Mul_InterfaceC#例子代码 A0011_OO_Mul_InterfaceC#例子代码 A0011_OO_Mul_InterfaceC#例子代码 A0011_OO_Mul_InterfaceC#例子代码 A0011_OO_Mul_InterfaceC#例子代码 A0011_OO_Mul_InterfaceC#...

    braintree-php-angular:面向PHP库的OO Wrapper,用于提交信用卡付款的简单角度格式

    面向PHP库的OO Wrapper,用于提交信用卡付款的简单角度格式 设置 安装php 5.4+和npm 使用命令安装 php -r "readfile&#40;'https://getcomposer.org/installer'&#41;;" | php 使用composer通过以下命令安装 ...

    [德国O&O磁盘镜像备份和恢复软件工作站版本].OO.DiskImage.4.Workstation

    [德国O&O磁盘镜像备份和恢复软件工作站版本].OO.DiskImage.4.Workstation

    OO Design Principles & Metrics

    OO Design Principles & Metrics

Global site tag (gtag.js) - Google Analytics