package it.unibz.krdb.obda.io;

import java.util.Map;

public abstract class AbstractPrefixManager implements PrefixManager {

	public String getShortForm(String uri) {
		return getShortForm(uri, false);
	}
	
	public String getShortForm(String uri, boolean insideQuotes) {
		final Map<String, String> prefixMap = getPrefixMap();
		String shortUri = uri;
		for (String prefixUriDefinition : prefixMap.values()) {
			if (uri.contains(prefixUriDefinition)) {
				String prefix = getPrefix(prefixUriDefinition);
				if (insideQuotes) {
					prefix = String.format("&%s;", removeColon(prefix));
				}
				// Replace the URI with the corresponding prefix.
				shortUri = uri.replace(prefixUriDefinition, prefix);
				
				// Clean the URI string from <> signs, if exist
				if (shortUri.contains("<") && shortUri.contains(">")) {
					shortUri = shortUri.replace("<", "").replace(">", "");
				}
				return shortUri;
			}
		}
		return shortUri;
	}
	
	public String getDefaultPrefix() {
		return getPrefixMap().get(DEFAULT_PREFIX);
	}
		
	/**
	 * A utility method to ensure a proper naming for prefix URI
	 */
	protected String getProperPrefixURI(String prefixUri) {
		if (!prefixUri.endsWith("/")) {
			if (!prefixUri.endsWith("#")) {
				prefixUri += "#";
			}
		}
		return prefixUri;
	} 
	
	private String removeColon(String prefix) {
		if (prefix.equals(PrefixManager.DEFAULT_PREFIX)) {
			return prefix; // TODO Remove this code in the future.
		}
		return prefix.replace(":", "");
	}
}

@@ -340,30 +340,876 @@
 %0D%0A%09%09
-String shortUri = uri;
+%0D%0A%09%09/* Clean the URI string from %3C...%3E signs, if they exist.%0D%0A%09%09 * %3Chttp://www.example.org/library#Book%3E --%3E http://www.example.org/library#Book%0D%0A%09%09 */%0D%0A%09%09if (uri.contains(%22%3C%22) && uri.contains(%22%3E%22)) %7B%0D%0A%09%09%09uri = uri.replace(%22%3C%22, %22%22).replace(%22%3E%22, %22%22);%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09// Initial value for the short URI%0D%0A%09%09String shortUri = uri;%0D%0A%09%09%0D%0A%09%09String subUri = %22%22;%0D%0A%09%09if (uri.contains(%22#%22)) %7B%0D%0A%09%09%09/* %0D%0A%09%09%09 * Obtain the URI definition that contains a hash sign%0D%0A%09%09%09 * http://www.example.org/library#Book --%3E http://www.example.org/library#%0D%0A%09%09%09 */%0D%0A%09%09%09subUri = uri.substring(0, uri.lastIndexOf(%22#%22)+1);%0D%0A%09%09%7D else %7B%0D%0A%09%09%09/* %0D%0A%09%09%09 * Obtain the URI definition that contains a slash sign%0D%0A%09%09%09 * http://www.example.org/library/Book --%3E http://www.example.org/library/%0D%0A%09%09%09 */%0D%0A%09%09%09subUri = uri.substring(0, uri.lastIndexOf(%22/%22)+1);%0D%0A%09%09%7D%0D%0A%09%09%0D%0A%09%09// Check if the URI string has a matched prefix
 %0D%0A%09%09
@@ -1272,27 +1272,28 @@
 %0A%09%09%09if (
-uri.contain
+subUri.equal
 s(prefix
@@ -1572,196 +1572,14 @@
 );%0D%0A
+
 %09%09%09%09%0D%0A
-%09%09%09%09// Clean the URI string from %3C%3E signs, if exist%0D%0A%09%09%09%09if (shortUri.contains(%22%3C%22) && shortUri.contains(%22%3E%22)) %7B%0D%0A%09%09%09%09%09shortUri = shortUri.replace(%22%3C%22, %22%22).replace(%22%3E%22, %22%22);%0D%0A%09%09%09%09%7D%0D%0A
 %09%09%09%09

Fix the URI shorten method in the case there are two or more prefix definitions with overlapped strings. For example: