itaton
itaton

Reputation: 317

Pass a parameter from java-embedded fop to xsl file

I've embedded fop in a java programm:

public class run {

 public static void main(String [ ] args){

    FopFactory fopFactory = FopFactory.newInstance();

    OutputStream out = null;
    try {
        out = new BufferedOutputStream(new FileOutputStream(new File("F:/test.pdf")));
    } catch (FileNotFoundException e) {

        e.printStackTrace();
    }

    try {
      Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

      TransformerFactory factory = TransformerFactory.newInstance();
      Source xslt = new StreamSource(new File("F:/main_structure.xsl"));
      Transformer transformer = factory.newTransformer(xslt);

      Source src = new StreamSource(new File("F:/source.xml"));

      Result res = new SAXResult(fop.getDefaultHandler());

      transformer.transform(src, res);

    } catch (FOPException e) {
        e.printStackTrace();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerException e) {
        e.printStackTrace();
    } finally {
      try {
        out.close();
        System.out.println("ende");
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
 }
}

now my xsl (main_structure.xsl) needs a parameter to run

    <xsl:param name="OFFSET_LEFT" select="1"/>

in the console I would be using the "-param name value" attribute to pass that parameter to fop.

So how do I pass a parameter into my xsl file in the embedded version of fop?

Upvotes: 2

Views: 1843

Answers (1)

Mark Veenstra
Mark Veenstra

Reputation: 4739

For example read: http://www.onlamp.com/pub/a/onjava/excerpt/java_xslt_ch5/index.html?page=4

Transformer transformer = factory.newTransformer(xslt);
transformer.setParameter("PARAMETER", "VALUE");

Upvotes: 3

Related Questions