1 | package de.ugoe.cs.swe.bnftools.generator;
|
---|
2 |
|
---|
3 | import java.io.BufferedOutputStream;
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.FileOutputStream;
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.OutputStream;
|
---|
8 |
|
---|
9 | import javax.xml.transform.Result;
|
---|
10 | import javax.xml.transform.Source;
|
---|
11 | import javax.xml.transform.Transformer;
|
---|
12 | import javax.xml.transform.TransformerException;
|
---|
13 | import javax.xml.transform.TransformerFactory;
|
---|
14 | import javax.xml.transform.sax.SAXResult;
|
---|
15 | import javax.xml.transform.stream.StreamSource;
|
---|
16 |
|
---|
17 |
|
---|
18 |
|
---|
19 | import org.apache.fop.apps.FOPException;
|
---|
20 | import org.apache.fop.apps.Fop;
|
---|
21 | import org.apache.fop.apps.FopFactory;
|
---|
22 | import org.apache.fop.apps.MimeConstants;
|
---|
23 |
|
---|
24 |
|
---|
25 | public class FoToPdfOrRtf {
|
---|
26 |
|
---|
27 |
|
---|
28 |
|
---|
29 | public static void createPdfFromFo(String fileName) throws IOException, FOPException,
|
---|
30 | TransformerException{
|
---|
31 |
|
---|
32 | createfoFromPdf(fileName,fileName);
|
---|
33 | }
|
---|
34 |
|
---|
35 | public static void createRtfFromFo(String fileName) throws IOException, FOPException,
|
---|
36 | TransformerException{
|
---|
37 |
|
---|
38 | createRtfFromFo(fileName,fileName);
|
---|
39 | }
|
---|
40 |
|
---|
41 | private static void createRtfFromFo(String foFileName, String pdfFileName)throws IOException, FOPException,
|
---|
42 | TransformerException{
|
---|
43 | File file = new File(foFileName+".fo");
|
---|
44 | // Step 1: Construct a FopFactory
|
---|
45 | FopFactory fopFactory = FopFactory.newInstance();
|
---|
46 |
|
---|
47 | // Step 2: Set up output stream.
|
---|
48 | OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(pdfFileName+".rtf")));
|
---|
49 |
|
---|
50 | try {
|
---|
51 | // Step 3: Construct fop with desired output format
|
---|
52 | Fop fop = fopFactory.newFop(MimeConstants.MIME_RTF, out);
|
---|
53 |
|
---|
54 | // Step 4: Setup JAXP using identity transformer
|
---|
55 | TransformerFactory factory = TransformerFactory.newInstance();
|
---|
56 | Transformer transformer = factory.newTransformer();
|
---|
57 |
|
---|
58 | // Step 5: Setup input and output for XSLT transformation
|
---|
59 | Source src = new StreamSource(file);
|
---|
60 |
|
---|
61 | // Resulting SAX events (the generated FO) must be piped through to FOP
|
---|
62 | Result res = new SAXResult(fop.getDefaultHandler());
|
---|
63 |
|
---|
64 | // Step 6: Start XSLT transformation and FOP processing
|
---|
65 | transformer.transform(src, res);
|
---|
66 | } finally {
|
---|
67 | out.close();
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | private static void createfoFromPdf(String foFileName, String pdfFileName)throws IOException, FOPException,
|
---|
73 | TransformerException{
|
---|
74 | File file = new File(foFileName+".fo");
|
---|
75 | // Step 1: Construct a FopFactory
|
---|
76 | FopFactory fopFactory = FopFactory.newInstance();
|
---|
77 |
|
---|
78 | // Step 2: Set up output stream.
|
---|
79 | OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(pdfFileName+".pdf")));
|
---|
80 |
|
---|
81 | try {
|
---|
82 | // Step 3: Construct fop with desired output format
|
---|
83 | Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
|
---|
84 |
|
---|
85 | // Step 4: Setup JAXP using identity transformer
|
---|
86 | TransformerFactory factory = TransformerFactory.newInstance();
|
---|
87 | Transformer transformer = factory.newTransformer();
|
---|
88 |
|
---|
89 | // Step 5: Setup input and output for XSLT transformation
|
---|
90 | Source src = new StreamSource(file);
|
---|
91 |
|
---|
92 | // Resulting SAX events (the generated FO) must be piped through to FOP
|
---|
93 | Result res = new SAXResult(fop.getDefaultHandler());
|
---|
94 |
|
---|
95 | // Step 6: Start XSLT transformation and FOP processing
|
---|
96 | transformer.transform(src, res);
|
---|
97 | } finally {
|
---|
98 | out.close();
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | public static void createPdfFromFo(File file,String fileName) throws IOException, FOPException,
|
---|
103 | TransformerException{
|
---|
104 |
|
---|
105 | createfoFromPdf(file,"mytest");
|
---|
106 | }
|
---|
107 |
|
---|
108 | private static void createfoFromPdf(File file, String pdfFileName)throws IOException, FOPException,
|
---|
109 | TransformerException{
|
---|
110 | // Step 1: Construct a FopFactory
|
---|
111 | FopFactory fopFactory = FopFactory.newInstance();
|
---|
112 |
|
---|
113 | // Step 2: Set up output stream.
|
---|
114 | OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(pdfFileName+".pdf")));
|
---|
115 |
|
---|
116 | try {
|
---|
117 | // Step 3: Construct fop with desired output format
|
---|
118 | Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
|
---|
119 |
|
---|
120 | // Step 4: Setup JAXP using identity transformer
|
---|
121 | TransformerFactory factory = TransformerFactory.newInstance();
|
---|
122 | Transformer transformer = factory.newTransformer();
|
---|
123 |
|
---|
124 | // Step 5: Setup input and output for XSLT transformation
|
---|
125 | Source src = new StreamSource(file);
|
---|
126 |
|
---|
127 | // Resulting SAX events (the generated FO) must be piped through to FOP
|
---|
128 | Result res = new SAXResult(fop.getDefaultHandler());
|
---|
129 |
|
---|
130 | // Step 6: Start XSLT transformation and FOP processing
|
---|
131 | transformer.transform(src, res);
|
---|
132 | } finally {
|
---|
133 | out.close();
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|