Search in sources :

Example 1 with NodeTransformer

use of org.apache.marmotta.ldpath.api.transformers.NodeTransformer in project stanbol by apache.

the class LdpathSourceProcessor method process.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Representation process(Representation source) {
    if (log.isTraceEnabled()) {
        log.trace(" - process {} (backend: {}, program: {}, append: {})", source.getId(), backend, program, appendMode);
    }
    Object context = backend.createURI(source.getId());
    Representation result = appendMode ? source : vf.createRepresentation(source.getId());
    /*
         * NOTE: LDPath will return Node instances of the RDFRepositroy if no
         * transformation is defined for a statement (line) in the configured
         * LDpath program (the ":: xsd:int" at the end). this Nodes need to be
         * converted to valid Entityhub Representation values.
         * As we can not know the generic type used by the RDFRepository
         * implementation of the indexing source this is a little bit tricky.
         * What this does is:
         *   - for URIs it creates References
         *   - for plain literal it adds natural texts
         *   - for typed literals it uses the NodeTransformer registered with 
         *     the LDPath (or more precise the Configuration object parsed to 
         *     the LDPath in the constructor) to transform the values to
         *     Java objects. If no transformer is found or an Exeption occurs
         *     than the lexical form is used and added as String to the 
         *     Entityhub.
         */
    Map<String, Collection<Object>> resultMap = (Map<String, Collection<Object>>) program.execute(backend, context);
    for (Entry<String, Collection<Object>> entry : resultMap.entrySet()) {
        NodeTransformer fieldTransformer = program.getField(entry.getKey()).getTransformer();
        if (fieldTransformer == null || fieldTransformer instanceof IdentityTransformer<?>) {
            //we need to convert the RDFBackend Node to an Representation object
            for (Object value : entry.getValue()) {
                if (backend.isURI(value)) {
                    result.addReference(entry.getKey(), backend.stringValue(value));
                } else if (backend.isLiteral(value)) {
                    //literal
                    Locale locale = backend.getLiteralLanguage(value);
                    if (locale != null) {
                        //text with language
                        String lang = locale.getLanguage();
                        result.addNaturalText(entry.getKey(), backend.stringValue(value), lang.isEmpty() ? null : lang);
                    } else {
                        // no language
                        URI type = backend.getLiteralType(value);
                        if (type != null) {
                            //typed literal -> need to transform
                            NodeTransformer nt = transformer.get(type.toString());
                            if (nt != null) {
                                //add typed literal
                                try {
                                    result.add(entry.getKey(), nt.transform(backend, value, Collections.<String, String>emptyMap()));
                                } catch (RuntimeException e) {
                                    log.info("Unable to transform {} to dataType {} -> will use lexical form", value, type);
                                    result.add(entry.getKey(), backend.stringValue(value));
                                }
                            } else {
                                //no transformer
                                log.info("No transformer for type {} -> will use lexical form", type);
                                result.add(entry.getKey(), backend.stringValue(value));
                            }
                        } else {
                            //no langauge and no type -> literal with no language
                            result.addNaturalText(entry.getKey(), backend.stringValue(value));
                        }
                    }
                } else {
                    //bNode
                    log.info("Ignore bNode {} (class: {})", value, value.getClass());
                }
            }
        //end for all values
        } else {
            //already a transformed values
            //just add all values
            result.add(entry.getKey(), entry.getValue());
        }
    }
    return result;
}
Also used : Locale(java.util.Locale) NodeTransformer(org.apache.marmotta.ldpath.api.transformers.NodeTransformer) Collection(java.util.Collection) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Map(java.util.Map) URI(java.net.URI)

Aggregations

URI (java.net.URI)1 Collection (java.util.Collection)1 Locale (java.util.Locale)1 Map (java.util.Map)1 NodeTransformer (org.apache.marmotta.ldpath.api.transformers.NodeTransformer)1 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)1