1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.rwx.maven.asciidoc.services.impl;
18
19 import java.io.BufferedOutputStream;
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.OutputStream;
23 import javax.xml.transform.Result;
24 import javax.xml.transform.Source;
25 import javax.xml.transform.Transformer;
26 import javax.xml.transform.TransformerFactory;
27 import javax.xml.transform.sax.SAXResult;
28 import javax.xml.transform.stream.StreamSource;
29 import net.rwx.maven.asciidoc.backends.Backend;
30 import net.rwx.maven.asciidoc.configuration.Document;
31 import net.rwx.maven.asciidoc.constants.Extension;
32 import net.rwx.maven.asciidoc.services.FopService;
33 import net.rwx.maven.asciidoc.utils.FileUtils;
34 import org.apache.fop.apps.Fop;
35 import org.apache.fop.apps.FopFactory;
36 import org.apache.fop.apps.MimeConstants;
37
38
39
40
41
42 public class FopServiceImpl extends RootServiceImpl implements FopService {
43
44 @Override
45 public void execute( String inputPath, Document document, Backend backend ) throws Exception {
46 setOuputPath(inputPath, backend);
47 if( backend.isIsFopTransformation() ) {
48 OutputStream out = null;
49 try {
50
51 FopFactory fopFactory = FopFactory.newInstance();
52
53 File outputFile = new File( getOuputPath() );
54 if( outputFile.exists() ) {
55 FileUtils.forceDelete( outputFile );
56 }
57 out = new BufferedOutputStream( new FileOutputStream( outputFile) );
58
59 Fop fop = fopFactory.newFop( MimeConstants.MIME_PDF, out );
60
61 TransformerFactory factory = TransformerFactory.newInstance();
62 Transformer transformer = factory.newTransformer();
63
64 File inputFile = new File( inputPath );
65 FileUtils.copyFileToDirectory( inputFile, new File("/tmp") );
66 Source src = new StreamSource( inputFile );
67 Result res = new SAXResult( fop.getDefaultHandler() );
68
69 transformer.transform( src, res );
70 } finally {
71 if( out != null ) {
72 out.close();
73 }
74 }
75 }
76 }
77
78 @Override
79 protected void setOuputPath(String inputPath, Backend backend) {
80 if( backend.isIsFopTransformation() ) {
81 setOutputPath(inputPath, Extension.PDF);
82 }else {
83 setOutputPath(inputPath);
84 }
85 }
86 }