All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ioinformarics.oss.jackson.module.jsonld.util.JsonldResourceUtils Maven / Gradle / Ivy

The newest version!
package ioinformarics.oss.jackson.module.jsonld.util;

import com.fasterxml.jackson.databind.node.ObjectNode;
import ioinformarics.oss.jackson.module.jsonld.JsonldContextFactory;
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldType;
import ioinformarics.oss.jackson.module.jsonld.annotation.JsonldTypeFromJavaClass;
import ioinformarics.oss.jackson.module.jsonld.internal.AnnotationConstants;

import java.util.Optional;

/**
 * @author Alexander De Leon ([email protected])
 */
public abstract class JsonldResourceUtils {

    public static Optional getContext(Object scopedObj) {
        return JsonldContextFactory.fromAnnotations(scopedObj);
    }

    public static Optional dynamicTypeLookup(Class objType){
        Optional typeFromAnnotation = Optional.ofNullable(objType.getAnnotation(JsonldType.class))
                    .map(JsonldType::value);
        return typeFromAnnotation.isPresent() ? typeFromAnnotation : typeFromJavaClass(objType);
    }

    private static Optional typeFromJavaClass(Class objType) {
        return Optional.ofNullable(objType.getAnnotation(JsonldTypeFromJavaClass.class))
                .map((t) -> {
                    String prefix = t.namespace();
                    if(prefix.equals(AnnotationConstants.UNASSIGNED)) {
                        prefix = t.namespacePrefix().equals(AnnotationConstants.UNASSIGNED) ? "" : t.namespacePrefix() + ":";
                    }
                    return prefix + objType.getSimpleName();

                });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy