package it.unibz.krdb.obda.io;

import java.util.Collection;
import java.util.Iterator;

/**
 * The prefix manager is administrating the prefixes for ontolgyie. It allows to
 * register and unregister prefixes for ontolgies and to query them.
 * 
 * @author Manfred Gerstgrasser
 * 
 */

public abstract class AbstractPrefixManager implements PrefixManager {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2034866002642539028L;
	private String defaultNamespace = null;

	@Override
	public String getDefaultNamespace() {
		return defaultNamespace;
	}

	@Override
	public void setDefaultNamespace(String uri) {
		if ((uri.charAt(uri.length() - 1) == '#') || (uri.charAt(uri.length() - 1) == '/')) {
			defaultNamespace = uri;
			return;
		} else if (uri.charAt(uri.length() - 1) != '/') {
			defaultNamespace = uri + "#";
		} else {
			defaultNamespace = uri;
		}
		this.setPrefix(":", defaultNamespace);
	}

	public String getShortForm(String uri) {
		return getShortForm(uri, true, false);
	}

	public String getShortForm(String uri, boolean useDefaultPrefix) {
		return getShortForm(uri, useDefaultPrefix, false);
	}

	public String getShortForm(String uri, boolean useDefaultPrefix, boolean isLiteral) {
		StringBuilder result = new StringBuilder(uri);
		if (getDefaultNamespace() != null && useDefaultPrefix) {
			if (uri.length() > getDefaultNamespace().length()) {
				if (uri.contains(getDefaultNamespace())) {
					int index = uri.indexOf(getDefaultNamespace());
					result.replace(index, getDefaultNamespace().length(), "");
					if (isLiteral) {
						result.insert(0, "&:;");
					} else {
						result.insert(0, ":");
					}
					return result.toString();
				}
			}
		}

		Collection<String> longnamespacesset = this.getPrefixMap().values();
		Iterator<String> longnamespaces = longnamespacesset.iterator();
		while (longnamespaces.hasNext()) {
			String longnamespace = longnamespaces.next();
			if (uri.length() > longnamespace.length()) {
				if (uri.substring(0, longnamespace.length()).equals(longnamespace)) {
					if (isLiteral) {
						result.replace(0, longnamespace.length(), "&" + getPrefixForURI(longnamespace) + ":;");
					} else {
						result.replace(0, longnamespace.length(), getPrefixForURI(longnamespace) + ":");
					}
					break;
				}
			}
		}
		return result.toString();
	}
}

@@ -767,20 +767,8 @@
 i;%0D%0A
-%09%09%09return;%0D%0A
 %09%09%7D 
@@ -1581,16 +1581,22 @@
 th()
++index
 , %22%22);%0D%0A
 %09%09%09%09
@@ -1587,24 +1587,24 @@
 ndex, %22%22);%0D%0A
-
 %09%09%09%09%09if (isL
@@ -1653,32 +1653,339 @@
 %0A%09%09%09%09%09%7D else %7B%0D%0A
+%09%09%09%09%09%09// TODO: BAD CODE! The precondition for the input URI should not include any symbols. Only a valid URI string!%0D%0A%09%09%09%09%09%09if (uri.charAt(0) == '%3C') %7B %0D%0A%09%09%09%09%09%09%09// If the URI is enclosed by %3C...%3E brackets then put the colon after the opening bracket.%0D%0A%09%09%09%09%09%09%09result.insert(1, %22:%22);%0D%0A%09%09%09%09%09%09%7D%0D%0A%09%09%09%09%09%09else %7B%0D%0A%09
 %09%09%09%09%09%09result.ins
@@ -1990,32 +1990,41 @@
 nsert(0, %22:%22);%0D%0A
+%09%09%09%09%09%09%7D%0D%0A
 %09%09%09%09%09%7D%0D%0A%09%09%09%09%09ret

Fix the "Show short URIs" feature in the query panel. The fix contains a TODO note for future improvement.