org.codeblessing.sourceamazing.xmlschema.parser.XmlFacetValueConverter.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sourceamazing-xml-schema Show documentation
Show all versions of sourceamazing-xml-schema Show documentation
Creates out of the SourceAmazing schema a XML Schema to maintain the input data.
package org.codeblessing.sourceamazing.xmlschema.parser
import org.codeblessing.sourceamazing.api.process.schema.ConceptIdentifier
import org.codeblessing.sourceamazing.api.process.schema.FacetSchema
import org.codeblessing.sourceamazing.api.process.schema.FacetTypeEnum
object XmlFacetValueConverter {
fun convertString(facetSchema: FacetSchema, attributeValue: String): Any? {
return when(facetSchema.facetType) {
FacetTypeEnum.TEXT -> attributeValue
FacetTypeEnum.NUMBER -> attributeValue.toLong()
FacetTypeEnum.BOOLEAN -> attributeValue.toBoolean()
FacetTypeEnum.REFERENCE -> ConceptIdentifier.of(attributeValue)
FacetTypeEnum.TEXT_ENUMERATION -> enumerationValue(facetSchema, attributeValue)
}
}
private fun enumerationValue(facetSchema: FacetSchema, attributeValue: String): Any {
val enumerationType = facetSchema.enumerationType
?: throw IllegalStateException("No enumeration type defined for facet ${facetSchema.facetName} but value was '$attributeValue'")
return enumerationType.java.enumConstants
.filterIsInstance(Enum::class.java)
.firstOrNull { enumConstant ->
return@firstOrNull enumConstant.name == attributeValue
}
?: throw IllegalStateException("Value '$attributeValue' is not within the possible values ${enumerationType.java.enumConstants.joinToString(",") { "'${it}'" }} for facet ${facetSchema.facetName}.")
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy