1
|
package eu.dnetlib.data.mapreduce.hbase.statsExport.utils;
|
2
|
|
3
|
import org.apache.commons.io.IOUtils;
|
4
|
import javax.xml.transform.Transformer;
|
5
|
import javax.xml.transform.TransformerFactory;
|
6
|
import javax.xml.transform.stream.StreamResult;
|
7
|
import javax.xml.transform.stream.StreamSource;
|
8
|
import java.io.ByteArrayInputStream;
|
9
|
import java.io.ByteArrayOutputStream;
|
10
|
import java.io.InputStream;
|
11
|
import java.nio.charset.StandardCharsets;
|
12
|
|
13
|
public class ContextTransformer {
|
14
|
|
15
|
public String transformXSL(String xml) throws Exception {
|
16
|
Transformer transformer;
|
17
|
TransformerFactory tFactory = TransformerFactory.newInstance();
|
18
|
|
19
|
if (xml == null) {
|
20
|
throw new IllegalArgumentException("Input xml should not be null!");
|
21
|
}
|
22
|
|
23
|
InputStream inputStream = null;
|
24
|
ByteArrayInputStream readerStream = null;
|
25
|
ByteArrayOutputStream writerStream = null;
|
26
|
|
27
|
try {
|
28
|
inputStream = ClassLoader.getSystemResourceAsStream("eu/dnetlib/data/mapreduce/hbase/statsExport/" + "context.xsl");
|
29
|
transformer = tFactory.newTransformer(new StreamSource(inputStream));
|
30
|
|
31
|
readerStream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
|
32
|
|
33
|
writerStream = new ByteArrayOutputStream();
|
34
|
transformer.transform(new StreamSource(readerStream), new StreamResult(writerStream));
|
35
|
|
36
|
return writerStream.toString("UTF8");
|
37
|
} finally {
|
38
|
IOUtils.closeQuietly(inputStream);
|
39
|
IOUtils.closeQuietly(readerStream);
|
40
|
IOUtils.closeQuietly(writerStream);
|
41
|
}
|
42
|
}
|
43
|
}
|