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

edu.stanford.protege.webprotege.crud.PrefixedNameExpander Maven / Gradle / Ivy

The newest version!
package edu.stanford.protege.webprotege.crud;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.vocab.Namespaces;

import java.util.Map;
import java.util.Optional;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @author Matthew Horridge, Stanford University, Bio-Medical Informatics Research Group, Date: 16/04/2014
 */
public class PrefixedNameExpander {

    private final ImmutableMap prefixName2PrefixMap;

    private PrefixedNameExpander(ImmutableMap prefixName2PrefixMap) {
        this.prefixName2PrefixMap = checkNotNull(prefixName2PrefixMap);
    }

    public Optional getExpandedPrefixName(String suppliedName) {
        checkNotNull(suppliedName);
        int prefixNameSeparatorIndex = suppliedName.indexOf(':');
        if(prefixNameSeparatorIndex == -1) {
            return Optional.empty();
        }
        String suppliedPrefixName = suppliedName.substring(0, prefixNameSeparatorIndex + 1);
        if(suppliedPrefixName.isEmpty()) {
            return Optional.empty();
        }
        if(prefixNameSeparatorIndex + 1 >= suppliedName.length()) {
            return Optional.empty();
        }
        String prefix = prefixName2PrefixMap.get(suppliedPrefixName);
        if(prefix == null) {
            return Optional.empty();
        }
        String suffix = suppliedName.substring(suppliedPrefixName.length());
        String fullIRI = prefix + suffix;
        return Optional.of(IRI.create(fullIRI));
    }


    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {

        private final Map prefixName2PrefixMap = Maps.newHashMap();

        public Builder() {
        }

        public PrefixedNameExpander build() {
            return new PrefixedNameExpander(ImmutableMap.copyOf(prefixName2PrefixMap));
        }

        public Builder withPrefixNamePrefix(String prefixName, String prefix) {
            checkNotNull(prefixName);
            checkNotNull(prefix);
            checkPrefixNameEndsWithColon(prefixName);
            checkPrefixNameContainsExactlyOneColon(prefixName, prefix);
            prefixName2PrefixMap.put(prefixName, prefix);
            return this;
        }

        public Builder withNamespaces(Namespaces [] namespaces) {
            checkNotNull(namespaces);
            for(Namespaces ns : namespaces) {
                withPrefixNamePrefix(ns.getPrefixName() + ":", ns.getPrefixIRI());
            }
            return this;
        }

        private void checkPrefixNameContainsExactlyOneColon(String prefixName, String prefix) {
            checkArgument(prefixName.indexOf(":") == prefixName.length() - 1, "prefixName must only contain one colon");
        }

        private void checkPrefixNameEndsWithColon(String prefixName) {
            checkArgument(prefixName.endsWith(":"), "prefixName must end with colon");
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy