source: default/v2/trunk/de.ugoe.cs.swe.bnftools.ebnf/src/de/ugoe/cs/swe/bnftools/generator/foToPdf.java @ 70

Last change on this file since 70 was 70, checked in by hkaulbersch, 10 years ago

Added a method to get the whole path of generated files.

  • Property svn:mime-type set to text/plain
File size: 3.2 KB
Line 
1package de.ugoe.cs.swe.bnftools.generator;
2
3import java.io.BufferedOutputStream;
4import java.io.File;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.OutputStream;
8
9import javax.xml.transform.Result;
10import javax.xml.transform.Source;
11import javax.xml.transform.Transformer;
12import javax.xml.transform.TransformerException;
13import javax.xml.transform.TransformerFactory;
14import javax.xml.transform.sax.SAXResult;
15import javax.xml.transform.stream.StreamSource;
16
17import org.apache.fop.apps.FOPException;
18import org.apache.fop.apps.FopFactory;
19import org.apache.fop.apps.Fop;
20import org.apache.fop.apps.MimeConstants;
21
22public class foToPdf {
23
24        public static void main(String args[]) throws IOException, FOPException,
25                        TransformerException {
26
27                createfoFromPdf("test1","test1");
28        }
29       
30        public static void createPdfFromFo(String fileName) throws IOException, FOPException,
31        TransformerException{
32
33                createfoFromPdf(fileName,fileName);
34        }
35       
36        private static void createfoFromPdf(String foFileName, String pdfFileName)throws IOException, FOPException,
37        TransformerException{
38                File file = new File(foFileName+".fo");
39                // Step 1: Construct a FopFactory
40                FopFactory fopFactory = FopFactory.newInstance();
41
42                // Step 2: Set up output stream.
43                OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(pdfFileName+".pdf")));
44
45                try {
46                        // Step 3: Construct fop with desired output format
47                        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
48
49                        // Step 4: Setup JAXP using identity transformer
50                        TransformerFactory factory = TransformerFactory.newInstance();
51                        Transformer transformer = factory.newTransformer();
52
53                        // Step 5: Setup input and output for XSLT transformation
54                        Source src = new StreamSource(file);
55
56                        // Resulting SAX events (the generated FO) must be piped through to FOP         
57                        Result res = new SAXResult(fop.getDefaultHandler());
58
59                        // Step 6: Start XSLT transformation and FOP processing
60                        transformer.transform(src, res);
61                } finally {
62                        out.close();
63                }
64        }
65       
66        public static void createPdfFromFo(File file,String fileName) throws IOException, FOPException,
67        TransformerException{
68
69                createfoFromPdf(file,"mytest");
70        }
71       
72        private static void createfoFromPdf(File file, String pdfFileName)throws IOException, FOPException,
73        TransformerException{
74                // Step 1: Construct a FopFactory
75                FopFactory fopFactory = FopFactory.newInstance();
76
77                // Step 2: Set up output stream.
78                OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(pdfFileName+".pdf")));
79
80                try {
81                        // Step 3: Construct fop with desired output format
82                        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
83
84                        // Step 4: Setup JAXP using identity transformer
85                        TransformerFactory factory = TransformerFactory.newInstance();
86                        Transformer transformer = factory.newTransformer();
87
88                        // Step 5: Setup input and output for XSLT transformation
89                        Source src = new StreamSource(file);
90
91                        // Resulting SAX events (the generated FO) must be piped through to FOP         
92                        Result res = new SAXResult(fop.getDefaultHandler());
93
94                        // Step 6: Start XSLT transformation and FOP processing
95                        transformer.transform(src, res);
96                } finally {
97                        out.close();
98                }
99        }
100}
Note: See TracBrowser for help on using the repository browser.