Converters { public static Node makeLiteral(Object o) { RDFDatatype dt = TypeMapper.getInstance().getTypeByValue( o ); if (dt == null) { String msg = "No TypeDef defined for %s. Use TypeMapper.getInstance().register() to " + "register one or use makeLiteral() method in query builder instance."; throw new IllegalArgumentException( String.format( msg, o.getClass())); } return NodeFactory.createLiteral(LiteralLabelFactory.createTypedLiteral(o)); } private  Converters(); static Node checkVar(Node n ); static Node makeLiteral(Object o); static Node makeLiteral(String value, String typeUri); static Node makeNode(Object o, PrefixMapping pMapping); static Object makeNodeOrPath(Object o, PrefixMapping pMapping); static Var makeVar(Object o); static String quoted(String q); static Collection<Node> makeValueNodes( Iterator<?> iter, PrefixMapping prefixMapping );  }
@Test public void makeLiteralObjectTest() throws MalformedURLException { Node n = Converters.makeLiteral( 5 ); assertEquals( "5", n.getLiteralLexicalForm() ); assertEquals( Integer.valueOf(5), n.getLiteralValue()); assertEquals( "\"5\"^^http: n = Converters.makeLiteral( "Hello" ); assertEquals( "Hello", n.getLiteralLexicalForm() ); assertEquals( "Hello", n.getLiteralValue()); assertEquals( "\"Hello\"", n.toString( null, true )); URL url = new URL( "http: n = Converters.makeLiteral( url); assertEquals( "http: assertEquals( url, n.getLiteralValue()); assertEquals( "\"http: UUID uuid = UUID.randomUUID(); try { n = Converters.makeLiteral( uuid ); fail( "Should throw exception"); } catch (IllegalArgumentException expected) { } TypeMapper.getInstance().registerDatatype(new UuidDataType()); try { n = Converters.makeLiteral( uuid ); assertEquals( uuid.toString(), n.getLiteralLexicalForm() ); assertEquals( uuid, n.getLiteralValue()); String value = String.format( "\"%s\"^^java:java.util.UUID", uuid); assertEquals( value, n.toString(null, true)); } catch (IllegalArgumentException expected) { fail( "Unexpected IllegalArgumentException"); } }
@Test public void makeLiteralStringStringTest() { Node n = Converters.makeLiteral( "5", "http: assertEquals( "5", n.getLiteralLexicalForm() ); assertEquals( Integer.valueOf(5), n.getLiteralValue()); assertEquals( "\"5\"^^http: n = Converters.makeLiteral( "one", "some:stuff" ); assertEquals( "one", n.getLiteralLexicalForm() ); try { n.getLiteralValue(); fail( "Should have thrown DatatypeFormatException"); } catch (DatatypeFormatException expected) { } assertEquals( "\"one\"^^some:stuff", n.toString(null, true)); try { Converters.makeLiteral( "NaN", "http: fail( "Should have thrown DatatypeFormatException"); } catch (DatatypeFormatException expected) { } }