2019-06-18 16:15:23 +02:00
|
|
|
package eu.dnetlib.x3m;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2019-10-31 14:08:37 +01:00
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
2019-06-18 16:15:23 +02:00
|
|
|
|
|
|
|
import eu.dnetlib.msro.workflows.nodes.transform.ApplyX3Mapping;
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.dom4j.Document;
|
|
|
|
import org.dom4j.DocumentException;
|
|
|
|
import org.dom4j.io.SAXReader;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.springframework.core.io.ClassPathResource;
|
|
|
|
|
|
|
|
public class ApplyX3MappingTest {
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(ApplyX3MappingTest.class);
|
|
|
|
|
|
|
|
private static String basePathProfiles = "/eu/dnetlib/bootstrap/profiles/TransformationRuleDSResources/TransformationRuleDSResourceType/";
|
|
|
|
|
2019-10-31 14:08:37 +01:00
|
|
|
private String mappingUrl = "https://mapping.d4science.org/3MEditor/Services?id=575&output=text/xml&method=export";
|
2019-06-18 16:15:23 +02:00
|
|
|
|
2019-10-31 14:08:37 +01:00
|
|
|
//Ariadne policy
|
|
|
|
final String ariadnePolicyPath = "ARIADNE_Policy.xml";
|
2019-06-18 16:15:23 +02:00
|
|
|
|
2019-10-31 14:08:37 +01:00
|
|
|
//ADS record
|
|
|
|
final String adsRecordPath = "/eu/dnetlib/x3m/ads-record.xml";
|
2019-06-18 16:15:23 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testAriadne() {
|
2019-10-31 14:08:37 +01:00
|
|
|
doBasicTest(ariadnePolicyPath, adsRecordPath);
|
2019-06-18 16:15:23 +02:00
|
|
|
}
|
|
|
|
|
2019-10-31 14:08:37 +01:00
|
|
|
public void doBasicTest(String policyPath, String recordPath){
|
2019-06-18 16:15:23 +02:00
|
|
|
String g = loadFromTransformationProfile(policyPath);
|
|
|
|
String r = getString(recordPath);
|
2019-10-31 14:08:37 +01:00
|
|
|
|
|
|
|
URL mappingURL = null;
|
|
|
|
try {
|
|
|
|
mappingURL = new URL(mappingUrl);
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
ApplyX3Mapping x3m = new ApplyX3Mapping(mappingURL, g, true);
|
2019-06-18 16:15:23 +02:00
|
|
|
String res = x3m.apply(r);
|
|
|
|
System.out.println("res is \n"+res);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String getString(final String classpath) {
|
|
|
|
try {
|
|
|
|
final ClassPathResource resource = new ClassPathResource(classpath);
|
|
|
|
return IOUtils.toString(resource.getInputStream(), "UTF-8");
|
|
|
|
}catch(IOException e){
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String loadFromTransformationProfile(String profilePath){
|
|
|
|
log.info("Loading xslt from: " + basePathProfiles + profilePath);
|
|
|
|
InputStream profile = getClass().getResourceAsStream(basePathProfiles + profilePath);
|
|
|
|
SAXReader saxReader = new SAXReader();
|
|
|
|
Document doc = null;
|
|
|
|
try {
|
|
|
|
doc = saxReader.read(profile);
|
|
|
|
} catch (DocumentException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
String s = doc.selectSingleNode("//SCRIPT/CODE").getText();
|
|
|
|
//log.debug(profilePath+": "+s);
|
|
|
|
return s;
|
|
|
|
}
|
2019-07-12 18:12:49 +02:00
|
|
|
}
|