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.BufferedOutputStream;
30  import java.io.File;
31  import java.io.FileNotFoundException;
32  import java.io.FileOutputStream;
33  import java.io.InputStream;
34  import java.io.OutputStream;
35  
36  import javax.xml.transform.Transformer;
37  import javax.xml.transform.TransformerConfigurationException;
38  import javax.xml.transform.TransformerFactory;
39  import javax.xml.transform.stream.StreamSource;
40  
41  import org.xmlcv.util.JarResolver;
42  
43  /***
44   * Abstract class for convertion
45   * 
46   * @version $Revision: 114 $
47   * @author <a href="mailto:drazzib@drazzib.com">Damien Raude-Morvan </a>
48   */
49  public abstract class Convert {
50  
51  	/*** Stream for output resulting data after XSLT */
52  	protected OutputStream _doneStream;
53  
54  	/*** Factory for building a <code>transformer</code> */
55  	protected TransformerFactory _transformerFactory;
56  
57  	/*** Load an XSLT stylesheel and apply transformation */
58  	protected Transformer _transformer;
59  
60  	/*** Storage of XML Source */
61  	protected StreamSource _srcXml;
62  
63  	/*** Storage of XSLT Source */
64  	protected StreamSource _xsltXml;
65  
66  	/***
67  	 * Convert a File <code>in</code> (XMLCV Xml) to a <code>out</code> file
68  	 * with applying <code>xslt</code>.<br>
69  	 * Use this fonction when you want an output :)
70  	 * 
71  	 * @param p_in
72  	 *            xml file at XMLCV format
73  	 * @param p_out
74  	 *            pdf/rtf/html file, will be written/overwritten
75  	 * @throws XmlCVException
76  	 */
77  	public Convert(StreamSource p_in, File p_out) throws XmlCVException {
78  		try {
79  			// Setup output
80  			this._doneStream = new FileOutputStream(p_out);
81  			this._doneStream = new BufferedOutputStream(this._doneStream);
82  
83  			this._srcXml = p_in;
84  
85  		} catch (FileNotFoundException e) {
86  			throw new XmlCVException(e);
87  		}
88  	}
89  
90  	/***
91  	 * Convert a File <code>in</code> (XMLCV Xml) with applying
92  	 * <code>xslt</code>.<br>
93  	 * Used for preview.
94  	 * 
95  	 * @param p_in
96  	 *            xml file at XMLCV format
97  	 * @param p_xslt
98  	 *            xslt stylsheet applied to <code>in</code>
99  	 */
100 	public Convert(StreamSource p_in, StreamSource p_xslt) {
101 		this._xsltXml = p_xslt;
102 		this._srcXml = p_in;
103 	}
104 
105 	protected void setXslt(String p_xslt) {
106 		InputStream stream = getClass().getResourceAsStream(p_xslt);
107 
108 		if (stream != null) {
109 			this._xsltXml = new StreamSource(stream);
110 		} else {
111 			this._xsltXml = null;
112 			System.err.println("Unable to load the XSL Stylesheet : " + p_xslt); //$NON-NLS-1$
113 		}
114 	}
115 
116 	protected void setupTransformer() throws TransformerConfigurationException {
117 		this._transformerFactory = TransformerFactory.newInstance();
118 		this._transformerFactory.setURIResolver(new JarResolver());
119 		this._transformer = this._transformerFactory
120 				.newTransformer(this._xsltXml);
121 	}
122 
123 }