Apache FOP

Der Formatting Objects Processor (FOP) ist eine Java-Anwendung der Apache Software Foundation, die aus XSL-FO-Layout-Beschreibungen verschiedene Ausgabeformate erstellt.

Voraussetzungen sind folgende Bibliotheken:

Abhängigkeiten für nachfolgende Beispiele:

class Generator


package de.snowbits.fop;

import java.io.File;
import java.io.InputStream;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

public abstract class Generator {

	public void convert(InputStream inputXSLT, InputStream inputXML, File outputPDF) throws Exception {
		convert(new StreamSource(inputXSLT), new StreamSource(inputXML), outputPDF.getAbsolutePath());
		FopHelper.close(inputXSLT);
		FopHelper.close(inputXML);
	}	
	
	public void convert(InputStream inputXSLT, InputStream inputXML, String outputPDF) throws Exception {
		convert(new StreamSource(inputXSLT), new StreamSource(inputXML), outputPDF);
		FopHelper.close(inputXSLT);
		FopHelper.close(inputXML);
	}	
	
	public void convert(String inputXSLT, InputStream inputXML, File outputPDF) throws Exception {
		convert(new StreamSource(inputXSLT), new StreamSource(inputXML), outputPDF.getAbsolutePath());
		FopHelper.close(inputXML);
	}	
	
	public void convert(String inputXSLT, InputStream inputXML, String outputPDF) throws Exception {
		convert(new StreamSource(inputXSLT), new StreamSource(inputXML), outputPDF);
		FopHelper.close(inputXML);
	}
	
	public void convert(File inputXSLT, File inputXML, File outputPDF) throws Exception {
		convert(new StreamSource(inputXSLT), new StreamSource(inputXML), outputPDF.getAbsolutePath());
	}	
	
	public void convert(String inputXSLTPath, String inputXMLPath, String outputPDFPath) throws Exception {
		convert(new StreamSource(inputXSLTPath), new StreamSource(inputXMLPath), outputPDFPath);
	}

	public abstract void convert(Source xsltSource, Source xmlSource, String outputPDF) throws Exception;
	
}
	

class UserAgent


package de.snowbits.fop;


public class UserAgent {

	private String _title;
	private String _subject;
	private String _author;
	private String _creator;
	private String _producer;
	
	public UserAgent() {
		this(null, null, null, null, null);
	}

	public UserAgent(String title, String subject, String author,
			         String creator, String producer) {
		_title = title;
		_subject = subject;
		_author = author;
		_creator = creator;
		_producer = producer;
	}

	public String getTitle() {
		return _title;
	}

	public void setTitle(String title) {
		_title = title;
	}

	public String getSubject() {
		return _subject;
	}

	public void setSubject(String subject) {
		_subject = subject;
	}

	public String getAuthor() {
		return _author;
	}

	public void setAuthor(String author) {
		_author = author;
	}

	public String getCreator() {
		return _creator;
	}

	public void setCreator(String creator) {
		_creator = creator;
	}

	public String getProducer() {
		return _producer;
	}

	public void setProducer(String producer) {
		_producer = producer;
	}

}
	

class FopHelper


package de.snowbits.fop;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.fop.apps.FOUserAgent;

public class FopHelper {

	public static final String VERSION_PARAM = "versionParam";
	public static final String VERSION = "2.0";
	
	private FopHelper() { }
	
	public static final void setUserAgent(FOUserAgent foUserAgent, UserAgent userAgent) {
		if (userAgent != null) {
			if (userAgent.getTitle() != null)
				foUserAgent.setTitle(userAgent.getTitle());
			if (userAgent.getSubject() != null)
				foUserAgent.setSubject(userAgent.getSubject());
			if (userAgent.getAuthor() != null)
				foUserAgent.setAuthor(userAgent.getAuthor());
			if (userAgent.getCreator() != null)
				foUserAgent.setCreator(userAgent.getCreator());
			if (userAgent.getProducer() != null)
				foUserAgent.setProducer(userAgent.getProducer());
		}
	}
	
	public static final void close(OutputStream out) throws IOException {
		if (out != null)
			out.close();
	}

	public static final void close(BufferedReader reader) throws IOException {
		if (reader != null)
			reader.close();
	}
	
	public static final void close(FileWriter writer) throws IOException {
		if (writer != null)
			writer.close();
	}
	
	public static final void close(InputStream input) throws IOException {
		if (input != null)
			input.close();
	}		
}
	

class AbstractFoTestBase


package de.snowbits;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;

import de.snowbits.fop.FopHelper;
import de.snowbits.fop.UserAgent;

public abstract class AbstractFoTestBase {

	protected static final String RESOURCES_PATH  = "src/test/resources/";
	protected static final String INPUT_FOP_PATH  = RESOURCES_PATH + "Adressen.fo";
	protected static final String INPUT_XSL_PATH  = RESOURCES_PATH + "Adressen.xsl";
	protected static final String INPUT_XML_PATH  = RESOURCES_PATH + "Adressen.xml";
	protected static final String OUTPUT_PDF_PATH = RESOURCES_PATH + "Adressbuch.pdf";
	
	protected UserAgent getUserAgent() {
		return new UserAgent("Mein Adressbuch", "OpenSource", 
				             "Wolfgang Garbe", "snowbits.de", 
				             "Apache FOP 1.1");
	}	
	
	protected final InputStream getXslStylesStream() throws IOException {
		return new ByteArrayInputStream(readFile(INPUT_XSL_PATH).getBytes());
	}
	
	protected final InputStream getXmlDataStream() throws IOException {
		return new ByteArrayInputStream(readFile(INPUT_XML_PATH).getBytes());
	}
	
	protected final String readFile(String filePath) throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(filePath));
		StringBuilder sb = new StringBuilder();
		String line = null;
				
		while ((line = reader.readLine()) != null) {
			sb.append(line);
		}
		
		FopHelper.close(reader);
		
		return sb.toString();			
	}	
	
	protected final boolean existFile(String outputPath) {
		File file = new File(outputPath);
		
		if (file.exists() && file.isFile())
			return true;
			
		return false;
	}
	
	protected final long getFileSize(String outputPath) {
		return new File(outputPath).length();
	}
	
	protected final void deleteFile(String outputPath) {
		File file = new File(outputPath);
		if (file.exists())
			file.delete();
	}
		
}
	

Top


Example
Sitemap Kontakt Impressum