1
0
Fork 0

code refactored

This commit is contained in:
sab 2024-08-01 09:52:19 +02:00
parent bbb79273a3
commit 53787dbf67
2 changed files with 24 additions and 16 deletions

View File

@ -1,18 +1,11 @@
package eu.dnetlib.dhp.transformation.xslt;
import com.mongodb.util.JSON;
import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import scala.Char;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* This class fetches JSON from a provided link and returns
* 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
*/
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 {
String json = IOUtils.toString(url);
@ -36,12 +47,9 @@ public class DataFetcher {
* @return
*/
static List<String> getAuthorsFromJson(JSONObject jsonObject) {
List<String> authors = new ArrayList<>();
// count of authors
int countOfAuthors = jsonObject.getJSONArray("data").length();
for (int i = 0; i < countOfAuthors; i++) {
authors.add(jsonObject
@ -52,12 +60,15 @@ public class DataFetcher {
.getJSONObject("data")
.getJSONObject("attributes")
.getString("full_name"));
}
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) {
List<String> dublinCoreAuthors = new ArrayList<>();
@ -73,7 +84,6 @@ public class DataFetcher {
dublinCoreAuthors.add(
"<dc:creator>" + lastName + ", " + initialOfFirstName + ". (" + firstName + ")" + "</dc:creator>");
}
return dublinCoreAuthors;
}

View File

@ -5,7 +5,6 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
@ -48,7 +47,6 @@ class DataFetcherTest {
JSONObject testJsonObj = DataFetcher.getJson(contributorsUrl);
List<String> authors = DataFetcher.getAuthorsFromJson(testJsonObj);
System.out.println(authors);
System.out.println(DataFetcher.transformListToDublinCore(authors));
}