1
|
package eu.dnetlib.msro.openaireplus.utils;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.StringReader;
|
5
|
import java.io.StringWriter;
|
6
|
import javax.annotation.Resource;
|
7
|
import javax.xml.transform.Transformer;
|
8
|
import javax.xml.transform.TransformerException;
|
9
|
import javax.xml.transform.TransformerFactory;
|
10
|
import javax.xml.transform.stream.StreamResult;
|
11
|
import javax.xml.transform.stream.StreamSource;
|
12
|
|
13
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
|
14
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
15
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
16
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
17
|
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
|
18
|
import org.apache.commons.io.IOUtils;
|
19
|
import org.springframework.beans.factory.annotation.Required;
|
20
|
import org.springframework.core.io.ClassPathResource;
|
21
|
|
22
|
/**
|
23
|
* Created by michele on 15/12/15.
|
24
|
*/
|
25
|
public class OafToIndexRecordFactory {
|
26
|
|
27
|
private ClassPathResource layoutToRecord;
|
28
|
|
29
|
@Resource
|
30
|
private UniqueServiceLocator serviceLocator;
|
31
|
|
32
|
public ApplyXslt newTransformer(final String format) throws ISLookUpException, IOException, TransformerException {
|
33
|
final TransformerFactory factory = TransformerFactory.newInstance();
|
34
|
final Transformer layoutTransformer = factory.newTransformer(readLayoutToRecord());
|
35
|
|
36
|
final StreamResult layoutToXsltXslt = new StreamResult(new StringWriter());
|
37
|
|
38
|
layoutTransformer.setParameter("format", format);
|
39
|
layoutTransformer.transform(new StreamSource(new StringReader(getLayoutSource(format))), layoutToXsltXslt);
|
40
|
|
41
|
return new ApplyXslt(layoutToXsltXslt.getWriter().toString());
|
42
|
}
|
43
|
|
44
|
private StreamSource readLayoutToRecord() throws IOException {
|
45
|
return new StreamSource(new StringReader(IOUtils.toString(layoutToRecord.getInputStream())));
|
46
|
}
|
47
|
|
48
|
private String getLayoutSource(final String format) throws ISLookUpDocumentNotFoundException, ISLookUpException {
|
49
|
return serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(
|
50
|
"collection('/db/DRIVER/MDFormatDSResources/MDFormatDSResourceType')[.//NAME='" + format + "']//LAYOUT[@name='index']");
|
51
|
}
|
52
|
|
53
|
public ClassPathResource getLayoutToRecord() {
|
54
|
return layoutToRecord;
|
55
|
}
|
56
|
|
57
|
@Required
|
58
|
public void setLayoutToRecord(final ClassPathResource layoutToRecord) {
|
59
|
this.layoutToRecord = layoutToRecord;
|
60
|
}
|
61
|
|
62
|
}
|