Project

General

Profile

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.net.MalformedURLException;
8
import java.net.URL;
9
import java.util.Arrays;
10
import java.util.Iterator;
11
import java.util.function.Function;
12
import java.util.function.IntFunction;
13

    
14
import gr.forth.ics.isl.x3ml.X3MLEngineFactory;
15
import gr.forth.ics.isl.x3ml.X3MLEngineFactory.OutputFormat;
16
import org.apache.commons.logging.Log;
17
import org.apache.commons.logging.LogFactory;
18
import org.junit.Ignore;
19
import org.junit.Test;
20
import org.springframework.core.io.ClassPathResource;
21

    
22
/**
23
 * Created by alessia on 31/01/17.
24
 */
25

    
26
public class X3MLEngineTest {
27

    
28
	private static final Log log = LogFactory.getLog(X3MLEngineTest.class);
29

    
30
	final String recordPath = "/eu/dnetlib/x3m/new-10304736.xml";
31
	final String mappingPath = "/eu/dnetlib/x3m/mappings.x3ml";
32
	final String policyPath = "/eu/dnetlib/x3m/maria-policy.xml";
33

    
34
	final String forthInputPath ="/eu/dnetlib/x3m/input.xml";
35
	final String forthMappingPath = "/eu/dnetlib/x3m/mappingsWithoutGenerator.x3ml";
36

    
37
	final String[] allInputFiles =
38
			new String[] { recordPath, "/eu/dnetlib/x3m/new-10304737.xml", "/eu/dnetlib/x3m/new-10304738.xml", "/eu/dnetlib/x3m/new-10304739.xml",
39
					"/eu/dnetlib/x3m/new-10304740.xml", "/eu/dnetlib/x3m/new-10304741.xml", "/eu/dnetlib/x3m/new-10304742.xml" };
40

    
41
	@Test
42
	public void testWithURLMapping() throws MalformedURLException {
43
		X3MLEngineFactory x3mEngine = X3MLEngineFactory.create()
44
				.withMappings(new URL("https://mapping.d4science.org/3MEditor/Services?id=575&output=text/xml&method=export"))
45
				.withVerboseLogging()
46
				.withGeneratorPolicy(getInputStreamFromClasspath("/eu/dnetlib/x3m/ariadne_policy.xml"))
47
				.withInput(getInputStreamFromClasspath("/eu/dnetlib/x3m/ariadneADSsample.xml"))
48
				.withOutput(System.out, OutputFormat.RDF_XML);
49
		x3mEngine.execute();
50
	}
51

    
52
	@Test
53
	public void testForth() throws Exception{
54
		X3MLEngineFactory x3mEngine = X3MLEngineFactory.create()
55
				.withMappings(getInputStreamFromClasspath(forthMappingPath))
56
				.withVerboseLogging()
57
				.withInput(getInputStreamFromClasspath(forthInputPath))
58
				.withOutput(System.out, OutputFormat.RDF_XML);
59
		x3mEngine.execute();
60
	}
61

    
62
	@Test
63
	public void testURIShortener() throws Exception{
64
		X3MLEngineFactory x3mEngine = X3MLEngineFactory.create()
65
				.withMappings(getInputStreamFromClasspath("/eu/dnetlib/x3m/mapping464_with_shortener.x3ml"))
66
				.withGeneratorPolicy(getInputStreamFromClasspath("/eu/dnetlib/x3m/parthenos_policy_uri_shortener.xml"))
67
				.withVerboseLogging()
68
				.withInput(getInputStreamFromClasspath("/eu/dnetlib/x3m/record_shortener.xml"))
69
				.withOutput(System.out, OutputFormat.RDF_XML);
70
		x3mEngine.execute();
71
	}
72

    
73
	@Test
74
	public void test() throws Exception {
75
		final ByteArrayOutputStream os = new ByteArrayOutputStream();
76
		final PrintStream ps = new PrintStream(os);
77

    
78
		X3MLEngineFactory x3mEngine = X3MLEngineFactory.create().withGeneratorPolicy(getInputStreamFromClasspath(policyPath))
79
				.withMappings(getInputStreamFromClasspath(mappingPath))
80
				.withVerboseLogging()
81
				.withInput(getInputStreamFromClasspath(recordPath))
82
				.withOutput(ps, OutputFormat.RDF_XML);
83
		x3mEngine.execute();
84

    
85
		logStream(os);
86

    
87
	}
88

    
89
	@Test
90
	public void testRDFPlain() throws Exception {
91
		final ByteArrayOutputStream os = new ByteArrayOutputStream();
92
		final PrintStream ps = new PrintStream(os);
93

    
94
		X3MLEngineFactory x3mEngine = X3MLEngineFactory.create().withGeneratorPolicy(getInputStreamFromClasspath(policyPath))
95
				.withMappings(getInputStreamFromClasspath(mappingPath))
96
				.withVerboseLogging()
97
				.withInput(getInputStreamFromClasspath(recordPath))
98
				.withOutput(ps, OutputFormat.RDF_XML_PLAIN);
99
		x3mEngine.execute();
100

    
101
		logStream(os);
102

    
103
	}
104

    
105

    
106
	@Test
107
	public void testAll() throws Exception {
108
		final ByteArrayOutputStream os = new ByteArrayOutputStream();
109
		final PrintStream ps = new PrintStream(os);
110
		X3MLEngineFactory x3mEngine = X3MLEngineFactory.create().withGeneratorPolicy(getInputStreamFromClasspath(policyPath))
111
				.withMappings(getInputStreamFromClasspath(mappingPath))
112
				.withVerboseLogging()
113
				.withInput(convertArray(allInputFiles, X3MLEngineTest::getInputStreamFromClasspath, InputStream[]::new))
114
				.withOutput(ps, OutputFormat.RDF_XML);
115
		x3mEngine.execute();
116
		logStream(os);
117
	}
118

    
119
	@Test
120
	@Ignore
121
	/**
122
	 * Test if it is possible to re-use the same X3MLEngineFactory to transform different XMLs.
123
	 * The test fails with an exception when trying to parse the mapping file for the second time.
124
	 */
125
	public void testAllIteratorReuse() throws Exception {
126
		Iterator<InputStream> it = convertIterator(allInputFiles, X3MLEngineTest::getInputStreamFromClasspath);
127
		X3MLEngineFactory x3mEngine = X3MLEngineFactory.create().withGeneratorPolicy(getInputStreamFromClasspath(policyPath))
128
				.withMappings(getInputStreamFromClasspath(mappingPath))
129
				.withVerboseLogging();
130
		int count =0;
131
		while(it.hasNext()){
132
			count++;
133
			System.out.println("Transforming file number "+count);
134
			final ByteArrayOutputStream os = new ByteArrayOutputStream();
135
			final PrintStream ps = new PrintStream(os);
136
			InputStream s = it.next();
137
			x3mEngine.withInput(s).withOutput(ps, OutputFormat.RDF_XML);
138
			x3mEngine.execute();
139
			logStream(os);
140
			os.close();
141
		}
142
	}
143

    
144
	@Test
145
	public void testAllIterator() throws Exception {
146
		Iterator<InputStream> it = convertIterator(allInputFiles, X3MLEngineTest::getInputStreamFromClasspath);
147

    
148
		int count =0;
149
		while(it.hasNext()){
150
			count++;
151
			System.out.println("Transforming file number "+count);
152
			final ByteArrayOutputStream os = new ByteArrayOutputStream();
153
			final PrintStream ps = new PrintStream(os);
154
			X3MLEngineFactory x3mEngine = X3MLEngineFactory.create().withGeneratorPolicy(getInputStreamFromClasspath(policyPath))
155
					.withMappings(getInputStreamFromClasspath(mappingPath))
156
					.withVerboseLogging();
157
			InputStream s = it.next();
158
			x3mEngine.withInput(s).withOutput(ps, OutputFormat.RDF_XML).execute();
159
			logStream(os);
160
			os.close();
161
		}
162
	}
163

    
164
	private void logStream(final ByteArrayOutputStream os){
165
		log.info("**************Result:\n");
166
		String s = new String(os.toByteArray());
167
		log.info(s);
168
	}
169

    
170
	public static InputStream getInputStreamFromClasspath(final String s) {
171
		try {
172
			final ClassPathResource resource = new ClassPathResource(s);
173
			return resource.getInputStream();
174
		}catch(IOException e){
175
			return null;
176
		}
177
	}
178

    
179
	public static <T, U> U[] convertArray(T[] from,
180
			Function<T, U> func,
181
			IntFunction<U[]> generator) {
182
		return Arrays.stream(from).map(func).toArray(generator);
183
	}
184

    
185
	public static <T, U> Iterator<U> convertIterator(T[] from,
186
			Function<T, U> func) {
187
		return Arrays.stream(from).map(func).iterator();
188
	}
189
}
    (1-1/1)