1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package com.finalist.ant.tasks.docbook;
19
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22
23 import javax.xml.transform.Transformer;
24 import javax.xml.transform.TransformerConfigurationException;
25 import javax.xml.transform.TransformerException;
26 import javax.xml.transform.sax.SAXResult;
27 import javax.xml.transform.sax.SAXSource;
28
29 import org.apache.fop.apps.Driver;
30 import org.apache.fop.configuration.Configuration;
31 import org.apache.tools.ant.BuildException;
32 import org.xml.sax.InputSource;
33 import org.xml.sax.SAXException;
34 import org.xml.sax.XMLReader;
35 import org.xml.sax.helpers.XMLReaderFactory;
36
37 /***
38 * <code>PDFTask.java</code>
39 *
40 * This task extends the AbstractDocBookTask
41 * It does the transformation of the docbook file to a PDF file
42 *
43 * @author Stefan Lenselink
44 * @version $Revision: 1.1 $
45 */
46
47 public class PDFTask extends AbstractDocBookTask {
48
49 /***
50 * This method actually transforms the Docbook file to a pdf file
51 *
52 * @see com.finalist.ant.tasks.AbstractDocBookTask#execute()
53 */
54 public void execute() throws BuildException {
55 try {
56
57 Driver driver = new Driver();
58
59
60 driver.setRenderer(Driver.RENDER_PDF);
61
62
63 Transformer transformer = getTransformer();
64
65
66
67 transformer.setParameter(ParameterValues.FOPEXTENTIONS_KEY, ParameterValues.FOPEXTENTIONS_VALUE);
68
69
70
71 doSetParameter(transformer);
72
73
74
75 XMLReader reader = XMLReaderFactory.createXMLReader("org.apache.crimson.parser.XMLReaderImpl");
76 reader.setEntityResolver(new DTDResolver());
77
78 synchronized(this){
79 for (int i = 0; i < getNumberOfCycles(); i++) {
80
81 FileOutputStream out = new FileOutputStream(getOutputFile("pdf"));
82 driver.setOutputStream(out);
83
84
85 Configuration.put(ParameterValues.BASEDIR_KEY, getBaseDir());
86
87
88 SAXResult result = new SAXResult(driver.getContentHandler());
89 SAXSource source = new SAXSource(reader, new InputSource( getDocbookfile()));
90
91 transformer.transform(source, result);
92
93 driver.reset();
94 }
95 }
96 } catch (TransformerConfigurationException e) {
97
98 System.out.println("Error while setting a Configuration Option " + e.toString());
99 } catch (TransformerException e) {
100
101 System.out.println("Error while transforming the docbook file to PDF " + e.toString());
102 } catch (IOException e) {
103
104 System.out.println("Error while writing to the output file " + e.toString());
105 } catch (SAXException e){
106 System.out.println("Error loading docbook file " + e.toString());
107 }
108 }
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127