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.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
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
67
68
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 }