1
|
package eu.dnetlib.x3m;
|
2
|
|
3
|
import java.io.ByteArrayOutputStream;
|
4
|
import java.io.IOException;
|
5
|
import java.io.InputStream;
|
6
|
import java.io.PrintStream;
|
7
|
import java.util.Arrays;
|
8
|
import java.util.Iterator;
|
9
|
import java.util.function.Function;
|
10
|
import java.util.function.IntFunction;
|
11
|
|
12
|
import gr.forth.ics.isl.x3ml.X3MLEngineFactory;
|
13
|
import gr.forth.ics.isl.x3ml.X3MLEngineFactory.OutputFormat;
|
14
|
import org.apache.commons.logging.Log;
|
15
|
import org.apache.commons.logging.LogFactory;
|
16
|
import org.junit.Ignore;
|
17
|
import org.junit.Test;
|
18
|
import org.springframework.core.io.ClassPathResource;
|
19
|
|
20
|
/**
|
21
|
* Created by alessia on 31/01/17.
|
22
|
*/
|
23
|
|
24
|
public class X3MLEngineTest {
|
25
|
|
26
|
private static final Log log = LogFactory.getLog(X3MLEngineTest.class);
|
27
|
|
28
|
final String recordPath = "/eu/dnetlib/x3m/new-10304736.xml";
|
29
|
final String mappingPath = "/eu/dnetlib/x3m/mappings.x3ml";
|
30
|
final String policyPath = "/eu/dnetlib/x3m/maria-policy.xml";
|
31
|
|
32
|
final String forthInputPath ="/eu/dnetlib/x3m/input.xml";
|
33
|
final String forthMappingPath = "/eu/dnetlib/x3m/mappingsWithoutGenerator.x3ml";
|
34
|
|
35
|
final String[] allInputFiles =
|
36
|
new String[] { recordPath, "/eu/dnetlib/x3m/new-10304737.xml", "/eu/dnetlib/x3m/new-10304738.xml", "/eu/dnetlib/x3m/new-10304739.xml",
|
37
|
"/eu/dnetlib/x3m/new-10304740.xml", "/eu/dnetlib/x3m/new-10304741.xml", "/eu/dnetlib/x3m/new-10304742.xml" };
|
38
|
|
39
|
@Test
|
40
|
public void testForth() throws Exception{
|
41
|
X3MLEngineFactory x3mEngine = X3MLEngineFactory.create()
|
42
|
.withMappings(getInputStreamFromClasspath(forthMappingPath))
|
43
|
.withVerboseLogging()
|
44
|
.withInput(getInputStreamFromClasspath(forthInputPath))
|
45
|
.withOutput(System.out, OutputFormat.RDF_XML);
|
46
|
x3mEngine.execute();
|
47
|
}
|
48
|
|
49
|
@Test
|
50
|
public void testURIShortener() throws Exception{
|
51
|
X3MLEngineFactory x3mEngine = X3MLEngineFactory.create()
|
52
|
.withMappings(getInputStreamFromClasspath("/eu/dnetlib/x3m/mapping464_with_shortener.x3ml"))
|
53
|
.withGeneratorPolicy(getInputStreamFromClasspath("/eu/dnetlib/x3m/parthenos_policy_uri_shortener.xml"))
|
54
|
.withVerboseLogging()
|
55
|
.withInput(getInputStreamFromClasspath("/eu/dnetlib/x3m/record_shortener.xml"))
|
56
|
.withOutput(System.out, OutputFormat.RDF_XML);
|
57
|
x3mEngine.execute();
|
58
|
}
|
59
|
|
60
|
@Test
|
61
|
public void test() throws Exception {
|
62
|
final ByteArrayOutputStream os = new ByteArrayOutputStream();
|
63
|
final PrintStream ps = new PrintStream(os);
|
64
|
|
65
|
X3MLEngineFactory x3mEngine = X3MLEngineFactory.create().withGeneratorPolicy(getInputStreamFromClasspath(policyPath))
|
66
|
.withMappings(getInputStreamFromClasspath(mappingPath))
|
67
|
.withVerboseLogging()
|
68
|
.withInput(getInputStreamFromClasspath(recordPath))
|
69
|
.withOutput(ps, OutputFormat.RDF_XML);
|
70
|
x3mEngine.execute();
|
71
|
|
72
|
logStream(os);
|
73
|
|
74
|
}
|
75
|
|
76
|
@Test
|
77
|
public void testRDFPlain() throws Exception {
|
78
|
final ByteArrayOutputStream os = new ByteArrayOutputStream();
|
79
|
final PrintStream ps = new PrintStream(os);
|
80
|
|
81
|
X3MLEngineFactory x3mEngine = X3MLEngineFactory.create().withGeneratorPolicy(getInputStreamFromClasspath(policyPath))
|
82
|
.withMappings(getInputStreamFromClasspath(mappingPath))
|
83
|
.withVerboseLogging()
|
84
|
.withInput(getInputStreamFromClasspath(recordPath))
|
85
|
.withOutput(ps, OutputFormat.RDF_XML_PLAIN);
|
86
|
x3mEngine.execute();
|
87
|
|
88
|
logStream(os);
|
89
|
|
90
|
}
|
91
|
|
92
|
|
93
|
@Test
|
94
|
public void testAll() throws Exception {
|
95
|
final ByteArrayOutputStream os = new ByteArrayOutputStream();
|
96
|
final PrintStream ps = new PrintStream(os);
|
97
|
X3MLEngineFactory x3mEngine = X3MLEngineFactory.create().withGeneratorPolicy(getInputStreamFromClasspath(policyPath))
|
98
|
.withMappings(getInputStreamFromClasspath(mappingPath))
|
99
|
.withVerboseLogging()
|
100
|
.withInput(convertArray(allInputFiles, X3MLEngineTest::getInputStreamFromClasspath, InputStream[]::new))
|
101
|
.withOutput(ps, OutputFormat.RDF_XML);
|
102
|
x3mEngine.execute();
|
103
|
logStream(os);
|
104
|
}
|
105
|
|
106
|
@Test
|
107
|
@Ignore
|
108
|
/**
|
109
|
* Test if it is possible to re-use the same X3MLEngineFactory to transform different XMLs.
|
110
|
* The test fails with an exception when trying to parse the mapping file for the second time.
|
111
|
*/
|
112
|
public void testAllIteratorReuse() throws Exception {
|
113
|
Iterator<InputStream> it = convertIterator(allInputFiles, X3MLEngineTest::getInputStreamFromClasspath);
|
114
|
X3MLEngineFactory x3mEngine = X3MLEngineFactory.create().withGeneratorPolicy(getInputStreamFromClasspath(policyPath))
|
115
|
.withMappings(getInputStreamFromClasspath(mappingPath))
|
116
|
.withVerboseLogging();
|
117
|
int count =0;
|
118
|
while(it.hasNext()){
|
119
|
count++;
|
120
|
System.out.println("Transforming file number "+count);
|
121
|
final ByteArrayOutputStream os = new ByteArrayOutputStream();
|
122
|
final PrintStream ps = new PrintStream(os);
|
123
|
InputStream s = it.next();
|
124
|
x3mEngine.withInput(s).withOutput(ps, OutputFormat.RDF_XML);
|
125
|
x3mEngine.execute();
|
126
|
logStream(os);
|
127
|
os.close();
|
128
|
}
|
129
|
}
|
130
|
|
131
|
@Test
|
132
|
public void testAllIterator() throws Exception {
|
133
|
Iterator<InputStream> it = convertIterator(allInputFiles, X3MLEngineTest::getInputStreamFromClasspath);
|
134
|
|
135
|
int count =0;
|
136
|
while(it.hasNext()){
|
137
|
count++;
|
138
|
System.out.println("Transforming file number "+count);
|
139
|
final ByteArrayOutputStream os = new ByteArrayOutputStream();
|
140
|
final PrintStream ps = new PrintStream(os);
|
141
|
X3MLEngineFactory x3mEngine = X3MLEngineFactory.create().withGeneratorPolicy(getInputStreamFromClasspath(policyPath))
|
142
|
.withMappings(getInputStreamFromClasspath(mappingPath))
|
143
|
.withVerboseLogging();
|
144
|
InputStream s = it.next();
|
145
|
x3mEngine.withInput(s).withOutput(ps, OutputFormat.RDF_XML).execute();
|
146
|
logStream(os);
|
147
|
os.close();
|
148
|
}
|
149
|
}
|
150
|
|
151
|
private void logStream(final ByteArrayOutputStream os){
|
152
|
log.info("**************Result:\n");
|
153
|
String s = new String(os.toByteArray());
|
154
|
log.info(s);
|
155
|
}
|
156
|
|
157
|
public static InputStream getInputStreamFromClasspath(final String s) {
|
158
|
try {
|
159
|
final ClassPathResource resource = new ClassPathResource(s);
|
160
|
return resource.getInputStream();
|
161
|
}catch(IOException e){
|
162
|
return null;
|
163
|
}
|
164
|
}
|
165
|
|
166
|
public static <T, U> U[] convertArray(T[] from,
|
167
|
Function<T, U> func,
|
168
|
IntFunction<U[]> generator) {
|
169
|
return Arrays.stream(from).map(func).toArray(generator);
|
170
|
}
|
171
|
|
172
|
public static <T, U> Iterator<U> convertIterator(T[] from,
|
173
|
Function<T, U> func) {
|
174
|
return Arrays.stream(from).map(func).iterator();
|
175
|
}
|
176
|
}
|