org.apache.jena.datatypes.RDFDatatype Java Examples

The following examples show how to use org.apache.jena.datatypes.RDFDatatype. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RDFNodeFactory.java    From Processor with Apache License 2.0 6 votes vote down vote up
public static final RDFNode createTyped(String value, Resource valueType)
   {
if (value == null) throw new IllegalArgumentException("Param value cannot be null");

       // without value type, return default xsd:string value
       if (valueType == null) return ResourceFactory.createTypedLiteral(value, XSDDatatype.XSDstring);

       // if value type is from XSD namespace, value is treated as typed literal with XSD datatype
       if (valueType.getNameSpace().equals(XSD.getURI()))
       {
           RDFDatatype dataType = NodeFactory.getType(valueType.getURI());
           return ResourceFactory.createTypedLiteral(value, dataType);
       }
       // otherwise, value is treated as URI resource
       else
           return ResourceFactory.createResource(value);
   }
 
Example #2
Source File: IsValidForDatatypeFunction.java    From shacl with Apache License 2.0 6 votes vote down vote up
@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);
	}
}
 
Example #3
Source File: DatatypeConstraintExecutor.java    From shacl with Apache License 2.0 6 votes vote down vote up
private void validate(Constraint constraint, ValidationEngine engine, String message, String datatypeURI, RDFDatatype datatype, RDFNode focusNode, RDFNode valueNode) {
	if(!valueNode.isLiteral() || !datatypeURI.equals(valueNode.asNode().getLiteralDatatypeURI()) || !datatype.isValid(valueNode.asNode().getLiteralLexicalForm())) {

		// TBS-1629: Ignore violations of mapped datatypes, e.g. actual literal is xsd:integer and sh:datatype is xsd:nonNegativeInteger
		if(valueNode.isLiteral() && !datatypeURI.equals(valueNode.asNode().getLiteralDatatypeURI()) && datatype.isValid(valueNode.asNode().getLiteralLexicalForm())) {
			if(isTDB1(focusNode.getModel().getGraph())) {
				RDFDatatype tdbType = tdbTypes.get(datatype);
				if(valueNode.asNode().getLiteralDatatype().equals(tdbType)) {
					return; // Do nothing
				}
			}
		}
		
		engine.createValidationResult(constraint, focusNode, valueNode, () -> message);
	}
}
 
Example #4
Source File: RDFPatchReaderBinary.java    From rdf-delta with Apache License 2.0 6 votes vote down vote up
public static Node fromThrift(RDF_Term term) {
    if ( term.isSetIri() )
        return NodeFactory.createURI(term.getIri().getIri());

    if ( term.isSetBnode() )
        return NodeFactory.createBlankNode(term.getBnode().getLabel());

    if ( term.isSetLiteral() ) {
        RDF_Literal lit = term.getLiteral();
        String lex = lit.getLex();
        String dtString = null;
        if ( lit.isSetDatatype() )
            dtString = lit.getDatatype();
        RDFDatatype dt = NodeFactory.getType(dtString);

        String lang = lit.getLangtag();
        return NodeFactory.createLiteral(lex, lang, dt);
    }

    throw new PatchException("No conversion to a Node: "+term.toString());
}
 
Example #5
Source File: TriplePatternElementParserForJena.java    From Server.Java with MIT License 5 votes vote down vote up
/**
 *
 * @param label
 * @param typeURI
 * @return
 */
@Override
public RDFNode createTypedLiteral( final String label,
                                   final String typeURI )
{
    final RDFDatatype dt = TypeMapper.getInstance()
                                     .getSafeTypeByName( typeURI );
    return ResourceFactory.createTypedLiteral( label, dt );
}
 
Example #6
Source File: JSFactory.java    From shacl with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
public static Node getNode(Object obj) {
	if(obj == null) {
		return null;
	}
	else if(obj instanceof JSTerm) {
		return ((JSTerm)obj).getNode();
	}
	else if(obj instanceof Map) {
		Map som = (Map) obj;
		String value = (String) som.get(VALUE);
		if(value == null) {
			throw new IllegalArgumentException("Missing value");
		}
		String termType = (String) som.get(TERM_TYPE);
		if(NAMED_NODE.equals(termType)) {
			return NodeFactory.createURI(value);
		}
		else if(BLANK_NODE.equals(termType)) {
			return NodeFactory.createBlankNode(value);
		}
		else if(LITERAL.equals(termType)) {
			String lang = (String) som.get(LANGUAGE);
			Map dt = (Map)som.get(DATATYPE);
			String dtURI = (String)dt.get(VALUE);
			RDFDatatype datatype = TypeMapper.getInstance().getSafeTypeByName(dtURI);
			return NodeFactory.createLiteral(value, lang, datatype);
		}
		else {
			throw new IllegalArgumentException("Unsupported term type " + termType);
		}
	}
	else {
		return null;
	}
}
 
Example #7
Source File: DatatypeConstraintExecutor.java    From shacl with Apache License 2.0 5 votes vote down vote up
@Override
public void executeConstraint(Constraint constraint, ValidationEngine engine, Collection<RDFNode> focusNodes) {
	long startTime = System.currentTimeMillis();
	RDFNode datatypeNode = constraint.getParameterValue();
	String datatypeURI = datatypeNode.asNode().getURI();
	RDFDatatype datatype = NodeFactory.getType(datatypeURI);
	String message = "Value must be a valid literal of type " + ((Resource)datatypeNode).getLocalName();
	for(RDFNode focusNode : focusNodes) {
		for(RDFNode valueNode : engine.getValueNodes(constraint, focusNode)) {
			validate(constraint, engine, message, datatypeURI, datatype, focusNode, valueNode);
		}
		engine.checkCanceled();
	}
	addStatistics(constraint, startTime);
}
 
Example #8
Source File: SourcePlan.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
final protected Binding exec(
        final Binding binding,
        final Context context) {

    LOG.debug("Start " + this);
    Objects.nonNull(binding);
    // generate the source URI.
    final String sourceUri = getActualSource(binding);
    final String acceptHeader = getAcceptHeader(binding);
    LOG.trace("... resolved to SOURCE <" + sourceUri + "> ACCEPT " + acceptHeader + " AS " + var);
    final LookUpRequest request = new LookUpRequest(sourceUri, acceptHeader);
    final SPARQLExtStreamManager sm = (SPARQLExtStreamManager) context.get(SysRIOT.sysStreamManager);
    Objects.requireNonNull(sm);
    final TypedInputStream stream = sm.open(request);
    if (stream == null) {
        LOG.info("Exec SOURCE <" + sourceUri + "> ACCEPT " + acceptHeader + " AS " + var + " returned nothing.");
        return BindingFactory.binding(binding);
    }
    try (InputStream in = stream.getInputStream()) {
        final String literal = IOUtils.toString(in, "UTF-8");
        final RDFDatatype dt;
        if (stream.getMediaType() != null && stream.getMediaType().getContentType() != null) {
            dt = tm.getSafeTypeByName("http://www.iana.org/assignments/media-types/" + stream.getMediaType().getContentType());
        } else {
            dt = tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#string");
        }
        final Node n = NodeFactory.createLiteral(literal, dt);
        LOG.debug("Exec " + this + " returned. "
                + "Enable TRACE level for more.");
        if (LOG.isTraceEnabled()) {
            LOG.trace("Exec " + this + " returned\n" + LogUtils.compress(n));
        }
        return BindingFactory.binding(binding, var, n);
    } catch (IOException | DatatypeFormatException ex) {
        LOG.warn("Exception while looking up " + sourceUri + ":", ex);
        return BindingFactory.binding(binding);
    }
}
 
Example #9
Source File: AbstractDatatype.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
@Deprecated
private static RDFDatatype getDataType(DataType dataType) {
    switch (dataType) {
        case Date:
            return XSDDatatype.XSDdate;
        case DateTime:
            return XSDDatatype.XSDdateTime;
        case GEO_JSON:
            return RDFDataTypes.GEO_JSON;
        case WKT_LITERAL:
            return RDFDataTypes.WKT_LITERAL;
        default:
            return new BaseDatatype(dataType.getType());
    }
}
 
Example #10
Source File: RdflintParserRdfxml.java    From rdflint with MIT License 5 votes vote down vote up
private static Node convert(ALiteral lit) {
  String dtURI = lit.getDatatypeURI(); // SUPPRESS CHECKSTYLE AbbreviationAsWordInName
  if (dtURI == null) {
    return NodeFactory.createLiteral(lit.toString(), lit.getLang());
  }

  if (lit.isWellFormedXML()) {
    return NodeFactory.createLiteral(lit.toString(), null, true);
  }

  RDFDatatype dt = TypeMapper.getInstance().getSafeTypeByName(dtURI);
  return NodeFactory.createLiteral(lit.toString(), dt);
}
 
Example #11
Source File: ITER_CBOR.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public List<List<NodeValue>> exec(NodeValue cbor, NodeValue jsonpath) {
    if (cbor.getDatatypeURI() != null
            && !cbor.getDatatypeURI().equals(datatypeUri)
            && !cbor.getDatatypeURI().equals("http://www.w3.org/2001/XMLSchema#string")) {
        LOG.debug("The URI of NodeValue1 MUST be"
                + " <" + datatypeUri + "> or"
                + " <http://www.w3.org/2001/XMLSchema#string>. Got <"
                + cbor.getDatatypeURI() + ">. Returning null.");
    }

    Configuration conf = Configuration.builder()
            .options(Option.ALWAYS_RETURN_LIST).build();

    String json = new String(Base64.getDecoder().decode(cbor.asNode().getLiteralLexicalForm().getBytes()));

    Gson gson = new Gson();
    RDFDatatype dt = TypeMapper.getInstance().getSafeTypeByName(datatypeUri);
    
    try {
        List<Object> values = JsonPath
                .using(conf)
                .parse(json)
                .read(jsonpath.getString());

        final List<List<NodeValue>> nodeValues = new ArrayList<>();

        Node node;
        NodeValue nodeValue;
        for (Object value : values) {
            String jsonstring = gson.toJson(value);
            node = NodeFactory.createLiteral(jsonstring, dt);
            nodeValue = new NodeValueNode(node);
            nodeValues.add(Collections.singletonList(nodeValue));
        }
        return nodeValues;
    } catch (Exception ex) {
        if(LOG.isDebugEnabled()) {
            Node compressed = LogUtils.compress(cbor.asNode());
            LOG.debug("No evaluation of " + compressed + ", " + jsonpath, ex);
        }
        throw new ExprEvalException("No evaluation of " + jsonpath, ex);
    }
}
 
Example #12
Source File: AbstractDatatype.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
public RDFDatatype getDataType() {
    return dataType;
}
 
Example #13
Source File: AbstractDatatype.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
public AbstractDatatype(RDFDatatype dataType, String value) {
    super(value);
    this.dataType = dataType;
}
 
Example #14
Source File: Modified.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
public Modified(RDFDatatype dataType, String value) {
    super(dataType, value);
}
 
Example #15
Source File: Issued.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
public Issued(RDFDatatype dataType, String value) {
    super(dataType, value);
}
 
Example #16
Source File: Geometry.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
public Geometry(RDFDatatype dataType, String value) {
    super(dataType, value);
}
 
Example #17
Source File: ParserProfileTurtleStarWrapperImpl.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
@Override
public Node createTypedLiteral(String lexical, RDFDatatype datatype, long line, long col) {
    return profile.createTypedLiteral(lexical, datatype, line, col);
}