Created
July 1, 2011 11:24
-
-
Save theresajayne/1058338 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package uk.co.inbrand.test; | |
import java.io.BufferedOutputStream; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import javax.xml.transform.Result; | |
import javax.xml.transform.Source; | |
import javax.xml.transform.Transformer; | |
import javax.xml.transform.TransformerConfigurationException; | |
import javax.xml.transform.TransformerException; | |
import javax.xml.transform.TransformerFactory; | |
import javax.xml.transform.sax.SAXResult; | |
import javax.xml.transform.stream.StreamSource; | |
import org.apache.avalon.framework.configuration.Configuration; | |
import org.apache.avalon.framework.configuration.ConfigurationException; | |
import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; | |
import org.apache.fop.apps.FOPException; | |
import org.apache.fop.apps.FOUserAgent; | |
import org.apache.fop.apps.Fop; | |
import org.apache.fop.apps.FopFactory; | |
import org.apache.fop.apps.MimeConstants; | |
import org.xml.sax.SAXException; | |
/** | |
* | |
* @author Theresa | |
*/ | |
public class TestFopFactory { | |
//Instantiate Fop Factory | |
public static void main(String[] args) throws SAXException, IOException, ConfigurationException { | |
System.out.println("Before instantiation"); | |
FopFactory fopFactory = FopFactory.newInstance(); | |
fopFactory.setStrictValidation(false); | |
DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder(); | |
Configuration cfg = cfgBuilder.buildFromFile(new File("C:/InBrand/fopconfig.xml")); | |
fopFactory.setUserConfig(cfg); | |
System.out.println("After Instantiation"); | |
BufferedOutputStream out = null; | |
File tempFile; | |
try { | |
System.out.print("!"); | |
tempFile = new File("c:/output.pdf"); | |
System.out.print("!"); | |
out = new BufferedOutputStream(new FileOutputStream(tempFile)); | |
System.out.print("!"); | |
FOUserAgent useragent= fopFactory.newFOUserAgent(); | |
useragent.setOutputFile(new File("C:/myfile.pdf")); | |
useragent.setTargetResolution(300); | |
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,useragent,out); | |
System.out.print("!"); | |
TransformerFactory factory = TransformerFactory.newInstance(); | |
System.out.print("!"); | |
Source xslt = new StreamSource(new File("C:/sample.xsl")); | |
Transformer transformer = factory.newTransformer(xslt); | |
System.out.print("!"); | |
Source src = new StreamSource(new File("C:/input.xml")); | |
System.out.print("!"); | |
Result res = new SAXResult(fop.getDefaultHandler()); | |
System.out.print("!"); | |
transformer.transform(src, res); | |
out.flush(); | |
out.close(); | |
System.out.print("!"); | |
System.out.println("Output File = "+tempFile.getAbsolutePath()); | |
} catch (TransformerConfigurationException ex) { | |
ex.printStackTrace(); | |
} catch (TransformerException ex) { | |
ex.printStackTrace(); | |
} catch (FOPException ex) { | |
ex.printStackTrace(); | |
} catch (FileNotFoundException ex) { | |
ex.printStackTrace(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
} finally { | |
try { | |
System.out.print("*"); | |
out.close(); | |
} catch (IOException ex) { | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment