View Javadoc

1   /*   Copyright (C) 2004 Finalist IT Group
2    *
3    *   This file is part of JAG - the Java J2EE Application Generator
4    *
5    *   JAG is free software; you can redistribute it and/or modify
6    *   it under the terms of the GNU General Public License as published by
7    *   the Free Software Foundation; either version 2 of the License, or
8    *   (at your option) any later version.
9    *   JAG 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   *   You should have received a copy of the GNU General Public License
14   *   along with JAG; if not, write to the Free Software
15   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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  			//Set the driver to render PDF
60  			driver.setRenderer(Driver.RENDER_PDF);
61  		
62  			//Get the transformer
63  			Transformer transformer = getTransformer();
64  
65  			//No mather what the specifyed parameters say, set this one to prevent errors
66  			//on book creation...
67  			transformer.setParameter(ParameterValues.FOPEXTENTIONS_KEY, ParameterValues.FOPEXTENTIONS_VALUE);
68  
69  			//call the doSetParameter method, after the defined values are set to
70  			//let the user overwrite the values
71  			doSetParameter(transformer);
72  			
73  			//Do the actual transformation, after transforming to XSL-FO it will be
74  			//piped to FOP and it will render a PDF
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  					//Setup a file to write too
81  					FileOutputStream out = new FileOutputStream(getOutputFile("pdf"));			
82  					driver.setOutputStream(out);
83  					
84  					//Setup FOP for the new file
85  					Configuration.put(ParameterValues.BASEDIR_KEY, getBaseDir());
86  					
87  					//Make sure the XSL transformation's result is piped through to FOP
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  			//System.out.println(); needed to tel the use that we recieved a problem
98  			System.out.println("Error while setting a Configuration Option " + e.toString());
99  		} catch (TransformerException e) {
100 			//System.out.println(); needed to tel the use that we recieved a problem
101 			System.out.println("Error while transforming the docbook file to PDF " + e.toString());
102 		} catch (IOException e) {
103 			//System.out.println(); needed to tel the use that we recieved a problem
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  * $Log: PDFTask.java,v $
114  * Revision 1.1  2004/03/12 22:16:00  ekkelenkamp
115  * no message
116  *
117  * Revision 1.3  2004/01/14 14:40:13  stefan
118  * Added support for nested filesets, see manual for details
119  *
120  * Revision 1.2  2004/01/06 13:26:39  stefan
121  * Added local dtd support and local watermark image support, now it's independent from the Internet
122  *
123  * Revision 1.1  2003/12/29 15:46:41  stefan
124  * initial checkin of the DocBook Ant Task
125  *
126  *
127  */