1 package fr.in2p3.jsaga.helpers.xslt;
2
3 import fr.in2p3.jsaga.Base;
4 import fr.in2p3.jsaga.helpers.XMLFileParser;
5 import org.w3c.dom.Document;
6 import org.w3c.dom.Element;
7
8 import javax.xml.parsers.DocumentBuilderFactory;
9 import javax.xml.parsers.ParserConfigurationException;
10 import javax.xml.transform.*;
11 import javax.xml.transform.dom.DOMResult;
12 import javax.xml.transform.dom.DOMSource;
13 import javax.xml.transform.stream.StreamResult;
14 import javax.xml.transform.stream.StreamSource;
15 import java.io.*;
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public class XSLTransformer {
30 private Transformer m_transformer;
31 private File m_debugFile;
32
33 public XSLTransformer(Transformer transformer, File debugFile) {
34 m_transformer = transformer;
35 m_debugFile = debugFile;
36 }
37
38 public byte[] transform(Document xmlInput) throws TransformerException, IOException {
39 return transform(new DOMSource(xmlInput));
40 }
41
42 public byte[] transform(Element xmlInput) throws TransformerException, IOException {
43 return transform(new DOMSource(xmlInput));
44 }
45
46 public byte[] transform(byte[] xmlInput) throws TransformerException, IOException {
47 return transform(new StreamSource(new ByteArrayInputStream(xmlInput)));
48 }
49
50 public Document transformToDOM(Document xmlInput) throws TransformerException, IOException {
51 return transformToDOM(new DOMSource(xmlInput));
52 }
53
54 public Document transformToDOM(byte[] xmlInput) throws TransformerException, IOException {
55 return transformToDOM(new StreamSource(new ByteArrayInputStream(xmlInput)));
56 }
57
58 public byte[] transform(Source source) throws TransformerException, IOException {
59
60 ByteArrayOutputStream out = new ByteArrayOutputStream();
61 Result result = new StreamResult(out);
62 try {
63 m_transformer.transform(source, result);
64 } catch(TransformerException e) {
65
66 m_transformer.getErrorListener().fatalError(e);
67 }
68 byte[] outBytes = out.toByteArray();
69
70
71 if (Base.DEBUG && m_debugFile!=null) {
72 if (outBytes!=null) {
73 OutputStream f = new FileOutputStream(m_debugFile);
74 f.write(outBytes);
75 f.close();
76 }
77 }
78
79
80 return outBytes;
81 }
82
83 private Document transformToDOM(Source source) throws TransformerException, IOException {
84
85 Document doc;
86 try {
87 doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
88 } catch (ParserConfigurationException e) {
89 throw new TransformerException(e);
90 }
91 Result result = new DOMResult(doc);
92 try {
93 m_transformer.transform(source, result);
94 } catch(TransformerException e) {
95
96 m_transformer.getErrorListener().fatalError(e);
97 }
98
99
100 if (Base.DEBUG && m_debugFile!=null) {
101 ByteArrayOutputStream out = new ByteArrayOutputStream();
102 XMLFileParser.dump(doc, out);
103 byte[] outBytes = out.toByteArray();
104 if (outBytes!=null) {
105 OutputStream f = new FileOutputStream(m_debugFile);
106 f.write(outBytes);
107 f.close();
108 }
109 }
110
111
112 return doc;
113 }
114 }