Skip to content

Commit

Permalink
#61 Add 'Funding Reference' to DataCite Metadata XML (Second draft)
Browse files Browse the repository at this point in the history
Add 'Funding Reference' to DataCite Metadata XML using:
- Java DOM Parser
- Extra prettyPrint method
- Inner Class DataCiteMetadataUtil
  • Loading branch information
helkv committed Aug 28, 2023
1 parent d84e8e5 commit c14c48a
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,34 @@

import edu.harvard.iq.dataverse.branding.BrandingUtil;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import edu.harvard.iq.dataverse.settings.JvmSettings;
import org.apache.commons.text.StringEscapeUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
*
Expand Down Expand Up @@ -561,6 +566,30 @@ public String generateXML(DvObject dvObject) {
xmlMetadata = xmlMetadata.replace("${relatedIdentifiers}", relIdentifiers);

xmlMetadata = xmlMetadata.replace("{$contributors}", contributorsElement.toString());

xmlMetadata = extendXmlMetadata(dvObject, xmlMetadata);

return xmlMetadata;
}

private String extendXmlMetadata(DvObject dvObject, String xmlMetadata) {
try {
Optional<String> grantAgency = DataCiteMetadataUtil.readDatasetFieldValue(dvObject, DatasetFieldConstant.grantNumber, DatasetFieldConstant.grantNumberAgency);
Optional<String> grantNumber = DataCiteMetadataUtil.readDatasetFieldValue(dvObject, DatasetFieldConstant.grantNumber, DatasetFieldConstant.grantNumberValue);

if(grantAgency.isPresent() || grantNumber.isPresent()) {
org.w3c.dom.Document xmlDocument = DataCiteMetadataUtil.parseXml(xmlMetadata);
DataCiteMetadataUtil.appendElementToDocument(xmlDocument, "resource", "fundingReferences", null);
DataCiteMetadataUtil.appendElementToDocument(xmlDocument, "fundingReferences", "fundingReference", null);
grantAgency.ifPresent(grantAgencyValue -> DataCiteMetadataUtil.appendElementToDocument(xmlDocument, "fundingReference", "funderName", grantAgencyValue));
grantNumber.ifPresent(grantNumberValue -> DataCiteMetadataUtil.appendElementToDocument(xmlDocument, "fundingReference", "awardNumber", grantNumberValue));

xmlMetadata = DataCiteMetadataUtil.prettyPrintXML(xmlDocument, 4);
}
} catch(Exception e) {
logger.log(Level.SEVERE, "Error extending xmlMetadata: {0}", e.getMessage());
}

return xmlMetadata;
}

Expand Down Expand Up @@ -726,3 +755,58 @@ public static String getStrFromList(List<String> authors) {
}

}

class DataCiteMetadataUtil {

public static org.w3c.dom.Document parseXml(String xml) throws ParserConfigurationException, IOException, SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document document = builder.parse(new InputSource(new StringReader(xml)));

return document;
}

public static void appendElementToDocument(org.w3c.dom.Document document, String parentTagName, String tagName, String textContent) {
org.w3c.dom.Element element = document.createElement(tagName);
if(textContent != null && !textContent.isEmpty()) {
element.setTextContent(textContent);
}
org.w3c.dom.Element parentElement = (org.w3c.dom.Element) document.getElementsByTagName(parentTagName).item(0);
if(parentElement != null){
parentElement.appendChild(element);
}
}

public static String prettyPrintXML(org.w3c.dom.Document document, int indent) throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
InputStream inputStream = DataCiteMetadataTemplate.class.getResourceAsStream("prettyprint.xsl");
String prettyPrintXsl = Util.readAndClose(inputStream, "utf-8");
Transformer transformer = transformerFactory.newTransformer(new StreamSource(new StringReader(prettyPrintXsl)));
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));
transformer.setOutputProperty(OutputKeys.STANDALONE, "no");

StringWriter stringWriter = new StringWriter();
transformer.transform(new DOMSource(document), new StreamResult(stringWriter));
return stringWriter.toString();
}

public static Optional<String> readDatasetFieldValue(DvObject dvObject, String parentFieldName, String fieldName) {
if (dvObject.isInstanceofDataset()) {
Dataset dataset = (Dataset) dvObject;
for (DatasetField field : dataset.getLatestVersion().getDatasetFields()) {
if (field.getDatasetFieldType().getName().equals(parentFieldName)) {
for (DatasetFieldCompoundValue compoundValue : field.getDatasetFieldCompoundValues()) {
for (DatasetField child : compoundValue.getChildDatasetFields()) {
if (child.getDatasetFieldType().getName().equals(fieldName)) {
return Optional.of(child.getValue());
}
}
}
}
}
}
return Optional.empty();
}
}
11 changes: 11 additions & 0 deletions src/main/resources/edu/harvard/iq/dataverse/prettyprint.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" encoding="UTF-8"/>

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

0 comments on commit c14c48a

Please sign in to comment.