1
0
Fork 0

conversion to Dublin Core has been implemented

This commit is contained in:
sab 2024-08-01 01:23:04 +02:00
parent 7f39375ba8
commit bbb79273a3
2 changed files with 48 additions and 28 deletions

View File

@ -3,10 +3,13 @@ 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;
@ -27,38 +30,51 @@ public class DataFetcher {
}
/**
* This method extracts authors (contributors) from a given JSON
* This method extracts authors from a given JSON
*
* @param jsonObject
* @return
*/
static List<String> getContributorsFromJson(JSONObject jsonObject){
List<String> contributors;
static List<String> getAuthorsFromJson(JSONObject jsonObject) {
List<String> authors = new ArrayList<>();
// count of authors
int countAuthors = jsonObject.getJSONArray("data").length();
int countOfAuthors = jsonObject.getJSONArray("data").length();
for (int i = 0; i < countOfAuthors; i++) {
// jsonObject.getJSONArray("data").forEach(json1 -> System.out.println(json1));
// String x = testJsonObj
// .getJSONArray("data")
// .getJSONObject(0)
// .getJSONObject("embeds")
// .getJSONObject("users")
// .getJSONObject("data")
// .getJSONObject("attributes")
// .getString("full_name");
return null;
authors.add(jsonObject
.getJSONArray("data")
.getJSONObject(i)
.getJSONObject("embeds")
.getJSONObject("users")
.getJSONObject("data")
.getJSONObject("attributes")
.getString("full_name"));
}
return authors;
}
// transform list of authors into Dublin Core
static List<String> transformListToDublinCore(List<String> authors) {
// transform it into Dublin Core
private void transformToDublinCore(JSON jsonData) {
System.out.println(jsonData);
List<String> dublinCoreAuthors = new ArrayList<>();
for (String author : authors){
//splitting full name into first and last names according to OpenAIRE v3 guidelines at:
// https://guidelines.openaire.eu/en/latest/literature/field_creator.html
// surname, initials (first name) prefix.
String[] parts = author.split(" ");
String firstName = parts[0];
String lastName = parts[1];
char initialOfFirstName = firstName.charAt(0);
dublinCoreAuthors.add(
"<dc:creator>" + lastName + ", " + initialOfFirstName + ". (" + firstName + ")" + "</dc:creator>");
}
return dublinCoreAuthors;
}
}

View File

@ -1,18 +1,16 @@
package eu.dnetlib.dhp.transformation.xslt;
import org.json.JSONArray;
import org.json.JSONObject;
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;
import java.net.URL;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.*;
@ -39,13 +37,19 @@ class DataFetcherTest {
.getJSONObject("data")
.getJSONObject("attributes")
.getString("full_name");
System.out.println(x);
System.out.println(testJsonObj.getJSONArray("data").length());
testJsonObj.getJSONArray("data").forEach(json1 -> System.out.println(json1));
}
@Test
void getAuthorsFromJson() throws IOException, URISyntaxException {
URL contributorsUrl = new URI("https://api.osf.io/v2/preprints/mrwqb/contributors/?format=json").toURL();
JSONObject testJsonObj = DataFetcher.getJson(contributorsUrl);
List<String> authors = DataFetcher.getAuthorsFromJson(testJsonObj);
System.out.println(authors);
System.out.println(DataFetcher.transformListToDublinCore(authors));
}
}