tags:

views:

315

answers:

2

Hello friends I try to use fop engine programatically I search for an example and I find this class

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;

import org.apache.avalon.framework.ExceptionUtil;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;

import org.apache.fop.apps.Driver;
import org.apache.fop.apps.FOPException;
import org.apache.fop.messaging.MessageHandler;

public class Invokefop 
{

    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);       
        driver.setRenderer(Driver.RENDER_PDF);
        OutputStream out = new java.io.FileOutputStream(pdf);
        try {
            driver.setOutputStream(out);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer(new StreamSource(xslt));
            Source src = new StreamSource(xml);
            Result res = new SAXResult(driver.getContentHandler());
            transformer.transform(src, res);
        } finally {
            out.close();
        }
    }


    public static void main(String[] args) {
        try {
            System.out.println("JAVA XML2PDF:: FOP ExampleXML2PDF\n");
            System.out.println("JAVA XML2PDF:: Preparing...");

            File base = new File("../");
            File baseDir = new File(base, "in");
            File outDir = new File(base, "out");
            outDir.mkdirs();


            File xmlfile = new File(baseDir, args[0]);
            File xsltfile = new File(baseDir, args[1]);
            File pdffile = new File(outDir, args[2]);

            System.out.println("JAVA XML2PDF:: Input: XML (" + xmlfile + ")");
            System.out.println("JAVA XML2PDF:: Stylesheet: " + xsltfile);
            System.out.println("JAVA XML2PDF:: Output: PDF (" + pdffile + ")");
            System.out.println();
            System.out.println("JAVA XML2PDF:: Transforming...");

            Invokefop app = new Invokefop();
            app.convertXML2PDF(xmlfile, xsltfile, pdffile);

            System.out.println("JAVA XML2PDF:: Success!");
        } catch (Exception e) {
            System.err.println(ExceptionUtil.printStackTrace(e));
            System.exit(-1);
        }
    }
}

All the Libs from Fop are in the Classpath including the fop.jar in build directory. After I run thejavac Invokefop.java I get this error:

> C:\....\fop>javac Invokefop.java
Invokefop.java:21: cannot find symbol
symbol  : class Driver
location: package org.apache.fop.apps
import org.apache.fop.apps.Driver;
                          ^
Invokefop.java:23: package org.apache.fop.messaging does not exist
import org.apache.fop.messaging.MessageHandler;
                               ^
Invokefop.java:31: cannot find symbol
symbol  : class Driver
location: class Invokefop
        Driver driver = new Driver();
        ^
Invokefop.java:31: cannot find symbol
symbol  : class Driver
location: class Invokefop
        Driver driver = new Driver();
                            ^
Invokefop.java:36: cannot find symbol
symbol  : variable MessageHandler
location: class Invokefop
        MessageHandler.setScreenLogger(logger);
        ^
Invokefop.java:39: cannot find symbol
symbol  : variable Driver
location: class Invokefop
        driver.setRenderer(Driver.RENDER_PDF);
                           ^
6 errors

I am relatively new to Java, but with this approach I try to execute the fop engine in c++ using this java class..

Have anybody some Idea, how to solve this errors... Thanx in advance..

+1  A: 

The classes that are coming up with "cannot find symbol" errors don't exist in the fop.jar file.

I just downloaded v0.95 of the FOP library, and I can't see those classes in any of the jar files in the distribution. (You can open a jar file in any zip program and have a look - a jar is basically a zip file with a few extra bits in there that Java uses).

I would guess that the classes referenced in your example code were removed at some stage from the FOP library, and the example is for the older version. So you could try and find an older version of the library that contains those classes, or look for newer examples. The binary distribution comes with some examples - maybe have a look through those to see if any are useful for you?

Ash
A: 

Ok actually I take the example in embedding directory:

/*

* Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */

/* $Id: ExampleXML2PDF.java 426576 2006-07-28 15:44:37Z jeremias $ */

package embedding;

//Java import java.io.File; import java.io.OutputStream;

//JAXP import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.Source; import javax.xml.transform.Result; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.sax.SAXResult;

//FOP import org.apache.fop.apps.FOUserAgent; import org.apache.fop.apps.Fop; import org.apache.fop.apps.FopFactory; import org.apache.fop.apps.MimeConstants;

/** * This class demonstrates the conversion of an XML file to PDF using * JAXP (XSLT) and FOP (XSL-FO). */ public class ExampleXML2PDF {

/**
 * Main method.
 * @param args command-line arguments
 */
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, "xml/xml/projectteam.xml");
        File xsltfile = new File(baseDir, "xml/xslt/projectteam2fo.xsl");
        File pdffile = new File(outDir, "ResultXML2PDF.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...");

        // configure fopFactory as desired
        FopFactory fopFactory = FopFactory.newInstance();

        FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
        // configure foUserAgent as desired

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

        try {
            // Construct fop with desired output format
            Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

            // Setup XSLT
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));

            // Set the value of a <param> in the stylesheet
            transformer.setParameter("versionParam", "2.0");

            // Setup input for XSLT transformation
            Source src = new StreamSource(xmlfile);

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

            // Start XSLT transformation and FOP processing
            transformer.transform(src, res);
        } finally {
            out.close();
        }

        System.out.println("Success!");
    } catch (Exception e) {
        e.printStackTrace(System.err);
        System.exit(-1);
    }
}

}

It compiles without errors know. But when I run it with java ExampleXML2PDF I get this errors:

C:\.....\fop-0.95\examples\embedding\java\embedding>java ExampleXML2PDF

Exception in thread "main" java.lang.NoClassDefFoundError: ExampleXML2PDF Caused by: java.lang.ClassNotFoundException: ExampleXML2PDF at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) Could not find the main class: ExampleXML2PDF. Program will exit.

It is really hard to imagine whats going here wrong cause it compiles correct but dont run..

Any suggestions for me out there.... Thanx

Did you build the example using ant? The examples come with their own build files (`build.xml`). It should set all the classpath things you need, and if the build works, the run target should work too. For example: `ant example1`. (You might need to install ant though - see http://ant.apache.org/)
Ash