文谷首页 | 业界传真 | 网络技术 | 服务器 | 数据库 | 存储技术 | 系统安全 | 无线技术 | Cisco | .Net | Windows | Linux | Unix | Java
电子商务 | 网站工程 | 网页设计 | 平面设计 | 多媒体 | 编程语言 | Oracle | MSSQL | Photoshop | ASP | PHP | 实用技巧 | 进程查询 | 文谷论坛
Java频道
 资讯动态   考试认证   新手入门   核心技术   高级技术   J2EE   J2ME   XML   开源技术   其他技术
您现在的位置: IT文谷 >> 开发平台 >> Java >> 核心技术 >> 高级编程 >> 文章正文
基于OO模版技术的文档Web输出新法基于OO模版技术的文档Web输出新法2006-7-19 21:10:07基于OO模版技术的文档Web输出新法2006-7-19 21:10:07基于OO模版技术的文档Web输出新法
基于OO模版技术的文档Web输出新法
基于OO模版技术的文档Web输出新法基于OO模版技术的文档Web输出新法2006-7-19 21:10:07基于OO模版技术的文档Web输出新法2006-7-19 21:10:07基于OO模版技术的文档Web输出新法
基于OO模版技术的文档Web输出新法基于OO模版技术的文档Web输出新法2006-7-19 21:10:07基于OO模版技术的文档Web输出新法2006-7-19 21:10:07基于OO模版技术的文档Web输出新法
前言:
        近日,事务繁忙,无暇写作,正式声明并非“封笔”。愿对诸位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安装时的目录。

基于OO模版技术的文档Web输出新法基于OO模版技术的文档Web输出新法2006-7-19 21:10:07基于OO模版技术的文档Web输出新法2006-7-19 21:10:07基于OO模版技术的文档Web输出新法
  • 上一篇文章:

  • 下一篇文章:
  • 进入论坛讨论

    相关文章
    我的Thinking in Java学习笔记
    使用JBoss和Eclipse创建J2EE应用
    SpringFramework(9)
    NoTitle385
    向Web Service进军--Axis+Tomcat模拟一个银行存取款
    JAVA中的指针,引用及对象的clone
    JAVA中用动态代理类实现记忆功能(二)
    JAVA中用动态代理类实现记忆功能(一)
    DB Test
    基金系统的初步研究(1)
    SpringFramework(8)
    深入浅出Java clone技术(2)
    热门文章最新推荐

    版权与免责声明:
    ① 本网转载其他媒体稿件是为传播更多的信息,此类稿件不代表本网观点,版权归原作者所有,本网不承担此类稿件侵权行为的连带责任。
    ② 本站原创文章,转载时请注明出自文谷及作者姓名
    ③在本网BBS上发表言论者,文责自负。
    ④如您因版权等问题需要与本网联络,请在30日内联系 。
    基于OO模版技术的文档Web输出新法基于OO模版技术的文档Web输出新法2006-7-19 21:10:07基于OO模版技术的文档Web输出新法2006-7-19 21:10:07基于OO模版技术的文档Web输出新法
    基于OO模版技术的文档Web输出新法基于OO模版技术的文档Web输出新法2006-7-19 21:10:07基于OO模版技术的文档Web输出新法2006-7-19 21:10:07基于OO模版技术的文档Web输出新法

    全站热点
    最新推荐
    关于文谷 | 联系文谷 | 免责声明 | 文谷论坛
    Tel: 0577-65690019      E-mail: ichenjian@gmail.com    MSN:ichenjian@hotmail.com    QQ:2911194
    Copyright © 2004-2008 wengu.com 文谷 All Rights Reserved
    浙ICP备05000327号