/*
 * ComparisonHelper.java
 *
 */

package de.uni_stuttgart.vis.vowl.owl2vowl.parser.helper;

import de.uni_stuttgart.vis.vowl.owl2vowl.parser.container.OntologyInformation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.semanticweb.owlapi.model.OWLAnnotation;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLEntity;
import org.semanticweb.owlapi.model.OWLOntology;

import java.util.Set;

/**
 *
 */
public class ComparisonHelper {
	private static final Logger logger = LogManager.getLogger(ComparisonHelper.class);

	public static boolean hasDifferentNameSpace(OWLEntity entity, OntologyInformation information) {
		return hasDifferentNameSpace(entity.getIRI().toString(), information);
	}

	private static boolean hasDifferentNameSpace(String elementNamespace, OntologyInformation information) {
		if (elementNamespace == null) {
			logger.info("Namespace check: Element has no namespace!");
			return false;
		}

		String ontologyIri;

		// Use ontology iri or the iri path to the ontology.
		if (information.getOntology().isAnonymous()) {
			ontologyIri = information.getLoadedIri().toString();
		} else {
			ontologyIri = information.getOntology().getOntologyID().getOntologyIRI().toString();
		}

		return hasDifferentNameSpace(elementNamespace, ontologyIri);
	}

	public static boolean hasDifferentNameSpace(String elementNamespace, String ontologyIri) {
		// The trailing hash has no meaning and can be removed
		elementNamespace = removeTrailingHash(elementNamespace);
		ontologyIri = removeTrailingHash(ontologyIri);

		if (elementNamespace.equals(ontologyIri)) {
			return false;
		}

		if (elementNamespace.contains("#")) {
			String elementNamespaceWithoutHashParameter = elementNamespace.split("#")[0];
			if (elementNamespaceWithoutHashParameter.equals(ontologyIri)) {
				return false;
			}
		}

		if (elementNamespace.contains("/") && !elementNamespace.endsWith("/")) {
			int lastSlashIndex = elementNamespace.lastIndexOf("/");
			int indexAfterSlash = lastSlashIndex + 1;

			String elementNamespaceWithoutLastPart = elementNamespace.substring(0, indexAfterSlash);

			if (elementNamespaceWithoutLastPart.equals(ontologyIri)) {
				return false;
			}
		}

		return true;
	}

	private static String removeTrailingHash(String text) {
		return text.replaceAll("#$", "");
	}
}

@@ -1415,25 +1415,19 @@
  element
-Namespace
+Iri
 , String
@@ -1501,32 +1501,40 @@
 moved%0A%09%09
-elementNamespace
+String trimmedElementIri
  = remov
@@ -1558,23 +1558,31 @@
 ment
-Namespace);%0A%09%09o
+Iri);%0A%09%09String trimmedO
 ntol
@@ -1634,33 +1634,41 @@
 if (
-elementNamespace.equals(o
+trimmedElementIri.equals(trimmedO
 ntol
@@ -1706,32 +1706,33 @@
 %0A%0A%09%09if (
-elementNamespace
+trimmedElementIri
 .contain
@@ -1790,32 +1790,33 @@
 meter = 
-elementNamespace
+trimmedElementIri
 .split(%22
@@ -1866,33 +1866,40 @@
 arameter.equals(
-o
+trimmedO
 ntologyIri)) %7B%0A%09
@@ -1931,32 +1931,33 @@
 %0A%0A%09%09if (
-elementNamespace
+trimmedElementIri
 .contain
@@ -1967,32 +1967,33 @@
 /%22) && !
-elementNamespace
+trimmedElementIri
 .endsWit
@@ -2026,32 +2026,33 @@
 Index = 
-elementNamespace
+trimmedElementIri
 .lastInd
@@ -2152,32 +2152,33 @@
 tPart = 
-elementNamespace
+trimmedElementIri
 .substri
@@ -2248,17 +2248,24 @@
 .equals(
-o
+trimmedO
 ntologyI

Refactor code