Project

General

Profile

« Previous | Next » 

Revision 37156

added parameter aware factory method

View differences:

modules/dnet-hadoop-commons/trunk/src/main/java/eu/dnetlib/data/transform/XsltRowTransformerFactory.java
1 1
package eu.dnetlib.data.transform;
2 2

  
3 3
import java.io.StringReader;
4
import java.util.Map;
5
import java.util.Map.Entry;
4 6

  
7
import javax.xml.transform.Transformer;
5 8
import javax.xml.transform.TransformerFactory;
6 9

  
10
import org.apache.commons.collections.MapUtils;
7 11
import org.apache.commons.logging.Log;
8 12
import org.apache.commons.logging.LogFactory;
9 13
import org.dom4j.io.DocumentSource;
......
12 16
public class XsltRowTransformerFactory {
13 17

  
14 18
	private static final Log log = LogFactory.getLog(XsltRowTransformerFactory.class); // NOPMD by marko on 11/24/08 5:02 PM
15
	
19

  
16 20
	public XsltRowTransformer getTransformer(final String xslt) {
21
		return doGetTransformer(xslt, null);
22
	}
23

  
24
	public XsltRowTransformer getTransformer(final String xslt, final Map<String, String> params) {
25
		return doGetTransformer(xslt, params);
26
	}
27

  
28
	private XsltRowTransformer doGetTransformer(final String xslt, final Map<String, String> params) {
17 29
		try {
18
			if ((xslt == null) || xslt.isEmpty()) { return new XsltRowTransformer(null); }
30
			if ((xslt == null) || xslt.isEmpty()) return new XsltRowTransformer(null);
19 31

  
20 32
			final TransformerFactory factory = TransformerFactory.newInstance();
21
			
33

  
22 34
			if (log.isDebugEnabled()) {
23 35
				log.debug("using transformer factory: '" + factory.getClass().getCanonicalName() + "'");
24 36
			}
25
			
26
			return new XsltRowTransformer(factory.newTransformer(new DocumentSource(new SAXReader().read(new StringReader(xslt)))));
27
		} catch (Exception e) {
37

  
38
			final Transformer t = factory.newTransformer(new DocumentSource(new SAXReader().read(new StringReader(xslt))));
39
			if (!MapUtils.isEmpty(params)) {
40
				for (final Entry<String, String> e : params.entrySet()) {
41
					log.debug(String.format("using xslt param: %s - %s", e.getKey(), e.getValue()));
42
					t.setParameter(e.getKey(), e.getValue());
43
				}
44
			}
45
			return new XsltRowTransformer(t);
46
		} catch (final Exception e) {
28 47
			throw new RuntimeException("Error generating transformer from xslt:\n" + xslt, e);
29 48
		}
30 49
	}
......
32 51
	public static XsltRowTransformer newInstance(final String xslt) {
33 52
		return new XsltRowTransformerFactory().getTransformer(xslt);
34 53
	}
54

  
55
	public static XsltRowTransformer newInstance(final String xslt, final Map<String, String> params) {
56
		return new XsltRowTransformerFactory().getTransformer(xslt, params);
57
	}
58

  
35 59
}

Also available in: Unified diff