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 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.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; 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 * @param jsonObject
* @return * @return
*/ */
static List<String> getContributorsFromJson(JSONObject jsonObject){ static List<String> getAuthorsFromJson(JSONObject jsonObject) {
List<String> contributors;
List<String> authors = new ArrayList<>();
// count of authors // 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)); authors.add(jsonObject
.getJSONArray("data")
.getJSONObject(i)
.getJSONObject("embeds")
// String x = testJsonObj .getJSONObject("users")
// .getJSONArray("data") .getJSONObject("data")
// .getJSONObject(0) .getJSONObject("attributes")
// .getJSONObject("embeds") .getString("full_name"));
// .getJSONObject("users")
// .getJSONObject("data")
// .getJSONObject("attributes")
// .getString("full_name");
return null;
} }
return authors;
}
// transform list of authors into Dublin Core
static List<String> transformListToDublinCore(List<String> authors) {
// transform it into Dublin Core List<String> dublinCoreAuthors = new ArrayList<>();
private void transformToDublinCore(JSON jsonData) { for (String author : authors){
System.out.println(jsonData);
//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; package eu.dnetlib.dhp.transformation.xslt;
import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.junit.jupiter.api.AfterEach; 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;
import java.net.URL; import java.net.URL;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@ -39,13 +37,19 @@ class DataFetcherTest {
.getJSONObject("data") .getJSONObject("data")
.getJSONObject("attributes") .getJSONObject("attributes")
.getString("full_name"); .getString("full_name");
System.out.println(x); System.out.println(x);
System.out.println(testJsonObj.getJSONArray("data").length()); System.out.println(testJsonObj.getJSONArray("data").length());
testJsonObj.getJSONArray("data").forEach(json1 -> System.out.println(json1)); 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));
} }
} }