Search in sources :

Example 1 with RDFDatatype

use of com.hp.hpl.jena.datatypes.RDFDatatype in project stanbol by apache.

the class RdfIndexingSource method processValue.

/**
     * Processes a {@link Node} and adds the according value to the parsed
     * Representation.
     * @param value The node to convert to an value for the Representation
     * @param source the representation (MUST NOT be <code>null</code>
     * @param field the field (MUST NOT be <code>null</code>)
     */
private void processValue(Node value, Representation source, String field) {
    if (value == null) {
        log.warn("Encountered NULL value for field {} and entity {}", field, source.getId());
    } else if (value.isURI()) {
        //add a reference
        source.addReference(field, value.getURI());
    } else if (value.isLiteral()) {
        //add a value or a text depending on the dataType
        LiteralLabel ll = value.getLiteral();
        //            log.debug("LL: lexical {} | value {} | dataType {} | language {}",
        //                new Object[]{ll.getLexicalForm(),ll.getValue(),ll.getDatatype(),ll.language()});
        //if the dataType == null , than we can expect a plain literal
        RDFDatatype dataType = ll.getDatatype();
        if (dataType != null) {
            //add a value
            Object literalValue;
            try {
                literalValue = ll.getValue();
                if (literalValue instanceof BaseDatatype.TypedValue) {
                    //used for unknown data types
                    // -> in such cases just use the lexical type
                    String lexicalValue = ((BaseDatatype.TypedValue) literalValue).lexicalValue;
                    if (lexicalValue != null && !lexicalValue.isEmpty()) {
                        source.add(field, lexicalValue);
                    }
                } else if (literalValue instanceof XSDDateTime) {
                    //Entityhub uses the time
                    source.add(field, ((XSDDateTime) literalValue).asCalendar().getTime());
                } else if (literalValue instanceof XSDDuration) {
                    String duration = literalValue.toString();
                    if (duration != null && !duration.isEmpty()) {
                        source.add(field, literalValue.toString());
                    }
                } else if (!ll.getLexicalForm().isEmpty()) {
                    source.add(field, literalValue);
                }
            //else ignore literals that are empty
            } catch (DatatypeFormatException e) {
                log.warn(" Unable to convert {} to {} -> use lecicalForm", ll.getLexicalForm(), ll.getDatatype());
                literalValue = ll.getLexicalForm();
            }
        } else {
            //add a text
            String lexicalForm = ll.getLexicalForm();
            if (lexicalForm != null && !lexicalForm.isEmpty()) {
                String language = ll.language();
                if (language != null && language.length() < 1) {
                    language = null;
                }
                source.addNaturalText(field, lexicalForm, language);
            }
        //else ignore empty literals
        }
    // "" is parsed if there is no language
    } else if (value.isBlank()) {
        if (bnodePrefix != null) {
            //STANBOL-765: convert Bnodes to URIs
            StringBuilder sb = new StringBuilder(bnodePrefix);
            sb.append(value.getBlankNodeId().getLabelString());
            source.addReference(field, sb.toString());
        } else {
            logIgnoredBnode(log, source, field, value);
        }
    } else {
        log.warn("ignoreing value {} for field {} and RDFTerm {} because it is of an unsupported type!", new Object[] { value, field, source.getId() });
    }
//end different value node type
}
Also used : DatatypeFormatException(com.hp.hpl.jena.datatypes.DatatypeFormatException) XSDDateTime(com.hp.hpl.jena.datatypes.xsd.XSDDateTime) XSDDuration(com.hp.hpl.jena.datatypes.xsd.XSDDuration) RDFDatatype(com.hp.hpl.jena.datatypes.RDFDatatype) LiteralLabel(com.hp.hpl.jena.graph.impl.LiteralLabel)

Aggregations

DatatypeFormatException (com.hp.hpl.jena.datatypes.DatatypeFormatException)1 RDFDatatype (com.hp.hpl.jena.datatypes.RDFDatatype)1 XSDDateTime (com.hp.hpl.jena.datatypes.xsd.XSDDateTime)1 XSDDuration (com.hp.hpl.jena.datatypes.xsd.XSDDuration)1 LiteralLabel (com.hp.hpl.jena.graph.impl.LiteralLabel)1