forked from D-Net/dnet-hadoop
code adapted as per Michele's recommendations
This commit is contained in:
parent
53787dbf67
commit
df82f8beb9
|
@ -1,4 +1,8 @@
|
|||
package eu.dnetlib.dhp.transformation.xslt;
|
||||
|
||||
import java.io.Serializable;
|
||||
import net.sf.saxon.s9api.*;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.json.JSONObject;
|
||||
import java.io.IOException;
|
||||
|
@ -11,12 +15,8 @@ import java.util.List;
|
|||
* a Dublin Core. This functionality is particularly needed for OSF Preprints
|
||||
*/
|
||||
|
||||
/**
|
||||
* this method fetches JSON from a provided URL and returns it as JSON Object
|
||||
*/
|
||||
|
||||
|
||||
public class DataFetcher {
|
||||
public class DataFetcher implements ExtensionFunction, Serializable {
|
||||
|
||||
/**
|
||||
* this method fetches JSON from a provided URL and returns it as Dublin Core
|
||||
|
@ -30,7 +30,7 @@ public class DataFetcher {
|
|||
|
||||
/**
|
||||
* This method fetches JSON object from a given URL
|
||||
* @param url
|
||||
* @param url a url in the metadata for fetching authors in JSON format
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
|
@ -87,4 +87,32 @@ public class DataFetcher {
|
|||
}
|
||||
return dublinCoreAuthors;
|
||||
}
|
||||
|
||||
/**
|
||||
* todo: add documentation
|
||||
*/
|
||||
public static String getAndTransformAuthors(URL url) throws IOException{
|
||||
return String.join(", ", transformListToDublinCore(getAuthorsFromJson(getJson(url))));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public QName getName() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SequenceType getResultType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SequenceType[] getArgumentTypes() {
|
||||
return new SequenceType[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public XdmValue call(XdmValue[] xdmValues) throws SaxonApiException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -55,6 +55,8 @@ public class XSLTTransformationFunction implements MapFunction<MetadataRecord, M
|
|||
processor.registerExtensionFunction(new DateCleaner());
|
||||
processor.registerExtensionFunction(new PersonCleaner());
|
||||
|
||||
processor.registerExtensionFunction(new DataFetcher());
|
||||
|
||||
final XsltCompiler comp = processor.newXsltCompiler();
|
||||
QName datasourceIDParam = new QName(DATASOURCE_ID_PARAM);
|
||||
comp.setParameter(datasourceIDParam, new XdmAtomicValue(value.getProvenance().getDatasourceId()));
|
||||
|
|
|
@ -38,7 +38,7 @@ class DataFetcherTest {
|
|||
.getString("full_name");
|
||||
System.out.println(x);
|
||||
System.out.println(testJsonObj.getJSONArray("data").length());
|
||||
testJsonObj.getJSONArray("data").forEach(json1 -> System.out.println(json1));
|
||||
testJsonObj.getJSONArray("data").forEach(System.out::println);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -48,6 +48,13 @@ class DataFetcherTest {
|
|||
List<String> authors = DataFetcher.getAuthorsFromJson(testJsonObj);
|
||||
System.out.println(authors);
|
||||
System.out.println(DataFetcher.transformListToDublinCore(authors));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAndTransformAuthors() throws IOException, URISyntaxException {
|
||||
URL contributorsUrl = new URI("https://api.osf.io/v2/preprints/mrwqb/contributors/?format=json").toURL();
|
||||
System.out.println(DataFetcher.getAndTransformAuthors(contributorsUrl));
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue