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