如何创建PDF,其中包含List <list <string>&gt;中的值仅使用FOP </list <string>

时间:2014-01-08 13:26:01

标签: java apache-fop

我正在尝试使用LIST&gt;中的值创建一个pdf在java中使用FOP,但得到一些异常,如javax.xml.transform.TransformerConfigurationException:无法编译样式表。这是我的代码看起来像:

1.JaXBChildList.java

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;


@XmlRootElement(name="list")
public class JaXBChildList<List>  extends JaxbList<JaXBChildList>{

protected java.util.List<String> list;

public JaXBChildList(){}

public JaXBChildList(java.util.List<String> list){
    this.list=list;
}

@XmlElement(name="i")
public java.util.List<String> getList(){
    return list;
}

}

2.JaxbList.java

import java.util.List;

import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;


@XmlRootElement(name="records")
@XmlSeeAlso({JaXBChildList.class})
public  class JaxbList<T>{

protected List<T> records;

public JaxbList(){}

public JaxbList(List<T> list){
    this.records=list;
}

@XmlMixed
public List<T> getRecords(){
    return records;
}
}

3.PDFHandler.java

 public class PDFHandler {
public static final String EXTENSION = ".pdf";
public String PRESCRIPTION_URL = "template.xsl";

public String createPDFFile(ByteArrayOutputStream xmlSource, String  templateFilePath) throws IOException {
    File file = File.createTempFile("" + System.currentTimeMillis(), EXTENSION);
    URL url = new File(templateFilePath + PRESCRIPTION_URL).toURI().toURL();
    // creation of transform source
    StreamSource transformSource = new StreamSource(url.openStream());
    // create an instance of fop factory
    FopFactory fopFactory = FopFactory.newInstance();
    // a user agent is needed for transformation
    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
    // to store output
    ByteArrayOutputStream pdfoutStream = new ByteArrayOutputStream();
    StreamSource source = new StreamSource(new ByteArrayInputStream(xmlSource.toByteArray()));
    Transformer xslfoTransformer;
    try {
        TransformerFactory transfact = TransformerFactory.newInstance();

        xslfoTransformer = transfact.newTransformer(transformSource);
        // Construct fop with desired output format
        Fop fop;
        try {
            fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, pdfoutStream);
            // Resulting SAX events (the generated FO)
            // must be piped through to FOP
            Result res = new SAXResult(fop.getDefaultHandler());

            // Start XSLT transformation and FOP processing
            try {
                // everything will happen here..
                xslfoTransformer.transform(source, res);

                // if you want to save PDF file use the following code
                OutputStream out = new java.io.FileOutputStream(file);
                out = new java.io.BufferedOutputStream(out);
                FileOutputStream str = new FileOutputStream(file);
                str.write(pdfoutStream.toByteArray());
                str.close();
                out.close();

            } catch (TransformerException e) {
                e.printStackTrace();
            }
        } catch (FOPException e) {
            e.printStackTrace();
        }
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        e.printStackTrace();
    }
    return file.getPath();
}

public ByteArrayOutputStream getXMLSource(JaxbList jaxbList) throws Exception {
    JAXBContext context;

    ByteArrayOutputStream outStream = new ByteArrayOutputStream();

    try {
        context = JAXBContext.newInstance(JaxbList.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        m.marshal(jaxbList, outStream);
        System.out.println(outStream.toString());
    } catch (JAXBException e) {

        e.printStackTrace();
    }
    return outStream;

}

}

4.ListToXSLT.java

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;



 public class ListToXSLT {

public  static void main(String[] args)
{


    List<String> list = new ArrayList<String>();
    list.add("COlumn 1");
    list.add("COlumn 2");
    list.add("COlumn 3");

    List<String> list1 = new ArrayList<String>();
    list1.add("Value 1");
    list1.add("Value 2");
    list1.add("Value 3");

    JaXBChildList<List> cList = new JaXBChildList(list);
    JaXBChildList<List> cList1 = new JaXBChildList(list1);


    List<JaXBChildList<List>> child= new ArrayList();


    child.add(cList);
    child.add(cList1);

    PDFHandler handler = new PDFHandler();


    @SuppressWarnings({ "rawtypes", "unchecked" })
    JaxbList jaxbList = new JaxbList(child);


    try {
            ByteArrayOutputStream baos = handler.getXMLSource(jaxbList);
        handler.createPDFFile(baos, "");
        System.out.println(jaxbList);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

这是我的template.xsl文件:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:list="java.util.List"
xmlns:mylist="JaxbList"
exclude-result-prefixes="filecounter">

<!-- <xsl:variable name="list" select="validator:getErrorList($validator,  'model')" /> -->
<xsl:variable name="list">
    <xsl:value-of select="mylist:getJaxbList($mylist)"/>
</xsl:variable>

<xsl:variable name="length" select="list:size($list)"/>
<xsl:if test="$length > 0">
<xsl:call-template name="looper">
    <xsl:with-param name="iterations" select="$length - 1"/>
    <xsl:with-param name="list" select="$list"/>
</xsl:call-template>
</xsl:if>

<xsl:template name="looper">
<xsl:param name="iterations"/>
<xsl:param name="list"/>
<xsl:if test="$iterations > -1">
    <xsl:value-of select="list:get($list, $iterations)"></xsl:value-of>
    <xsl:call-template name="looper">
         <xsl:with-param name="iterations" select="$iterations - 1"/>
           <xsl:with-param name="list" select="$list"/>
      </xsl:call-template>
 </xsl:if>
</xsl:template>
</xsl:stylesheet>

任何人都可以解决我的问题 提前谢谢

0 个答案:

没有答案