XSLTTransformer.java
3.8 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.apache.cocoon.xml.sax.AbstractSAXPipe
* org.apache.felix.scr.annotations.Component
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.osgi.service.component.ComponentContext
*/
package com.day.cq.rewriter.xml;
import com.day.cq.rewriter.pipeline.Transformer;
import com.day.cq.rewriter.processor.ProcessingComponentConfiguration;
import com.day.cq.rewriter.processor.ProcessingContext;
import com.day.cq.rewriter.xml.SourceResolverImpl;
import com.day.cq.rewriter.xml.XSLTProcessorException;
import com.day.cq.rewriter.xml.XSLTProcessorImpl;
import java.io.IOException;
import java.util.Map;
import javax.xml.transform.Result;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.TransformerHandler;
import org.apache.cocoon.xml.sax.AbstractSAXPipe;
import org.apache.excalibur.source.Source;
import org.apache.excalibur.source.SourceResolver;
import org.apache.felix.scr.annotations.Component;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.component.ComponentContext;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;
@Component(factory="com.day.cq.rewriter.pipeline.Transformer/xslt")
public class XSLTTransformer
extends AbstractSAXPipe
implements Transformer {
protected TransformerHandler transformerHandler;
private Source inputSource;
private SourceResolver resolver;
private XSLTProcessorImpl xsltProcessor;
private SAXException exceptionDuringSetConsumer;
private Resource resource;
@Override
public void init(ProcessingContext context, ProcessingComponentConfiguration config) throws IOException {
String src = (String)config.getConfiguration().get("source");
if (src == null) {
throw new RuntimeException("Source is missing.");
}
this.resolver = new SourceResolverImpl(context.getRequest().getResourceResolver(), context.getRequest(), context.getResponse());
this.inputSource = this.resolver.resolveURI(src);
this.xsltProcessor = new XSLTProcessorImpl(this.resolver);
this.resource = context.getRequest().getResource();
}
@Override
public void setContentHandler(ContentHandler ch) {
try {
this.transformerHandler = this.xsltProcessor.getTransformerHandler(this.inputSource);
}
catch (XSLTProcessorException se) {
this.exceptionDuringSetConsumer = new SAXException("Unable to get transformer handler for " + this.inputSource.getURI(), se);
return;
}
super.setContentHandler((ContentHandler)this.transformerHandler);
SAXResult result = new SAXResult(ch);
if (ch instanceof LexicalHandler) {
result.setLexicalHandler((LexicalHandler)((Object)ch));
}
this.transformerHandler.setResult(result);
this.transformerHandler.getTransformer().setParameter("resource", this.resource.getPath());
}
protected void deactivate(ComponentContext context) {
if (this.inputSource != null) {
this.resolver.release(this.inputSource);
this.inputSource = null;
}
this.resolver = null;
this.transformerHandler = null;
this.exceptionDuringSetConsumer = null;
}
@Override
public void startDocument() throws SAXException {
if (this.exceptionDuringSetConsumer != null) {
throw this.exceptionDuringSetConsumer;
}
super.startDocument();
}
}