1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
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);
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 }