forked from D-Net/dnet-hadoop
code refactored
This commit is contained in:
parent
bbb79273a3
commit
53787dbf67
|
@ -1,18 +1,11 @@
|
||||||
package eu.dnetlib.dhp.transformation.xslt;
|
package eu.dnetlib.dhp.transformation.xslt;
|
||||||
|
|
||||||
import com.mongodb.util.JSON;
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import scala.Char;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class fetches JSON from a provided link and returns
|
* This class fetches JSON from a provided link and returns
|
||||||
* a Dublin Core. This functionality is particularly needed for OSF Preprints
|
* a Dublin Core. This functionality is particularly needed for OSF Preprints
|
||||||
|
@ -21,8 +14,26 @@ import java.util.List;
|
||||||
/**
|
/**
|
||||||
* this method fetches JSON from a provided URL and returns it as JSON Object
|
* this method fetches JSON from a provided URL and returns it as JSON Object
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
public class DataFetcher {
|
public class DataFetcher {
|
||||||
// fetch data
|
|
||||||
|
/**
|
||||||
|
* this method fetches JSON from a provided URL and returns it as Dublin Core
|
||||||
|
*/
|
||||||
|
public static List<String> fetchAndTransform(URL url) throws IOException {
|
||||||
|
|
||||||
|
JSONObject jsonObject = getJson(url);
|
||||||
|
List<String> authors = getAuthorsFromJson(jsonObject);
|
||||||
|
return transformListToDublinCore(authors);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method fetches JSON object from a given URL
|
||||||
|
* @param url
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
static JSONObject getJson(URL url) throws IOException {
|
static JSONObject getJson(URL url) throws IOException {
|
||||||
|
|
||||||
String json = IOUtils.toString(url);
|
String json = IOUtils.toString(url);
|
||||||
|
@ -36,12 +47,9 @@ public class DataFetcher {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
static List<String> getAuthorsFromJson(JSONObject jsonObject) {
|
static List<String> getAuthorsFromJson(JSONObject jsonObject) {
|
||||||
|
|
||||||
List<String> authors = new ArrayList<>();
|
List<String> authors = new ArrayList<>();
|
||||||
|
|
||||||
// count of authors
|
// count of authors
|
||||||
int countOfAuthors = jsonObject.getJSONArray("data").length();
|
int countOfAuthors = jsonObject.getJSONArray("data").length();
|
||||||
|
|
||||||
for (int i = 0; i < countOfAuthors; i++) {
|
for (int i = 0; i < countOfAuthors; i++) {
|
||||||
|
|
||||||
authors.add(jsonObject
|
authors.add(jsonObject
|
||||||
|
@ -52,12 +60,15 @@ public class DataFetcher {
|
||||||
.getJSONObject("data")
|
.getJSONObject("data")
|
||||||
.getJSONObject("attributes")
|
.getJSONObject("attributes")
|
||||||
.getString("full_name"));
|
.getString("full_name"));
|
||||||
|
|
||||||
}
|
}
|
||||||
return authors;
|
return authors;
|
||||||
}
|
}
|
||||||
|
|
||||||
// transform list of authors into Dublin Core
|
/**
|
||||||
|
* This method transforms list of authors into Dublin Core
|
||||||
|
* @param authors
|
||||||
|
* @return Dublin Core list of authors
|
||||||
|
*/
|
||||||
static List<String> transformListToDublinCore(List<String> authors) {
|
static List<String> transformListToDublinCore(List<String> authors) {
|
||||||
|
|
||||||
List<String> dublinCoreAuthors = new ArrayList<>();
|
List<String> dublinCoreAuthors = new ArrayList<>();
|
||||||
|
@ -73,7 +84,6 @@ public class DataFetcher {
|
||||||
|
|
||||||
dublinCoreAuthors.add(
|
dublinCoreAuthors.add(
|
||||||
"<dc:creator>" + lastName + ", " + initialOfFirstName + ". (" + firstName + ")" + "</dc:creator>");
|
"<dc:creator>" + lastName + ", " + initialOfFirstName + ". (" + firstName + ")" + "</dc:creator>");
|
||||||
|
|
||||||
}
|
}
|
||||||
return dublinCoreAuthors;
|
return dublinCoreAuthors;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import javax.xml.crypto.Data;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
@ -48,7 +47,6 @@ class DataFetcherTest {
|
||||||
JSONObject testJsonObj = DataFetcher.getJson(contributorsUrl);
|
JSONObject testJsonObj = DataFetcher.getJson(contributorsUrl);
|
||||||
List<String> authors = DataFetcher.getAuthorsFromJson(testJsonObj);
|
List<String> authors = DataFetcher.getAuthorsFromJson(testJsonObj);
|
||||||
System.out.println(authors);
|
System.out.println(authors);
|
||||||
|
|
||||||
System.out.println(DataFetcher.transformListToDublinCore(authors));
|
System.out.println(DataFetcher.transformListToDublinCore(authors));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue