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.IOException;
23  import java.io.InputStream;
24  import javax.xml.transform.Result;
25  import javax.xml.transform.Source;
26  import javax.xml.transform.Transformer;
27  import javax.xml.transform.TransformerFactory;
28  import javax.xml.transform.stream.StreamResult;
29  import javax.xml.transform.stream.StreamSource;
30  import net.rwx.maven.asciidoc.backends.Backend;
31  import net.rwx.maven.asciidoc.configuration.Document;
32  import net.rwx.maven.asciidoc.services.TransformationService;
33  import net.rwx.maven.asciidoc.utils.FileUtils;
34  
35  /**
36   *
37   * @author Arnaud Fonce <arnaud.fonce@r-w-x.net>
38   */
39  public class TransformationServiceImpl extends RootServiceImpl implements TransformationService {
40  
41      private String pathToXslFiles;
42  
43      public TransformationServiceImpl() throws IOException {
44          ClassLoader loader = this.getClass().getClassLoader();
45          InputStream is = loader.getResourceAsStream(ARCHIVE_NAME);
46          pathToXslFiles = FileUtils.getTemporaryDirectory();
47          FileUtils.uncompress(is, pathToXslFiles);
48      }
49  
50      @Override
51      public void execute(String inputPath, Document document, Backend backend) throws Exception {
52          setOuputPath(inputPath, backend);
53          if( backend.isIsTransformation()) {
54              File xmlFile = new File(inputPath);
55              File xsltFile = new File(getXsl(backend.getTransformationStylesheet()));
56              File resultFile = new File(getOuputPath());
57  
58              Source xmlSource = new StreamSource(xmlFile);
59              Source xsltSource = new StreamSource(xsltFile);
60              Result result = new StreamResult(new BufferedOutputStream(new FileOutputStream(resultFile)));
61  
62              TransformerFactory transFact = TransformerFactory.newInstance();
63              Transformer trans = transFact.newTransformer(xsltSource);
64              trans.setParameter("paper.type", "A4");
65  
66              // TODO: will be used with images management - does not work yet
67              /*String imgDir = new File(documentPath).getParentFile().getAbsolutePath();
68              trans.setParameter("img.src.path", imgDir + "/");*/
69              trans.transform(xmlSource, result);
70          }
71      }
72  
73      private String getXsl(String name) {
74  
75          StringBuilder builder = new StringBuilder();
76          builder.append(pathToXslFiles);
77          builder.append(File.separator);
78          builder.append("docbook-xsl");
79          builder.append(File.separator);
80          builder.append(name);
81  
82          return builder.toString();
83      }
84  
85      @Override
86      protected void setOuputPath(String inputPath, Backend backend) {
87          if( backend.isIsTransformation() ) {
88              setOutputPath( inputPath, backend.getTransformationExtension() );
89          }else {
90              setOutputPath(inputPath);
91          }
92      }
93  }