* Apache xmlgraphics stack upgraded to the latest versions :
[scilab.git] / scilab / modules / helptools / src / java / org / scilab / modules / helptools / FopConverter.java
1 package org.scilab.modules.helptools;
2
3 import java.io.BufferedOutputStream;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.io.OutputStream;
9
10 import javax.xml.transform.Result;
11 import javax.xml.transform.Source;
12 import javax.xml.transform.Transformer;
13 import javax.xml.transform.TransformerConfigurationException;
14 import javax.xml.transform.TransformerException;
15 import javax.xml.transform.TransformerFactory;
16 import javax.xml.transform.sax.SAXResult;
17 import javax.xml.transform.stream.StreamSource;
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.FormattingResults;
23 import org.apache.fop.apps.MimeConstants;
24 import org.scilab.forge.jlatexmath.fop.JLaTeXMathElementMapping;
25 import org.scilab.forge.jlatexmath.fop.JLaTeXMathXMLHandler;
26 import org.scilab.modules.commons.xml.ScilabTransformerFactory;
27 import org.xml.sax.SAXException;
28
29 public class FopConverter extends ContainerConverter {
30
31     final Backend format;
32
33     public FopConverter(SciDocMain sciDocMain) {
34         super(sciDocMain.getOutputDirectory(), sciDocMain.getLanguage());
35         this.format = sciDocMain.getFormat();
36     }
37
38     @Override
39     public void convert() throws SAXException, IOException {
40         String baseName = Helpers.getBaseName(language);
41         /* the following '..' is used because we are in the current working
42            directory with all the tmp stuff in it */
43         String fileName = outputDirectory + "/" + baseName + "." + format.name().toLowerCase();
44
45         try {
46             final File configuration = new File(System.getenv("SCI") + "/modules/helptools/etc/fopconf.xml");
47             FopFactory fopFactory = FopFactory.newInstance(configuration);
48             fopFactory.addElementMapping(new JLaTeXMathElementMapping());
49             fopFactory.getXMLHandlerRegistry().addXMLHandler(new JLaTeXMathXMLHandler());
50
51             // Step 3: Construct fop with desired output format
52             OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
53             final Fop fop;
54             switch (format) {
55                 case PS:
56                     fop = fopFactory.newFop(MimeConstants.MIME_POSTSCRIPT, out);
57                     break;
58                 case PDF:
59                     fop = fopFactory.newFop(MimeConstants.MIME_POSTSCRIPT, out);
60                     break;
61
62                 default:
63                     out.close();
64                     throw new IOException(String.format("%s is not a supported format.\n", format));
65             }
66
67             // Step 4: Setup JAXP using identity transformer
68             String factoryName = ScilabTransformerFactory.useDefaultTransformerFactoryImpl();
69             TransformerFactory factory = TransformerFactory.newInstance();
70             Transformer transformer = factory.newTransformer(); // identity transformer
71             // Step 5: Setup input and output for XSLT transformation
72             // Setup input stream
73             Source src = new StreamSource(new File(Helpers.getTemporaryNameFo(outputDirectory)));
74
75             // Resulting SAX events (the generated FO) must be piped through to FOP
76             Result res = new SAXResult(fop.getDefaultHandler());
77
78             // Step 6: Start XSLT transformation and FOP processing
79             transformer.transform(src, res);
80             ScilabTransformerFactory.restoreTransformerFactoryImpl(factoryName);
81
82             FormattingResults foResults = fop.getResults();
83             System.out.println("Generated " + foResults.getPageCount() + " pages in total.");
84
85             //Clean-up
86             out.close();
87         } catch (FOPException e) {
88             System.out.println(e.getLocalizedMessage());
89         } catch (FileNotFoundException e) {
90             System.out.println(e.getLocalizedMessage());
91         } catch (TransformerConfigurationException e) {
92             System.out.println(e.getLocalizedMessage());
93         } catch (TransformerException e) {
94             System.out.println(e.getLocalizedMessage());
95         } catch (IOException e) {
96             System.out.println(e.getLocalizedMessage());
97         } catch (SAXException e) {
98             System.out.println(e.getLocalizedMessage());
99         }
100     }
101
102     @Override
103     public void install() throws IOException {
104     }
105 }