1
|
package eu.dnetlib.msro.workflows.xslt;
|
2
|
|
3
|
import static org.junit.Assert.assertFalse;
|
4
|
import static org.junit.Assert.assertNotNull;
|
5
|
|
6
|
import java.io.IOException;
|
7
|
import java.io.InputStream;
|
8
|
import java.io.StringReader;
|
9
|
import java.io.StringWriter;
|
10
|
|
11
|
import javax.xml.transform.Transformer;
|
12
|
import javax.xml.transform.TransformerException;
|
13
|
import javax.xml.transform.TransformerFactory;
|
14
|
import javax.xml.transform.stream.StreamResult;
|
15
|
import javax.xml.transform.stream.StreamSource;
|
16
|
|
17
|
import org.apache.commons.io.IOUtils;
|
18
|
import org.junit.Test;
|
19
|
import org.springframework.core.io.ClassPathResource;
|
20
|
|
21
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
22
|
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
|
23
|
import eu.dnetlib.miscutils.functional.xml.IndentXmlString;
|
24
|
|
25
|
public class LayoutToRecordStylesheetTest {
|
26
|
|
27
|
private static final String LAYOUT_TO_RECORD_STYLESHEET_XSL = "/eu/dnetlib/msro/workflows/xslt/layoutToRecordStylesheet.xsl";
|
28
|
|
29
|
private static final String MDFORMAT_FIELDS = "/eu/dnetlib/msro/workflows/xslt/fields.xml";
|
30
|
|
31
|
private static final String OAF_RECORD = "/eu/dnetlib/msro/workflows/xslt/exampleRecord.xml";
|
32
|
|
33
|
@Test
|
34
|
public void test1() throws ISLookUpException, IOException, TransformerException {
|
35
|
String xsl = prepareXslt("DMF");
|
36
|
assertNotNull(xsl);
|
37
|
assertFalse(xsl.isEmpty());
|
38
|
|
39
|
System.out.println(xsl);
|
40
|
|
41
|
ApplyXslt xslt = new ApplyXslt(xsl);
|
42
|
|
43
|
String indexRecord = xslt.evaluate(readFromClasspath(OAF_RECORD));
|
44
|
|
45
|
assertNotNull(indexRecord);
|
46
|
assertFalse(indexRecord.isEmpty());
|
47
|
|
48
|
System.out.println(IndentXmlString.apply(indexRecord));
|
49
|
|
50
|
// StreamingInputDocumentFactory factory = new StreamingInputDocumentFactory();
|
51
|
//
|
52
|
// SolrInputDocument doc = factory.parseDocument(DateUtils.now_ISO8601(), indexRecord, "dsId", "dnetResult");
|
53
|
// assertNotNull(doc);
|
54
|
// assertFalse(doc.isEmpty());
|
55
|
|
56
|
// System.out.println(doc);
|
57
|
|
58
|
}
|
59
|
|
60
|
protected String prepareXslt(final String format) throws ISLookUpException, IOException, TransformerException {
|
61
|
|
62
|
final TransformerFactory factory = TransformerFactory.newInstance();
|
63
|
|
64
|
final Transformer layoutTransformer = factory.newTransformer(streamSource(LAYOUT_TO_RECORD_STYLESHEET_XSL));
|
65
|
|
66
|
final StreamResult layoutToXsltXslt = new StreamResult(new StringWriter());
|
67
|
|
68
|
layoutTransformer.setParameter("format", format);
|
69
|
layoutTransformer.transform(streamSource(MDFORMAT_FIELDS), layoutToXsltXslt);
|
70
|
|
71
|
String layoutToXsltXsltString = layoutToXsltXslt.getWriter().toString();
|
72
|
System.out.println(IndentXmlString.apply(layoutToXsltXsltString) + "\n\n\n");
|
73
|
|
74
|
return layoutToXsltXsltString;
|
75
|
}
|
76
|
|
77
|
private StreamSource streamSource(final String s) throws IOException {
|
78
|
return new StreamSource(new StringReader(readFromClasspath(s)));
|
79
|
}
|
80
|
|
81
|
private String readFromClasspath(final String s) throws IOException {
|
82
|
ClassPathResource resource = new ClassPathResource(s);
|
83
|
InputStream inputStream = resource.getInputStream();
|
84
|
return IOUtils.toString(inputStream);
|
85
|
}
|
86
|
|
87
|
}
|