国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

基于OO模版技術的文檔Web輸出新法

2019-11-18 11:57:22
字體:
來源:轉載
供稿:網友

前言:
        近日,事務繁忙,無暇寫作,正式聲明并非“封筆”。愿對諸位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安裝時的目錄。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 彩票| 中江县| 汪清县| 安宁市| 鄂托克前旗| 盘山县| 平山县| 绥江县| 明水县| 巨野县| 南部县| 武山县| 南靖县| 来凤县| 苍溪县| 长寿区| 晴隆县| 博乐市| 长宁县| 肇州县| 肃南| 攀枝花市| 从江县| 嘉义县| 咸宁市| 孟连| 上栗县| 信丰县| 萨嘎县| 磐石市| 高密市| 涟源市| 大厂| 喀什市| 昆明市| 黑河市| 玛曲县| 淮南市| 辉县市| 留坝县| 白城市|