Last active
February 2, 2023 23:07
-
-
Save kjgarza/2b1be71e3d2a2a4e700b546af794960b to your computer and use it in GitHub Desktop.
Metadata converter in kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class DataCiteMetadata( | |
val identifier: String, | |
val creators: List<Creator>, | |
val titles: List<Title>, | |
val publisher: String, | |
val publicationYear: String, | |
val subjects: List<String>, | |
val resourceType: ResourceType, | |
val versions: List<String>, | |
val formats: List<String>, | |
val language: String, | |
val rightsList: List<String>, | |
val descriptions: List<Description>, | |
val geoLocations: List<GeoLocation>, | |
val fundingReferences: List<FundingReference> | |
) | |
data class Creator( | |
val name: String, | |
val givenName: String?, | |
val familyName: String?, | |
val affiliation: List<Affiliation> | |
) | |
data class Title( | |
val title: String, | |
val type: String? | |
) | |
data class ResourceType( | |
val resourceTypeGeneral: String, | |
val resourceType: String | |
) | |
data class Description( | |
val description: String, | |
val type: String | |
) | |
data class GeoLocation( | |
val placeName: String, | |
val latitude: Double?, | |
val longitude: Double? | |
) | |
data class FundingReference( | |
val funderName: String, | |
val funderIdentifier: String, | |
val awardNumber: String?, | |
val awardTitle: String?, | |
val awardURI: String? | |
) | |
data class Affiliation( | |
val name: String, | |
val identifier: String?, | |
val scheme: String? | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class DataCiteMetadata( | |
val identifier: String, | |
val creators: List<String>, | |
val titles: List<String>, | |
val publisher: String, | |
val publicationYear: String, | |
val subjects: List<String> | |
) | |
data class SchemaMetadata( | |
val identifier: String, | |
val author: List<String>, | |
val name: String, | |
val publisher: String, | |
val datePublished: String, | |
val keywords: List<String> | |
) | |
data class CrossrefMetadata( | |
val identifier: String, | |
val authors: List<String>, | |
val title: String, | |
val publisher: String, | |
val publicationYear: String, | |
val subjects: List<String> | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MetadataConverter { | |
fun convert(inputFormat: String, input: String, outputFormat: String): String { | |
val metadata = when (inputFormat) { | |
"datacite" -> parseDataCiteMetadata(input) | |
"crossref" -> parseCrossrefMetadata(input) | |
else -> throw IllegalArgumentException("Unsupported input format: $inputFormat") | |
} | |
return when (outputFormat) { | |
"schema" -> serializeSchemaMetadata(metadata) | |
else -> throw IllegalArgumentException("Unsupported output format: $outputFormat") | |
} | |
} | |
private fun parseCrossrefMetadata(input: String): CrossrefMetadata { | |
// Parsing logic here | |
} | |
private fun parseDataCiteMetadata(input: String): DataCiteMetadata { | |
// Parsing logic here | |
} | |
private fun serializeSchemaMetadata(metadata: DataCiteMetadata): String { | |
// Serialization logic here | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.w3c.dom.Node | |
import javax.xml.parsers.DocumentBuilderFactory | |
fun parseDataCiteMetadata(xml: String): DataCiteMetadata { | |
val factory = DocumentBuilderFactory.newInstance() | |
val builder = factory.newDocumentBuilder() | |
val doc = builder.parse(xml.byteInputStream()) | |
val root = doc.documentElement | |
fun getNodeValue(node: Node, tagName: String): String { | |
return node.getElementsByTagName(tagName).item(0).textContent | |
} | |
fun parseCreators(node: Node): List<Creator> { | |
val creatorsNodeList = node.getElementsByTagName("creators").item(0).childNodes | |
return (0 until creatorsNodeList.length).map { i -> | |
val creatorNode = creatorsNodeList.item(i) | |
Creator( | |
getNodeValue(creatorNode, "creatorName"), | |
getNodeValue(creatorNode, "givenName"), | |
getNodeValue(creatorNode, "familyName"), | |
listOf() | |
) | |
} | |
} | |
fun parseTitles(node: Node): List<Title> { | |
val titlesNodeList = node.getElementsByTagName("titles").item(0).childNodes | |
return (0 until titlesNodeList.length).map { i -> | |
val titleNode = titlesNodeList.item(i) | |
Title( | |
getNodeValue(titleNode, "title"), | |
getNodeValue(titleNode, "type") | |
) | |
} | |
} | |
fun parseResourceType(node: Node): ResourceType { | |
val resourceTypeNode = node.getElementsByTagName("resourceType").item(0) | |
return ResourceType( | |
getNodeValue(resourceTypeNode, "resourceTypeGeneral"), | |
getNodeValue(resourceTypeNode, "resourceType") | |
) | |
} | |
fun parseVersions(node: Node): List<String> { | |
val versionsNodeList = node.getElementsByTagName("versions").item(0).childNodes | |
return (0 until versionsNodeList.length).map { i -> | |
versionsNodeList.item(i).textContent | |
} | |
} | |
fun parseFormats(node: Node): List<String> { | |
val formatsNodeList = node.getElementsByTagName("formats").item(0).childNodes | |
return (0 until formatsNodeList.length).map { i -> | |
formatsNodeList.item(i).textContent | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment