Project

General

Profile

1
package eu.dnetlib.msro.openaireplus.workflows.index;
2

    
3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.io.StringReader;
6
import java.io.StringWriter;
7
import javax.xml.stream.XMLStreamException;
8
import javax.xml.transform.Transformer;
9
import javax.xml.transform.TransformerException;
10
import javax.xml.transform.TransformerFactory;
11
import javax.xml.transform.stream.StreamResult;
12
import javax.xml.transform.stream.StreamSource;
13

    
14
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
15
import eu.dnetlib.functionality.index.solr.feed.StreamingInputDocumentFactory;
16
import eu.dnetlib.miscutils.datetime.DateUtils;
17
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
18
import eu.dnetlib.miscutils.functional.xml.IndentXmlString;
19
import org.apache.commons.io.IOUtils;
20
import org.apache.solr.common.SolrInputDocument;
21
import org.junit.Test;
22
import org.springframework.core.io.ClassPathResource;
23

    
24
import static org.junit.Assert.assertFalse;
25
import static org.junit.Assert.assertNotNull;
26

    
27
public class OpenaireLayoutToRecordStylesheetTest {
28

    
29
	private static final String OPENAIRE_LAYOUT_TO_RECORD_STYLESHEET_XSL = "/eu/dnetlib/msro/openaireplus/workflows/index/openaireLayoutToRecordStylesheet.xsl";
30

    
31
	private static final String MDFORMAT_FIELDS = "/eu/dnetlib/msro/openaireplus/workflows/index/fields.xml";
32

    
33
	private static final String OAF_RECORD = "/eu/dnetlib/msro/openaireplus/workflows/index/oafRecord.xml";
34

    
35
	@Test
36
	public void test1() throws ISLookUpException, IOException, TransformerException, XMLStreamException {
37
		String xsl = prepareXslt("DMF");
38
		assertNotNull(xsl);
39
		assertFalse(xsl.isEmpty());
40

    
41
		System.out.println(xsl);
42

    
43
		ApplyXslt xslt = new ApplyXslt(xsl);
44

    
45
		String indexRecord = xslt.evaluate(readFromClasspath(OAF_RECORD));
46

    
47
		assertNotNull(indexRecord);
48
		assertFalse(indexRecord.isEmpty());
49

    
50
		System.out.println(IndentXmlString.apply(indexRecord));
51

    
52
		StreamingInputDocumentFactory factory = new StreamingInputDocumentFactory();
53

    
54
		SolrInputDocument doc = factory.parseDocument(DateUtils.now_ISO8601(), indexRecord, "dsId", "dnetResult");
55
		assertNotNull(doc);
56
		assertFalse(doc.isEmpty());
57

    
58
		// System.out.println(doc);
59

    
60
	}
61

    
62
	protected String prepareXslt(final String format) throws ISLookUpException, IOException, TransformerException {
63

    
64
		final TransformerFactory factory = TransformerFactory.newInstance();
65

    
66
		final Transformer layoutTransformer = factory.newTransformer(streamSource(OPENAIRE_LAYOUT_TO_RECORD_STYLESHEET_XSL));
67

    
68
		final StreamResult layoutToXsltXslt = new StreamResult(new StringWriter());
69

    
70
		layoutTransformer.setParameter("format", format);
71
		layoutTransformer.transform(streamSource(MDFORMAT_FIELDS), layoutToXsltXslt);
72

    
73
		return layoutToXsltXslt.getWriter().toString();
74
	}
75

    
76
	private StreamSource streamSource(final String s) throws IOException {
77
		return new StreamSource(new StringReader(readFromClasspath(s)));
78
	}
79

    
80
	private String readFromClasspath(final String s) throws IOException {
81
		ClassPathResource resource = new ClassPathResource(s);
82
		InputStream inputStream = resource.getInputStream();
83
		return IOUtils.toString(inputStream);
84
	}
85

    
86
}
    (1-1/1)