View Javadoc

1   /*
2    * XMLCV - Gestion de CV sous forme XML
3    * 
4    * Copyright (C) 2004-2005, Damien Raude-Morvan
5    * 
6    * This file is part of XMLCV.
7    *
8    * XMLCV is free software; you can redistribute it and/or modify
9    * it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * XMLCV is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   *
18   * You should have received a copy of the GNU General Public License
19   * along with XMLCV; if not, write to the Free Software
20   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21   * 
22   * Contacts :
23   * - Sébastien Mathy <smathy@tuxfamily.org>
24   * - Emmanuel Berre <eberre@tuxfamily.org>
25   * - Damien Raude-Morvan <drazzib@drazzib.com>
26   */
27  package org.xmlcv;
28  
29  import java.io.File;
30  import java.io.IOException;
31  
32  import javax.xml.transform.TransformerException;
33  import javax.xml.transform.sax.SAXResult;
34  import javax.xml.transform.stream.StreamSource;
35  
36  import org.apache.fop.apps.Driver;
37  import org.xmlcv.util.Config;
38  import org.xmlcv.util.I18n;
39  
40  /***
41   * Thread for PDF convertion
42   * 
43   * @see org.xmlcv.Convert
44   * @version $Revision: 114 $
45   * @author <a href="mailto:drazzib@drazzib.com">Damien Raude-Morvan </a>
46   */
47  public class ConvertPDF extends Convert {
48  
49  	/***
50  	 * Convert a <code>p_cv</code> CvModel to a PDF <code>p_file</code>
51  	 * file.
52  	 * 
53  	 * @param p_cv
54  	 *            CVModel object
55  	 * @param p_file
56  	 *            file, will be written/overwritten
57  	 * @throws XmlCVException
58  	 */
59  	public ConvertPDF(CVModel p_cv, File p_file) throws XmlCVException {
60  		super(new StreamSource(p_cv.getInputStream()), p_file);
61  		this.go();
62  	}
63  
64  	public void go() {
65  		// Construct driver and output format
66  		Driver fop = new Driver();
67  		fop.setRenderer(Driver.RENDER_PDF);
68  
69  		try {
70  			setXslt(Config.STYLES_XSL_FO);
71  			setupTransformer();
72  
73  			fop.setOutputStream(this._doneStream);
74  
75  			// Resulting SAX events (the generated FO) must be piped through to
76  			// FOP
77  			SAXResult res = new SAXResult(fop.getContentHandler());
78  
79  			// Start XSLT transformation and FOP processing
80  			this._transformer.transform(this._srcXml, res);
81  		} catch (TransformerException e) {
82  			e.printStackTrace();
83  		} finally {
84  			try {
85  				this._doneStream.close();
86  			} catch (IOException e1) {
87  				e1.printStackTrace();
88  			}
89  		}
90  	}
91  
92  	public static void main(String[] args) throws XmlCVException {
93  		if (args.length != 2) {
94  			System.out.println(I18n.getString("Convert.WrongParameters")); //$NON-NLS-1$
95  			System.out.println(I18n.getString("ConvertPDF.Usage")); //$NON-NLS-1$
96  		} else {
97  			CVModel cv = new CVModel(args[0]);
98  			new ConvertPDF(cv, new File(args[1]));
99  		}
100 	}
101 
102 }