View Javadoc

1   /*
2    * Copyright (C) 2013 Room Work eXperience
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
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   * @author Arnaud Fonce <arnaud.fonce@r-w-x.net>
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  }