1 package org
.argeo
.cms
.integration
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.ByteArrayOutputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
7 import java
.io
.OutputStream
;
10 import javax
.jcr
.RepositoryException
;
11 import javax
.xml
.transform
.Result
;
12 import javax
.xml
.transform
.Source
;
13 import javax
.xml
.transform
.Transformer
;
14 import javax
.xml
.transform
.TransformerConfigurationException
;
15 import javax
.xml
.transform
.TransformerException
;
16 import javax
.xml
.transform
.TransformerFactory
;
17 import javax
.xml
.transform
.TransformerFactoryConfigurationError
;
18 import javax
.xml
.transform
.stream
.StreamResult
;
19 import javax
.xml
.transform
.stream
.StreamSource
;
21 public class XslTemplate
{
22 private Transformer transformer
;
24 public XslTemplate(InputStream in
, String systemId
) {
25 this(loadTransformer(in
, systemId
));
28 public XslTemplate(Transformer transformer
) {
29 this.transformer
= transformer
;
32 private static Transformer
loadTransformer(InputStream in
, String systemId
) {
34 TransformerFactory tFactory
= TransformerFactory
.newInstance();
35 StreamSource stylesource
= new StreamSource(in
, systemId
);
36 return tFactory
.newTransformer(stylesource
);
37 } catch (TransformerConfigurationException
| TransformerFactoryConfigurationError e
) {
38 throw new IllegalArgumentException("Cannot initialise stylesheet with systemId " + systemId
, e
);
42 public synchronized void apply(Node node
, OutputStream out
) {
43 // TODO use a pool of Transformer instead of synchronized
44 try (ByteArrayOutputStream xml
= new ByteArrayOutputStream()) {
45 node
.getSession().exportDocumentView(node
.getPath(), xml
, true, false);
46 try (ByteArrayInputStream xmlIn
= new ByteArrayInputStream(xml
.toByteArray())) {
47 Source source
= new StreamSource(xmlIn
);
48 Result results
= new StreamResult(out
);
49 transformer
.transform(source
, results
);
51 } catch (IOException
| RepositoryException
| TransformerException e
) {
52 throw new RuntimeException("Cannot process XSL template on " + node
, e
);