Java Code Examples for org.apache.jena.sparql.expr.NodeValue#getDatatypeURI()

The following examples show how to use org.apache.jena.sparql.expr.NodeValue#getDatatypeURI() . 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: FUN_JSONPath.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public NodeValue exec(NodeValue json, NodeValue jsonpath) {
    if (json.getDatatypeURI() != null
            && !json.getDatatypeURI().equals(datatypeUri)
            && !json.getDatatypeURI().equals("http://www.w3.org/2001/XMLSchema#string")) {
        LOG.debug("The datatype of the first argument should be <" + datatypeUri + ">"
                + " or <http://www.w3.org/2001/XMLSchema#string>. Got "
                + json.getDatatypeURI());
    }
    if (!jsonpath.isString()) {
        LOG.debug("The second argument should be a string. Got " + json);
    }

    try {
        Object value = JsonPath.parse(json.asNode().getLiteralLexicalForm())
                .limit(1).read(jsonpath.getString());
        return nodeForObject(value);
    } catch (Exception ex) {
        if(LOG.isDebugEnabled()) {
            Node compressed = LogUtils.compress(json.asNode());
            LOG.debug("No evaluation of " + compressed + ", " + jsonpath);
        }
        throw new ExprEvalException("No evaluation of " + jsonpath);
    }
}
 
Example 2
Source File: FUN_Markdown.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public NodeValue exec(NodeValue markdown) {
    if (markdown.getDatatypeURI() != null
            && !markdown.getDatatypeURI().equals(datatypeUri)
            && !markdown.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>."
        );
    }
    try {
    	String md = markdown.asNode().getLiteralLexicalForm();
     Parser parser = Parser.builder().build();
     Node document = parser.parse(md);
     HtmlRenderer renderer = HtmlRenderer.builder().build();
     String html = renderer.render(document);
     return new NodeValueString(html);
    } catch (Exception ex) {
        throw new ExprEvalException("FunctionBase: no evaluation", ex);
    }
}
 
Example 3
Source File: FUN_Markdown.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
@Override
public NodeValue exec(NodeValue markdown) {
    if (markdown.getDatatypeURI() != null
            && !markdown.getDatatypeURI().equals(datatypeUri)
            && !markdown.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>."
        );
    }
    try {
    	String md = markdown.asNode().getLiteralLexicalForm();
     Parser parser = Parser.builder().build();
     Node document = parser.parse(md);
     HtmlRenderer renderer = HtmlRenderer.builder().build();
     String html = renderer.render(document);
     return new NodeValueString(html);
    } catch (Exception ex) {
        throw new ExprEvalException("FunctionBase: no evaluation", ex);
    }
}
 
Example 4
Source File: ITER_JSONListKeys.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public List<List<NodeValue>> exec(NodeValue json) {
    if (json.getDatatypeURI() != null
            && !json.getDatatypeURI().equals(datatypeUri)
            && !json.getDatatypeURI().equals("http://www.w3.org/2001/XMLSchema#string")) {
        LOG.debug("The URI of NodeValue1 MUST have been"
                + " <" + datatypeUri + "> or"
                + " <http://www.w3.org/2001/XMLSchema#string>."
                + " Got <" + json.getDatatypeURI() + ">"
        );
    }
    try {
        Set<String> keys = GSON.fromJson(json.asNode().getLiteralLexicalForm(), Map.class).keySet();
        List<List<NodeValue>> listNodeValues = new ArrayList<>(keys.size());
        for (String key : keys) {
            NodeValue nodeValue
                    = NodeValue.makeNode(NodeFactory.createLiteral(key));
            listNodeValues.add(Collections.singletonList(nodeValue));
        }
        LOG.trace("end JSONListKeys");
        return listNodeValues;
    } catch (Exception ex) {
        if(LOG.isDebugEnabled()) {
            Node compressed = LogUtils.compress(json.asNode());
            LOG.debug("No evaluation for " + compressed, ex);
        }
        throw new ExprEvalException("No evaluation", ex);
    }
}
 
Example 5
Source File: ITER_CSVHeaders.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public List<List<NodeValue>> exec(NodeValue csv) {
    if (csv.getDatatypeURI() != null
            && !csv.getDatatypeURI().equals(datatypeUri)
            && !csv.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>."
        );
    }
    try {
        String sourceCSV = String.valueOf(csv.asNode().getLiteralLexicalForm());

        InputStream is = new ByteArrayInputStream(sourceCSV.getBytes("UTF-8"));
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

        CsvMapReader mapReader = new CsvMapReader(br, CsvPreference.STANDARD_PREFERENCE);
        String headers_str[] = mapReader.getHeader(true);

        final List<List<NodeValue>> listNodeValues = new ArrayList<>();
        Node node;
        NodeValue nodeValue;
        for (String header : headers_str) {
            node = NodeFactory.createLiteral(header);
            nodeValue = new NodeValueNode(node);
            listNodeValues.add(Collections.singletonList(nodeValue));
        }
        return listNodeValues;
    } catch (Exception ex) {
        if(LOG.isDebugEnabled()) {
            Node compressed = LogUtils.compress(csv.asNode());
            LOG.debug("No evaluation for " + compressed, ex);
        }
        throw new ExprEvalException("No evaluation ", ex);
    }
}
 
Example 6
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 7
Source File: FUN_CBOR.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public 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>."
        );
    }

    String json = new String(Base64.getDecoder().decode(cbor.asNode().getLiteralLexicalForm().getBytes()));
    try {
        Object value = JsonPath.parse(json)
                .limit(1).read(jsonpath.getString());

        if (value instanceof String) {
            return new NodeValueString((String) value);
        } else if (value instanceof Float) {
            return new NodeValueFloat((Float) value);
        } else if (value instanceof Boolean) {
            return new NodeValueBoolean((Boolean) value);
        } else if (value instanceof Integer) {
            return new NodeValueInteger((Integer) value);
        } else if (value instanceof Double) {
            return new NodeValueDouble((Double) value);
        } else if (value instanceof BigDecimal) {
            return new NodeValueDecimal((BigDecimal) value);
        } else {
            String strValue = String.valueOf(value);

            JsonParser parser = new JsonParser();
            JsonElement valElement = parser.parse(strValue);
            JsonArray list = valElement.getAsJsonArray();

            if (list.size() == 1) {
                String jsonstring = list.get(0).getAsString();
                Node node = NodeFactory.createLiteral(jsonstring);
                NodeValue nodeValue = new NodeValueNode(node);
                return nodeValue;

            } else {
                return new NodeValueString(String.valueOf(value));
            }
        }
    } catch (Exception ex) {
        if(LOG.isDebugEnabled()) {
            LOG.debug("No evaluation of " + jsonpath + "  on " + LogUtils.compress(cbor.asNode()), ex);
        }
        throw new ExprEvalException("FunctionBase: no evaluation", ex);
    }
}
 
Example 8
Source File: FUN_XPath.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
@Override
public NodeValue exec(NodeValue xml, NodeValue xpath) {
    if (xml.getDatatypeURI() != null
            && !xml.getDatatypeURI().equals(XML_URI)
            && !xml.getDatatypeURI().equals("http://www.w3.org/2001/XMLSchema#string")) {
        LOG.debug("The URI of NodeValue1 MUST be <" + XML_URI + ">"
                + " or <http://www.w3.org/2001/XMLSchema#string>. Got "
                + xml.getDatatypeURI());
    }
    if (!xpath.isString()) {
        LOG.debug("The second argument should be a string. Got " + xpath);
    }
    DocumentBuilderFactory builderFactory
            = DocumentBuilderFactory.newInstance();
    builderFactory.setNamespaceAware(true);
    DocumentBuilder builder = null;
    try {
        // THIS IS A HACK !! FIND A BETTER WAY TO MANAGE NAMESPACES
        String xmlstring = xml.asNode().getLiteralLexicalForm().replaceAll("xmlns=\"[^\"]*\"", "");
        
        builder = builderFactory.newDocumentBuilder();
        InputStream in = new ByteArrayInputStream(xmlstring.getBytes("UTF-8"));
        Document document = builder.parse(in);

        XPath xPath = XPathFactory.newInstance().newXPath();
        xPath.setNamespaceContext(new UniversalNamespaceResolver(document));
        //Node node = (Node) xPath.compile(xpath.getString()).evaluate(document, XPathConstants.NODE);

        org.w3c.dom.Node xmlNode = (org.w3c.dom.Node) xPath
                .compile(xpath.getString())
                .evaluate(document, XPathConstants.NODE);
        if (xmlNode == null) {
            LOG.debug("No evaluation of " + xpath);
            throw new ExprEvalException("No evaluation of " + xpath);
        }
        return nodeForNode(xmlNode);
    } catch (Exception ex) {
        if(LOG.isDebugEnabled()) {
            Node compressed = LogUtils.compress(xml.asNode());
            LOG.debug("No evaluation of " + compressed + ", " + xpath, ex);
        }
        throw new ExprEvalException("No evaluation of " + xpath, ex);
    }
}