<commit_before>package org.sagebionetworks.doi.datacite;

import org.apache.xerces.jaxp.DocumentBuilderFactoryImpl;
import org.sagebionetworks.repo.model.doi.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

public class DataciteTranslator {

	/*
	 * Translates a Datacite XML String into a DoiMetadata object
	 * Works for XML adherent to Datacite Schemas 2.2 and 4.1
	 */
	public DoiMetadata translate(String xml) {
		Document dom = null;
		try {
			DocumentBuilder documentBuilder = DocumentBuilderFactoryImpl.newInstance().newDocumentBuilder();
			dom = documentBuilder.parse(new InputSource(new StringReader(xml)));
		} catch (ParserConfigurationException e) {
			throw new RuntimeException("Error occurred while configuring XML parser", e);
		} catch (SAXException | IOException e) {
			throw new RuntimeException("Error occurred while parsing metadata", e);
		}

		DoiMetadata doiMetadata = new DoiMetadata();

		doiMetadata.setCreators(getCreators(dom));
		doiMetadata.setTitles(getTitles(dom));
		doiMetadata.setPublicationYear(getPublicationYear(dom));
		doiMetadata.setResourceType(getResourceType(dom));

		return doiMetadata;
	}

	DoiCreator getCreator(Element creatorElement) {
		DoiCreator creator = new DoiCreator();
		creator.setCreatorName(creatorElement.getElementsByTagName("creatorName").item(0).getTextContent());

		NodeList idNodes = creatorElement.getElementsByTagName("nameIdentifier");

		if (idNodes.getLength() > 0) {
			List<DoiNameIdentifier> ids = new ArrayList<>();
			for (int i = 0; i < idNodes.getLength(); i++) {
				DoiNameIdentifier id = new DoiNameIdentifier();
				id.setIdentifier(idNodes.item(i).getTextContent());
				id.setNameIdentifierScheme(NameIdentifierSchemes.valueOf(idNodes.item(i).getAttributes()
						.getNamedItem("nameIdentifierScheme").getNodeValue()));
				ids.add(id);
			}
			creator.setNameIdentifiers(ids);
		}
		return creator;
	}

	List<DoiCreator> getCreators(Document dom) {
		List<DoiCreator> creators = new ArrayList<>();
		Element creatorsElement = (Element)dom.getElementsByTagName("creators").item(0);
		NodeList creatorList = creatorsElement.getElementsByTagName("creator");<commit_msg>extracted new method getNameIdentifier<commit_after>package org.sagebionetworks.doi.datacite;

import org.apache.xerces.jaxp.DocumentBuilderFactoryImpl;
import org.sagebionetworks.repo.model.doi.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

public class DataciteTranslator {

	/*
	 * Translates a Datacite XML String into a DoiMetadata object
	 * Works for XML adherent to Datacite Schemas 2.2 and 4.1
	 */
	public DoiMetadata translate(String xml) {
		Document dom = null;
		try {
			DocumentBuilder documentBuilder = DocumentBuilderFactoryImpl.newInstance().newDocumentBuilder();
			dom = documentBuilder.parse(new InputSource(new StringReader(xml)));
		} catch (ParserConfigurationException e) {
			throw new RuntimeException("Error occurred while configuring XML parser", e);
		} catch (SAXException | IOException e) {
			throw new RuntimeException("Error occurred while parsing metadata", e);
		}

		DoiMetadata doiMetadata = new DoiMetadata();

		doiMetadata.setCreators(getCreators(dom));
		doiMetadata.setTitles(getTitles(dom));
		doiMetadata.setPublicationYear(getPublicationYear(dom));
		doiMetadata.setResourceType(getResourceType(dom));

		return doiMetadata;
	}

	DoiNameIdentifier getNameIdentifier(Element nameIdElement) {
		DoiNameIdentifier id = new DoiNameIdentifier();
		id.setIdentifier(nameIdElement.getTextContent());
		id.setNameIdentifierScheme(NameIdentifierSchemes.valueOf(nameIdElement.getAttributes()
				.getNamedItem("nameIdentifierScheme").getNodeValue()));
		return id;
	}

	DoiCreator getCreator(Element creatorElement) {
		DoiCreator creator = new DoiCreator();
		creator.setCreatorName(creatorElement.getElementsByTagName("creatorName").item(0).getTextContent());

		NodeList idNodes = creatorElement.getElementsByTagName("nameIdentifier");
		if (idNodes.getLength() > 0) {
			List<DoiNameIdentifier> ids = new ArrayList<>();
			for (int i = 0; i < idNodes.getLength(); i++) {
				DoiNameIdentifier id = getNameIdentifier((Element)idNodes.item(i));
				ids.add(id);

			}
			creator.setNameIdentifiers(ids);
		}
		return creator;
	}

	List<DoiCreator> getCreators(Document dom) {
		List<DoiCreator> creators = new ArrayList<>();
		Element creatorsElement = (Element)dom.getElementsByTagName("creators").item(0);
		NodeList creatorList = creatorsElement.getElementsByTagName("creator");
{'fraction_non_alphanumeric': 0.11093871217998448, 'fraction_numerical': 0.004654771140418929, 'mean_word_length': 5.848605577689243, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7063037', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1561, 'n_words': 310}