一. 序
在一些运用中,我们通常会把一些文本和配置信息转换成xml文件进行传输,修改,保存.特别是具有一定模板性质的文档用xml文件来实现其管理就显得相当的方便了.提供对于xml文件的操作的java api很多,诸于dom,jdom,castor,sax,xmlreader,xpath,xslt等等. 具体的这些api的用法这里就不多提了. 当使用这些接口实现xml的操作后,对于有些文档而言最终必须呈现给用户看的还是我们通常所熟悉的word和pdf文档.我们这里就来看一下从一个xml文件到rtf和pdf文件转换的实现.
——————————————————————————–
二. 从xml到pdf
对于一个具有一定模板性质的xml文件,我们可以用fop api来实现其到pdf的转换.
fop需要fop.jar. 我们可以到http://xml.apache.org/fop/ 上获取和了解其用法.
以一个一般复杂的xml文件为例:
要转换xml文档 test.xml 如下:
<featuresrs title="srs">
<introduction>
<objective>objective here</objective>
<scope>scope here</scope>
<responsibilities>responsibilities here</responsibilities>
<references>reference here</references>
<daa>
<term>
term here
</term>
<definition>
definition here
</definition>
</daa>
</introduction>
<generaldescription>
<featurename>
<summary>summary here</summary>
<breakdown>breakdown here</breakdown>
</featurename>
<requirement>
<content>
content here.
</content>
</requirement>
<requirement>
<content>
content2 here.
</content>
</requirement>
<featureinteractions>featureinteractions here</featureinteractions>
</generaldescription>
<strresources>
<strresource>
<estring>
estring here
</estring>
<resourceid>
resourceid here
</resourceid>
<rqmt>
rqmt here.
</rqmt>
</strresource>
</strresources>
</featuresrs>
对于这样一个xml文档,我们要将其转化成pdf格式必须建立一个xsl-fo文件,来定义对各element和value格
式的转换.
我们建立xsl-fo文件 test.xsl 如下:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:fo="http://www.w3.org/1999/xsl/format" exclude-result-prefixes="fo">
<xsl:output method="xml" version="1.0" omit-xml-declaration="no" indent="yes"/>
<!– ========================= –>
<!– root element: projectteam –>
<!– ========================= –>
<xsl:template match="featuresrs">
<fo:root xmlns:fo="http://www.w3.org/1999/xsl/format">
<fo:layout-master-set>
<fo:simple-page-master master-name="simplea4" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm">
<fo:region-body/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simplea4">
<fo:flow flow-name="xsl-region-body">
<fo:block font-size="20pt" font-weight="bold" space-after="5mm" text-align="center">cardiac feature srs
</fo:block>
<fo:block font-size="10pt">
<xsl:apply-templates/>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<!– ========================= –>
<!– child element: member –>
<!– ========================= –>
<xsl:template name="introduction" match="introduction">
<fo:block font-size="18pt" font-weight="bold" space-after="5mm">1. intruction</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">1.1 objective</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="7mm">
<xsl:value-of select="objective"/>
</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">1.2 scope</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="7mm">
<xsl:value-of select="scope"/>
</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">1.3. responsibilities</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="7mm">
<xsl:value-of select="responsibilities"/>
</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">1.4. references</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="7mm">
<xsl:value-of select="references"/>
</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">1.5. definitions, acronyms, and abbreviations</fo:block>
<fo:block font-size="10pt" font-weight="bold" space-after="5mm" margin-left="5mm">
<fo:table table-layout="fixed" border="2cm" background-color="#fff2d9" >
<fo:table-column column-width="4cm"/>
<fo:table-column column-width="6cm"/>
<fo:table-body>
<fo:table-row border="2">
<fo:table-cell>
<fo:block>
<xsl:text>term</xsl:text>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:text>definition</xsl:text>
</fo:block>
</fo:table-cell>
</fo:table-row>
<xsl:for-each select="daa">
<fo:table-row border="2">
<fo:table-cell>
<fo:block>
<xsl:value-of select="term"/>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:value-of select="definition"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:block>
</xsl:template>
<xsl:template name="generaldescription" match="generaldescription">
<fo:block font-size="18pt" font-weight="bold" space-after="5mm">2. general description</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">2.1. feature name</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="7mm">2.1.1. feature summary</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="9mm">
<xsl:value-of select="featurename/summary"/>
</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="7mm">2.1.2. feature breakdown</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="9mm">
<xsl:value-of select="featurename/breakdown"/>
</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">2.2. feature requirements</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="7mm">
<xsl:for-each select="requirement">
<xsl:value-of select="content"/>
</xsl:for-each>
</fo:block>
<fo:block font-size="14pt" font-weight="bold" space-after="5mm" margin-left="5mm">2.3. feature interactions</fo:block>
<fo:block font-size="10pt" font-weight="normal" space-after="5mm" margin-left="7mm">
<xsl:value-of select="featureinteractions"/>
</fo:block>
</xsl:template>
<xsl:template name="strresources" match="strresources">
<fo:block font-size="18pt" font-weight="bold" space-after="5mm">3. string resources </fo:block>
<fo:block font-size="10pt" font-weight="bold" space-after="5mm" margin-left="5mm">
<fo:table table-layout="fixed" border="2cm" background-color="#fff2d9" >
<fo:table-column column-width="4cm"/>
<fo:table-column column-width="10cm"/>
<fo:table-column column-width="4cm"/>
<fo:table-body>
<fo:table-row border="2">
<fo:table-cell>
<fo:block>
<xsl:text>english string</xsl:text>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:text>resource id</xsl:text>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:text>rqmt</xsl:text>
</fo:block>
</fo:table-cell>
</fo:table-row>
<xsl:for-each select="strresource">
<fo:table-row border="2">
<fo:table-cell>
<fo:block>
<xsl:value-of select="estring"/>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:value-of select="resourceid"/>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:value-of select="rqmt"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:block>
</xsl:template>
</xsl:stylesheet>
其具体的xsl-fo文件格式的语法可以参照一些其他资料.
建立好了此文件之后,我们就可以用fop提供的一些接口方便的进行转换了.
fop提供了xml->fo,xml->pdf,fo-pdf,obj->fo,obj->pdf的转换接口.
我们这里以xml->pdf的为例,其余的可以参照fop包里相应的demo.
public class examplexml2pdf {
public void convertxml2pdf(file xml, file xslt, file pdf)
throws ioexception, fopexception, transformerexception {
driver driver = new driver();
logger logger = new consolelogger(consolelogger.level_info);
driver.setlogger(logger);
messagehandler.setscreenlogger(logger);
//setup renderer (output format)
driver.setrenderer(driver.render_pdf);
//setup output
outputstream out = new java.io.fileoutputstream(pdf);
try {
driver.setoutputstream(out);
//setup xslt
transformerfactory factory = transformerfactory.newinstance();
transformer transformer = factory.newtransformer(new streamsource(xslt));
//setup input for xslt transformation
source src = new streamsource(xml);
//resulting sax events (the generated fo) must be piped through to fop
result res = new saxresult(driver.getcontenthandler());
//start xslt transformation and fop processing
transformer.transform(src, res);
} finally {
out.close();
}
}
public static void main(string[] args) {
try {
system.out.println("fop examplexml2pdf\n");
system.out.println("preparing…");
//setup directories
file basedir = new file(".");
file outdir = new file(basedir, "out");
outdir.mkdirs();
//setup input and output files
file xmlfile = new file(basedir, "test.xml");
file xsltfile = new file(basedir, "test.xsl");
file pdffile = new file(outdir, "test.pdf");
system.out.println("input: xml (" + xmlfile + ")");
system.out.println("stylesheet: " + xsltfile);
system.out.println("output: pdf (" + pdffile + ")");
system.out.println();
system.out.println("transforming…");
examplexml2pdf app = new examplexml2pdf();
app.convertxml2pdf(xmlfile, xsltfile, pdffile);
system.out.println("success!");
} catch (exception e) {
system.err.println(exceptionutil.printstacktrace(e));
system.exit(-1);
}
}
}
这样我们就很轻易地实现了xml文档到pdf文档的转换.
如果这些用在webservice的servlet中,想从xml文件直接生成pdf传输给浏览者而并不生成的pdf文件,我们可以如
下实现:
public class fopservlet extends httpservlet {
public static final string fo_request_param = "fo";
public static final string xml_request_param = "xml";
public static final string xsl_request_param = "xsl";
public void doget(httpservletrequest request,
httpservletresponse response) throws servletexception {
try {
string xmlparam =getservletcontext().getrealpath("web-inf/doc/xml/test.xml");
string xslparam =getservletcontext().getrealpath("web-inf/doc/xsl/test.xsl");
if ((xmlparam != null) && (xslparam != null)) {
xsltinputhandler input =
new xsltinputhandler(new file(xmlparam),
new file(xslparam));
renderxml(input, response);
} else {
printwriter out = response.getwriter();
out.println("<html><head><title>error</title></head>\n"+
"<body><h1>fopservlet error</h1><h3>no fo "+
"request param given.</body></html>");
}
} catch (servletexception ex) {
throw ex;
}
catch (exception ex) {
throw new servletexception(ex);
}
}
public void renderxml(xsltinputhandler input,
httpservletresponse response) throws servletexception {
try {
bytearrayoutputstream out = new bytearrayoutputstream();
response.setcontenttype("application/pdf");
driver driver = new driver();
driver.setrenderer(driver.render_pdf);
driver.setoutputstream(out);
driver.render(input.getparser(), input.getinputsource());
byte[] content = out.tobytearray();
response.setcontentlength(content.length);
response.getoutputstream().write(content);
response.getoutputstream().flush();
} catch (exception ex) {
throw new servletexception(ex);
}
}
}
——————————————————————————–
三. xml to rtf
xml到rtf的转换稍微有一些麻烦,我们没有直接从xml到rtf的api, 我们将要用的jfor api还没有整合到fop
中去. jfor api可以实现 从 fo文件到rtf文件的转换, 它也提供了consle接口.
我们可以从 www.jfor.org 上获取jfor相关信息.
我们从xml文件到rtf文件的转换可以分为两步:
1. 用fop将 xml 转换成 fo
2. 用jfor将 fo 转换成rtf
3.1 用fop将 xml 转换成 fo
这一步我们可以很轻易的沿用上面所述的方法,如下实现xml到fo 的转换,依然会用到上面所用的xml文件
和xsl-fo文件.
outputstream foout = new fileoutputstream(fofile);
transformerfactory factory = transformerfactory.newinstance();
transformer transformer = factory.newtransformer(new streamsource(
xsltfile));
source src = new streamsource(xmlfile);
result res = new streamresult(foout);
transformer.transform(src, res);
foout.close();
3.2 用jfor将 fo 转换成rtf
仅以serlvet需求的实现为例:
inputstream foinput = new fileinputstream(fofile);
inputsource inputsource = new inputsource(foinput);
bytearrayoutputstream out = new bytearrayoutputstream();
writer output = new outputstreamwriter(out);
response.setcontenttype("application/msword");
new converter(inputsource,output,converter.createconverteroption ());
output.flush();
byte[] content = out.tobytearray();
system.out.println(out.tostring());
response.setcontentlength(content.length);
response.getoutputstream().write(content);
response.getoutputstream().flush();
foinput.close();
output.close();
out.close();
这样我们就成功地将xml转化成了rtf格式的文件.
本文仅简述了大体的实现过程,具体的细节可参照各技术点的详细自述.
