DatatypeTransformer { public static Object transform(final OWLLiteral literal) { if (literal.isRDFPlainLiteral()) { return literal.getLiteral(); } else if (literal.getDatatype().isBuiltIn()) { switch (literal.getDatatype().getBuiltInDatatype()) { case XSD_SHORT: case XSD_UNSIGNED_BYTE: return Short.parseShort(literal.getLiteral()); case XSD_LONG: case XSD_UNSIGNED_INT: return Long.parseLong(literal.getLiteral()); case XSD_INT: case XSD_INTEGER: case XSD_NON_NEGATIVE_INTEGER: case XSD_NON_POSITIVE_INTEGER: case XSD_POSITIVE_INTEGER: case XSD_NEGATIVE_INTEGER: case XSD_UNSIGNED_SHORT: return Integer.parseInt(literal.getLiteral()); case XSD_DOUBLE: return Double.parseDouble(literal.getLiteral()); case XSD_FLOAT: return Float.parseFloat(literal.getLiteral()); case XSD_DECIMAL: return new BigDecimal(literal.getLiteral()); case XSD_STRING: case RDF_XML_LITERAL: return literal.getLiteral(); case RDF_LANG_STRING: return new LangString(literal.getLiteral(), literal.getLang()); case XSD_BOOLEAN: return Boolean.parseBoolean(literal.getLiteral()); case XSD_ANY_URI: return URI.create(literal.getLiteral()); case XSD_DATE_TIME_STAMP: case XSD_DATE_TIME: try { return new SimpleDateFormat(DATE_TIME_FORMAT).parse(literal.getLiteral()); } catch (ParseException e) { throw new IllegalArgumentException( "The date time '" + literal.getLiteral() + "' cannot be parsed using format " + DATE_TIME_FORMAT + "."); } default: break; } } throw new IllegalArgumentException("Unsupported datatype: " + literal.getDatatype()); } private  DatatypeTransformer(); static Class<?> transformOWLType(final OWLDatatype dt); static boolean isSupportedJavaType(Class<?> dt); static Object transform(final OWLLiteral literal); static OWLLiteral transform(Object value, String lang);  }
@Test void transformOwlLiteralToObjectReturnsOntoDriverLangStringForRdfLangString() { final OWLLiteral literal = DATA_FACTORY.getOWLLiteral("test", "en"); final Object result = DatatypeTransformer.transform(literal); assertThat(result, instanceOf(LangString.class)); final LangString lsResult = (LangString) result; assertEquals(literal.getLiteral(), lsResult.getValue()); assertTrue(lsResult.getLanguage().isPresent()); assertEquals(literal.getLang(), lsResult.getLanguage().get()); }
@Test void transformOwlLiteralToObjectReturnsStringForSimpleLiteral() { final OWLLiteral literal = DATA_FACTORY.getOWLLiteral("test"); final Object result = DatatypeTransformer.transform(literal); assertThat(result, instanceOf(String.class)); assertEquals(literal.getLiteral(), result); }
@Test void transformObjectToOwlLiteralReturnsRdfLangStringForOntoDriverLangStringWithLanguage() { final LangString ls = new LangString("test", "en"); final OWLLiteral result = DatatypeTransformer.transform(ls, null); assertEquals(OWL2Datatype.RDF_LANG_STRING.getDatatype(DATA_FACTORY), result.getDatatype()); assertEquals(ls.getValue(), result.getLiteral()); assertEquals(ls.getLanguage().get(), result.getLang()); }
@Test void transformObjectToOwlLiteralReturnsSimpleLiteralForOntoDriverLangStringWithoutLanguage() { final LangString ls = new LangString("test"); final OWLLiteral result = DatatypeTransformer.transform(ls, null); assertEquals(OWL2Datatype.XSD_STRING.getDatatype(DATA_FACTORY), result.getDatatype()); assertEquals(ls.getValue(), result.getLiteral()); assertFalse(result.hasLang()); assertTrue(result.getLang().isEmpty()); }