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

org.aksw.jena_sparql_api.utils.UriUtils Maven / Gradle / Ivy

There is a newer version: 3.17.0-1
Show newest version
package org.aksw.jena_sparql_api.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import org.apache.jena.rdf.model.impl.Util;

/**
 * This class does not really belong to the SPARQL api,
 * but still it is useful for the server component.
 *
 * Code originated from a post on stackoverflow (forgot to note the link)
 *
 * @author Claus Stadler
 *
 */
public class UriUtils {

    public static final Pattern replaceNamespacePattern = Pattern.compile("(?<=/)[^/]+(?=/[^/]+/*$)");


    /**
     * Taken from Node_URI.getNameSpace()
     *
     * @return
     */
    public static String getNameSpace(String s) {
        return s.substring( 0, Util.splitNamespaceXML( s ) );
    }

    /**
     * Taken from Node_URI.getLocalName()
     *
     * @return
     */
    public static String getLocalName(String s) {
        return s.substring( Util.splitNamespaceXML( s ) );
    }

    /**
     * http://example.org/foo/bar
     * becomes
     * http://example.org/baz/bar
     *
     *
     * @param base
     * @param replacement
     * @return
     */
    public static String replaceNamespace(String base, String replacement) {
        Matcher m = replaceNamespacePattern.matcher(base);
        String result = m.replaceAll(replacement);

        //String result = base.replace("([^/]+)/([^/]+)$", replacement);
        return result;
    }


    public static Multimap parseQueryString(String queryString) {
        try {
            return parseQueryStringEx(queryString);
        } catch(Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Multimap parseQueryStringEx(String queryString)
            throws UnsupportedEncodingException
    {
        Multimap result = ArrayListMultimap.create();

        if(queryString == null) {
            return result;
        }

        for (String param : queryString.split("&")) {
            String pair[] = param.split("=");
            String key = URLDecoder.decode(pair[0], "UTF-8");
            String value = "";
            if (pair.length > 1) {
                value = URLDecoder.decode(pair[1], "UTF-8");
            }
            result.put(new String(key), new String(value));
        }

        return result;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy