public static String stringForLiteral(Node_Literal literal, SerializationContext context) { StringBuilder result = new StringBuilder( ); stringForLiteral( result, literal, context ); return result.toString(); }
public static void stringForNode(StringBuilder result, Node n, SerializationContext context) { if ( n == null ) { result.append("<<null>>") ; return ; } // mappable? if ( context != null && context.getBNodeMap() != null ) { String str = context.getBNodeMap().asString(n) ; if ( str != null ) { result.append(str) ; return ; } } if ( n.isBlank() ) { result.append("_:").append(n.getBlankNodeLabel()) ; } else if ( n.isLiteral() ) { stringForLiteral(result, (Node_Literal)n, context) ; } else if ( n.isURI() ) { String uri = n.getURI() ; stringForURI(result, uri, context) ; } else if ( n.isVariable() ) { result.append("?").append(n.getName()) ; } else if ( n.equals(Node.ANY) ) { result.append("ANY") ; } else { Log.warn(FmtUtils.class, "Failed to turn a node into a string: " + n) ; result.append(n.toString()) ; } }
@Test public void doubleLiteral() throws Exception { Node_Literal nl = (Node_Literal)NodeFactory.createLiteral("2.1e2", new XSDDatatype("double")) ; assertEquals("2.1e2", FmtUtils.stringForLiteral(nl, getContext())) ; }
@Test public void booleanLiteral() throws Exception { Node_Literal nl = (Node_Literal)NodeFactory.createLiteral("false", new XSDDatatype("boolean")) ; assertEquals("false", FmtUtils.stringForLiteral(nl, getContext())) ; }
@Test public void stringLiteral() throws Exception { Node_Literal nl = (Node_Literal)NodeFactory.createLiteral("abc", "no", new XSDDatatype("string")) ; assertEquals("\"abc\"@no", FmtUtils.stringForLiteral(nl, getContext())) ; }
@Test public void decimalLiteral() throws Exception { Node_Literal nl = (Node_Literal)NodeFactory.createLiteral("2.4", new XSDDatatype("decimal")) ; assertEquals("2.4", FmtUtils.stringForLiteral(nl, getContext())) ; }
@Test public void integerLiteral() throws Exception { Node_Literal nl = (Node_Literal)NodeFactory.createLiteral("2", new XSDDatatype("int")) ; assertEquals("\"2\"^^<http://www.w3.org/2001/XMLSchema#int>", FmtUtils.stringForLiteral(nl, getContext())) ; }