Ejemplo n.º 1
0
  /**
   * Setup the UserAgent for FOP, from given parameters *
   *
   * @param transformer Created based on the XSLT, so containing any parameters to the XSL-FO
   *     specified in the XQuery
   * @param parameters any user defined parameters to the XSL-FO process
   * @return FOUserAgent The generated FOUserAgent to include any parameters passed in
   */
  private FOUserAgent setupFOUserAgent(
      FOUserAgent foUserAgent, Properties parameters, Transformer transformer)
      throws TransformerException {

    // setup the foUserAgent as per the parameters given
    foUserAgent.setProducer("eXist with Apache FOP");

    if (transformer.getParameter("FOPauthor") != null)
      foUserAgent.setAuthor(parameters.getProperty("author"));

    if (transformer.getParameter("FOPtitle") != null)
      foUserAgent.setTitle(parameters.getProperty("title"));

    if (transformer.getParameter("FOPkeywords") != null)
      foUserAgent.setTitle(parameters.getProperty("keywords"));

    if (transformer.getParameter("FOPdpi") != null) {
      String dpiStr = (String) transformer.getParameter("dpi");
      try {
        foUserAgent.setTargetResolution(Integer.parseInt(dpiStr));
      } catch (NumberFormatException nfe) {
        throw new TransformerException(
            "Cannot parse value of \"dpi\" - " + dpiStr + " to configure FOUserAgent");
      }
    }

    return foUserAgent;
  }
Ejemplo n.º 2
0
  public void generateGeneric() {
    String tMIME = null;
    if (genMode.equals("PDF")) {
      tMIME = MimeConstants.MIME_PDF;
    } else {
      tMIME = MimeConstants.MIME_RTF;
    }

    try {
      System.out.println("FOP Transformation\n");
      System.out.println("Preparing...");

      String fileName = new File(xsltfile).getName();
      // File xsltfile = new File("lib/xsl2/xslt/base/fo/" + fileName);
      File xsltfile = new File("lib/xsl/fo/" + fileName);

      // configure fopFactory.4. as desired
      // FopFactoryConfig ffc = new FopFactoryConfig();

      // FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI()) ; // .newInstance();
      FopFactory fopFactory = FopFactory.newInstance();
      FOUserAgent foUserAgent = fopFactory.newFOUserAgent();

      foUserAgent.setTitle("TEST");

      // configure foUserAgent as desired

      // Setup output
      OutputStream out = new java.io.FileOutputStream(outputfile);
      out = new java.io.BufferedOutputStream(out);

      try {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));

        // test run to get total pagecount
        Fop fop = fopFactory.newFop(tMIME, foUserAgent, out);
        Source src = new StreamSource(xmlfile);
        Result res = new SAXResult(fop.getDefaultHandler());
        transformer.transform(src, res);

        // Get total page count
        String pageCount = "UNAVAILABLE";
        if (fop.getResults() != null) {
          pageCount = Integer.toString(fop.getResults().getPageCount());
        } else {

        }
        System.out.println("totalcc " + pageCount);

        // real run
        // Construct fop with desired output format
        fop = fopFactory.newFop(tMIME, foUserAgent, out);

        // set draft parameter
        String draftImageFn =
            new File("lib/xsl/images/draft.png").toURI().toString(); // .getAbsolutePath();
        transformer.setParameter("draft.watermark.image", draftImageFn);
        System.out.println(transformer.getParameter("draft.watermark.image"));

        if (isDraftMode) {
          transformer.setParameter("draft.mode", "yes");
        } else {
          transformer.setParameter("draft.mode", "no");
        }

        transformer.setParameter("draft.mode", pageCount);

        // set pagecount parameter
        transformer.setParameter("ebnf.statement.terminator", pageCount);

        // Resulting SAX events (the generated FO) must be piped
        // through
        // to FOP
        res = new SAXResult(fop.getDefaultHandler());

        // Start XSLT transformation and FOP processing
        transformer.transform(src, res);

      } finally {
        out.close();
        status = 0;
      }

      System.out.println("Success!");
      System.out.println("DocBook " + genMode + " generation COMPLETED");

    } catch (Exception e) {
      e.printStackTrace(System.err);
      StringWriter sw = new StringWriter();
      PrintWriter pw = new PrintWriter(sw);
      e.printStackTrace(pw);
      status = 2;
      return;
    }
  }
Ejemplo n.º 3
0
 public Object getParameter(String name) {
   return transformer.getParameter(name);
 }