org.apache.jena.datatypes.TypeMapper Java Examples

The following examples show how to use org.apache.jena.datatypes.TypeMapper. 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: ITER_GeoJSON.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public List<List<NodeValue>> exec(NodeValue json) {
    String s = getString(json);
    List<List<NodeValue>> nodeValues = new ArrayList<>();
    FeatureCollection featureCollection = GSON.fromJson(s, FeatureCollection.class);

    for (Feature feature : featureCollection.features()) {
        List<NodeValue> values = new ArrayList<>();
        NodeValue geometry = geoJSONGeom.getNodeValue(feature.geometry());
        values.add(geometry);
        Node properties = NodeFactory.createLiteral(
                GSON.toJson(feature.properties()),
                TypeMapper.getInstance().getSafeTypeByName("http://www.iana.org/assignments/media-types/application/json"));
        values.add(new NodeValueNode(properties));
        nodeValues.add(values);
    }
    return nodeValues;
}
 
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: 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 #4
Source File: TermFactory.java    From shacl with Apache License 2.0 5 votes vote down vote up
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");
	}
}
 
Example #5
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 #6
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 #7
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 #8
Source File: String2Node.java    From quetzal with Eclipse Public License 2.0 4 votes vote down vote up
private void assignLiteral(String value, short datatype)
{

if (datatype == TypeMap.IRI_ID)
   {
   node = ResourceFactory.createResource(value);
   return;
   }

if (datatype == TypeMap.SIMPLE_LITERAL_ID || datatype == 0)
   {
   node = ResourceFactory.createPlainLiteral(value);
   return;
   }

if (datatype > TypeMap.DATATYPE_IDS_START && datatype < TypeMap.DATATYPE_IDS_END)
   {
   node = ResourceFactory.createTypedLiteral(value, TypeMapper.getInstance()
         .getTypeByName(TypeMap.getTypedString(datatype)));
   return;
   }

// TODO language literals
if (datatype >= TypeMap.LANG_IDS_START)
   {
   node = ModelFactory.createDefaultModel().createLiteral(value, TypeMap.getLanguageString(datatype));
   return;
   }

throw new RdfStoreException("Unhandled literal type:" + datatype);
/*
 * if (value.contains(Constants.TYPED_LITERAL_DELIMITER) && value.contains(Constants.LITERAL_LANGUAGE)) { // TODO
 * 
 * } else if (value.contains(Constants.LITERAL_LANGUAGE)) { String[] temp = value.split(Constants.LITERAL_LANGUAGE); if
 * (!Pattern.matches("[a-zA-Z]+(-[a-zA-Z0-9]+)*$", value.substring(value .lastIndexOf(Constants.LITERAL_LANGUAGE) + 1))) { if
 * (value.startsWith("mailto:")) { node = ResourceFactory.createResource(value); } else { node =
 * ModelFactory.createDefaultModel().createLiteral( value); } } else if (temp == null || temp.length != 2) { node =
 * ModelFactory.createDefaultModel().createLiteral(value); } else { node = ModelFactory.createDefaultModel().createLiteral(temp[0], temp[1]); }
 * } else { node = ResourceFactory.createResource(value); }
 */
}