1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.exorcist.core;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23
24 import javax.xml.transform.Transformer;
25 import javax.xml.transform.TransformerException;
26 import javax.xml.transform.TransformerFactory;
27 import javax.xml.transform.stream.StreamResult;
28 import javax.xml.transform.stream.StreamSource;
29
30 import net.sf.exorcist.api.ContentConverter;
31 import net.sf.exorcist.api.ContentException;
32 import net.sf.exorcist.api.ContentState;
33
34 /***
35 * TODO
36 */
37 public class XsltConverter implements ContentConverter {
38
39 private static final TransformerFactory FACTORY =
40 TransformerFactory.newInstance();
41
42 private String stylesheet;
43
44 public void setStylesheet(String stylesheet) {
45 this.stylesheet = stylesheet;
46 }
47
48 public void convertContent(ContentState state) throws ContentException {
49 try {
50 File file = File.createTempFile("exorcist", ".xml");
51 file.deleteOnExit();
52
53 InputStream input = state.getContent();
54 try {
55 FileOutputStream output = new FileOutputStream(file);
56 try {
57 Transformer transformer = FACTORY.newTransformer(
58 new StreamSource(new FileInputStream(stylesheet)));
59 transformer.transform(
60 new StreamSource(input), new StreamResult(output));
61 } finally {
62 try { output.close(); } catch (Exception e) {}
63 }
64 } finally {
65 try { input.close(); } catch (Exception e) {}
66 }
67
68 InputStream xml = new FileInputStream(file);
69 try {
70 state.setContent(xml);
71 } finally {
72 try { xml.close(); } catch (Exception e) {}
73 }
74 } catch (IOException e) {
75 throw new ContentException(e);
76 } catch (TransformerException e) {
77 throw new ContentException(e);
78 }
79 }
80
81 }