Refine search
/** * Check if a literal value node is a legal value for the given datatype. * @param val the literal value node * @param dt the Node designating a datatype URI */ public static boolean isTypeOK(Node val, Node dt) { if (!dt.isURI()) return false; if (val.isBlank()) return true; if (val.isLiteral()) { LiteralLabel ll = val.getLiteral(); if (ll.getDatatype() != null && (! ll.isWellFormed())) return false; if (dt.equals(RDFS.Nodes.Literal)) { return true; } else { RDFDatatype dtype = TypeMapper.getInstance().getSafeTypeByName(dt.getURI()); return dtype.isValidLiteral(val.getLiteral()); } } return false; }
@Override protected NodeValue exec(Node literalNode, Node datatypeNode, FunctionEnv env) { if(literalNode == null || !literalNode.isLiteral()) { throw new ExprEvalException(); } String lex = literalNode.getLiteralLexicalForm(); if(!datatypeNode.isURI()) { throw new ExprEvalException(); } RDFDatatype datatype = TypeMapper.getInstance().getTypeByName(datatypeNode.getURI()); if(datatype == null) { return NodeValue.TRUE; } else { boolean valid = datatype.isValid(lex); return NodeValue.makeBoolean(valid); } } }
public static <T> Literal createTypedLiteral(Model model, Class<T> clazz, T o) { TypeMapper tm = TypeMapper.getInstance(); RDFDatatype dtype = tm.getTypeByClass(clazz); Literal result = model.createTypedLiteral((Object)o, dtype); return result; }
public static RDFDatatype getType(String s) { if ( s == null ) return null ; return TypeMapper.getInstance().getSafeTypeByName(s) ; }
protected Node createLiteral(String lexicalForm, String langTag, String datatypeURI) { Node n = null ; // Can't have type and lang tag. if ( datatypeURI != null) { RDFDatatype dType = TypeMapper.getInstance().getSafeTypeByName(datatypeURI) ; n = NodeFactory.createLiteral(lexicalForm, null, dType) ; } else n = NodeFactory.createLiteral(lexicalForm, langTag, null) ; return n ; }
public static void enableAsFirstClassDatatype() { if ( ! firstClassDatatype ) TypeMapper.getInstance().registerDatatype(get()) ; }
case BNODE : return NodeFactory.createBlankNode(tokenImage) ; case IRI : return NodeFactory.createURI(tokenImage) ; case PREFIXED_NAME : if ( pmap == null ) return NodeFactory.createURI("urn:prefixed-name:"+tokenImage+":"+tokenImage2) ; String x = pmap.expand(tokenImage, tokenImage2) ; if ( x == null ) if ( ! n.isURI() ) throw new RiotException("Invalid token: "+this) ; RDFDatatype dt = TypeMapper.getInstance().getSafeTypeByName(n.getURI()) ; return NodeFactory.createLiteral(lexToken.getImage(), dt) ;
public void testRDFLangString_1() { // Registration RDFDatatype dt = TypeMapper.getInstance().getTypeByName(RDF.langString.getURI()) ; assertEquals(RDF.dtLangString, dt) ; assertTrue(RDF.dtLangString == dt) ; }
@Test public void test_param_string_boolean_3() { // Test invalid boolean injection String cmdText = "SELECT * WHERE { ?s ?p ?o }"; ParameterizedSparqlString query = new ParameterizedSparqlString(cmdText); query.setLiteral("o", "xyz", TypeMapper.getInstance().getSafeTypeByName(XSD.xboolean.toString())); // We expect #boolean as booleans with invalid lexical values should not // be formatted as plain literals test(query, new String[] { "xyz", XSD.xboolean.toString() }, new String[] { "?o" }); }
/** * Return a map from property nodes to a list of RDFDatatype objects * which have been declared as the range of that property. */ protected HashMap<Node, List<RDFDatatype>> getDTRange() { if (dtRange == null) { dtRange = new HashMap<>(); for (Iterator<Triple> i = find(null, RDFS.range.asNode(), null); i.hasNext(); ) { Triple triple = i.next(); Node prop = triple.getSubject(); Node rangeValue = triple.getObject(); if (rangeValue.isURI()) { RDFDatatype dt = TypeMapper.getInstance().getTypeByName(rangeValue.getURI()); if (dt != null) { List<RDFDatatype> range = dtRange.get(prop); if (range == null) { range = new ArrayList<>(); dtRange.put(prop, range); } range.add(dt); } } } } return dtRange; }
} else if(nv.isIRI()) { Node node = nv.asNode(); url = node.getURI(); } else { url = null; RDFDatatype jsonDatatype = TypeMapper.getInstance().getTypeByClass(JsonElement.class); Node jsonNode = NodeFactory.createLiteral(str, jsonDatatype); result = NodeValue.makeNode(jsonNode); } else {
@Override public NodeValue exec(NodeValue a, NodeValue b) { // Try to parse the lexical forms as WKT String aStr = a.asNode().getLiteralLexicalForm(); String bStr = b.asNode().getLiteralLexicalForm(); NodeValue result; Geometry aGeo = null; Geometry bGeo = null; try { aGeo = wktParser.apply(aStr); bGeo = wktParser.apply(bStr); } catch(Exception e) { logger.warn("Error parsing provided argument as WKT", e); } if(aGeo != null && bGeo != null) { Geometry g = eval(aGeo, bGeo); RDFDatatype dtype = TypeMapper.getInstance().getSafeTypeByName("http://www.opengis.net/ont/geosparql#wktLiteral"); String str = g != null ? wktWriter.apply(g) : null; result = str == null ? NodeValue.nvNothing : NodeValue.makeNode(NodeFactory.createLiteral(str, dtype)); } else { result = NodeValue.nvNothing; } return result; }
public JSLiteral literal(String value, Object langOrDatatype) { if(langOrDatatype instanceof JSNamedNode) { return new JSLiteral(NodeFactory.createLiteral(value, TypeMapper.getInstance().getTypeByName(((JSNamedNode)langOrDatatype).getValue()))); } else if(langOrDatatype instanceof String) { return new JSLiteral(NodeFactory.createLiteral(value, (String)langOrDatatype)); } else { throw new IllegalArgumentException("Invalid type of langOrDatatype argument"); } }
RDFDatatype xmlDatatype = TypeMapper.getInstance().getTypeByClass(org.w3c.dom.Node.class); Objects.requireNonNull(xmlDatatype); Node node = subject.isVariable() ? binding.get((Var)subject) : subject; Node outputNode = object.getArg(1); if(!outputNode.isVariable()) { throw new RuntimeException("Object of xml array splitting must be a variable"); boolean isXml = node.isLiteral() && node.getLiteralDatatype() instanceof RDFDatatypeXml; if(isXml) { org.w3c.dom.Node xml = (org.w3c.dom.Node)node.getLiteralValue(); Node xmlNode = NodeFactory.createLiteralByValue(item, xmlDatatype); Binding b = BindingFactory.binding(binding, outputVar, xmlNode); bindings.add(b);
/** * Test a user error report concerning date/time literals */ public void testDateTimeBug() { // Bug in serialization String XSDDateURI = XSD.date.getURI(); TypeMapper typeMapper=TypeMapper.getInstance(); RDFDatatype dt = typeMapper.getSafeTypeByName(XSDDateURI); Object obj = dt.parse("2003-05-21"); Literal literal = m.createTypedLiteral(obj, dt); literal.toString(); Object value2 = dt.parse(obj.toString()); assertEquals(obj, value2); // Check alternative form doesn't provoke exceptions RDFDatatype dateType = XSDDatatype.XSDdate; m.createTypedLiteral("2003-05-21", dateType); // Check alt time times checkSerialization("2003-05-21", XSDDatatype.XSDdate); checkSerialization("2003-05-21T12:56:10Z", XSDDatatype.XSDdateTime); checkSerialization("2003-05", XSDDatatype.XSDgYearMonth); checkSerialization("2003", XSDDatatype.XSDgYear); checkSerialization("--05", XSDDatatype.XSDgMonth); checkSerialization("--05-12", XSDDatatype.XSDgMonthDay); checkSerialization("---12", XSDDatatype.XSDgDay); }
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 ) ); }
@Test public void test_param_string_append_integer_2() { // Test appending text ParameterizedSparqlString query = new ParameterizedSparqlString(); query.append("SELECT *"); query.append('\n'); query.append("WHERE { ?s ?p "); query.appendLiteral("xyz", TypeMapper.getInstance().getSafeTypeByName(XSD.integer.toString())); query.append("}"); test(query, new String[] { "SELECT", "*", "\n", "WHERE", "?s", "?p", "xyz", XSD.integer.toString() }, new String[] {}); testAsQuery(query); }
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 ) ); }
TypeMapper tm = TypeMapper.getInstance(); RDFDatatype dt = tm.getTypeByClass(value.getClass()); Node node = NodeFactory.createLiteral(lex, dt); Object o = dt.parse(lex); System.out.println(o.getClass()); Object x = node.getLiteralValue(); System.out.println("Got value: " + x.getClass() + " " + node);
TypeMapper tm = TypeMapper.getInstance(); tm.registerDatatype(new RDFDatatypeDate()); model.write(System.out, "TTL"); JobExecution lr = em.find(JobExecution.class, NodeFactory.createURI("http://ex.org/11")); System.out.println("Lookup result: " + lr); sparqlService.getQueryExecutionFactory().createQueryExecution("CONSTRUCT WHERE { ?s ?p ?o }").execConstruct().write(System.out, "TTL"); System.out.println("Lookup result: " + em.find(JobExecution.class, NodeFactory.createURI("http://ex.org/11")));