public static Node jsonToNode(JsonElement e, Gson gson, RDFDatatype jsonDatatype) { Node result; if(e == null) { result = null; } else if(e.isJsonPrimitive()) { //JsonPrimitive p = e.getAsJsonPrimitive(); Object o = gson.fromJson(e, Object.class); //JsonTransformerUtils.toJavaObject(p); if(o != null) { RDFDatatype dtype = TypeMapper.getInstance().getTypeByValue(o); result = NodeFactory.createLiteralByValue(o, dtype); } else { throw new RuntimeException("Datatype not supported " + e); } } else if(e.isJsonObject() || e.isJsonArray()) { // arrays are json objects / array e.isJsonArray() || result = NodeFactory.createLiteralByValue(e, jsonDatatype);//new NodeValueJson(e); } else { throw new RuntimeException("Datatype not supported " + e); } return result; }
public static Node jsonToNode(JsonElement e, Gson gson, RDFDatatype jsonDatatype) { Node result; if(e == null) { result = null; } else if(e.isJsonPrimitive()) { //JsonPrimitive p = e.getAsJsonPrimitive(); Object o = gson.fromJson(e, Object.class); //JsonTransformerUtils.toJavaObject(p); if(o != null) { RDFDatatype dtype = TypeMapper.getInstance().getTypeByValue(o); result = NodeFactory.createLiteralByValue(o, dtype); } else { throw new RuntimeException("Datatype not supported " + e); } } else if(e.isJsonObject() || e.isJsonArray()) { // arrays are json objects / array e.isJsonArray() || result = NodeFactory.createLiteralByValue(e, jsonDatatype);//new NodeValueJson(e); } else { throw new RuntimeException("Datatype not supported " + e); } return result; }
@Override public Expr visit(ValueExpression<?> e) { Object value = e.getValue(); RDFDatatype rdfDatatype = typeMapper.getTypeByValue(value); Node node = NodeFactory.createLiteralByValue(value, rdfDatatype); NodeValue result = NodeValue.makeNode(node); return result; }
@Override public Expr visit(ValueExpression<?> e) { Object value = e.getValue(); RDFDatatype rdfDatatype = typeMapper.getTypeByValue(value); Node node = NodeFactory.createLiteralByValue(value, rdfDatatype); NodeValue result = NodeValue.makeNode(node); return result; }
/** * Build a typed literal label from its value form using * whatever datatype is currently registered as the default * representation for this java class. No language tag is supplied. * @param value the literal value to encapsulate */ LiteralLabelImpl( Object value ) { RDFDatatype dt = TypeMapper.getInstance().getTypeByValue( value ); if (dt == null) { setWithNewDatatypeForValueClass(value); } else { setLiteralLabel_2( value, "", dt ); } }
/** * Build a typed literal label from its value form using * whatever datatype is currently registered as the default * representation for this java class. No language tag is supplied. * @param value the literal value to encapsulate */ LiteralLabelImpl( Object value ) { RDFDatatype dt = TypeMapper.getInstance().getTypeByValue( value ); if (dt == null) { setWithNewDatatypeForValueClass(value); } else { setLiteralLabel_2( value, "", dt ); } }
public static Node jsonToNode(Object o, Gson gson, RDFDatatype jsonDatatype) { boolean isPrimitive = o instanceof Boolean || o instanceof Number || o instanceof String; Node result; if(o == null) { result = null; } else if(isPrimitive) { RDFDatatype dtype = TypeMapper.getInstance().getTypeByValue(o); result = NodeFactory.createLiteralByValue(o, dtype); } else if(o instanceof JsonElement) { JsonElement e = (JsonElement)o; result = jsonToNode(e, gson, jsonDatatype); } else { // Write the object to json and re-read it as a json-element String str = gson.toJson(o); JsonElement e = gson.fromJson(str, JsonElement.class); result = jsonToNode(e, gson, jsonDatatype); } // else { // throw new RuntimeException("Unknown type: " + o); // } return result; }
public static Node jsonToNode(Object o, Gson gson, RDFDatatype jsonDatatype) { boolean isPrimitive = o instanceof Boolean || o instanceof Number || o instanceof String; Node result; if(o == null) { result = null; } else if(isPrimitive) { RDFDatatype dtype = TypeMapper.getInstance().getTypeByValue(o); result = NodeFactory.createLiteralByValue(o, dtype); } else if(o instanceof JsonElement) { JsonElement e = (JsonElement)o; result = jsonToNode(e, gson, jsonDatatype); } else { // Write the object to json and re-read it as a json-element String str = gson.toJson(o); JsonElement e = gson.fromJson(str, JsonElement.class); result = jsonToNode(e, gson, jsonDatatype); } // else { // throw new RuntimeException("Unknown type: " + o); // } return result; }
public void testDataMatches() { TypeMapper tm = TypeMapper.getInstance(); RDFDatatype dt1 = tm.getTypeByValue( new Integer( 10 ) ); RDFDatatype dt2 = tm.getTypeByValue( new Short( (short) 10 ) ); Node a = NodeFactory.createLiteral( "10", dt1 ); Node b = NodeFactory.createLiteral( "10", dt2 ); assertDiffer( "types must make a difference", a, b ); assertTrue( "A and B must express the same value", a.sameValueAs( b ) ); assertTrue( "matching literals must respect sameValueAs", a.matches( b ) ); }
public void testDataMatches() { TypeMapper tm = TypeMapper.getInstance(); RDFDatatype dt1 = tm.getTypeByValue( new Integer( 10 ) ); RDFDatatype dt2 = tm.getTypeByValue( new Short( (short) 10 ) ); Node a = NodeFactory.createLiteral( "10", dt1 ); Node b = NodeFactory.createLiteral( "10", dt2 ); assertDiffer( "types must make a difference", a, b ); assertTrue( "A and B must express the same value", a.sameValueAs( b ) ); assertTrue( "matching literals must respect sameValueAs", a.matches( b ) ); }
public void testLiteralToString() { TypeMapper tm = TypeMapper.getInstance(); RDFDatatype dtInt = tm.getTypeByValue( new Integer( 10 ) ); Node plain = NodeFactory.createLiteral( "rhubarb", ""); Node english = NodeFactory.createLiteral( "eccentric", "en_UK"); Node typed = NodeFactory.createLiteral( "10", dtInt ); assertEquals( "\"rhubarb\"", plain.toString() ); assertEquals( "rhubarb", plain.toString( false ) ); assertEquals( "\"eccentric\"@en_UK", english.toString() ); assertEquals( "10^^http://www.w3.org/2001/XMLSchema#int", typed.toString( false ) ); }
public void testLiteralToString() { TypeMapper tm = TypeMapper.getInstance(); RDFDatatype dtInt = tm.getTypeByValue( new Integer( 10 ) ); Node plain = NodeFactory.createLiteral( "rhubarb", ""); Node english = NodeFactory.createLiteral( "eccentric", "en_UK"); Node typed = NodeFactory.createLiteral( "10", dtInt ); assertEquals( "\"rhubarb\"", plain.toString() ); assertEquals( "rhubarb", plain.toString( false ) ); assertEquals( "\"eccentric\"@en_UK", english.toString() ); assertEquals( "10^^http://www.w3.org/2001/XMLSchema#int", typed.toString( false ) ); }
public void testSameAdhocClassUS() { try { final UniqueValueClass1 ra = new UniqueValueClass1("rhubarb"); final UniqueValueClass1 rb = new UniqueValueClass1("cottage"); Assert.assertNull("not expecting registered RDF Datatype", TypeMapper.getInstance().getTypeByValue(ra)); final Literal la = model.createTypedLiteral(ra); // Sets the type // mapper // - contaminates it // with // UniqueValueClass1 final Literal lb = model.createTypedLiteral(rb); JenaTestBase .assertInstanceOf(AdhocDatatype.class, la.getDatatype()); Assert.assertSame(la.getDatatype(), lb.getDatatype()); Assert.assertNotNull(TypeMapper.getInstance().getTypeByValue(ra)); } finally { TypeMapper.reset(); } }
public void testSameAdhocClassUS() { try { final UniqueValueClass1 ra = new UniqueValueClass1("rhubarb"); final UniqueValueClass1 rb = new UniqueValueClass1("cottage"); Assert.assertNull("not expecting registered RDF Datatype", TypeMapper.getInstance().getTypeByValue(ra)); final Literal la = model.createTypedLiteral(ra); // Sets the type // mapper // - contaminates it // with // UniqueValueClass1 final Literal lb = model.createTypedLiteral(rb); JenaTestBase .assertInstanceOf(AdhocDatatype.class, la.getDatatype()); Assert.assertSame(la.getDatatype(), lb.getDatatype()); Assert.assertNotNull(TypeMapper.getInstance().getTypeByValue(ra)); } finally { TypeMapper.reset(); } }
public void testTypedLiteralTypesAndValues() { // Resource r = model.createResource( "eh:/rhubarb" ); final UniqueValueClass2 r = new UniqueValueClass2("rhubarb"); Assert.assertNull("not expecting registered RDF Datatype", TypeMapper .getInstance().getTypeByValue(r)); final Literal typed = model.createTypedLiteral(r); // Sets the type // mapper - // contaminates it // with // UniqueValueClass2 final Literal string = model.createLiteral(r.value); Assert.assertEquals(string.getLexicalForm(), typed.getLexicalForm()); Assert.assertEquals(string.getLanguage(), typed.getLanguage()); JenaTestBase.assertDiffer(string.getDatatypeURI(), typed.getDatatypeURI()); Assert.assertNotNull( "a datatype should have been invented for Resource[Impl]", typed.getDatatype()); JenaTestBase.assertDiffer(typed, string); JenaTestBase.assertDiffer(typed.getValue(), string.getValue()); Assert.assertEquals(r, typed.getValue()); JenaTestBase.assertDiffer(typed.hashCode(), string.hashCode()); } }
public void testTypedLiteralTypesAndValues() { // Resource r = model.createResource( "eh:/rhubarb" ); final UniqueValueClass2 r = new UniqueValueClass2("rhubarb"); Assert.assertNull("not expecting registered RDF Datatype", TypeMapper .getInstance().getTypeByValue(r)); final Literal typed = model.createTypedLiteral(r); // Sets the type // mapper - // contaminates it // with // UniqueValueClass2 final Literal string = model.createLiteral(r.value); Assert.assertEquals(string.getLexicalForm(), typed.getLexicalForm()); Assert.assertEquals(string.getLanguage(), typed.getLanguage()); JenaTestBase.assertDiffer(string.getDatatypeURI(), typed.getDatatypeURI()); Assert.assertNotNull( "a datatype should have been invented for Resource[Impl]", typed.getDatatype()); JenaTestBase.assertDiffer(typed, string); JenaTestBase.assertDiffer(typed.getValue(), string.getValue()); Assert.assertEquals(r, typed.getValue()); JenaTestBase.assertDiffer(typed.hashCode(), string.hashCode()); } }