1
|
package eu.dnetlib.data.transform;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.Map;
|
5
|
import java.util.Map.Entry;
|
6
|
import javax.xml.transform.Transformer;
|
7
|
import javax.xml.transform.TransformerFactory;
|
8
|
|
9
|
import org.apache.commons.logging.Log;
|
10
|
import org.apache.commons.logging.LogFactory;
|
11
|
import org.dom4j.io.DocumentSource;
|
12
|
import org.dom4j.io.SAXReader;
|
13
|
|
14
|
public class XsltRowTransformerFactory {
|
15
|
|
16
|
private static final Log log = LogFactory.getLog(XsltRowTransformerFactory.class); // NOPMD by marko on 11/24/08 5:02 PM
|
17
|
|
18
|
public XsltRowTransformer getTransformer(final String xslt) {
|
19
|
return doGetTransformer(xslt, null);
|
20
|
}
|
21
|
|
22
|
public XsltRowTransformer getTransformer(final String xslt, final Map<String, Object> params) {
|
23
|
return doGetTransformer(xslt, params);
|
24
|
}
|
25
|
|
26
|
private XsltRowTransformer doGetTransformer(final String xslt, final Map<String, Object> params) {
|
27
|
try {
|
28
|
if ((xslt == null) || xslt.isEmpty()) return new XsltRowTransformer(null);
|
29
|
|
30
|
final TransformerFactory factory = TransformerFactory.newInstance();
|
31
|
|
32
|
if (log.isDebugEnabled()) {
|
33
|
log.debug("using transformer factory: '" + factory.getClass().getCanonicalName() + "'");
|
34
|
}
|
35
|
|
36
|
final Transformer t = factory.newTransformer(new DocumentSource(new SAXReader().read(new StringReader(xslt))));
|
37
|
if (params != null && !params.isEmpty()) {
|
38
|
for (final Entry<String, Object> e : params.entrySet()) {
|
39
|
log.debug(String.format("using xslt param: %s - %s", e.getKey(), e.getValue()));
|
40
|
t.setParameter(e.getKey(), e.getValue());
|
41
|
}
|
42
|
}
|
43
|
return new XsltRowTransformer(t);
|
44
|
} catch (final Exception e) {
|
45
|
throw new RuntimeException("Error generating transformer from xslt:\n" + xslt, e);
|
46
|
}
|
47
|
}
|
48
|
|
49
|
public static XsltRowTransformer newInstance(final String xslt) {
|
50
|
return new XsltRowTransformerFactory().getTransformer(xslt);
|
51
|
}
|
52
|
|
53
|
public static XsltRowTransformer newInstance(final String xslt, final Map<String, Object> params) {
|
54
|
return new XsltRowTransformerFactory().getTransformer(xslt, params);
|
55
|
}
|
56
|
|
57
|
}
|